all repos — h3rald @ 4a30a33ac9439b1fb669e06899e0e1ffed43f3cc

The sources of https://h3rald.com

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
<!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="robots" content="all, follow" />
    <meta name="Revisit-After" content="2 Days" />
    <meta name="description" content="H3RALD - Fabio Cevasco's Web Site" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    <meta name="keywords" content="h3rald, fabio cevasco, glyph, ruby, opensource" />
    <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/style.css" /> -->
    <!--[if lt IE 9]>
    <script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>
    <![endif]-->
    <link rel="stylesheet" type="text/css" href="/styles/bootstrap.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://code.jquery.com/jquery-latest.js" type="text/javascript"></script>
    <script src="/js/jquery-timeago.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>
    <script src="/js/bootstrap.js" type="text/javascript"></script>
  </head>
  <body>

    <div class="container">
      <div class="row" id="header-row">
        <div class="span6 main-title">
          <h1>
            <a href="/"><img src="/img/h3rald.png" alt="H3RALD" /></a>
          </h1>
        </div>
        <div class="span4 offset2">
          <div class="gcse-searchbox" data-enableAutoComplete="true"></div>
        </div>
      </div>
      <div id="search_results">
        <div class="gcse-searchresults" data-linkTarget="_top"></div>
      </div> 
      <div id=main-navbar class="navbar navbar-inverse">
        <div class="navbar-inner">
          <button type="button" class="btn btn-navbar" data-toggle="collapse" data-target=".nav-collapse">
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
          </button>
          <div class="nav-collapse collapse">
            <ul class="nav">
              <li><a href="/">HOME</a></li>
              <li><a href="/projects/">PROJECTS</a></li>
              <li><a href="/tags/">TAGS</a></li>
              <li><a href="/archives/">ARCHIVES</a></li>
              <li><a href="/about/">ABOUT</a></li>
              <li><a href="/contact/">CONTACT</a></li>
            </ul>
          </div>
        </div>
      </div>
    </div>
    <div class="container">
      <article class="page">
      <header class="row">
      <hgroup class="span9">
      <h1>Interpreting Glyph Code</h1>
      <h2>Glyph</h2>
      </hgroup>
      </header>
      <section id="body-text" class="hyphenate">

							<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="line-numbers"><a href="#n1" name="n1">1</a></span>macro <span class="symbol">:fmi</span> <span class="keyword">do</span>
<span class="line-numbers"><a href="#n2" name="n2">2</a></span>  topic, href = <span class="instance-variable">@params</span>
<span class="line-numbers"><a href="#n3" name="n3">3</a></span>  link = placeholder <span class="keyword">do</span> |document| 
<span class="line-numbers"><a href="#n4" name="n4">4</a></span>    interpret <span class="string"><span class="delimiter">&quot;</span><span class="content">link[</span><span class="inline"><span class="inline-delimiter">#{</span>href<span class="inline-delimiter">}</span></span><span class="content">]</span><span class="delimiter">&quot;</span></span>
<span class="line-numbers"><a href="#n5" name="n5">5</a></span>  <span class="keyword">end</span>
<span class="line-numbers"><a href="#n6" name="n6">6</a></span>  <span class="string"><span class="delimiter">%{</span><span class="content">&lt;span class=&quot;fmi&quot;&gt;for more information on </span><span class="inline"><span class="inline-delimiter">#{</span>topic<span class="inline-delimiter">}</span></span><span class="content">, see </span><span class="inline"><span class="inline-delimiter">#{</span>link<span class="inline-delimiter">}</span></span><span class="content">&lt;/span&gt;</span><span class="delimiter">}</span></span>
<span class="line-numbers"><a href="#n7" name="n7">7</a></span><span class="keyword">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&#8217;ll eventually be replaced when <em>the main document</em> is finalized.</li>
</ol>

  <section class="section">
<header><h1 id="h_93" class="toc">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="line-numbers"> <a href="#n1" name="n1">1</a></span>macro <span class="symbol">:s</span> <span class="keyword">do</span>
<span class="line-numbers"> <a href="#n2" name="n2">2</a></span>  dispatch <span class="keyword">do</span> |node|
<span class="line-numbers"> <a href="#n3" name="n3">3</a></span>    forbidden = [<span class="symbol">:each</span>, <span class="symbol">:each_line</span>, <span class="symbol">:each_byte</span>, <span class="symbol">:upto</span>, <span class="symbol">:intern</span>, <span class="symbol">:to_sym</span>, <span class="symbol">:to_f</span>]
<span class="line-numbers"> <a href="#n4" name="n4">4</a></span>    meth = node[<span class="symbol">:name</span>]
<span class="line-numbers"> <a href="#n5" name="n5">5</a></span>    infer_type = lambda <span class="keyword">do</span> |str|
<span class="line-numbers"> <a href="#n6" name="n6">6</a></span>      <span class="comment"># Code omitted...</span>
<span class="line-numbers"> <a href="#n7" name="n7">7</a></span>    <span class="keyword">end</span>
<span class="line-numbers"> <a href="#n8" name="n8">8</a></span>    macro_error <span class="string"><span class="delimiter">&quot;</span><span class="content">Macro 's/</span><span class="inline"><span class="inline-delimiter">#{</span>meth<span class="inline-delimiter">}</span></span><span class="content">' takes at least one parameter</span><span class="delimiter">&quot;</span></span> <span class="keyword">unless</span> node.params.length &gt; <span class="integer">0</span>
<span class="line-numbers"> <a href="#n9" name="n9">9</a></span>    macro_error <span class="string"><span class="delimiter">&quot;</span><span class="content">String method '</span><span class="inline"><span class="inline-delimiter">#{</span>meth<span class="inline-delimiter">}</span></span><span class="content">' is not supported</span><span class="delimiter">&quot;</span></span> <span class="keyword">if</span> meth.in?(forbidden) || meth.to_s.match(<span class="regexp"><span class="delimiter">/</span><span class="content">!$</span><span class="delimiter">/</span></span>)
<span class="line-numbers"><strong><a href="#n10" name="n10">10</a></strong></span>    str = node.param(<span class="integer">0</span>).evaluate(node, <span class="symbol">:params</span> =&gt; <span class="predefined-constant">true</span>)
<span class="line-numbers"><a href="#n11" name="n11">11</a></span>    <span class="keyword">begin</span>
<span class="line-numbers"><a href="#n12" name="n12">12</a></span>      <span class="keyword">if</span> node.param(<span class="integer">1</span>) <span class="keyword">then</span>
<span class="line-numbers"><a href="#n13" name="n13">13</a></span>        meth_params = node.params[<span class="integer">1</span>..node.params.length-<span class="integer">1</span>].map <span class="keyword">do</span> |p| 
<span class="line-numbers"><a href="#n14" name="n14">14</a></span>          infer_type.call(p.evaluate(node, <span class="symbol">:params</span> =&gt; <span class="predefined-constant">true</span>))
<span class="line-numbers"><a href="#n15" name="n15">15</a></span>        <span class="keyword">end</span>
<span class="line-numbers"><a href="#n16" name="n16">16</a></span>        str.send(meth, *meth_params).to_s
<span class="line-numbers"><a href="#n17" name="n17">17</a></span>      <span class="keyword">else</span>
<span class="line-numbers"><a href="#n18" name="n18">18</a></span>        str.send(meth).to_s
<span class="line-numbers"><a href="#n19" name="n19">19</a></span>      <span class="keyword">end</span>
<span class="line-numbers"><strong><a href="#n20" name="n20">20</a></strong></span>    <span class="keyword">rescue</span> <span class="constant">Exception</span> =&gt; e
<span class="line-numbers"><a href="#n21" name="n21">21</a></span>      <span class="comment"># Code omittted</span>
<span class="line-numbers"><a href="#n22" name="n22">22</a></span>    <span class="keyword">end</span>
<span class="line-numbers"><a href="#n23" name="n23">23</a></span>  <span class="keyword">end</span>
<span class="line-numbers"><a href="#n24" name="n24">24</a></span><span class="keyword">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&#8217;s no <code>sub</code> macro defined in Glyph, but it doesn&#8217;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 &#8220;dynamic&#8221; macros.</p>

</section>

	<section class="section">
<header><h1 id="rewriting" class="toc">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="line-numbers"><a href="#n1" name="n1">1</a></span>macro <span class="symbol">:issue</span> <span class="keyword">do</span>
<span class="line-numbers"><a href="#n2" name="n2">2</a></span>  interpret <span class="string"><span class="delimiter">%{</span><span class="content"></span></span>
<span class="line-numbers"><a href="#n3" name="n3">3</a></span><span class="string"><span class="content">    tr[</span></span>
<span class="line-numbers"><a href="#n4" name="n4">4</a></span><span class="string"><span class="content">      td[/=&gt;[http://github.com/h3rald/glyph/issues/closed#issue/</span><span class="inline"><span class="inline-delimiter">#{</span>param[<span class="integer">0</span>]<span class="inline-delimiter">}</span></span><span class="content">|#</span><span class="inline"><span class="inline-delimiter">#{</span>param(<span class="integer">0</span>)<span class="inline-delimiter">}</span></span><span class="content">]]</span></span>
<span class="line-numbers"><a href="#n5" name="n5">5</a></span><span class="string"><span class="content">      td[txt[</span><span class="inline"><span class="inline-delimiter">#{</span>param(<span class="integer">1</span>)<span class="inline-delimiter">}</span></span><span class="content">]]</span></span>
<span class="line-numbers"><a href="#n6" name="n6">6</a></span><span class="string"><span class="content">    ]</span></span>
<span class="line-numbers"><a href="#n7" name="n7">7</a></span><span class="string"><span class="content">  </span><span class="delimiter">}</span></span>
<span class="line-numbers"><a href="#n8" name="n8">8</a></span><span class="keyword">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="line-numbers"><a href="#n1" name="n1">1</a></span>define:[issue|
<span class="line-numbers"><a href="#n2" name="n2">2</a></span>  tr[
<span class="line-numbers"><a href="#n3" name="n3">3</a></span>    td[/=&gt;[http://github.com/h3rald/glyph/issues/closed#issue/{{0}}|#{{0}}]]
<span class="line-numbers"><a href="#n4" name="n4">4</a></span>    td[txt[{{1}}]]
<span class="line-numbers"><a href="#n5" name="n5">5</a></span>  ]
<span class="line-numbers"><a href="#n6" name="n6">6</a></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>

      </section>
      </article>
    </div>

    <footer>
    <section>
    <p>H3RALD Web Site v8.3 &copy; 2004 &mdash; 2013 <em>Fabio Cevasco</em></p>
    </section>
    </footer>



    <!-- 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>