all repos — hastyscribe @ f06926d8c9036e8b5c750a93def2578a981c02c1

A professional markdown compiler.

Implemented alternative snippet definition syntax.
h3rald h3rald@h3rald.com
Sun, 12 Mar 2017 13:01:36 +0100
commit

f06926d8c9036e8b5c750a93def2578a981c02c1

parent

bfe685ed7a2e0bbd4cb2c9e1da2953844e57b4e1

3 files changed, 35 insertions(+), 33 deletions(-)

jump to
M doc/-syntax.mddoc/-syntax.md

@@ -34,7 +34,7 @@ ## Snippets

If you want to reuse a few words or even entire blocks of texts, you can use {{hs}}'s snippets. -A snippet definition is constituted by an identifier, followed by an arrow (->), followed by some text -- all wrapped in double curly brackets. +A snippet definition is constituted by an identifier, followed by an arrow ([->](class:kwd)), followed by some text -- all wrapped in double curly brackets. The following definition creates a snippet called [test](class:kwd) which is transformed into the text "This is a test snippet.".

@@ -42,11 +42,12 @@ <code>\{\{test -> This is a test snippet.\}\}</code>

Once a snippet is defined _anywhere_ in the document, you can use its identifier wrapped in double curly brackets (<code>\{\{test}\}\}</code> in the previous example) anywhere in the document to reuse the specified text. -> %note% -> Remarks +> %sidebar% +> Alternative Snippet Definition Syntax +> +> When a document is compiled, both snippets _and snippet defininotions_ are evaluated to their body text. To avoid snippet definitions being evaluated, you can use a double arrow ([=>](class:kwd)) in the definition: > -> * It doesn't matter where a snippet is defined. Snippets can be used anywhere in the document, before or after their definition. -> * When a document is compiled, both snippets _and snippets definitions_ are evaluated their body text. +> <code>\{\{test => This snippet definition will not be evaluated to its body text.\}\}</code> ## Fields
M doc/HastyScribe_UserGuide.htmdoc/HastyScribe_UserGuide.htm

@@ -4677,7 +4677,7 @@ <h3>Snippets<a href="#document-top" title="Go to top"></a></h3>

<p>If you want to reuse a few words or even entire blocks of texts, you can use HastyScribe&rsquo;s snippets.</p> -<p>A snippet definition is constituted by an identifier, followed by an arrow (->), followed by some text &ndash; all wrapped in double curly brackets.</p> +<p>A snippet definition is constituted by an identifier, followed by an arrow (<span class="kwd">-></span>), followed by some text &ndash; all wrapped in double curly brackets.</p> <p>The following definition creates a snippet called <span class="kwd">test</span> which is transformed into the text &ldquo;This is a test snippet.&rdquo;.</p>

@@ -4685,13 +4685,11 @@ <p><code>{{test -> This is a test snippet.}}</code></p>

<p>Once a snippet is defined <em>anywhere</em> in the document, you can use its identifier wrapped in double curly brackets (<code>{{test}}}</code> in the previous example) anywhere in the document to reuse the specified text.</p> -<div class="note"><p>Remarks</p> +<div class="sidebar"><p>Alternative Snippet Definition Syntax</p> -<ul> -<li>It doesn&rsquo;t matter where a snippet is defined. Snippets can be used anywhere in the document, before or after their definition.</li> -<li>When a document is compiled, both snippets <em>and snippets definitions</em> are evaluated their body text.</li> -</ul> -</div> +<p>When a document is compiled, both snippets <em>and snippet defininotions</em> are evaluated to their body text. To avoid snippet definitions being evaluated, you can use a double arrow (<span class="kwd">=></span>) in the definition:</p> + +<p><code>{{test => This snippet definition will not be evaluated to its body text.}}</code></p></div> <a name="Fields"></a> <h3>Fields<a href="#document-top" title="Go to top"></a></h3>

@@ -4708,51 +4706,51 @@ </thead>

<tbody> <tr> <td><code>{{$timestamp}}</code> </td> -<td> 1489248321</td> +<td> 1489319929</td> </tr> <tr> <td><code>{{$date}}</code> </td> -<td> 2017-03-11</td> +<td> 2017-03-12</td> </tr> <tr> <td><code>{{$full-date}}</code> </td> -<td> Saturday, March 11, 2017</td> +<td> Sunday, March 12, 2017</td> </tr> <tr> <td><code>{{$long-date}}</code> </td> -<td> March 11, 2017</td> +<td> March 12, 2017</td> </tr> <tr> <td><code>{{$medium-date}}</code> </td> -<td> Mar 11, 2017</td> +<td> Mar 12, 2017</td> </tr> <tr> <td><code>{{$short-date}}</code> </td> -<td> 3/11/17</td> +<td> 3/12/17</td> </tr> <tr> <td><code>{{$short-time}}</code> </td> -<td> 17:05 PM</td> +<td> 12:58 PM</td> </tr> <tr> <td><code>{{$short-time-24}}</code> </td> -<td> 17:05</td> +<td> 12:58</td> </tr> <tr> <td><code>{{$time}}</code> </td> -<td> 17:05:21 PM</td> +<td> 12:58:49 PM</td> </tr> <tr> <td><code>{{$time-24}}</code> </td> -<td> 17:05:21</td> +<td> 12:58:49</td> </tr> <tr> <td><code>{{$day}}</code> </td> -<td> 11</td> +<td> 12</td> </tr> <tr> <td><code>{{$short-day}}</code> </td> -<td> 11</td> +<td> 12</td> </tr> <tr> <td><code>{{$month}}</code> </td>

@@ -4772,11 +4770,11 @@ <td> 17</td>

</tr> <tr> <td><code>{{$weekday}}</code> </td> -<td> Saturday</td> +<td> Sunday</td> </tr> <tr> <td><code>{{$weekday-abbr}}</code> </td> -<td> 11</td> +<td> 12</td> </tr> <tr> <td><code>{{$month-name}}</code> </td>

@@ -5699,7 +5697,7 @@

<p style="text-align:center;"> <img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAFgAAAAfCAYAAABjyArgAAAGa0lEQVRo3u1aW08bRxTmuSqIV0SlIFWtAKkVtEpfWiVI6Q+gT60qpeIhTXkrSpqoaZ1AkJMobSRkkUahEtAWCVWBYMDcfME22MZ3rw0hrzzwAxC/4FTfrGeYXXa9u8Y0yGGkEeu5nGW+OfOdy2wDETU1NDTQea19JcKf8o/lwBKthlbIH16j0EaQwrEwbSSiFEvFaCuToFQuSel8mrKFDOWULOWKObUqWcoqGcrk02wMxsZTMTY3EgszWf6wn8nGO3xri7SwOk/zy16aW3pJL32zrM4uztDMwou6qRLQR+AGIn5a3wxRNB6hWHKTgZXOpxiAhVKeijsKlV4VaXt3m3ZeqxXPpVcl1ocxWSXL5mAuZEQTESYTsvUge5fnBMgAuE5BbtCCm4gyDUxmk0xbARpAdT9w05Uvr1BjU+Oxo4A29LkfutnYwnaBbQo0Op6OM21e31zXgLy4tkDzK14VZN8RyHdcd6ijo+PYO9CGPruLOwtyBMCgBRlcAJMrZplWekY91Nraapt3MNbz1MPmgkKgzQkBckjQxZLfR4urKsjQ4j/GnlJ7R7uQ09PTQwMDA6zimbdjzOjzUdNFoe+syBEAgydBC0fg5pgm9n7VqwGvubmZ+vr6aGhoiCKRCKt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blogEhCZNZYfXpqjX+7WYbqSBxpymKz3Z2sZaGyWAw2kLnlWjact6+l2QwCcSBuHyk7i8WpDZTn5XrcAy8meib/Ga5/sGfUw2Uj24F08ZSkn3vnNRj1dHQmAUV2DLpGuvPrdVQ3ITpI+0HrDdGUhQ8MPht/G2+WjHwM3fmRuVLGKhDv6MEZOuF/87CKjBvjZrnLe960GGBUJcAQDAFnW5GqujABuvpijZ2PP7H5HYBhk6MfYkWP22yyZ5EROpaSUJcACZEW99AQnO770HOWXnlnb4BoBYbQwJxtVab7djaqBHOMO0IV8bQ/QrK7tcXPBru3xfUQh45gWrNKVTjfK7G8t5VQNMLvf+rSbvIte5sLBT1bEhycl6cOTEmuTPzyZ93np0uVL1XxqdCJqsKKEajS4BnKs/2GA9fjJYwpFQ9KnU+WKT6cKGdaHMdUAe1oafGY5+LzWsBLRO+dAnN7Xlf8BOGLNYHeHmZwAAAAASUVORK5CYII=" alt="Creative Commons Attribution-Noncommercial-No Derivative Works 3.0 Unported License" /></p> </div> <div id="footer"> - <p><span class="copy"></span> Fabio Cevasco &ndash; March 11, 2017</p> + <p><span class="copy"></span> Fabio Cevasco &ndash; March 12, 2017</p> <p><span>Powered by</span> <a href="https://h3rald.com/hastyscribe"><span class="hastyscribe"></span></a></p> </div> </div>
M hastyscribe.nimhastyscribe.nim

@@ -240,7 +240,8 @@ # Snippet Usage:

# {{test}} proc parse_snippets(hs: var HastyScribe, document: string): string = let peg_snippet_def = peg""" - definition <- '{{' \s* {id} \s* '->' {@} '}}' + definition <- '{{' \s* {id} \s* {deftype} {@} '}}' + deftype <- '->' / '=>' id <- [a-zA-Z0-9_-]+ """ let peg_snippet = peg"""

@@ -248,25 +249,27 @@ snippet <- '{{' \s* {id} \s* '}}'

id <- [a-zA-Z0-9_-]+ """ type - TSnippetDef = array[0..1, string] + TSnippetDef = array[0..2, string] TSnippet = array[0..0, string] result = document for def in document.findAll(peg_snippet_def): var matches:TSnippetDef discard def.match(peg_snippet_def, matches) var id = matches[0].strip - var value = matches[1].strip(true, false) + var value = matches[2].strip(true, false) hs.snippets[id] = value + if matches[1] == "=>": + value = "" result = result.replace(def, value) for snippet in document.findAll(peg_snippet): var matches:TSnippet discard snippet.match(peg_snippet, matches) var id = matches[0].strip - if hs.snippets[id] == nil: + if hs.snippets.hasKey(id): + result = result.replace(snippet, hs.snippets[id]) + else: stderr.writeLine "Warning: Snippet '" & id & "' not defined." result = result.replace(snippet, "") - else: - result = result.replace(snippet, hs.snippets[id]) proc preprocess(hs: var HastyScribe, document, dir: string, offset = 0): string = result = hs.parse_transclusions(document, dir, offset)