Skip to content

Commit a4799ca

Browse files
committed
Fixed #46; implemented substitution macros.
1 parent 61f2922 commit a4799ca

5 files changed

+131
-41
lines changed

LICENSE.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
## HastyScribe (program/library)
22

3-
Copyright (c) 2013-2014 Fabio Cevasco
3+
Copyright (c) 2013-2016 Fabio Cevasco
44

55
Released under the terms of the [MIT License](http://opensource.org/licenses/MIT).
66

77
## HastyScribe (font)
88

9-
Copyright (c) 2013-2014, Fabio Cevasco, with Reserved Font Name 'HastyScribe'.
9+
Copyright (c) 2013-2016, Fabio Cevasco, with Reserved Font Name 'HastyScribe'.
1010

1111
Released under the terms of the [Open Font License](http://scripts.sil.org/OFL).
1212

doc/HastyScribe_UserGuide.htm

+50-25
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@
8484
<li><a href="#Discount.Extensions">Discount Extensions</a></li>
8585
<li><a href="#Text.Snippets">Text Snippets</a></li>
8686
<li><a href="#Custom.Fields">Custom Fields</a></li>
87+
<li><a href="#Substitution.Macros">Substitution Macros</a></li>
8788
<li><a href="#Image..and.font..Embedding">Image (and font) Embedding</a></li>
8889
<li><a href="#FontAwesome.Icons">FontAwesome Icons</a></li>
8990
<li><a href="#Notes..tips..warnings..sidebars.and.badges">Notes, tips, warnings, sidebars and badges</a></li>
@@ -113,6 +114,7 @@
113114
<li><a href="#Document.Headers">Document Headers</a></li>
114115
<li><a href="#Snippets">Snippets</a></li>
115116
<li><a href="#Fields">Fields</a></li>
117+
<li><a href="#Macros">Macros</a></li>
116118
<li><a href="#Inline.Formatting">Inline Formatting</a>
117119
<ul>
118120
<li><a href="#SmartyPants.Substitutions">SmartyPants Substitutions</a></li>
@@ -210,6 +212,10 @@ <h4 id="Custom.Fields">Custom Fields<a href="#document-top" title="Go to top"></
210212

211213
<p>HastyScribe also supports <a href="#Fields">fields</a> to easily include things like the current date or time, but also custom values specified as command-line parameters.</p>
212214

215+
<h4 id="Substitution.Macros">Substitution Macros<a href="#document-top" title="Go to top"></a></h4>
216+
217+
<p>If you find yourself writing chunks of text that follows the same format except for some content, you can define simple text substitution <a href="#Macros">macros</a> for even higher content reuse.</p>
218+
213219
<h4 id="Image..and.font..Embedding">Image (and font) Embedding<a href="#document-top" title="Go to top"></a></h4>
214220

215221
<p>HastyScribe only produces single HTML files. With <em>no dependencies</em>:</p>
@@ -247,12 +253,12 @@ <h3 id="Downloading.Pre-built.Binaries">Downloading Pre-built Binaries<a href="#
247253
<p>The easiest way to get HastyScribe is by downloading one of the prebuilt binaries from the <a href="https://github.com/h3rald/hastyscribe/releases/download/v">Github Release Page</a>:</p>
248254

249255
<ul>
250-
<li><a href="https://github.com/h3rald/hastyscribe/releases/download/v/hastyscribe_v_macosx_x64.zip">HastyScribe for Mac OS X (x64)</a> &ndash; Compiled on OS X El Capitan (LLVM CLANG 7.0)</li>
251-
<li><a href="https://github.com/h3rald/hastyscribe/releases/download/v/hastyscribe_v_windows_x64.zip">HastyScribe for Windows (x64)</a> &ndash; Cross-compiled on OS X El Capitan (MinGW-w64 GCC 4.8.2)</li>
252-
<li><a href="https://github.com/h3rald/hastyscribe/releases/download/v/hastyscribe_v_windows_x86.zip">HastyScribe for Windows (x86)</a> &ndash; Cross-compiled on OS X El Capitan (MinGW-w64 GCC 4.8.2)</li>
253-
<li><a href="https://github.com/h3rald/hastyscribe/releases/download/v/hastyscribe_v_linux_x64.zip">HastyScribe for Linux (x64)</a> &ndash; Cross-compiled on OS X El Capitan (GNU GCC 4.8.1)</li>
254-
<li><a href="https://github.com/h3rald/hastyscribe/releases/download/v/hastyscribe_v_linux_x86.zip">HastyScribe for Linux (x86)</a> &ndash; Cross-compiled on OS X El Capitan (GNU GCC 4.8.1)</li>
255-
<li><a href="https://github.com/h3rald/hastyscribe/releases/download/v/hastyscribe_v_linux_arm.zip">HastyScribe for Linux (ARM)</a> &ndash; Cross-compiled on OS X El Capitan (GNU GCC 4.8.2)</li>
256+
<li><a href="https://github.com/h3rald/hastyscribe/releases/download/v1.3.0/hastyscribe_v1.3.0_macos_x64.zip">HastyScribe for Mac OS X (x64)</a> &ndash; Compiled on Mac OS X Sierra (LLVM CLANG 8.0.0)</li>
257+
<li><a href="https://github.com/h3rald/hastyscribe/releases/download/v1.3.0/hastyscribe_v1.3.0_windows_x64.zip">HastyScribe for Windows (x64)</a> &ndash; Cross-compiled on Mac OS X Sierra (MinGW-w64 GCC 4.8.2)</li>
258+
<li><a href="https://github.com/h3rald/hastyscribe/releases/download/v1.3.0/hastyscribe_v1.3.0_windows_x86.zip">HastyScribe for Windows (x86)</a> &ndash; Cross-compiled on Mac OS X Sierra (MinGW-w64 GCC 4.8.2)</li>
259+
<li><a href="https://github.com/h3rald/hastyscribe/releases/download/v1.3.0/hastyscribe_v1.3.0_linux_x64.zip">HastyScribe for Linux (x64)</a> &ndash; Cross-compiled on Mac OS X Sierra (GNU GCC 4.8.1)</li>
260+
<li><a href="https://github.com/h3rald/hastyscribe/releases/download/v1.3.0/hastyscribe_v1.3.0_linux_x86.zip">HastyScribe for Linux (x86)</a> &ndash; Cross-compiled on Mac OS X Sierra (GNU GCC 4.8.1)</li>
261+
<li><a href="https://github.com/h3rald/hastyscribe/releases/download/v1.3.0/hastyscribe_v1.3.0_linux_arm.zip">HastyScribe for Linux (ARM)</a> &ndash; Cross-compiled on Mac OS X Sierra (GNU GCC 4.8.2)</li>
256262
</ul>
257263

258264

@@ -396,59 +402,59 @@ <h3 id="Fields">Fields<a href="#document-top" title="Go to top"></a></h3>
396402
<tbody>
397403
<tr>
398404
<td><code>{{$timestamp}}</code> </td>
399-
<td> 1466790332</td>
405+
<td> 1476008117</td>
400406
</tr>
401407
<tr>
402408
<td><code>{{$date}}</code> </td>
403-
<td> 2016-06-24</td>
409+
<td> 2016-10-09</td>
404410
</tr>
405411
<tr>
406412
<td><code>{{$full-date}}</code> </td>
407-
<td> Friday, June 24, 2016</td>
413+
<td> Sunday, October 9, 2016</td>
408414
</tr>
409415
<tr>
410416
<td><code>{{$long-date}}</code> </td>
411-
<td> June 24, 2016</td>
417+
<td> October 9, 2016</td>
412418
</tr>
413419
<tr>
414420
<td><code>{{$medium-date}}</code> </td>
415-
<td> Jun 24, 2016</td>
421+
<td> Oct 9, 2016</td>
416422
</tr>
417423
<tr>
418424
<td><code>{{$short-date}}</code> </td>
419-
<td> 6/24/16</td>
425+
<td> 10/9/16</td>
420426
</tr>
421427
<tr>
422428
<td><code>{{$short-time}}</code> </td>
423-
<td> 19:45 PM</td>
429+
<td> 12:15 PM</td>
424430
</tr>
425431
<tr>
426432
<td><code>{{$short-time-24}}</code> </td>
427-
<td> 19:45</td>
433+
<td> 12:15</td>
428434
</tr>
429435
<tr>
430436
<td><code>{{$time}}</code> </td>
431-
<td> 19:45:32 PM</td>
437+
<td> 12:15:17 PM</td>
432438
</tr>
433439
<tr>
434440
<td><code>{{$time-24}}</code> </td>
435-
<td> 19:45:32</td>
441+
<td> 12:15:17</td>
436442
</tr>
437443
<tr>
438444
<td><code>{{$day}}</code> </td>
439-
<td> 24</td>
445+
<td> 09</td>
440446
</tr>
441447
<tr>
442448
<td><code>{{$short-day}}</code> </td>
443-
<td> 24</td>
449+
<td> 9</td>
444450
</tr>
445451
<tr>
446452
<td><code>{{$month}}</code> </td>
447-
<td> 06</td>
453+
<td> 10</td>
448454
</tr>
449455
<tr>
450456
<td><code>{{$short-month}}</code> </td>
451-
<td> 6</td>
457+
<td> 10</td>
452458
</tr>
453459
<tr>
454460
<td><code>{{$year}}</code> </td>
@@ -460,19 +466,19 @@ <h3 id="Fields">Fields<a href="#document-top" title="Go to top"></a></h3>
460466
</tr>
461467
<tr>
462468
<td><code>{{$weekday}}</code> </td>
463-
<td> Friday</td>
469+
<td> Sunday</td>
464470
</tr>
465471
<tr>
466472
<td><code>{{$weekday-abbr}}</code> </td>
467-
<td> 24</td>
473+
<td> 09</td>
468474
</tr>
469475
<tr>
470476
<td><code>{{$month-name}}</code> </td>
471-
<td> June</td>
477+
<td> October</td>
472478
</tr>
473479
<tr>
474480
<td><code>{{$month-name-abbr}}</code> </td>
475-
<td> Jun</td>
481+
<td> Oct</td>
476482
</tr>
477483
<tr>
478484
<td><code>{{$timezone}}</code> </td>
@@ -492,6 +498,25 @@ <h3 id="Fields">Fields<a href="#document-top" title="Go to top"></a></h3>
492498

493499
<p>In this case it will be possible to access the <span class="kwd">product</span> and <span class="kwd">product</span> fields within <span class="file">my-document.md</span> using <code>{{$product}}</code> and <code>{{$version}}</code>.</p>
494500

501+
<h3 id="Macros">Macros<a href="#document-top" title="Go to top"></a></h3>
502+
503+
<p>If snippets are not enough, and you want to reuse chunks of <em>similar</em> content, you can define substitution macros using the following syntax:</p>
504+
505+
<p><code>{#greet -> Hello, $1! Are you $2?#}</code></p>
506+
507+
<p>This defines a macro called <span class="kwd">greet</span> that takes two parameters which will be substituted instead of <span class="kwd">$1</span> and <span class="kwd">$2</span>. To use the macro, use the following syntax:</p>
508+
509+
<p><code>{#greet||Fabio||ready#}</code></p>
510+
511+
<div class="note"><p>Note</p>
512+
513+
<ul>
514+
<li>Like snippets, macros can be multiline.</li>
515+
<li>Spaces and newline character are preseved ad the start and end of parameters.</li>
516+
<li>You can use snippets and fields within macros (but you cannot nest macros inside other macros).</li>
517+
</ul>
518+
</div>
519+
495520
<h3 id="Inline.Formatting">Inline Formatting<a href="#document-top" title="Go to top"></a></h3>
496521

497522
<p>The following table lists all the most common ways to format inline text:</p>
@@ -1341,7 +1366,7 @@ <h2 id="Credits">Credits<a href="#document-top" title="Go to top"></a></h2>
13411366
<p style="text-align:center;"> <img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAFgAAAAfCAYAAABjyArgAAAGa0lEQVRo3u1aW08bRxTmuSqIV0SlIFWtAKkVtEpfWiVI6Q+gT60qpeIhTXkrSpqoaZ1AkJMobSRkkUahEtAWCVWBYMDc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.0 Unported License" /></p>
13421367
</div>
13431368
<div id="footer">
1344-
<p><span class="copy"></span> Fabio Cevasco &ndash; June 24, 2016</p>
1369+
<p><span class="copy"></span> Fabio Cevasco &ndash; October 9, 2016</p>
13451370
<p><span>Powered by</span> <a href="https://h3rald.com/hastyscribe"><span class="hastyscribe"></span></a></p>
13461371
</div>
13471372
</div>

doc/HastyScribe_UserGuide.md

+30-6
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,11 @@ Although not part of neither {{md}} nor Discount, {{hs}} allows you to create te
4747

4848
{{hs}} also supports [fields](#Fields) to easily include things like the current date or time, but also custom values specified as command-line parameters.
4949

50+
51+
#### Substitution Macros
52+
53+
If you find yourself writing chunks of text that follows the same format except for some content, you can define simple text substitution [macros](#Macros) for even higher content reuse.
54+
5055
#### Image (and font) Embedding
5156

5257
{{hs}} only produces single HTML files. With _no dependencies_:
@@ -79,14 +84,16 @@ All HTML documents created by {{hs}} are responsive and can be viewed perfectly
7984

8085
### Downloading Pre-built Binaries
8186

87+
{# release -> [HastyScribe for $1]({{release}}{{$version}}/hastyscribe_v{{$version}}_$2.zip) -- $3 #}
88+
8289
The easiest way to get {{hs}} is by downloading one of the prebuilt binaries from the [Github Release Page][release]:
8390

84-
* [HastyScribe for Mac OS X (x64)]({{release}}{{$version}}/hastyscribe_v{{$version}}_macosx_x64.zip) -- Compiled on OS X El Capitan (LLVM CLANG 7.0)
85-
* [HastyScribe for Windows (x64)]({{release}}{{$version}}/hastyscribe_v{{$version}}_windows_x64.zip) -- Cross-compiled on OS X El Capitan (MinGW-w64 GCC 4.8.2)
86-
* [HastyScribe for Windows (x86)]({{release}}{{$version}}/hastyscribe_v{{$version}}_windows_x86.zip) -- Cross-compiled on OS X El Capitan (MinGW-w64 GCC 4.8.2)
87-
* [HastyScribe for Linux (x64)]({{release}}{{$version}}/hastyscribe_v{{$version}}_linux_x64.zip) -- Cross-compiled on OS X El Capitan (GNU GCC 4.8.1)
88-
* [HastyScribe for Linux (x86)]({{release}}{{$version}}/hastyscribe_v{{$version}}_linux_x86.zip) -- Cross-compiled on OS X El Capitan (GNU GCC 4.8.1)
89-
* [HastyScribe for Linux (ARM)]({{release}}{{$version}}/hastyscribe_v{{$version}}_linux_arm.zip) -- Cross-compiled on OS X El Capitan (GNU GCC 4.8.2)
91+
* {#release||Mac OS X (x64)||macos_x64||Compiled on {{osx -> Mac OS X Sierra}} (LLVM CLANG 8.0.0)#}
92+
* {#release||Windows (x64)||windows_x64||{{xcc -> Cross-compiled on}} {{osx}} (MinGW-w64 GCC 4.8.2)#}
93+
* {#release||Windows (x86)||windows_x86||{{xcc}} {{osx}} (MinGW-w64 GCC 4.8.2)#}
94+
* {#release||Linux (x64)||linux_x64||{{xcc}} {{osx}} (GNU GCC 4.8.1)#}
95+
* {#release||Linux (x86)||linux_x86||{{xcc}} {{osx}} (GNU GCC 4.8.1)#}
96+
* {#release||Linux (ARM)||linux_arm||{{xcc}} {{osx}} (GNU GCC 4.8.2)#}
9097

9198
### Installing using Nimble
9299

@@ -243,6 +250,23 @@ Additionally, you can define your own custom fields via command-line parameters,
243250
244251
In this case it will be possible to access the [product](class:kwd) and [product](class:kwd) fields within [my-document.md](class:file) using <code>\{\{$product\}\}</code> and <code>\{\{$version\}\}</code>.
245252

253+
### Macros
254+
255+
If snippets are not enough, and you want to reuse chunks of _similar_ content, you can define substitution macros using the following syntax:
256+
257+
<code>\{#greet -> Hello, $1! Are you $2?#\}</code>
258+
259+
This defines a macro called [greet](class:kwd) that takes two parameters which will be substituted instead of [$1](class:kwd) and [$2](class:kwd). To use the macro, use the following syntax:
260+
261+
<code>\{#greet||Fabio||ready#\}</code>
262+
263+
> %note%
264+
> Note
265+
>
266+
> * Like snippets, macros can be multiline.
267+
> * Spaces and newline character are preseved ad the start and end of parameters.
268+
> * You can use snippets and fields within macros (but you cannot nest macros inside other macros).
269+
246270
### Inline Formatting
247271

248272
The following table lists all the most common ways to format inline text:

hastyscribe.nim

+47-6
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ let
7373
image <- '<img' \s+ 'src=' ["] {file} ["]
7474
file <- [^"]+
7575
"""
76-
peg_def = peg"""
76+
peg_snippet_def = peg"""
7777
definition <- '{{' \s* {id} \s* '->' {@} '}}'
7878
id <- [a-zA-Z0-9_-]+
7979
"""
@@ -85,11 +85,19 @@ let
8585
field <- '{{' \s* '$' {id} \s* '}}'
8686
id <- [a-zA-Z0-9_-]+
8787
"""
88+
peg_macro_def = peg"""
89+
definition <- '{#' \s* {id} \s* '->' {@} '#}'
90+
id <- [a-zA-Z0-9_-]+
91+
"""
92+
peg_macro_instance = peg"""
93+
instance <- "{#" \s* {id} \s* "||" \s* {@} "#}"
94+
id <- [a-zA-Z0-9_-]+
95+
"""
8896

8997
var FIELDS = initTable[string, proc():string]()
9098

9199

92-
var NOW:TimeInfo
100+
var NOW:TimeInfo = getTime().getLocalTime()
93101

94102
FIELDS["timestamp"] = proc():string =
95103
return $NOW.toTime.toSeconds().int
@@ -232,7 +240,8 @@ proc embed_images*(document, dir: string): string =
232240
continue
233241
elif imgfile.startsWith(peg"http[s]?'://'"):
234242
try:
235-
imgcontent = encode_image(getContent(imgfile, timeout = 5000), imgformat)
243+
let client = newHttpClient()
244+
imgcontent = encode_image(client.getContent(imgfile), imgformat)
236245
except:
237246
stderr.writeLine "Warning: Unable to download '" & imgfile & "'"
238247
stderr.writeLine " Reason: " & getCurrentExceptionMsg()
@@ -280,6 +289,36 @@ var fonts* = [
280289
proc embed_fonts*(): string=
281290
return style_tag(fonts.join);
282291

292+
# Macro Definition:
293+
# {{#test -> This is a $1}}
294+
#
295+
# Macro Usage:
296+
# {{#test||simple test}}
297+
proc parse_macros*(document: string): string =
298+
var macros:Table[string, string] = initTable[string, string]()
299+
var doc = document
300+
for def in findAll(document, peg_macro_def):
301+
var matches: array[0..1, string]
302+
discard def.match(peg_macro_def, matches)
303+
let id = matches[0].strip
304+
let value = matches[1].strip
305+
macros[id] = value
306+
doc = doc.replace(def, "")
307+
for instance in findAll(doc, peg_macro_instance):
308+
var matches: array[0..1, string]
309+
discard instance.match(peg_macro_instance, matches)
310+
let id = matches[0].strip
311+
let value = matches[1].strip
312+
let params = value.split("||")
313+
if macros.hasKey(id):
314+
try:
315+
doc = doc.replace(instance, macros[id] % params)
316+
except:
317+
stderr.writeLine "Warning: Incorrect number of parameters specified for macro '$1'\n -> Instance: $2" % [id, instance]
318+
else:
319+
stderr.writeLine "Warning: Macro '" & id & "' not defined."
320+
doc = doc.replace(instance, "")
321+
return doc
283322

284323
# Field Usage:
285324
# {{$timestamp}}
@@ -310,9 +349,9 @@ proc parse_snippets*(document: string): string =
310349
TSnippetDef = array[0..1, string]
311350
TSnippet = array[0..0, string]
312351
var doc = document
313-
for def in findAll(document, peg_def):
352+
for def in findAll(document, peg_snippet_def):
314353
var matches:TSnippetDef
315-
discard def.match(peg_def, matches)
354+
discard def.match(peg_snippet_def, matches)
316355
var id = matches[0].strip
317356
var value = matches[1].strip(true, false)
318357
snippets[id] = value
@@ -337,9 +376,10 @@ proc compile*(input_file: string) =
337376

338377
var source = input_file.readFile
339378

340-
# Parse fields and snippets
379+
# Parse fields, snippets, and macros
341380
source = parse_fields(source)
342381
source = parse_snippets(source)
382+
source = parse_macros(source)
343383

344384
# Document Variables
345385
var metadata = TMDMetaData(title:"", author:"", date:"", toc:"", css:"")
@@ -443,6 +483,7 @@ when isMainModule:
443483
output_file = val
444484
else:
445485
if key.startsWith("field/"):
486+
let val = val
446487
FIELDS[key.replace("field/", "")] = proc(): string =
447488
return val
448489
discard

hastyscribe.nimble

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[Package]
22
name = "hastyscribe"
3-
version = "1.2.1"
3+
version = "1.3.0"
44
author = "Fabio Cevasco"
55
description = "Self-contained markdown compiler generating self-contained HTML documents"
66
license = "MIT"
@@ -45,4 +45,4 @@ vendor/libmarkdown_linux_arm.a
4545
"""
4646

4747
[Deps]
48-
Requires: "nimrod >= 0.14.0"
48+
Requires: "nimrod >= 0.15.0"

0 commit comments

Comments
 (0)