/usr/share/doc/rest2web/html/macros.html is in rest2web-doc 0.5.2~alpha+svn-r248-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 | <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
<title>Macros in rest2web</title>
<link rel="stylesheet" href="stylesheets/rest2web.css" type="text/css" />
<link rel="stylesheet" href="stylesheets/voidspace_docutils2.css" type="text/css" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="description" content="rest2web - build websites with Python and docutils." />
<meta name="author" content="Michael Foord" />
<meta name="copyright" content="© 2003-2006 Michael Foord, subject to BSD License" />
<meta name="keywords" content="rest2web - build websites with Python and docutils,
rest, restructured text, text, website, internet, web, net, web builder, site,
site builder, docutils, i18n, internationalization, templates, templating,
python, " />
</head>
<body style="background: url(images/logos/img_background.gif) top center repeat-y;">
<div id="wrap">
<div id="header-section">
<a href="http://www.voidspace.org.uk/python/index.shtml" title="Voidspace"><img src="images/logos/header760.gif" alt="Voidspace"/></a>
<p align="center" class="headertitle"><span style="font-size:130%">rest2web:</span> Building Websites Across the Known Universe</p>
</div>
<div id="header">
<ul>
<li><a href="index.html">rest2web</a></li>
<li>></li>
<li>Macros</li>
</ul>
</div>
<table>
<tr>
<td><img src="images/logos/1.gif" width="1" height="1" alt="" /></td>
<!-- main content cell first, with rowspan=2 -->
<td valign="top" align="left" rowspan="2">
<div id="middle-column">
<a name="startcontent" id="startcontent"></a>
<div id="gallery">
<div class="document" id="macros-in-rest2web">
<h1 class="title">Macros in rest2web</h1>
<h2 class="subtitle" id="using-macros">Using Macros</h2>
<div class="contents topic" id="macros">
<p class="topic-title first">Macros</p>
<ul class="simple">
<li><a class="reference internal" href="#about-macros" id="id7">About macros</a></li>
<li><a class="reference internal" href="#where-are-macros-defined" id="id8">Where are Macros Defined?</a><ul>
<li><a class="reference internal" href="#the-macros-file" id="id9">The Macros File</a></li>
</ul>
</li>
<li><a class="reference internal" href="#things-you-should-know" id="id10">Things you should know</a><ul>
<li><a class="reference internal" href="#the-order" id="id11">The Order</a></li>
<li><a class="reference internal" href="#filepaths" id="id12">Filepaths</a></li>
<li><a class="reference internal" href="#modules" id="id13">Modules</a></li>
</ul>
</li>
<li><a class="reference internal" href="#advanced-macros" id="id14">Advanced Macros</a></li>
<li><a class="reference internal" href="#the-example-macros" id="id15">The Example Macros</a><ul>
<li><a class="reference internal" href="#curlyl-and-curlyr" id="id16">curlyl and curlyr</a></li>
<li><a class="reference internal" href="#lt" id="id17">lt</a></li>
<li><a class="reference internal" href="#smiley" id="id18">smiley</a></li>
<li><a class="reference internal" href="#acronym" id="id19">acronym</a></li>
<li><a class="reference internal" href="#emoticon" id="id20">emoticon</a></li>
<li><a class="reference internal" href="#include" id="id21">include</a></li>
<li><a class="reference internal" href="#colorize" id="id22">colorize</a></li>
<li><a class="reference internal" href="#coloring" id="id23">+/- coloring</a></li>
<li><a class="reference internal" href="#small" id="id24">small</a></li>
<li><a class="reference internal" href="#name" id="id25">name</a></li>
<li><a class="reference internal" href="#title" id="id26">title</a></li>
</ul>
</li>
<li><a class="reference internal" href="#including-macros-in-rest-pages" id="id27">Including Macros in ReST Pages</a><ul>
<li><a class="reference internal" href="#the-raw-role" id="id28">The Raw Role</a></li>
<li><a class="reference internal" href="#the-raw-directive" id="id29">The Raw Directive</a></li>
<li><a class="reference internal" href="#paragraphs" id="id30">Paragraphs</a></li>
</ul>
</li>
<li><a class="reference internal" href="#namespace-and-uservalues" id="id31">namespace and uservalues</a></li>
<li><a class="reference internal" href="#credits" id="id32">Credits</a></li>
<li><a class="reference internal" href="#footnotes" id="id33">Footnotes</a></li>
</ul>
</div>
<div class="section" id="about-macros">
<h1><a class="toc-backref" href="#id7">About macros</a></h1>
<p>Macros allow you to use a shorthand for often-used text or HTML.</p>
<p>As a simple example, let's say that I use a certain link a lot. I don't want to type it over and over again. I can define a macro foo to include this link every time I type <tt class="docutils literal"><span class="pre">{foo}</span></tt>:</p>
<p class="ex">foo = 'My link'</p>
<p>In this case, the "macro definition" is simply a string that contains the HTML we want to substitute. We can use more sophisticated macros, though.</p>
<p>Let's assume that I use acronyms a lot, and I'm getting tired of having to type that tag again and again. It would be nice if I could type something like <tt class="docutils literal"><span class="pre">{acronym;IMO;In</span> <span class="pre">My</span> <span class="pre">Opinion}</span></tt></p>
<p>which expands to <acronym title="In My Opinion">IMO</acronym>. (Move your mouse over the "IMO" to see the effect. Not all browsers may support this.)</p>
<p>To achieve this, we define a simple Python function:</p>
<div class="pysrc"><span class="pykeyword">def</span> <span class="pytext">acronym</span><span class="pyoperator">(</span><span class="pytext">acronym</span><span class="pyoperator">,</span> <span class="pytext">meaning</span><span class="pyoperator">)</span><span class="pyoperator">:</span><br />
<span class="pykeyword">return</span> <span class="pystring">'<acronym title="%s">%s</acronym>'</span> <span class="pyoperator">%</span> <span class="pyoperator">(</span><br />
<span class="pytext">meaning</span><span class="pyoperator">,</span> <span class="pytext">acronym</span><span class="pyoperator">)</span><span class="pytext"></span></div><p>{acronym;IMO;In My Opinion} is equivalent to the function call <tt class="docutils literal"><span class="pre">acronym("IMO",</span> <span class="pre">"In</span> <span class="pre">My</span> <span class="pre">Opinion")</span></tt>.</p>
<p>These macros can also be used to do more complex things, like insert pictures, include whole files etc.</p>
<div class="note">
<p class="first admonition-title">Note</p>
<p>Macros pass through the ReST processing untouched (unless you include characters that ReStructured text normal escapes, like '<').</p>
<p class="last">This means that macros on a line of their own will be processed as a paragraph. The generated HTML will be between paragraph tags: <tt class="docutils literal"><span class="pre"><p>...</p></span></tt>.</p>
</div>
</div>
<div class="section" id="where-are-macros-defined">
<h1><a class="toc-backref" href="#id8">Where are Macros Defined?</a></h1>
<p>As of <strong>rest2web 0.5.0</strong> all the macros described here are built-in to rest2web. That means you can use them without supplying a macros file.</p>
<p>Two of the built in macros require paths to operate properly. You can do this using the <a class="reference external" href="config_file.html">config file</a>. See the <a class="reference internal" href="#smiley">smiley</a> and <a class="reference internal" href="#emoticon">emoticon</a> macros. <img src="images/smilies/smile.gif" alt="Smile" /> </p>
<p>You can still override these built-in macros, by defining them in your own macros file. Anything in the macros file will override the built in ones.</p>
<div class="section" id="the-macros-file">
<h2><a class="toc-backref" href="#id9">The Macros File</a></h2>
<p>In the <a class="reference external" href="config_file.html">rest2web config file</a> you can specify a path to a macros module. This can have any name, but it should be a module that Python can import. Basically that means the filename has to end in <em>'.py'</em> <a class="footnote-reference" href="#id4" id="id1">[1]</a>.</p>
<p>So, to get started, here's what you need to do:</p>
<ul class="simple">
<li>Create a file called <em>macros.py</em>.</li>
<li>Add definitions to it. (It helps if you know some Python, of course, but even without that you can enter simple string macros of the form <tt class="docutils literal"><span class="pre">name</span> <span class="pre">=</span> <span class="pre">'text'</span></tt>.)</li>
<li>Put the location of this file in the rest2web config file for your site.</li>
</ul>
</div>
</div>
<div class="section" id="things-you-should-know">
<h1><a class="toc-backref" href="#id10">Things you should know</a></h1>
<ul class="simple">
<li>Macros are string-based. If a macro's return value isn't a string, then it's turned into one (if at all possible).</li>
<li>Arguments for a function call are strings too; <tt class="docutils literal"><span class="pre">{foobar;42}</span></tt> is the function call <tt class="docutils literal"><span class="pre">foobar("42")</span></tt>, not <tt class="docutils literal"><span class="pre">foobar(42)</span></tt>. If you want a different type, you'll have to explicitly convert in inside your function.</li>
<li>Functions with no arguments are called by their name: <tt class="docutils literal"><span class="pre">{baz}</span></tt> is equivalent to <tt class="docutils literal"><span class="pre">baz()</span></tt>, if baz is a callable object. If not, then the value is taken.</li>
<li>If a macro name isn't found, it is left alone. In fact, this piece of documentation exists as it is because of this rule. Text like <tt class="docutils literal"><span class="pre">{knarf}</span></tt> would be replaced' if it was a macro. Since we didn't define a macro knarf, it is left alone and shows up in text with curly braces and all.</li>
<li>If an error occurs in a macro (function call), it is left alone as well. So are macros containing newlines.</li>
<li>If you want to include a semicolon (;) or a curly brace in your arguments, you're out of luck. There are currently no ways to do this. You can always define escape sequences in your functions of course.</li>
<li>Ditto if you want to suppress a macro call that would normally be substituted. <em>other than</em> the <tt class="docutils literal"><span class="pre">{curlyl}</span></tt>, and <tt class="docutils literal"><span class="pre">{curlyr}</span></tt>, macros. See <a class="reference internal" href="#the-example-macros">The Example Macros</a> below.</li>
<li>Macros map to Python objects. Usually you'll want to use strings or functions, but other objects are possible as well.</li>
<li>If you define a function you can also give it a shorter name. For example, to give our <em>smiley</em> function the shorter name <em>sm</em>, I included the line <tt class="docutils literal"><span class="pre">sm</span> <span class="pre">=</span> <span class="pre">smiley</span></tt> after the function definition.</li>
</ul>
<div class="section" id="the-order">
<h2><a class="toc-backref" href="#id11">The Order</a></h2>
<p>Macros are resolved <em>after</em> the pages has been processed by docutils <a class="footnote-reference" href="#id5" id="id2">[2]</a>. This means your macros should return html for direct inclusion in the final page.</p>
<p>They are resolved <em>before</em> the page is passed to the template engine. This means that dynamic values like <tt class="docutils literal"><span class="pre"><%</span> <span class="pre">path_to_root</span> <span class="pre">%></span></tt> output by macros <em>will</em> be resolved.</p>
</div>
<div class="section" id="filepaths">
<h2><a class="toc-backref" href="#id12">Filepaths</a></h2>
<p>Before the macros are run, the current directory is changed to be the same as the macros file. This means all file paths supplied to macros should be relative to this file.</p>
</div>
<div class="section" id="modules">
<h2><a class="toc-backref" href="#id13">Modules</a></h2>
<p>The modules directory in the <strong>rest2web</strong> distribution includes various Python modules used by the macros. If you aren't using macros then you <em>don't</em> need this directory. Credits for all the modules here are in the <a class="reference internal" href="#credits">Credits</a> section below.</p>
<p>This directory is put in the Python search path. If you move them elsewhere you should make sure they are on the <tt class="docutils literal"><span class="pre">sys.path</span></tt>. If you don't know what I'm on about then leave them where they are <img src="images/smilies/biggrin.gif" alt="Very Happy" /> </p>
</div>
</div>
<div class="section" id="advanced-macros">
<h1><a class="toc-backref" href="#id14">Advanced Macros</a></h1>
<p>As well as the normal macros discussed above, there is a more advanced system of macros as well. This allows you to apply a macro to a whole chunk of text. Unlike the simple macros, these macros can be nested to apply several effects to passages.</p>
<p>The advanced macros work by enclosing a passage of text between a <tt class="docutils literal"><span class="pre">{+macro}</span></tt> and a <tt class="docutils literal"><span class="pre">{-macro}</span></tt>. The macro is applied to all the text between the <strong>+</strong> and the <strong>-</strong>. In this case it would be applied to <em>and a</em>.</p>
<p>You can also nest them So you can do things like :</p>
<pre class="literal-block">
{+second}{+first} The text {-second}{-first}
{+whole} Some text {+part} a bit in the middle {-part} more text{-whole}
</pre>
<p>These macros aren't the same as normal macros though. Here's the idea. While a regular macro <tt class="docutils literal"><span class="pre">{f;x;y}</span></tt> translates to a function
call <tt class="docutils literal"><span class="pre">f("x",</span> <span class="pre">"y")</span></tt>, the + and - versions use a class definition. A
simple example :</p>
<div class="pysrc"><span class="pykeyword">class</span> <span class="pytext">rot13</span><span class="pyoperator">(</span><span class="pytext">textmacros</span><span class="pyoperator">.</span><span class="pytext">BaseMacro</span><span class="pyoperator">)</span><span class="pyoperator">:</span><br />
<span class="pykeyword">def</span> <span class="pytext">open</span><span class="pyoperator">(</span><span class="pytext">self</span><span class="pyoperator">,</span> <span class="pytext">text</span><span class="pyoperator">)</span><span class="pyoperator">:</span><br />
<span class="pykeyword">return</span> <span class="pytext">text</span><span class="pyoperator">.</span><span class="pytext">encode</span><span class="pyoperator">(</span><span class="pystring">"rot13"</span><span class="pyoperator">)</span><span class="pytext"></span></div><p>Once this is defined, in your macros file (or just imported into it), you can
use it as follows :</p>
<pre class="literal-block">
The solution is {+rot13}not for your eyes{-rot13}.
</pre>
<p>Upon execution, this will convert the text between the rot13 tags. This becomes :</p>
<pre class="literal-block">
The solution is abg sbe lbhe rlrf.
</pre>
<p>In your class the +tag corresponds to the class's <tt class="docutils literal"><span class="pre">open(text)</span></tt> method, the -tag to the <tt class="docutils literal"><span class="pre">close()</span></tt> method.</p>
<p>Note that if you are using docutils to process your text then it will be processed by docutils before it is processed by the macro. If you want to bypass this, and pass the text to your macro <em>only</em>, then you need to use the <tt class="docutils literal"><span class="pre">..</span> <span class="pre">raw::</span> <span class="pre">html</span></tt> directive. e.g. :</p>
<pre class="literal-block">
.. raw:: html
{+rot13}
this text will *not* be processed as reST
{-rot13}
</pre>
</div>
<div class="section" id="the-example-macros">
<h1><a class="toc-backref" href="#id15">The Example Macros</a></h1>
<p>These are the macros that come built into rest2web.</p>
<div class="section" id="curlyl-and-curlyr">
<h2><a class="toc-backref" href="#id16">curlyl and curlyr</a></h2>
<p>These two macros perform simple substitution. They are a way of including curly left hand brackets and curly right hand brackets in your pages. They are also have the shorter forms <tt class="docutils literal"><span class="pre">cl</span></tt> and <tt class="docutils literal"><span class="pre">cr</span></tt>.</p>
<p>For example, to include <tt class="docutils literal"><span class="pre">{example}</span></tt> in your page - without it being interpreted as a macro - you can write <tt class="docutils literal"><span class="pre">{curlyl}example{curlyr}</span></tt> or <tt class="docutils literal"><span class="pre">{cl}example{cr}</span></tt>.</p>
<p>This came in very handy when creating this page <img src="images/smilies/lol.gif" alt="Laughing" /> </p>
</div>
<div class="section" id="lt">
<h2><a class="toc-backref" href="#id17">lt</a></h2>
<p>This is another simple substitution macro. It puts a '<' (less than) symbol into
pages.</p>
<p>It is especially where you need to include a literal <tt class="docutils literal"><span class="pre"><$</span> <span class="pre">...</span> <span class="pre">$></span></tt> or
<tt class="docutils literal"><span class="pre"><*</span> <span class="pre">..</span> <span class="pre">*></span></tt> in your pages.</p>
<p>Example :</p>
<blockquote>
<tt class="docutils literal"><span class="pre">{lt}</span></tt></blockquote>
</div>
<div class="section" id="smiley">
<h2><a class="toc-backref" href="#id18">smiley</a></h2>
<p>This is one of the nicest macros. It uses a modified version of <em>smiley.py</em> by <a class="reference external" href="http://www.la-la.com">Mark Andrews</a> to put smilies onto your site. <img src="images/smilies/razz.gif" alt="Razz" /> </p>
<p>In order to use the smilies, rest2web needs to know the path to the smiley images on your hard drive, and also what URL path you want to be used in the image links that rest2web generates. You do this in the <tt class="docutils literal"><span class="pre">[Macro</span> <span class="pre">Paths]</span></tt> section of your <a class="reference external" href="config_file.html">config file</a>.</p>
<p>The two values to supply are :</p>
<ul>
<li><p class="first"><tt class="docutils literal"><span class="pre">smiley_directory</span></tt></p>
<blockquote>
<p>The default is to use the standard set which come built in to rest2web.</p>
</blockquote>
</li>
<li><p class="first"><tt class="docutils literal"><span class="pre">smiley_url</span></tt></p>
<blockquote>
<p>The default is to use the following path, <tt class="docutils literal"><span class="pre">'<%</span> <span class="pre">path_to_root</span> <span class="pre">%>images/smilies/'</span></tt>.</p>
</blockquote>
</li>
</ul>
<p>You can use <tt class="docutils literal"><span class="pre">sm</span></tt> as a shorter alias for <tt class="docutils literal"><span class="pre">smiley</span></tt>.</p>
<p>Examples :</p>
<blockquote>
<p><tt class="docutils literal"><span class="pre">{smiley;:-)}</span></tt> becomes <img src="images/smilies/smile.gif" alt="Smile" /> </p>
<p><tt class="docutils literal"><span class="pre">{sm;:roll:}</span></tt> becomes <img src="images/smilies/rolleyes.gif" alt="Rolling Eyes" /> </p>
</blockquote>
<p>It will read standard smiley packages like the ones used by <a class="reference external" href="http://www.phpbb.com/">phpbb</a>. Download more from the <a class="reference external" href="http://www.stylesdb.com/smilies_styles.html">stylesdb</a> site.</p>
<p>You can see a full list of all of the smilies from the example set in the <a class="reference external" href="reference/smilies.html">Smilies Page</a>.</p>
</div>
<div class="section" id="acronym">
<h2><a class="toc-backref" href="#id19">acronym</a></h2>
<p>We`ve seen this in the first example. <tt class="docutils literal"><span class="pre">{acronym;acronym;meaning}</span></tt> produces <acronym title="meaning">acronym</acronym>. You can also use <tt class="docutils literal"><span class="pre">{acro;acronym;meaning}</span></tt>.</p>
<p>As an added bonus there are a few standard acronyms that can be called without the acronym definition. These are :</p>
<div class="pysrc"><span class="pycomment"># a dictionary of standard acronyms<br />
</span><span class="pycomment"># keys should be lowercase<br />
</span><span class="pytext">acronyms</span> <span class="pyoperator">=</span> <span class="pyoperator">{</span><br />
<span class="pystring">'wysiwyg'</span> <span class="pyoperator">:</span> <span class="pystring">'What You See Is What You Get'</span><span class="pyoperator">,</span><br />
<span class="pystring">'html'</span> <span class="pyoperator">:</span> <span class="pystring">'HyperText Markup Language'</span><span class="pyoperator">,</span><br />
<span class="pystring">'xml'</span> <span class="pyoperator">:</span> <span class="pystring">'eXtensible Markup Language'</span><span class="pyoperator">,</span><br />
<span class="pystring">'xhtml'</span> <span class="pyoperator">:</span> <span class="pystring">'eXtensible HyperText Markup Language'</span><span class="pyoperator">,</span><br />
<span class="pyoperator">}</span><span class="pytext"></span></div><p>So you can do <tt class="docutils literal"><span class="pre">{acro;WYSIWYG}</span></tt>, which becomes <acronym title="What You See Is What You Get">WYSIWYG</acronym> <img src="images/smilies/cool.gif" alt="Cool" /> . Feel free to add your own of course.</p>
<p>The built in acronyms are :</p>
<pre class="literal-block">
{
'wysiwyg': 'What You See Is What You Get',
'html': 'HyperText Markup Language',
'xml': 'eXtensible Markup Language',
'xhtml': 'eXtensible HyperText Markup Language',
'rest': 'ReStructuredText',
'css': 'Cascading Style Sheets',
'ie': 'Internet Exploder',
'afaik': 'As Far as I Know',
'ianal': 'I am not a Lawyer',
'ssi': 'Server Side Includes',
'cgi': 'Common Gateway Interface',
'lol': 'Laughing Out Loud',
'rotfl': 'Roll On the Floor Laughing',
'http': 'HyperText Transfer Protocol',
'ascii': 'American Standard Code for Information Interchange',
'gui': 'Graphical User Interface',
'cli': 'Command Line Interface',
'pda': 'Personal Digital Assistant',
'rtfm': 'Read the Manual',
'ftp': 'File Transfer Protocol',
'nntp': 'Network News Transfer Protocol',
'uk': 'United Kingdom',
'pc': 'Personal Computer',
'url': 'Uniform Resource Locator',
'uri': 'Uniform Resource Identifier',
'tcp/ip': 'Transport Control Protocol/Internet Protocol',
'udp': 'User Data Paragram'
}
</pre>
<p>The <tt class="docutils literal"><span class="pre">macros.py</span></tt> file that comes with rest2web contains an acronyms dictionary. Any acronyms that you add to this dictionary will be available to the acronyms macro.</p>
</div>
<div class="section" id="emoticon">
<h2><a class="toc-backref" href="#id20">emoticon</a></h2>
<p>This is another shorthand way of including images in your pages. It's useful for putting emoticons inline with text, hence the name. Unlike the <tt class="docutils literal"><span class="pre">smiley</span></tt> macro it doesn't need to read anything of disk.</p>
<p>rest2web needs to know what url path to use for the emoticon images. You can supply this using the <tt class="docutils literal"><span class="pre">emoticon_url</span></tt> value in the <tt class="docutils literal"><span class="pre">[Macro</span> <span class="pre">Paths]</span></tt> section of your <a class="reference external" href="config_file.html">config file</a>. If you don't supply a value, the default is <tt class="docutils literal"><span class="pre">'<%</span> <span class="pre">path_to_root</span> <span class="pre">%>images/'</span></tt>.</p>
<p>The emoticon macro assumes your images are all 'gif's.</p>
<div class="warning">
<p class="first admonition-title">Warning</p>
<p class="last">Including images without specifying a size may slow down the browser rendering of your pages. You could make all your images the same size and hardwire the sizes into the 'img' tag that this macro creates. Alternatively you could do something clever with the <acronym title="Python Imaging Library">PIL</acronym> by Frederik Lundh - and have it work out the sizes and insert them for you.</p>
</div>
<p>Examples :</p>
<blockquote>
<p><tt class="docutils literal"><span class="pre">{emoticon;eyeballz}</span></tt> becomes <img src="images/eyeballz.gif" alt="emoticon:eyeballz" /></p>
<p><tt class="docutils literal"><span class="pre">{emo;noise}</span></tt> becomes <img src="images/noise.gif" alt="emoticon:noise" /></p>
</blockquote>
<p>You can use <tt class="docutils literal"><span class="pre">emo</span></tt> as a shorter alias for <tt class="docutils literal"><span class="pre">emoticon</span></tt>.</p>
</div>
<div class="section" id="include">
<h2><a class="toc-backref" href="#id21">include</a></h2>
<p>This macro is very simple. Give it a filepath (relative to the macro module) and it will include the contents of that file into this page. The optional 'escape' parameter allows you to escape whitespace. This will insert files as they are - without having to use the '<pre>' tag, which breaks my layout - have I mentioned that before ? <img src="images/smilies/wink.gif" alt="Wink" /> </p>
<p>For example <tt class="docutils literal"><span class="pre"><tt>{include;r2w.ini;True}</tt></span></tt> escapes the <em>r2w.ini</em> file and inserts it into the page :</p>
<div class="ex"><tt>
# Attempt to use psyco ?<br />
psyco = True<br />
<br />
# pause after building ?<br />
pause = True<br />
<br />
# the root directory<br />
start_directory = 'docs'<br />
<br />
# the directory to generate files in<br />
target_directory = 'docs_html'<br />
<br />
# directory to compare against (defaults to target_directory)<br />
compare_directory = ''<br />
<br />
# file to log output to (if any)<br />
log_file = 'log.txt'<br />
<br />
# Skip errors (continue processing)<br />
skiperrors = False<br />
<br />
# file containing macros (if any)<br />
macros = 'macros.py'<br />
<br />
# Promote lone section headers to title<br />
# rest only and only in the absence of an<br />
# explicit page-title<br />
promote_headers = True<br />
<br />
# Enter DEBUG mode ?<br />
# (Interactive interpreter prompt in each page namespace)<br />
DEBUG = False<br />
<br />
[uservalues]<br />
# Any global uservalues you want to set<br />
__encoding__ = 'UTF8'<br />
uservalue1 = A Value<br />
uservalue2 = Another Value<br />
<br />
[Macro Paths]<br />
smiley_directory = ''<br />
smiley_url = 'images/smilies/'<br />
emoticon_url = 'images/'<br />
<br />
<br />
</tt></div><p>You can use <tt class="docutils literal"><span class="pre">inc</span></tt> as a shorter alias for <tt class="docutils literal"><span class="pre">include</span></tt>.</p>
<p>To include HTML files (without escaping), use <tt class="docutils literal"><span class="pre">{include;some_file.html}</span></tt>.</p>
</div>
<div class="section" id="colorize">
<h2><a class="toc-backref" href="#id22">colorize</a></h2>
<p>This macro takes a file and applies Python syntax highlighting to it. You need the right rules in your CSS file for the coloring to be visible. See the rules that start <em>py</em> in <tt class="docutils literal"><span class="pre">test.css</span></tt>.</p>
<p><tt class="docutils literal"><span class="pre">{colorize;docs/example_function.txt}</span></tt> becomes :</p>
<p><div class="pysrc"><span class="pycomment">#<br />
</span><span class="pycomment"># syntax coloring<br />
</span><span class="pykeyword">try</span><span class="pyoperator">:</span><br />
<span class="pykeyword">import</span> <span class="pytext">colorize</span> <span class="pykeyword">as</span> <span class="pytext">col</span><br />
<span class="pykeyword">except</span> <span class="pytext">ImportError</span><span class="pyoperator">:</span><br />
<span class="pykeyword">def</span> <span class="pytext">colorize</span><span class="pyoperator">(</span><span class="pytext">filename</span><span class="pyoperator">)</span><span class="pyoperator">:</span><br />
<span class="pykeyword">raise</span> <span class="pytext">ImportError</span><span class="pyoperator">,</span> <span class="pystring">'Importing colorize.py failed.'</span><br />
<br />
<span class="pykeyword">else</span><span class="pyoperator">:</span><br />
<span class="pykeyword">def</span> <span class="pytext">colorize</span><span class="pyoperator">(</span><span class="pytext">filename</span><span class="pyoperator">)</span><span class="pyoperator">:</span><br />
<span class="pystring">"""<br />
format a python script as html<br />
Using the appropriate css it will be nicely colored.<br />
<br />
Needs the colorize.py module.<br />
"""</span><br />
<span class="pytext">fullname</span> <span class="pyoperator">=</span> <span class="pytext">os</span><span class="pyoperator">.</span><span class="pytext">path</span><span class="pyoperator">.</span><span class="pytext">join</span><span class="pyoperator">(</span><span class="pytext">filename</span><span class="pyoperator">)</span><br />
<span class="pytext">f</span> <span class="pyoperator">=</span> <span class="pytext">open</span><span class="pyoperator">(</span><span class="pytext">fullname</span><span class="pyoperator">,</span> <span class="pystring">'r'</span><span class="pyoperator">)</span><br />
<span class="pytext">data</span> <span class="pyoperator">=</span> <span class="pytext">f</span><span class="pyoperator">.</span><span class="pytext">read</span><span class="pyoperator">(</span><span class="pyoperator">)</span><br />
<span class="pytext">f</span><span class="pyoperator">.</span><span class="pytext">close</span><span class="pyoperator">(</span><span class="pyoperator">)</span><br />
<br />
<span class="pytext">p</span> <span class="pyoperator">=</span> <span class="pytext">col</span><span class="pyoperator">.</span><span class="pytext">Parser</span><span class="pyoperator">(</span><span class="pytext">data</span><span class="pyoperator">)</span><br />
<span class="pytext">p</span><span class="pyoperator">.</span><span class="pytext">format</span><span class="pyoperator">(</span><span class="pytext">None</span><span class="pyoperator">,</span> <span class="pytext">None</span><span class="pyoperator">)</span><br />
<span class="pytext">src</span> <span class="pyoperator">=</span> <span class="pytext">p</span><span class="pyoperator">.</span><span class="pytext">getvalue</span><span class="pyoperator">(</span><span class="pyoperator">)</span><br />
<span class="pykeyword">return</span> <span class="pytext">src</span><span class="pyoperator">.</span><span class="pytext">replace</span><span class="pyoperator">(</span><span class="pystring">'\n'</span><span class="pyoperator">,</span> <span class="pystring">'<br />\n'</span><span class="pyoperator">)</span><span class="pyoperator">.</span><span class="pytext">replace</span><span class="pyoperator">(</span><span class="pystring">' '</span><span class="pyoperator">,</span> <span class="pystring">'&nbsp;&nbsp;'</span><span class="pyoperator">)</span><br />
<span class="pycomment"># to avoid having to use <pre>..</span><span class="pytext"></span></div></p>
<p>You can use <tt class="docutils literal"><span class="pre">col</span></tt> as a shorter alias for <tt class="docutils literal"><span class="pre">colorize</span></tt>.</p>
<p>To use the colorize macro, you need the right definitions in your CSS. Something like :</p>
<pre class="literal-block">
.pysrc {
border: #c0c0ff 2px dotted; padding:10px;
font-weight: normal; background: #e0e0ff; margin: 20px;
padding:10px;
}
.pykeyword {
font-weight: bold;
color: orange;
}
.pystring {
color: green
}
.pycomment {
color: red
}
.pynumber {
color:purple;
}
.pyoperator {
color:purple;
}
.pytext {
color:black;
}
.pyerror {
font-weight: bold;
color: red;
}
</pre>
<p>Change the color definitions to alter the appearance.</p>
</div>
<div class="section" id="coloring">
<h2><a class="toc-backref" href="#id23">+/- coloring</a></h2>
<p>This is the only example of an advanced macro included. It does the same job as the <tt class="docutils literal"><span class="pre">colorize</span></tt> macro, but instead of passing it a filename - it works on the text it encloses. This :</p>
<pre class="literal-block">
.. raw:: html
{+coloring}
class coloring:
"""A Macro for coloring chunks of text."""
def open(self, data):
p = color.Parser(data)
p.format(None, None)
src = p.getvalue()
src = src.replace('\n', '<br />\n')
return src.replace(' ', '&nbsp;&nbsp;')
def close(self, *args):
pass
{-coloring}
</pre>
<p>Becomes :</p>
<div class="pysrc"><span class="pykeyword">class</span> <span class="pytext">coloring</span><span class="pyoperator">:</span><br />
<span class="pystring">"""A Macro for coloring chunks of text."""</span><br />
<span class="pykeyword">def</span> <span class="pytext">open</span><span class="pyoperator">(</span><span class="pytext">self</span><span class="pyoperator">,</span> <span class="pytext">data</span><span class="pyoperator">)</span><span class="pyoperator">:</span><br />
<span class="pytext">p</span> <span class="pyoperator">=</span> <span class="pytext">color</span><span class="pyoperator">.</span><span class="pytext">Parser</span><span class="pyoperator">(</span><span class="pytext">data</span><span class="pyoperator">)</span><br />
<span class="pytext">p</span><span class="pyoperator">.</span><span class="pytext">format</span><span class="pyoperator">(</span><span class="pytext">None</span><span class="pyoperator">,</span> <span class="pytext">None</span><span class="pyoperator">)</span><br />
<span class="pytext">src</span> <span class="pyoperator">=</span> <span class="pytext">p</span><span class="pyoperator">.</span><span class="pytext">getvalue</span><span class="pyoperator">(</span><span class="pyoperator">)</span><br />
<span class="pykeyword">return</span> <span class="pytext">src</span><span class="pyoperator">.</span><span class="pytext">replace</span><span class="pyoperator">(</span><span class="pystring">'\n'</span><span class="pyoperator">,</span> <span class="pystring">'<br />\n'</span><span class="pyoperator">)</span><span class="pyoperator">.</span><span class="pytext">replace</span><span class="pyoperator">(</span><span class="pystring">' '</span><span class="pyoperator">,</span> <span class="pystring">'&nbsp;&nbsp;'</span><span class="pyoperator">)</span><br />
<br />
<span class="pykeyword">def</span> <span class="pytext">close</span><span class="pyoperator">(</span><span class="pytext">self</span><span class="pyoperator">,</span> <span class="pyoperator">*</span><span class="pytext">args</span><span class="pyoperator">)</span><span class="pyoperator">:</span><br />
<span class="pykeyword">pass</span><span class="pytext"></span></div></div>
<div class="section" id="small">
<h2><a class="toc-backref" href="#id24">small</a></h2>
<p>This macro puts the enclosed text between <small>.... </small> tags. This is a
feature missing from docutils.</p>
<p><cl}small;Some text that we would like to make smaller} becomes
<small>Some text that we would like to make smaller</small>.</p>
</div>
<div class="section" id="name">
<h2><a class="toc-backref" href="#id25">name</a></h2>
<p>This macro inserts a named anchor tag. This means you can link to the tag using
the name you provide.</p>
<p>{name;anchor} would insert the following HTML - <tt class="docutils literal"><span class="pre"><a</span> <span class="pre">name="anchor"</span> <span class="pre">id="anchor"></a></span></tt>.</p>
<p>You link to it using the HTML - <tt class="docutils literal"><span class="pre"><a</span> <span class="pre">href="#anchor">Link</span> <span class="pre">to</span> <span class="pre">Anchor</a></span></tt>.</p>
</div>
<div class="section" id="title">
<h2><a class="toc-backref" href="#id26">title</a></h2>
<p>This is a shortcut for inserting headlines. You pass in the text and the size
(which defaults to an <tt class="docutils literal"><span class="pre">h3</span></tt> headline).</p>
<p>{title;A Headline} becomes :</p>
<pre class="literal-block">
.. raw:: html
</pre>
<blockquote>
<h3>A Headline</h3></blockquote>
<p>{title;Another Headline;1} becomes :</p>
<pre class="literal-block">
.. raw:: html
</pre>
<blockquote>
<h1>Another Headline</h1></blockquote>
</div>
</div>
<div class="section" id="including-macros-in-rest-pages">
<h1><a class="toc-backref" href="#id27">Including Macros in ReST Pages</a></h1>
<p>Macros are just treated as ordinary text by <a class="reference external" href="http://docutils.sourceforge.net">docutils</a>. That means that they
must fit into the reST syntax. If they don't, then you should escape them using
the raw role or the raw directive.</p>
<div class="section" id="the-raw-role">
<h2><a class="toc-backref" href="#id28">The Raw Role</a></h2>
<p>The raw role can only be used if it is declared at the start of the document.
You must include the following declaration :</p>
<pre class="literal-block">
.. role:: raw-html(raw)
:format: html
</pre>
<p>From then on you can pass anything through docutils untouched, like this :
<tt class="docutils literal"><span class="pre">:raw-html:`{small;Something</span> <span class="pre">to</span> <span class="pre">be</span> <span class="pre">made</span> <span class="pre">small}`</span></tt></p>
<p>In the above example it's not very useful. However, macros return HTML. If you
tried to include HTML in your macro - docutils would escape the <em><</em> tags, and
they would be included as text (or break your macro).</p>
<p>So <tt class="docutils literal"><span class="pre">{small;<em>Something</span> <span class="pre">to</span> <span class="pre">be</span> <span class="pre">made</span> <span class="pre">small</em>}</span></tt> <em>doesn't work</em> in reST
documents. Try it if you don't believe me. <img src="images/smilies/smile.gif" alt="Smile" /> </p>
<p>Instead you can do <tt class="docutils literal"><span class="pre">:raw-html:`{small;<em>Something</span> <span class="pre">to</span> <span class="pre">be</span> <span class="pre">made</span> <span class="pre">small</em>}`</span></tt>,
which does work.</p>
</div>
<div class="section" id="the-raw-directive">
<h2><a class="toc-backref" href="#id29">The Raw Directive</a></h2>
<p>If you use the <a class="reference internal" href="#advanced-macros">Advanced Macros</a> then you almost certainly want to include a
passage of text to transform it. That transformation will be done <em>after</em>
docutils has seen the text. Usually you will want the <em>macro</em> to transform your
text verbatim - and have docutils leave it alone. In this case you need to use
the raw directive.</p>
<p>The classic example of this is the Python source coloring macro :</p>
<pre class="literal-block">
.. raw:: html
{+coloring}
section = sections['section-name']
pages = section['pages']
{-coloring}
</pre>
<p>If you didn't include the raw directive, docutils would do strange things to the
chunk of code - and the macro wouldn't be able to process it.</p>
</div>
<div class="section" id="paragraphs">
<h2><a class="toc-backref" href="#id30">Paragraphs</a></h2>
<p>Docutils treats macros as ordinary text. That means if it comes across one on
its own it will treat it as a paragraph. That may not be what you intend.</p>
<p>For example - the {title} macro is used to create headlines. If you put
this in your document on it's own, then docutils will encase it in paragraph
tags. The following :</p>
<pre class="literal-block">
{title;Some Heading}
</pre>
<p>Produces this HTML :</p>
<pre class="literal-block">
<p><h3>Some Heading</h3></p>
</pre>
<p>This is neither valid, nor what you intended. The way round it, is to use the
raw directive :</p>
<pre class="literal-block">
.. raw:: html
{title;Some Heading}
</pre>
</div>
</div>
<div class="section" id="namespace-and-uservalues">
<h1><a class="toc-backref" href="#id31">namespace and uservalues</a></h1>
<p>The <tt class="docutils literal"><span class="pre">macros.py</span></tt> file that comes with rest2web has a <tt class="docutils literal"><span class="pre">set_uservalues</span></tt> function. This is used to set the global values <tt class="docutils literal"><span class="pre">namespace</span></tt> and <tt class="docutils literal"><span class="pre">uservalues</span></tt>.</p>
<p>That means that you can use access the uservalues and namespace for each page from your macros.</p>
</div>
<div class="section" id="credits">
<h1><a class="toc-backref" href="#id32">Credits</a></h1>
<p>The example macro file uses various Python modules. These are included in the <tt class="docutils literal"><span class="pre">modules</span></tt> directory that comes with the <strong>rest2web</strong> distribution. If you don't use the macros, you don't need this folder.</p>
<p>The various modules come from the following people and places :</p>
<ul class="simple">
<li>A lot of the text in this page comes from the document <a class="reference external" href="http://zephyrfalcon.org/labs/firedrop_macros.html">Firedrop macros</a> by Hans Nowak</li>
<li>The macros module <a class="footnote-reference" href="#id6" id="id3">[3]</a> comes from <a class="reference external" href="http://zephyrfalcon.org/labs/">Firedrop</a> by Hans Nowak</li>
<li>The smilies use <em>smiley.py</em> by <a class="reference external" href="http://www.la-la.com">Mark Andrews</a>.</li>
<li><em>smiley.py</em> depends on <em>path.py</em> by <a class="reference external" href="http://www.jorendorff.com/articles/python/path">Jason Orendorff</a>.</li>
<li><em>smiley.py</em> uses smiley sets that follow the <a class="reference external" href="http://www.phpbb.com/">phpbb</a> convention, you can download alternative sets from <a class="reference external" href="http://www.stylesdb.com/smilies_styles.html">stylesdb</a>.</li>
<li>The smiley set included with <strong>rest2web</strong> is by a gentleman called <a class="reference external" href="http://web.spidercode.de/smilies">Spider</a>.</li>
<li>The colorize macro uses a module called <em>colorize.py</em>. It originated as part of the <a class="reference external" href="http://moinmoin.wikiwikiweb.de">MoinMoin</a> project. The version we use is the one from the <a class="reference external" href="http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/52298">Python Cookbook</a>.</li>
</ul>
</div>
<hr class="docutils" />
<div class="section" id="footnotes">
<h1><a class="toc-backref" href="#id33">Footnotes</a></h1>
<table class="docutils footnote" frame="void" id="id4" rules="none">
<colgroup><col class="label" /><col /></colgroup>
<tbody valign="top">
<tr><td class="label"><a class="fn-backref" href="#id1">[1]</a></td><td>And the filename must only consist of alphanumerics and the underscore character. It should start with a letter, not a number, and is case sensitive, got all that ? <img src="images/smilies/question.gif" alt="Question" /> </td></tr>
</tbody>
</table>
<table class="docutils footnote" frame="void" id="id5" rules="none">
<colgroup><col class="label" /><col /></colgroup>
<tbody valign="top">
<tr><td class="label"><a class="fn-backref" href="#id2">[2]</a></td><td>Assuming the page is in <acronym title="ReStructuredText">reST</acronym> format of course.</td></tr>
</tbody>
</table>
<table class="docutils footnote" frame="void" id="id6" rules="none">
<colgroup><col class="label" /><col /></colgroup>
<tbody valign="top">
<tr><td class="label"><a class="fn-backref" href="#id3">[3]</a></td><td><em>textmacros.py</em> - in the rest2web folder.</td></tr>
</tbody>
</table>
</div>
</div>
</div>
<div id="end">
<p><a href="#startcontent">Return to Top</a><br />
<small>Part of the <a href="http://www.voidspace.org.uk/python/rest2web/">rest2web Docs</a></small><br />
<small>Page last modified <strong>Thu Oct 12 22:24:09 2006</strong>.</small>
</p>
</div>
</div></td>
</tr>
<tr>
<td valign="top" align="left" width="25%">
<div id="left-column">
<div id="sidie">
<ul>
<li class="left-navheader-first">
<a href="index.html" class="left-navheader">Index Page</a>
</li>
<li class="left-navheader">Pages</li>
<li><a href="introduction.html">Introduction</a></li>
<li><a href="quickstart.html">Quickstart</a></li>
<li><a href="config_file.html">Config File</a></li>
<li><a href="tutorial.html">Tutorial</a></li>
<li><a href="command_line.html">Command Line</a></li>
<li><a href="force_mode.html">Force Mode</a></li>
<li><a href="templating.html">Templates</a></li>
<li><a href="restindex.html">restindex</a></li>
<li><a href="functions.html">Functions</a></li>
<li><a href="macros.html">Macros</a></li>
<li><a href="special_files.html">Special Files</a></li>
<li class="left-navheader">Sub Sections</li>
<li><a href="reference/index.html">Reference</a></li>
<li><a href="test_site/index.html">Test Site</a></li>
<li><a href="translation/index.html">Translation</a></li>
<li><a href="gallery_test/index.html">Gallery</a></li>
</ul>
</div>
<p class="sidieimg">
<a href="http://www.python.org">
<img src="images/logos/new_python.gif" width="88"
height="103" border="0" alt="Powered by Python" />
</a>
</p>
<p class="sidieimg">
<a href="http://sourceforge.net/donate/index.php?group_id=138579">
<img src="http://images.sourceforge.net/images/project-support.jpg" width="100"
height="32" border="0" alt="Support This Project" />
</a>
</p>
<p class="sidieimg">
<a href="http://www.voidspace.org.uk/python/rest2web/"><img
src="images/logos/rest2web140x62.gif" width="142" height="62"
alt="Site Built with rest2web" /></a><br />
</p>
</div>
</td>
</tr>
</table>
<hr />
<p class="sidieimg">
<a href="http://www.voidspace.org.uk/python/rest2web/"><img src="images/logos/rest2web200x80.gif" width="200" height="80" alt="Site Built with rest2web" /></a>
<a href="http://sourceforge.net"><img src="http://sourceforge.net/sflogo.php?group_id=138579&type=5" width="210" height="62" alt="SourceForge.net Logo" /></a>
<a href="http://www.opensource.org"><img src="images/logos/osi-certified-120x100.gif" width="120" height="100" alt="Certified Open Source" border="1" /></a>
</p>
<p class="sidieimg">
<script src="http://www.google-analytics.com/urchin.js" type="text/javascript">
</script>
<script type="text/javascript">
_uacct = "UA-203625-1";
urchinTracker();
</script>
</p>
<p class="sidieimg">
<a href="http://www.voidspace.org.uk/python/index.shtml"><img
src="images/logos/pythonbanner.gif" width="468" height="60"
alt="Python on Voidspace" /></a>
</p>
<div id="footer">
Copyright © Voidspace<br />Design by <a href="http://www.fuchsiashockz.co.uk">Fuchsiashockz</a> | <a href="http://validator.w3.org/check?uri=referer" title="Validate code as W3C XHTML 1.1 Strict Compliant">W3C XHTML 1.1</a> | <a href="http://jigsaw.w3.org/css-validator/check?uri=referer" title="Validate Style Sheet as W3C CSS 2.0 Compliant">W3C CSS 2.0</a>
</div>
</div>
</body>
</html>
|