invicticide / fractive

Fractive is a free, open-source, Markdown-based hypertext authoring tool for writing interactive fiction.
http://fractive.io
Other
38 stars 5 forks source link

Aliases can be defined as regexes and use regex replacement expressions #80

Closed NQNStudios closed 6 years ago

NQNStudios commented 6 years ago

I wanted to be able to define a single alias that would handle a range of expressions (essentially, giving aliases internal parameters that could be included in the replacement). So I made that a thing.

Changes proposed by this pull request:

@invicticide to review

invicticide commented 6 years ago

Can you comment back with an example of how this would work?

NQNStudios commented 6 years ago

Alongside a regular alias definition, a regex definition looks like this:

"aliases": [
        { "regex": "vibe:(?<tag>[A-Za-z]+)", "replaceWith": "<span class='conditional' data-vibe-tag='${tag}'>", "end": "</span>" },
        { "alias": "backButton", "replaceWith": "<a href='javascript:Core.GotoPreviousSection();'><i class=\"fa fa-arrow-left\" aria-hidden=\"true\"></i> Back</a>" }
]

A regex alias looks exactly like a regular one when it's invoked, depending on how complicated the regex is. In the given example, anything in the pattern {vibe:[SomeWord]} will be converted into the replaceWith pattern that can invoke [SomeWord] wherever it needs to.

What this means for me is, if I need to define a lot of divs in the body of my sections, and each with their own id, I could declare an alias like this:

{ "regex": "div:(?<id>[A-Za-z]+)", "replaceWith": "<div id='${id}'>" and suddenly I have a very convenient way to define an identifiable div in Markdown.

invicticide commented 6 years ago

What? Six weeks is a totally normal amount of time for reviewing and accepting a PR... right? >_>