123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247 |
- <?xml version="1.0" encoding="utf-8" ?>
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <!-- This file is generated by Nim. -->
- <html xmlns="https://www.w3.org/1999/xhtml" xml:lang="en" lang="en" data-theme="auto">
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <title>Source Code Filters</title>
- <!-- Google fonts -->
- <link href='https://fonts.googleapis.com/css?family=Lato:400,600,900' rel='stylesheet' type='text/css'/>
- <link href='https://fonts.googleapis.com/css?family=Source+Code+Pro:400,500,600' rel='stylesheet' type='text/css'/>
- <!-- Favicon -->
- <link rel="shortcut icon" href="data:image/x-icon;base64,AAABAAEAEBAAAAEAIABoBAAAFgAAACgAAAAQAAAAIAAAAAEAIAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AAAAAAUAAAAF////AP///wD///8A////AP///wD///8A////AP///wD///8A////AAAAAAIAAABbAAAAlQAAAKIAAACbAAAAmwAAAKIAAACVAAAAWwAAAAL///8A////AP///wD///8A////AAAAABQAAADAAAAAYwAAAA3///8A////AP///wD///8AAAAADQAAAGMAAADAAAAAFP///wD///8A////AP///wAAAACdAAAAOv///wD///8A////AP///wD///8A////AP///wD///8AAAAAOgAAAJ3///8A////AP///wAAAAAnAAAAcP///wAAAAAoAAAASv///wD///8A////AP///wAAAABKAAAAKP///wAAAABwAAAAJ////wD///8AAAAAgQAAABwAAACIAAAAkAAAAJMAAACtAAAAFQAAABUAAACtAAAAkwAAAJAAAACIAAAAHAAAAIH///8A////AAAAAKQAAACrAAAAaP///wD///8AAAAARQAAANIAAADSAAAARf///wD///8AAAAAaAAAAKsAAACk////AAAAADMAAACcAAAAnQAAABj///8A////AP///wAAAAAYAAAAGP///wD///8A////AAAAABgAAACdAAAAnAAAADMAAAB1AAAAwwAAAP8AAADpAAAAsQAAAE4AAAAb////AP///wAAAAAbAAAATgAAALEAAADpAAAA/wAAAMMAAAB1AAAAtwAAAOkAAAD/AAAA/wAAAP8AAADvAAAA3gAAAN4AAADeAAAA3gAAAO8AAAD/AAAA/wAAAP8AAADpAAAAtwAAAGUAAAA/AAAA3wAAAP8AAAD/AAAA/wAAAP8AAAD/AAAA/wAAAP8AAAD/AAAA/wAAAP8AAADfAAAAPwAAAGX///8A////AAAAAEgAAADtAAAAvwAAAL0AAADGAAAA7wAAAO8AAADGAAAAvQAAAL8AAADtAAAASP///wD///8A////AP///wD///8AAAAAO////wD///8A////AAAAAIcAAACH////AP///wD///8AAAAAO////wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A//8AAP//AAD4HwAA7/cAAN/7AAD//wAAoYUAAJ55AACf+QAAh+EAAAAAAADAAwAA4AcAAP5/AAD//wAA//8AAA=="/>
- <link rel="icon" type="image/png" sizes="32x32" href="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAYAAABzenr0AAAABmJLR0QA/wD/AP+gvaeTAAAACXBIWXMAAA3XAAAN1wFCKJt4AAAAB3RJTUUH4QQQEwksSS9ZWwAAAk1JREFUWMPtll2ITVEUx39nn/O7Y5qR8f05wtCUUr6ZIS++8pEnkZInPImneaCQ5METNdOkeFBKUhMPRIkHKfEuUZSUlGlKPN2TrgfncpvmnntnmlEyq1Z7t89/rf9a6+y99oZxGZf/XeIq61EdtgKXgdXA0xrYAvBjOIF1AI9zvjcC74BSpndrJPkBWDScTF8Aa4E3wDlgHbASaANmVqlcCnwHvgDvgVfAJ+AikAAvgfVZwLnSVZHZaOuKoQi3ZOMi4NkYkpe1p4J7A8BpYAD49hfIy/oqG0+hLomiKP2L5L+1ubn5115S+3OAn4EnwBlgMzCjyt6ZAnQCJ4A7wOs88iRJHvw50HoujuPBoCKwHWiosy8MdfZnAdcHk8dxXFJ3VQbQlCTJvRBCGdRbD4M6uc5glpY3eAihpN5S5w12diSEcCCEcKUO4ljdr15T76ur1FDDLIQQ3qv71EdDOe3Kxj3leRXyk+pxdWnFWod6Wt2bY3de3aSuUHcPBVimHs7mK9WrmeOF6lR1o9qnzskh2ar2qm1qizpfXaPeVGdlmGN5pb09qMxz1Xb1kLqgzn1RyH7JUXW52lr5e/Kqi9qpto7V1atuUzfnARrV7jEib1T76gG2qxdGmXyiekkt1GswPTtek0aBfJp6YySGBfWg2tPQ0FAYgf1stUfdmdcjarbYJEniKIq6gY/Aw+zWHAC+p2labGpqiorFYgGYCEzN7oQdQClN07O1/EfDyGgC0ALMBdYAi4FyK+4H3gLPsxfR1zRNi+NP7nH5J+QntnXe5B5mpfQAAAAASUVORK5CYII=">
- <!-- CSS -->
- <link rel="stylesheet" type="text/css" href="nimdoc.out.css?v=2.3.1">
- <!-- JS -->
- <script type="text/javascript" src="dochack.js?v=2.3.1"></script>
- </head>
- <body>
- <div class="document" id="documentId">
- <div class="container">
- <h1 class="title">Source Code Filters</h1>
- <div class="row">
- <div class="three columns">
- <div class="theme-select-wrapper">
- <label for="theme-select">Theme: </label>
- <select id="theme-select" onchange="setTheme(this.value)">
- <option value="auto">🌗 Match OS</option>
- <option value="dark">🌑 Dark</option>
- <option value="light">🌕 Light</option>
- </select>
- </div>
- <div id="global-links">
- <ul class="simple-boot">
- <li><a href="manual.html">Manual</a></li>
- <li><a href="lib.html">Standard library</a></li>
- <li> <a id="indexLink" href="theindex.html">Index</a></li>
- <li><a href="compiler/theindex.html">Compiler docs</a></li>
- <li><a href="https://nim-lang.github.io/fusion/theindex.html">Fusion docs</a></li>
- <li><a href="https://nim-lang.github.io/Nim/">devel</a>, <a href="https://nim-lang.org/documentation.html">stable</a></li>
- </ul>
- </div>
- <div id="searchInputDiv">
- Search: <input type="search" id="searchInput"
- oninput="search()" />
- </div>
- <div class="search-groupby">
- Group by:
- <select onchange="groupBy(this.value)">
- <option value="section">Section</option>
- <option value="type">Type</option>
- </select>
- </div>
- <ul class="simple simple-toc" id="toc-list">
- <li><a class="reference" id="usage_toc" href="#usage">Usage</a></li>
- <li><a class="reference" id="pipe-operator_toc" href="#pipe-operator">Pipe operator</a></li>
- <li><a class="reference" id="available-filters_toc" href="#available-filters">Available filters</a></li>
- <ul class="simple"><li><a class="reference" id="available-filters-replace-filter_toc" href="#available-filters-replace-filter">Replace filter</a></li>
- <li><a class="reference" id="available-filters-strip-filter_toc" href="#available-filters-strip-filter">Strip filter</a></li>
- <li><a class="reference" id="available-filters-stdtmpl-filter_toc" href="#available-filters-stdtmpl-filter">StdTmpl filter</a></li>
- </ul>
- </ul>
- </div>
- <div class="nine columns" id="content">
- <a href="https://github.com/nim-lang/Nim/tree/devel/doc/filters.md#L1" class="link-seesrc" target="_blank">Source</a>
- <a href="https://github.com/nim-lang/Nim/edit/devel/doc/filters.md#L1" class="link-seesrc" target="_blank" >Edit</a>
- <div id="tocRoot"></div>
-
- <p class="module-desc"><p>A <tt class="docutils literal"><span class="pre">Source Code Filter (SCF)</span></tt> transforms the input character stream to an in-memory output stream before parsing. A filter can be used to provide templating systems or preprocessors.</p>
- <p>To use a filter for a source file the <tt class="docutils literal"><span class="pre">#?</span></tt> notation is used:</p>
- <pre>#? stdtmpl(subsChar = '$', metaChar = '#')
- #proc generateXML(name, age: string): string =
- # result = ""
- <xml>
- <name>$name</name>
- <age>$age</age>
- </xml></pre>
- <p>As the example shows, passing arguments to a filter can be done just like an ordinary procedure call with named or positional arguments. The available parameters depend on the invoked filter. Before version 0.12.0 of the language <tt class="docutils literal"><span class="pre">#!</span></tt> was used instead of <tt class="docutils literal"><span class="pre">#?</span></tt>.</p>
- <p><strong>Hint:</strong> With <tt class="docutils literal"><span class="pre option">--hint:codeBegin:on</span></tt> or <tt class="docutils literal"><span class="pre option">--verbosity:2</span></tt> (or higher) while compiling or <tt class="docutils literal"><span class="pre"><span class="program">nim</span> <span class="option">check</span></span></tt>, Nim lists the processed code after each filter application.</p>
- <h1><a class="toc-backref" id="usage" href="#usage">Usage</a></h1><p>First, put your SCF code in a separate file with filters specified in the first line. <strong>Note:</strong> You can name your SCF file with any file extension you want, but the conventional extension is <tt class="docutils literal"><span class="pre">.nimf</span></tt> (it used to be <tt class="docutils literal"><span class="pre">.tmpl</span></tt> but that was too generic, for example preventing github to recognize it as Nim source file).</p>
- <p>If we use <tt class="docutils literal"><span class="pre">generateXML</span></tt> code shown above and call the SCF file <tt class="docutils literal"><span class="pre">xmlGen.nimf</span></tt> In your <tt class="docutils literal"><span class="pre">main.nim</span></tt>:</p>
- <p><pre class="listing"><span class="Keyword">include</span> <span class="StringLit">"xmlGen.nimf"</span>
- <span class="Identifier">echo</span> <span class="Identifier">generateXML</span><span class="Punctuation">(</span><span class="StringLit">"John Smith"</span><span class="Punctuation">,</span><span class="StringLit">"42"</span><span class="Punctuation">)</span></pre></p>
- <h1><a class="toc-backref" id="pipe-operator" href="#pipe-operator">Pipe operator</a></h1><p>Filters can be combined with the <tt class="docutils literal"><span class="pre">|</span></tt> pipe operator:</p>
- <pre>#? strip(startswith="<") | stdtmpl
- #proc generateXML(name, age: string): string =
- # result = ""
- <xml>
- <name>$name</name>
- <age>$age</age>
- </xml></pre>
- <h1><a class="toc-backref" id="available-filters" href="#available-filters">Available filters</a></h1>
- <h2><a class="toc-backref" id="available-filters-replace-filter" href="#available-filters-replace-filter">Replace filter</a></h2><p>The replace filter replaces substrings in each line.</p>
- <p>Parameters and their defaults:</p>
- <ul class="simple"><li><dl class="docutils"><dt><tt class="docutils literal"><span class="pre">sub: string = ""</span></tt></dt>
- <dd>the substring that is searched for</dd>
- </dl>
- </li>
- <li><dl class="docutils"><dt><tt class="docutils literal"><span class="pre">by: string = ""</span></tt></dt>
- <dd>the string the substring is replaced with</dd>
- </dl>
- </li>
- </ul>
- <h2><a class="toc-backref" id="available-filters-strip-filter" href="#available-filters-strip-filter">Strip filter</a></h2><p>The strip filter simply removes leading and trailing whitespace from each line.</p>
- <p>Parameters and their defaults:</p>
- <ul class="simple"><li><dl class="docutils"><dt><tt class="docutils literal"><span class="pre">startswith: string = ""</span></tt></dt>
- <dd>strip only the lines that start with <em>startswith</em> (ignoring leading whitespace). If empty every line is stripped.</dd>
- </dl>
- </li>
- <li><dl class="docutils"><dt><tt class="docutils literal"><span class="pre">leading: bool = true</span></tt></dt>
- <dd>strip leading whitespace</dd>
- </dl>
- </li>
- <li><dl class="docutils"><dt><tt class="docutils literal"><span class="pre">trailing: bool = true</span></tt></dt>
- <dd>strip trailing whitespace</dd>
- </dl>
- </li>
- </ul>
- <h2><a class="toc-backref" id="available-filters-stdtmpl-filter" href="#available-filters-stdtmpl-filter">StdTmpl filter</a></h2><p>The stdtmpl filter provides a simple templating engine for Nim. The filter uses a line based parser: Lines prefixed with a <em>meta character</em> (default: <tt class="docutils literal"><span class="pre">#</span></tt>) contain Nim code, other lines are verbatim. Because indentation-based parsing is not suited for a templating engine, control flow statements need <tt class="docutils literal"><span class="pre">end X</span></tt> delimiters.</p>
- <p>Parameters and their defaults:</p>
- <ul class="simple"><li><dl class="docutils"><dt><tt class="docutils literal"><span class="pre">metaChar: char = '#'</span></tt></dt>
- <dd>prefix for a line that contains Nim code</dd>
- </dl>
- </li>
- <li><dl class="docutils"><dt><tt class="docutils literal"><span class="pre">subsChar: char = '$'</span></tt></dt>
- <dd>prefix for a Nim expression within a template line</dd>
- </dl>
- </li>
- <li><dl class="docutils"><dt><tt class="docutils literal"><span class="pre">conc: string = " & "</span></tt></dt>
- <dd>the operation for concatenation</dd>
- </dl>
- </li>
- <li><dl class="docutils"><dt><tt class="docutils literal"><span class="pre">emit: string = "result.add"</span></tt></dt>
- <dd>the operation to emit a string literal</dd>
- </dl>
- </li>
- <li><dl class="docutils"><dt><tt class="docutils literal"><span class="pre">toString: string = "$"</span></tt></dt>
- <dd>the operation that is applied to each expression</dd>
- </dl>
- </li>
- </ul>
- <p>Example:</p>
- <pre>#? stdtmpl | standard
- #proc generateHTMLPage(title, currentTab, content: string,
- # tabs: openArray[string]): string =
- # result = ""
- <head><title>$title</title></head>
- <body>
- <div id="menu">
- <ul>
- #for tab in items(tabs):
- #if currentTab == tab:
- <li><a id="selected"
- #else:
- <li><a
- #end if
- href="${tab}.html">$tab</a></li>
- #end for
- </ul>
- </div>
- <div id="content">
- $content
- A dollar: $$.
- </div>
- </body></pre>
- <p>The filter transforms this into:</p>
- <p><pre class="listing"><span class="Keyword">proc</span> <span class="Identifier">generateHTMLPage</span><span class="Punctuation">(</span><span class="Identifier">title</span><span class="Punctuation">,</span> <span class="Identifier">currentTab</span><span class="Punctuation">,</span> <span class="Identifier">content</span><span class="Punctuation">:</span> <span class="Identifier">string</span><span class="Punctuation">,</span>
- <span class="Identifier">tabs</span><span class="Punctuation">:</span> <span class="Identifier">openArray</span><span class="Punctuation">[</span><span class="Identifier">string</span><span class="Punctuation">]</span><span class="Punctuation">)</span><span class="Punctuation">:</span> <span class="Identifier">string</span> <span class="Operator">=</span>
- <span class="Identifier">result</span> <span class="Operator">=</span> <span class="StringLit">""</span>
- <span class="Identifier">result</span><span class="Operator">.</span><span class="Identifier">add</span><span class="Punctuation">(</span><span class="StringLit">"<head><title>"</span> <span class="Operator">&</span> <span class="Operator">$</span><span class="Punctuation">(</span><span class="Identifier">title</span><span class="Punctuation">)</span> <span class="Operator">&</span> <span class="StringLit">"</title></head></span><span class="EscapeSequence">\n</span><span class="StringLit">"</span> <span class="Operator">&</span>
- <span class="StringLit">"<body></span><span class="EscapeSequence">\n</span><span class="StringLit">"</span> <span class="Operator">&</span>
- <span class="StringLit">" <div id=</span><span class="EscapeSequence">\"</span><span class="StringLit">menu</span><span class="EscapeSequence">\"</span><span class="StringLit">></span><span class="EscapeSequence">\n</span><span class="StringLit">"</span> <span class="Operator">&</span>
- <span class="StringLit">" <ul></span><span class="EscapeSequence">\n</span><span class="StringLit">"</span><span class="Punctuation">)</span>
- <span class="Keyword">for</span> <span class="Identifier">tab</span> <span class="Keyword">in</span> <span class="Identifier">items</span><span class="Punctuation">(</span><span class="Identifier">tabs</span><span class="Punctuation">)</span><span class="Punctuation">:</span>
- <span class="Keyword">if</span> <span class="Identifier">currentTab</span> <span class="Operator">==</span> <span class="Identifier">tab</span><span class="Punctuation">:</span>
- <span class="Identifier">result</span><span class="Operator">.</span><span class="Identifier">add</span><span class="Punctuation">(</span><span class="StringLit">" <li><a id=</span><span class="EscapeSequence">\"</span><span class="StringLit">selected</span><span class="EscapeSequence">\"</span><span class="StringLit"> </span><span class="EscapeSequence">\n</span><span class="StringLit">"</span><span class="Punctuation">)</span>
- <span class="Keyword">else</span><span class="Punctuation">:</span>
- <span class="Identifier">result</span><span class="Operator">.</span><span class="Identifier">add</span><span class="Punctuation">(</span><span class="StringLit">" <li><a</span><span class="EscapeSequence">\n</span><span class="StringLit">"</span><span class="Punctuation">)</span>
- <span class="Comment">#end</span>
- <span class="Identifier">result</span><span class="Operator">.</span><span class="Identifier">add</span><span class="Punctuation">(</span><span class="StringLit">" href=</span><span class="EscapeSequence">\"</span><span class="StringLit">"</span> <span class="Operator">&</span> <span class="Operator">$</span><span class="Punctuation">(</span><span class="Identifier">tab</span><span class="Punctuation">)</span> <span class="Operator">&</span> <span class="StringLit">".html</span><span class="EscapeSequence">\"</span><span class="StringLit">>"</span> <span class="Operator">&</span> <span class="Operator">$</span><span class="Punctuation">(</span><span class="Identifier">tab</span><span class="Punctuation">)</span> <span class="Operator">&</span> <span class="StringLit">"</a></li></span><span class="EscapeSequence">\n</span><span class="StringLit">"</span><span class="Punctuation">)</span>
- <span class="Comment">#end</span>
- <span class="Identifier">result</span><span class="Operator">.</span><span class="Identifier">add</span><span class="Punctuation">(</span><span class="StringLit">" </ul></span><span class="EscapeSequence">\n</span><span class="StringLit">"</span> <span class="Operator">&</span>
- <span class="StringLit">" </div></span><span class="EscapeSequence">\n</span><span class="StringLit">"</span> <span class="Operator">&</span>
- <span class="StringLit">" <div id=</span><span class="EscapeSequence">\"</span><span class="StringLit">content</span><span class="EscapeSequence">\"</span><span class="StringLit">></span><span class="EscapeSequence">\n</span><span class="StringLit">"</span> <span class="Operator">&</span>
- <span class="StringLit">" "</span> <span class="Operator">&</span> <span class="Operator">$</span><span class="Punctuation">(</span><span class="Identifier">content</span><span class="Punctuation">)</span> <span class="Operator">&</span> <span class="StringLit">"</span><span class="EscapeSequence">\n</span><span class="StringLit">"</span> <span class="Operator">&</span>
- <span class="StringLit">" A dollar: $.</span><span class="EscapeSequence">\n</span><span class="StringLit">"</span> <span class="Operator">&</span>
- <span class="StringLit">" </div></span><span class="EscapeSequence">\n</span><span class="StringLit">"</span> <span class="Operator">&</span>
- <span class="StringLit">"</body></span><span class="EscapeSequence">\n</span><span class="StringLit">"</span><span class="Punctuation">)</span></pre></p>
- <p>Each line that does not start with the meta character (ignoring leading whitespace) is converted to a string literal that is added to <tt class="docutils literal"><span class="pre">result</span></tt>.</p>
- <p>The substitution character introduces a Nim expression <em>e</em> within the string literal. <em>e</em> is converted to a string with the <em>toString</em> operation which defaults to <tt class="docutils literal"><span class="pre">$</span></tt>. For strong type checking, set <tt class="docutils literal"><span class="pre">toString</span></tt> to the empty string. <em>e</em> must match this PEG pattern:</p>
- <pre>e <- [a-zA-Z\128-\255][a-zA-Z0-9\128-\255_.]* / '{' x '}'
- x <- '{' x+ '}' / [^}]*</pre>
- <p>To produce a single substitution character it has to be doubled: <tt class="docutils literal"><span class="pre">$$</span></tt> produces <tt class="docutils literal"><span class="pre">$</span></tt>.</p>
- <p>The template engine is quite flexible. It is easy to produce a procedure that writes the template code directly to a file:</p>
- <pre>#? stdtmpl(emit="f.write") | standard
- #proc writeHTMLPage(f: File, title, currentTab, content: string,
- # tabs: openArray[string]) =
- <head><title>$title</title></head>
- <body>
- <div id="menu">
- <ul>
- #for tab in items(tabs):
- #if currentTab == tab:
- <li><a id="selected"
- #else:
- <li><a
- #end if
- href="${tab}.html" title = "$title - $tab">$tab</a></li>
- #end for
- </ul>
- </div>
- <div id="content">
- $content
- A dollar: $$.
- </div>
- </body></pre>
- </p>
-
- </div>
- </div>
- <div class="twelve-columns footer">
- <span class="nim-sprite"></span>
- <br>
- <small style="color: var(--hint);">Made with Nim. Generated: 2025-02-03 14:56:36 UTC</small>
- </div>
- </div>
- </div>
- <script defer data-domain="nim-lang.org" src="https://plausible.io/js/plausible.js"></script>
-
- </body>
- </html>
|