content/glyph/book/extending/interpreting.html
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 |
<!DOCTYPE html> <html lang="en"> <head> <title>Interpreting Glyph Code - Glyph</title> <link href='http://fonts.googleapis.com/css?family=Crimson+Text:400,400italic,600,600italic,700,700italic' rel='stylesheet' type='text/css'> <meta charset="utf-8" /> <meta name="author" content="Fabio Cevasco" /> <meta name="copyright" content="Fabio Cevasco" /> <meta name="robots" content="all, follow" /> <meta name="Revisit-After" content="2 Days" /> <meta name="language" content="en" /> <meta name="target_country" content="en-us" /> <meta name="country" content="United States" /> <meta name="readability-verification" content="XmaVzRjmcFL5Bb2Fu9TZVdDKzhA3apQdxexTB2rK"/> <meta name="description" content="H3RALD - Fabio Cevasco's Web Site" /> <meta name="keywords" content="h3rald, fabio cevasco, glyph" /> <link rel="shortcut icon" href="/favicon.png" type="image/png" /> <meta content="44.388041;9.073248" name="ICBM" /> <link rel="stylesheet" type="text/css" href="/styles/html5reset.css" /> <link rel="stylesheet" type="text/css" href="/styles/style.css" /> <script src="http://www.google.com/jsapi?key=ABQIAAAAr6RY1Z6dchG_sX9WDLSy3xRlq2n1sm52B5HDRR5tm6o8XM18FhR56xHNNH6CsX86uN5VoTrglpyOyQ" type="text/javascript"></script> <script src="/js/sevenup.js" type="text/javascript"></script> <script src="/js/sevenup_black.js" type="text/javascript"></script> <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js" type="text/javascript"></script> <script src="/js/jquery-timeago.js" type="text/javascript"></script> <script src="/js/jquery-easing.js" type="text/javascript"></script> <script src="/js/jquery-fancybox.js" type="text/javascript"></script> <script src="/js/date.js" type="text/javascript"></script> <script src="/js/feeds.js" type="text/javascript"></script> <script src="/js/hyphenator.js" type="text/javascript"></script> <script src="/js/init.js" type="text/javascript"></script> <script src="/js/search.js" type="text/javascript"></script> </head> <body> <section id="container"> <header class="page"> <nav class="home-link"> <a href="/"> <img src="/images/h3rald_small.png" alt="H3RALD" class="default"/> <![if !IE]> <img src="/images/h3rald_hover_small.png" alt="H3RALD" class="hover"/> <![endif]> </a> </nav> <nav class="section"> /<a href="/glyph/" rel="glyph">GLYPH</a> </nav> </header> <article class="page glyph-book"> <header> <hgroup> <h1>Interpreting Glyph Code</h1> <h2>Glyph</h2> </hgroup> </header> <section id="body-text" class="hyphenate glyph-book"> <nav class="navigation"><a href="/glyph/book/extending/validators.html">Using Validators</a> | <a href="/glyph/book/index.html">Contents</a> | <a href="/glyph/book/extending/layouts.html">Layouts</a></nav> <p>What if you need to evaluate some Glyph code <em>within</em> a macro? Say for example you want to transform a parameter in a link, and you want to make sure that link gets validated exactly like the others, in this case, you can use the <code>interpret</code> method, as follows:</p> <div class="CodeRay"> <div class="code"><pre><span class="no">1</span> macro <span class="sy">:fmi</span> <span class="r">do</span> <span class="no">2</span> topic, href = <span class="iv">@params</span> <span class="no">3</span> link = placeholder <span class="r">do</span> |document| <span class="no">4</span> interpret <span class="s"><span class="dl">"</span><span class="k">link[</span><span class="il"><span class="idl">#{</span>href<span class="idl">}</span></span><span class="k">]</span><span class="dl">"</span></span> <span class="no">5</span> <span class="r">end</span> <span class="no">6</span> <span class="s"><span class="dl">%{</span><span class="k"><span class="fmi">for more information on </span><span class="il"><span class="idl">#{</span>topic<span class="idl">}</span></span><span class="k">, see </span><span class="il"><span class="idl">#{</span>link<span class="idl">}</span></span><span class="k"></span></span><span class="dl">}</span></span> <span class="no">7</span> <span class="r">end</span></pre></div> </div> <p>When the <code>interpret</code> method is called, the following happens:</p> <ol> <li>A new Glyph document is created from the <code>String</code> passed to the method.</li> <li>Document-specific objects (bookmarks, headers, snippet, fragments, placeholders, etc.) are passed from the main document to the new one. Because they are stored in arrays and hashes, they are passed by reference, so for example any new bookmark stored in the new document will also become available in the main document.</li> <li>Any macro included in the <code>String</code> is evaluated, and the resulting text is returned by the method. Note that this new document does not get finalized: in other words, placeholders will be left as they are, and they’ll eventually be replaced when <em>the main document</em> is finalized.</li> </ol> <section class="section"> <header><h1 id="h_94">Dispatching</h1></header> <p><a href="/glyph/book/text_editing/macro_composition.html#composition">Macro Composition</a> can be useful to remove nesting, but you can also use it to create your own macro <em>dispatchers</em>. What is a macro dispatcher? The easies way to understand this is by looking at the source code of one of them, the <a href="/glyph/book/macros/macros_core.html#m_s"><code>s</code></a> macro:</p> <div class="CodeRay"> <div class="code"><pre><span class="no"> 1</span> macro <span class="sy">:s</span> <span class="r">do</span> <span class="no"> 2</span> dispatch <span class="r">do</span> |node| <span class="no"> 3</span> forbidden = [<span class="sy">:each</span>, <span class="sy">:each_line</span>, <span class="sy">:each_byte</span>, <span class="sy">:upto</span>, <span class="sy">:intern</span>, <span class="sy">:to_sym</span>, <span class="sy">:to_f</span>] <span class="no"> 4</span> meth = node[<span class="sy">:name</span>] <span class="no"> 5</span> infer_type = lambda <span class="r">do</span> |str| <span class="no"> 6</span> <span class="c"># Code omitted...</span> <span class="no"> 7</span> <span class="r">end</span> <span class="no"> 8</span> macro_error <span class="s"><span class="dl">"</span><span class="k">Macro 's/</span><span class="il"><span class="idl">#{</span>meth<span class="idl">}</span></span><span class="k">' takes at least one parameter</span><span class="dl">"</span></span> <span class="r">unless</span> node.params.length > <span class="i">0</span> <span class="no"> 9</span> macro_error <span class="s"><span class="dl">"</span><span class="k">String method '</span><span class="il"><span class="idl">#{</span>meth<span class="idl">}</span></span><span class="k">' is not supported</span><span class="dl">"</span></span> <span class="r">if</span> meth.in?(forbidden) || meth.to_s.match(<span class="rx"><span class="dl">/</span><span class="k">!$</span><span class="dl">/</span></span>) <span class="no"><strong>10</strong></span> str = node.param(<span class="i">0</span>).evaluate(node, <span class="sy">:params</span> => <span class="pc">true</span>) <span class="no">11</span> <span class="r">begin</span> <span class="no">12</span> <span class="r">if</span> node.param(<span class="i">1</span>) <span class="r">then</span> <span class="no">13</span> meth_params = node.params[<span class="i">1</span>..node.params.length-<span class="i">1</span>].map <span class="r">do</span> |p| <span class="no">14</span> infer_type.call(p.evaluate(node, <span class="sy">:params</span> => <span class="pc">true</span>)) <span class="no">15</span> <span class="r">end</span> <span class="no">16</span> str.send(meth, *meth_params).to_s <span class="no">17</span> <span class="r">else</span> <span class="no">18</span> str.send(meth).to_s <span class="no">19</span> <span class="r">end</span> <span class="no"><strong>20</strong></span> <span class="r">rescue</span> <span class="co">Exception</span> => e <span class="no">21</span> <span class="c"># Code omittted</span> <span class="no">22</span> <span class="r">end</span> <span class="no">23</span> <span class="r">end</span> <span class="no">24</span> <span class="r">end</span></pre></div> </div> <p>See the <code>dispatch</code> method at the very beginning? This method takes a block with a <code>node</code> parameter, corresponding to the MacroNode of the macro which is being composed with <code>s</code>. So, for example, if you write <code>s/sub[my string|/my/|your]</code> the node of a macro called <code>sub</code> will be passed to the block. Of course there’s no <code>sub</code> macro defined in Glyph, but it doesn’t matter: its name will be interpreted as the name of a method of the Ruby String class in this case, so no worries.</p> <p>Got it? Tricky, but damn useful to create your own “dynamic” macros.</p> </section> <section class="section"> <header><h1 id="rewriting">Defining macros using Glyph</h1></header> <p>While the <code>interpret</code> method is useful to evaluate Glyph code in a macro while performing other actions (storing a bookmark, checking for the presence of an anchor, etc.), in some cases it may not be necessary. If you simply want your macro to be converted into existing Glyph macro without performing any action excepting parameter substitution, you can just use the <a href="/glyph/book/macros/macros_core.html#m_define_"><code>define:</code></a> macro within your Glyph document</p> <p>Consider the following macro definition:</p> <div class="CodeRay"> <div class="code"><pre><span class="no">1</span> macro <span class="sy">:issue</span> <span class="r">do</span> <span class="no">2</span> interpret <span class="s"><span class="dl">%{</span><span class="k"> <span class="no">3</span> tr[ <span class="no">4</span> td[/=>[http://github.com/h3rald/glyph/issues/closed#issue/</span><span class="il"><span class="idl">#{</span>param[<span class="i">0</span>]<span class="idl">}</span></span><span class="k">|#</span><span class="il"><span class="idl">#{</span>param(<span class="i">0</span>)<span class="idl">}</span></span><span class="k">]] <span class="no">5</span> td[txt[</span><span class="il"><span class="idl">#{</span>param(<span class="i">1</span>)<span class="idl">}</span></span><span class="k">]] <span class="no">6</span> ] <span class="no">7</span> </span><span class="dl">}</span></span> <span class="no">8</span> <span class="r">end</span></pre></div> </div> <p>The <code>issue</code> macro is only rewriting existing Glyph code around the two parameters provided. In this case, it is possible to do exactly the same thing using the <a href="/glyph/book/macros/macros_core.html#m_define_"><code>define:</code></a> macro (aliased by @def:@):</p> <div class="CodeRay"> <div class="code"><pre><span class="no">1</span> define:[issue| <span class="no">2</span> tr[ <span class="no">3</span> td[/=<span class="er">></span>[http://github.com/h3rald/glyph/issues/closed#issue/{{0}}|#{{0}}]] <span class="no">4</span> td[txt[{{1}}]] <span class="no">5</span> ] <span class="no">6</span> ]</pre></div> </div> <p>Within the <a href="/glyph/book/macros/macros_core.html#m_define_"><code>define:</code></a> macro, it is possible to use a special syntax to call the <code>raw_attr</code> or <code>raw_param</code> methods: <br /> <code>{{</code><em>parameter_number</em> or <em>attribute_name</em><code>}}</code></p> </section> <nav class="navigation"><a href="/glyph/book/extending/validators.html">Using Validators</a> | <a href="/glyph/book/index.html">Contents</a> | <a href="/glyph/book/extending/layouts.html">Layouts</a></nav> <nav id="sharing-buttons"> <a href="http://twitter.com/share" class="twitter-share-button" data-count="horizontal" data-via="h3rald">Tweet</a> <script type="text/javascript" src="http://platform.twitter.com/widgets.js"></script> <g:plusone size="medium"></g:plusone> <script type="text/javascript"> (function() { var po = document.createElement('script'); po.type = 'text/javascript'; po.async = true; po.src = 'https://apis.google.com/js/plusone.js'; var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(po, s); })(); </script> </nav> </section> </article> <footer> <section class="ads"> <script type="text/javascript"><!-- google_ad_client = "pub-2871497824158668"; /* 728x90, created 9/10/10 */ google_ad_slot = "3963343166"; google_ad_width = 728; google_ad_height = 90; //--> </script> <script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script> </section> <section> <nav> <a href="/about/">ABOUT</a>|<a href="/contact/">CONTACT</a> </nav> <p>H3RALD Web Site v8.1.2 © 2004 — 2011 <em>Fabio Cevasco</em></p> </section> </footer> </section><!-- #container end --> <!-- Start Google Analytics --> <script type="text/javascript"> var _gaq = _gaq || []; _gaq.push(['_setAccount', 'UA-18587377-1']); _gaq.push(['_trackPageview']); (function() { var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true; ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js'; var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s); })(); </script> <!-- End Google Analytics --> <!-- Start of StatCounter Code --> <script type="text/javascript"> var sc_project=6193656; var sc_invisible=1; var sc_security="57f7ee2a"; </script> <script type="text/javascript" src="http://www.statcounter.com/counter/counter_xhtml.js"></script> <!-- End of StatCounter Code --> </body> </html> |