mdaines / grammophone

A tool for analyzing and transforming context-free grammars.
https://mdaines.github.io/grammophone
MIT License
205 stars 23 forks source link

Storing grammars in the url. #25

Closed modulovalue closed 1 year ago

modulovalue commented 1 year ago

Hello @mdaines,

First of all, thank you very much for this project!

(If you accept any feature suggestions/requests) I think it would be really great if we could pre-populate the editor with a grammar provided in the url, and share any given grammar by storing it in the url in e.g. a base64 encoded form.

This would allow people to store and share direct links to grammophone and have it be immediately initialized with a grammar. People could also use this to programmatically open grammophone with a grammar from any custom tools that they are building.

One service that does something similar is edotor.

mdaines commented 1 year ago

This would be useful. It may have been on a to-do list somewhere for a while, but I never got around to it.

I'm looking into something like https://github.com/pieroxy/lz-string for encoding. It seems like compression would work well in this case because the strings will have relatively few symbols.

mdaines commented 1 year ago

I had a quick look at this. Here's my in-progress branch: https://github.com/mdaines/grammophone/compare/spec-search-param

I was going to just use encodeURIComponent, but grammars may end with ., meaning that the resulting URL might look like it's at the end of a sentence to some parsers, and they'll strip that from the URL, making the resulting string a parse error. So it seems easiest to use base64 encoding.

The links will probably look like this: https://mdaines.github.io/grammophone/?s=UyAtPiBhIFMgYiAuClMgLT4gLgo= (doesn't work yet)

modulovalue commented 1 year ago

The links will probably look like this: https://mdaines.github.io/grammophone/?s=UyAtPiBhIFMgYiAuClMgLT4gLgo=

Thank you, this looks perfect to me. I personally also prefer base64 over url encoding.

mdaines commented 1 year ago

I've added support for this in d8c631c90b1f5968103e9d24b77545721c74fcc7. I plan to release the change closer to the end of the week.

modulovalue commented 1 year ago

@mdaines Thank you for adding support for this. The button seems to work, but links that contain a grammar don't seem to work (yet?) e.g. https://mdaines.github.io/grammophone/?s=UyAtPiBTdGFydCAkLgpTdGFydCAtPiBUeXBlIE5hbWUuClN0YXJ0IC0+IE5hbWUuClR5cGUgLT4gaWQgIjwiLgpOYW1lIC0+IGlkICI8Ii4=

Repro:

modulovalue commented 1 year ago
Bildschirm­foto 2023-04-09 um 23 17 33
modulovalue commented 1 year ago

Maybe it's because the application appends "#/" to the end of the copied url? (https://mdaines.github.io/grammophone/?s=UyAtPiBTdGFydCAkLgpTdGFydCAtPiBUeXBlIE5hbWUuClN0YXJ0IC0+IE5hbWUuClR5cGUgLT4gaWQgIjwiLgpOYW1lIC0+IGlkICI8Ii4=#/)

mdaines commented 1 year ago

I think it's because the + is interpreted as a space character.

mdaines commented 1 year ago

Of course, MDN mentions this in the article about URLSearchParams:

https://developer.mozilla.org/en-US/docs/Web/API/URLSearchParams#preserving_plus_signs

I'll deploy a fix for this.

mdaines commented 1 year ago

This should be addressed in b841ef86857971729ce6098566145d3d026ef2ed; the link you provided works now for me.

modulovalue commented 1 year ago

I can confirm that it also works for me. Thank you!