You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
pollen/pollen/scribblings/pollen/Quick_start.html

2 lines
42 KiB
HTML

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html><head><meta http-equiv="content-type" content="text/html; charset=utf-8"/><title>3&nbsp;Quick start</title><link rel="stylesheet" type="text/css" href="scribble.css" title="default"/><link rel="stylesheet" type="text/css" href="racket.css" title="default"/><link rel="stylesheet" type="text/css" href="manual-style.css" title="default"/><link rel="stylesheet" type="text/css" href="manual-racket.css" title="default"/><script type="text/javascript" src="scribble-common.js"></script><!--[if IE 6]><style type="text/css">.SIEHidden { overflow: hidden; }</style><![endif]--></head><body id="scribble-racket-lang-org"><div class="tocset"><div class="tocview"><div class="tocviewlist tocviewlisttopspace"><div class="tocviewtitle"><table cellspacing="0" cellpadding="0"><tr><td style="width: 1em;"><a href="javascript:void(0);" title="Expand/Collapse" class="tocviewtoggle" onclick="TocviewToggle(this,&quot;tocview_0&quot;);">&#9660;</a></td><td></td><td><a href="index.html" class="tocviewlink" data-pltdoc="x">Pollen:<span class="mywbr"> &nbsp;</span> the book is a program</a></td></tr></table></div><div class="tocviewsublisttop" style="display: block;" id="tocview_0"><table cellspacing="0" cellpadding="0"><tr><td align="right">1&nbsp;</td><td><a href="Installation.html" class="tocviewlink" data-pltdoc="x">Installation</a></td></tr><tr><td align="right">2&nbsp;</td><td><a href="Acknowledgments.html" class="tocviewlink" data-pltdoc="x">Acknowledgments</a></td></tr><tr><td align="right">3&nbsp;</td><td><a href="Quick_start.html" class="tocviewselflink" data-pltdoc="x">Quick start</a></td></tr><tr><td align="right">4&nbsp;</td><td><a href="Why_I_made_Pollen.html" class="tocviewlink" data-pltdoc="x">Why I made Pollen</a></td></tr><tr><td align="right">5&nbsp;</td><td><a href="Source_formats.html" class="tocviewlink" data-pltdoc="x">Source formats</a></td></tr><tr><td align="right">6&nbsp;</td><td><a href="reader.html" class="tocviewlink" data-pltdoc="x">&#9674; command overview</a></td></tr><tr><td align="right">7&nbsp;</td><td><a href="Module_reference.html" class="tocviewlink" data-pltdoc="x">Module reference</a></td></tr><tr><td align="right">8&nbsp;</td><td><a href="License___source_code.html" class="tocviewlink" data-pltdoc="x">License &amp; source code</a></td></tr></table></div></div><div class="tocviewlist"><table cellspacing="0" cellpadding="0"><tr><td style="width: 1em;"><a href="javascript:void(0);" title="Expand/Collapse" class="tocviewtoggle" onclick="TocviewToggle(this,&quot;tocview_1&quot;);">&#9658;</a></td><td>3&nbsp;</td><td><a href="Quick_start.html" class="tocviewselflink" data-pltdoc="x">Quick start</a></td></tr></table><div class="tocviewsublistbottom" style="display: none;" id="tocview_1"><table cellspacing="0" cellpadding="0"><tr><td align="right">3.1&nbsp;</td><td><a href="Quick_start.html#%28part._.Creating_a_source_file%29" class="tocviewlink" data-pltdoc="x">Creating a source file</a></td></tr><tr><td align="right">3.2&nbsp;</td><td><a href="Quick_start.html#%28part._.Running_a_source_file%29" class="tocviewlink" data-pltdoc="x">Running a source file</a></td></tr><tr><td align="right">3.3&nbsp;</td><td><a href="Quick_start.html#%28part._.Naming__saving__and_rendering_a_source_file%29" class="tocviewlink" data-pltdoc="x">Naming, saving, and rendering a source file</a></td></tr><tr><td align="right">3.4&nbsp;</td><td><a href="Quick_start.html#%28part._.The_project_server%29" class="tocviewlink" data-pltdoc="x">The project server</a></td></tr><tr><td align="right">3.5&nbsp;</td><td><a href="Quick_start.html#%28part._.Intermission%29" class="tocviewlink" data-pltdoc="x">Intermission</a></td></tr><tr><td align="right">3.6&nbsp;</td><td><a href="Quick_start.html#%28part._.Pollen_as_a_preprocessor%29" class="tocviewlink" data-pltdoc="x">Pollen as a preprocessor</a></td></tr><tr><td align="right">3.7&nbsp;</td><td><a href="Quick_start.html#%28part._.Markdown_mode%29" class="tocviewlink" data-pltdoc="x">Markdown mode</a></td></tr><tr><td align="right">3.8&nbsp;</td><td><a href="Quick_start.html#%28part._.Markup_mode%29" class="tocviewlink" data-pltdoc="x">Markup mode</a></td></tr><tr><td align="right">3.9&nbsp;</td><td><a href="Quick_start.html#%28part._.Templates%29" class="tocviewlink" data-pltdoc="x">Templates</a></td></tr></table></div></div></div><div class="tocsub"><div class="tocsubtitle">On this page:</div><table class="tocsublist" cellspacing="0"><tr><td><span class="tocsublinknumber">3.1<tt>&nbsp;</tt></span><a href="#%28part._.Creating_a_source_file%29" class="tocsubseclink" data-pltdoc="x">Creating a source file</a></td></tr><tr><td><span class="tocsublinknumber">3.2<tt>&nbsp;</tt></span><a href="#%28part._.Running_a_source_file%29" class="tocsubseclink" data-pltdoc="x">Running a source file</a></td></tr><tr><td><span class="tocsublinknumber">3.3<tt>&nbsp;</tt></span><a href="#%28part._.Naming__saving__and_rendering_a_source_file%29" class="tocsubseclink" data-pltdoc="x">Naming, saving, and rendering a source file</a></td></tr><tr><td><span class="tocsublinknumber">3.4<tt>&nbsp;</tt></span><a href="#%28part._.The_project_server%29" class="tocsubseclink" data-pltdoc="x">The project server</a></td></tr><tr><td><span class="tocsublinknumber">3.5<tt>&nbsp;</tt></span><a href="#%28part._.Intermission%29" class="tocsubseclink" data-pltdoc="x">Intermission</a></td></tr><tr><td><span class="tocsublinknumber">3.6<tt>&nbsp;</tt></span><a href="#%28part._.Pollen_as_a_preprocessor%29" class="tocsubseclink" data-pltdoc="x">Pollen as a preprocessor</a></td></tr><tr><td><span class="tocsublinknumber">3.7<tt>&nbsp;</tt></span><a href="#%28part._.Markdown_mode%29" class="tocsubseclink" data-pltdoc="x">Markdown mode</a></td></tr><tr><td><span class="tocsublinknumber">3.8<tt>&nbsp;</tt></span><a href="#%28part._.Markup_mode%29" class="tocsubseclink" data-pltdoc="x">Markup mode</a></td></tr><tr><td><span class="tocsublinknumber">3.9<tt>&nbsp;</tt></span><a href="#%28part._.Templates%29" class="tocsubseclink" data-pltdoc="x">Templates</a></td></tr></table></div></div><div class="maincolumn"><div class="main"><div class="versionbox"><span class="version">6.0.0.5</span></div><div class="navsettop"><span class="navleft">&nbsp;&nbsp;</span><span class="navright">&nbsp;&nbsp;<a href="Acknowledgments.html" title="backward to &quot;2 Acknowledgments&quot;" data-pltdoc="x">&larr; prev</a>&nbsp;&nbsp;<a href="index.html" title="up to &quot;Pollen: the book is a program&quot;" data-pltdoc="x">up</a>&nbsp;&nbsp;<a href="Why_I_made_Pollen.html" title="forward to &quot;4 Why I made Pollen&quot;" data-pltdoc="x">next &rarr;</a></span>&nbsp;</div><h3>3<tt>&nbsp;</tt><a name="(part._.Quick_start)"></a>Quick start</h3><h4>3.1<tt>&nbsp;</tt><a name="(part._.Creating_a_source_file)"></a>Creating a source file</h4><p>Assuming you&rsquo;ve installed Racket &amp; Pollen, launch DrRacket.</p><p>Open a new document. Change the top line to:</p><blockquote class="SCodeFlow"><p><font class="badlink"><span class="RktModLink"><span class="RktMod">#lang</span></span></font><span class="hspace">&nbsp;</span><a href="Source_formats.html" class="RktModLink" data-pltdoc="x"><span class="RktSym">pollen</span></a></p></blockquote><p>The first line of every Pollen source file will start with <span class="stt">#lang pollen</span>.</p><h4>3.2<tt>&nbsp;</tt><a name="(part._.Running_a_source_file)"></a>Running a source file</h4><p>Add a second line to your source file so it reads:</p><blockquote class="SCodeFlow"><table cellspacing="0" class="RktBlk"><tr><td><font class="badlink"><span class="RktModLink"><span class="RktMod">#lang</span></span></font><span class="hspace">&nbsp;</span><a href="Source_formats.html" class="RktModLink" data-pltdoc="x"><span class="RktSym">pollen</span></a></td></tr><tr><td><span class="RktSym">Hello</span><span class="hspace">&nbsp;</span><span class="RktSym">world</span></td></tr></table></blockquote><p>Click the <span class="ssansserif">Run</span> button. In the interactions window, you&rsquo;ll see the result:</p><blockquote class="SCodeFlow"><p><span class="RktVal">Hello world</span></p></blockquote><p>Not bad. I think Pollen&#160;just won the <a href="http://en.wikipedia.org/wiki/List_of_Hello_world_program_examples">Hello World Competition</a>.</p><p>You can work with Pollen source files in any text editor. The key advantage of DrRacket is that you can preview the results by running the file.</p><p>Try editing your source file:</p><blockquote class="SCodeFlow"><table cellspacing="0" class="RktBlk"><tr><td><font class="badlink"><span class="RktModLink"><span class="RktMod">#lang</span></span></font><span class="hspace">&nbsp;</span><a href="Source_formats.html" class="RktModLink" data-pltdoc="x"><span class="RktSym">pollen</span></a></td></tr><tr><td><span class="RktSym">Goodbye</span><span class="hspace">&nbsp;</span><span class="RktSym">Stranger</span></td></tr><tr><td><span class="RktSym">Breakfast</span><span class="hspace">&nbsp;</span><span class="RktSym">in</span><span class="hspace">&nbsp;</span><span class="RktSym">America</span></td></tr><tr><td><span class="RktSym">Take</span><span class="hspace">&nbsp;</span><span class="RktSym">the</span><span class="hspace">&nbsp;</span><span class="RktSym">Long</span><span class="hspace">&nbsp;</span><span class="RktSym">Way</span><span class="hspace">&nbsp;</span><span class="RktSym">Home</span></td></tr></table></blockquote><p>You don&rsquo;t have to use Supertramp song titles. Any text will do. When you click <span class="ssansserif">Run</span> again, you&rsquo;ll see whatever you typed:</p><blockquote class="SCodeFlow"><p><span class="RktVal">Goodbye Stranger</span><br/><span class="RktVal">Breakfast in America</span><br/><span class="RktVal">Take the Long Way Home</span></p></blockquote><p>We won&rsquo;t do it a third time. You get the point &#8212; any plain text is valid within a Pollen source file, and gets printed as is. You never have to perform the incantations of typical programming languages:</p><p><table cellspacing="0"><tr><td><p><span class="stt">print "Hello world"</span></p></td></tr><tr><td><p><span class="stt">document.write('Hello world');</span></p></td></tr><tr><td><p><span class="stt">printf("Hello world");</span></p></td></tr></table></p><p>In Pollen, what you write is what you get.</p><h4>3.3<tt>&nbsp;</tt><a name="(part._.Naming__saving__and_rendering_a_source_file)"></a>Naming, saving, and rendering a source file</h4><p>Save this file with the name <span class="stt">hello.txt.pp</span> in any convenient directory. The desktop is fine.</p><p>Open a terminal window and issue two commands:</p><p><table cellspacing="0"><tr><td><p><span class="stt">&gt; cd [directory containing your file]</span></p></td></tr><tr><td><p><span class="stt">&gt; raco pollen render hello.txt.pp</span></p></td></tr></table></p><p>After a moment, a new file will appear called <span class="stt">hello.txt</span>. Let&rsquo;s see what&rsquo;s in it:</p><p><table cellspacing="0"><tr><td><p><span class="stt">&gt; cat hello.txt</span></p></td></tr><tr><td><p><span class="stt">Goodbye Stranger</span></p></td></tr><tr><td><p><span class="stt">Breakfast in America</span></p></td></tr><tr><td><p><span class="stt">Take the Long Way Home</span></p></td></tr></table></p><p>You&rsquo;ve just learned three things:</p><ul><li><p>Pollen commands in the terminal begin with <span class="stt">raco pollen</span>, followed by a specific command (in this case <span class="stt">render</span>) and sometimes an argument (in this case <span class="stt">hello.txt.pp</span>).</p></li><li><p>The <span class="stt">render</span> command takes the ouput from your source file &#8212;&#160;meaning, the result you previewed in DrRacket in the previous step &#8212;&#160;and saves it to another file.</p></li><li><p>The name of the output file is the same as the source file, minus the Pollen source extension. So <span class="stt">hello.txt.pp</span> becomes <span class="stt">hello.txt</span>.</p></li></ul><p>Try editing the text in the <span class="stt">hello.txt.pp</span> source file and running <span class="stt">raco pollen render hello.txt.pp</span> again. The old <span class="stt">hello.txt</span> will be replaced with a new one showing your changes. And so you&rsquo;ve learned a fourth thing:</p><ul><li><p>Pollen works by rendering output files from source files. Output files can be overwritten. Therefore, you should only make edits to your source files.</p></li></ul><h4>3.4<tt>&nbsp;</tt><a name="(part._.The_project_server)"></a>The project server</h4><p>You&rsquo;ve just learned two ways to see the output of a Pollen source file &#8212;&#160;first, you ran it in DrRacket. Then, you rendered it to an output file.</p><p>Now here&rsquo;s a third: the Pollen project server. Here&rsquo;s how you start it. Return to your terminal window and issue two commands:</p><p><table cellspacing="0"><tr><td><p><span class="stt">&gt; cd [directory containing your hello.txt.pp file]</span></p></td></tr><tr><td><p><span class="stt">&gt; raco pollen start</span></p></td></tr></table></p><p>After a moment, you&rsquo;ll see the startup message:</p><p><table cellspacing="0"><tr><td><p><span class="stt">Welcome to Pollen 0.001 (Racket 6.0.0.5)</span></p></td></tr><tr><td><p><span class="stt">Project root is /path/to/your/directory</span></p></td></tr><tr><td><p><span class="stt">Project server is http://localhost:8080 (Ctrl-C to exit)</span></p></td></tr><tr><td><p><span class="stt">Project dashboard is http://localhost:8080/index.ptree</span></p></td></tr><tr><td><p><span class="stt">Ready to rock</span></p></td></tr></table></p><p>Open a web browser and point it at <a href="http://localhost:8080/index.ptree"><span class="stt">http://localhost:8080/index.ptree</span></a>. The top of the window will say <span class="stt">Project root</span>. Below that will be a listing of the files in the directory.</p><p>Among them will be <span class="stt">hello.txt</span>, with a greyed-out <span class="stt">.pp</span> extension. Click on it, and you&rsquo;ll be taken to <a href="http://localhost:8080/hello.txt"><span class="stt">http://localhost:8080/hello.txt</span></a>, where you&rsquo;ll see:</p><p><table cellspacing="0"><tr><td><p><span class="stt">Goodbye Stranger</span></p></td></tr><tr><td><p><span class="stt">Breakfast in America</span></p></td></tr><tr><td><p><span class="stt">Take the Long Way Home</span></p></td></tr></table></p><p>That&rsquo;s the boring part. Here&rsquo;s the good part. Leave the project server running. Open your source file again in DrRacket and edit it as follows:</p><blockquote class="SCodeFlow"><blockquote class="Rfilebox"><p class="Rfiletitle"><span class="Rfilename"><span class="stt">"hello.txt.pp"</span></span></p><blockquote class="Rfilecontent"><table cellspacing="0" class="RktBlk"><tr><td><font class="badlink"><span class="RktModLink"><span class="RktMod">#lang</span></span></font><span class="hspace">&nbsp;</span><a href="Source_formats.html" class="RktModLink" data-pltdoc="x"><span class="RktSym">pollen</span></a></td></tr><tr><td><span class="RktSym">Mean</span><span class="hspace">&nbsp;</span><span class="RktSym">Street</span></td></tr><tr><td><span class="RktSym">Panama</span></td></tr><tr><td><span class="RktSym">Hear</span><span class="hspace">&nbsp;</span><span class="RktSym">About</span><span class="hspace">&nbsp;</span><span class="RktSym">It</span><span class="hspace">&nbsp;</span><span class="RktSym">Later</span></td></tr></table></blockquote></blockquote></blockquote><p>Go back to your web browser and reload <a href="http://localhost:8080/hello.txt"><span class="stt">http://localhost:8080/hello.txt</span></a>. Now you&rsquo;ll see&#160;this:</p><p><table cellspacing="0"><tr><td><p><span class="stt">Mean Street</span></p></td></tr><tr><td><p><span class="stt">Panama</span></p></td></tr><tr><td><p><span class="stt">Hear About It Later</span></p></td></tr></table></p><p>Notice what happened &#8212;&#160;the Pollen project server dynamically regenerated the output file (<span class="stt">hello.txt</span>) from the source file (<span class="stt">hello.txt.pp</span>) after you edited the source. If you like, try making some more changes to <span class="stt">hello.txt.pp</span>, and reloading the browser to see the updates in <span class="stt">hello.txt</span>.</p><h4>3.5<tt>&nbsp;</tt><a name="(part._.Intermission)"></a>Intermission</h4><p>That covers input &amp; output. Now let&rsquo;s circle back and look at what else you can do with Pollen (beyond the epic achievement of displaying plain text&#160;in a web browser).</p><p>For the rest of this tutorial, I recommend keeping two windows on screen: a web-browser window pointed at your project server (the main URL is <a href="http://localhost:8080/index.ptree"><span class="stt">http://localhost:8080/index.ptree</span></a>) and the DrRacket editing window.</p><h4>3.6<tt>&nbsp;</tt><a name="(part._.Pollen_as_a_preprocessor)"></a>Pollen as a preprocessor</h4><p>A <span style="font-style: italic">preprocessor</span> is a tool for making systematic, automated changes to a source file before the main processing happens. A preprocessor can also be used to add programming logic to files that otherwise don&rsquo;t support it.</p><p>For instance, HTML. In DrRacket, create a new file called <span class="stt">margin.html.pp</span> in your project directory:</p><blockquote class="SCodeFlow"><blockquote class="Rfilebox"><p class="Rfiletitle"><span class="Rfilename"><span class="stt">"margin.html.pp"</span></span></p><blockquote class="Rfilecontent"><table cellspacing="0" class="RktBlk"><tr><td><font class="badlink"><span class="RktModLink"><span class="RktMod">#lang</span></span></font><span class="hspace">&nbsp;</span><a href="Source_formats.html" class="RktModLink" data-pltdoc="x"><span class="RktSym">pollen</span></a></td></tr><tr><td><span class="RktSym">&lt;body</span><span class="hspace">&nbsp;</span><span class="RktSym">style=</span><span class="RktVal">"margin: 5em; border:1px solid black"</span><span class="RktSym">&gt;</span></td></tr><tr><td><span class="RktSym">5em</span><span class="hspace">&nbsp;</span><span class="RktSym">is</span><span class="hspace">&nbsp;</span><span class="RktSym">the</span><span class="hspace">&nbsp;</span><span class="RktSym">inset.</span></td></tr><tr><td><span class="RktSym">&lt;/body&gt;</span></td></tr></table></blockquote></blockquote></blockquote><p>The &ldquo;<span class="stt">.pp</span>&rdquo; file extension &#8212; which you saw before, with <span class="stt">hello.txt.pp</span> &#8212; stands for &ldquo;Pollen preprocessor.&rdquo; You can use the Pollen preprocessor with any text-based file by inserting <span class="stt">#lang pollen</span> as the first line, and adding the <span class="stt">.pp</span> file extension.</p><p>But for now, go to your <a href="http://localhost:8080/index.ptree">project dashboard</a> and click on <a href="http://localhost:8080/margin.html"><span class="stt">margin.html</span></a>. You should see a black box containing the text &ldquo;5em is the inset.&rdquo;</p><p>Let&rsquo;s suppose you want to change the inset to 30%. Without a preprocessor, you&rsquo;d have to search &amp; replace each value. But with a preprocessor, you can move the inset value into a variable, and update it from that one location. So first, introduce a variable called <span class="stt">my-inset</span> by using the <span class="RktSym">define</span> command:</p><blockquote class="SCodeFlow"><blockquote class="Rfilebox"><p class="Rfiletitle"><span class="Rfilename"><span class="stt">"margin.html.pp"</span></span></p><blockquote class="Rfilecontent"><table cellspacing="0" class="RktBlk"><tr><td><font class="badlink"><span class="RktModLink"><span class="RktMod">#lang</span></span></font><span class="hspace">&nbsp;</span><a href="Source_formats.html" class="RktModLink" data-pltdoc="x"><span class="RktSym">pollen</span></a></td></tr><tr><td><span class="RktSym">&#9674;define</span><span class="RktPn">[</span><span class="RktSym">my-inset</span><span class="RktPn">]</span><span class="RktPn">{</span><span class="RktSym">30%</span><span class="RktPn">}</span></td></tr><tr><td><span class="RktSym">&lt;body</span><span class="hspace">&nbsp;</span><span class="RktSym">style=</span><span class="RktVal">"margin: 10em; border:1px solid black"</span><span class="RktSym">&gt;</span></td></tr><tr><td><span class="RktSym">10em</span><span class="hspace">&nbsp;</span><span class="RktSym">is</span><span class="hspace">&nbsp;</span><span class="RktSym">the</span><span class="hspace">&nbsp;</span><span class="RktSym">inset.</span></td></tr><tr><td><span class="RktSym">&lt;/body&gt;</span></td></tr></table></blockquote></blockquote></blockquote><p>The &#9674; character is called a <span style="font-style: italic">lozenge</span>. In Pollen, the lozenge is a special character that marks anything Pollen should interpret as a command (rather than plain text). The whole command <span class="stt">&#9674;define[my-inset]{30%}</span> means &ldquo;create a variable called <span class="stt">my-inset</span> and give it the value <span class="stt">30%</span>.&rdquo;</p><p>Then put the variable into the HTML like so, this time using the &#9674; character with the variable name in the two places the value appears:</p><blockquote class="SCodeFlow"><blockquote class="Rfilebox"><p class="Rfiletitle"><span class="Rfilename"><span class="stt">"margin.html.pp"</span></span></p><blockquote class="Rfilecontent"><table cellspacing="0" class="RktBlk"><tr><td><font class="badlink"><span class="RktModLink"><span class="RktMod">#lang</span></span></font><span class="hspace">&nbsp;</span><a href="Source_formats.html" class="RktModLink" data-pltdoc="x"><span class="RktSym">pollen</span></a></td></tr><tr><td><span class="RktSym">&#9674;define</span><span class="RktPn">[</span><span class="RktSym">my-inset</span><span class="RktPn">]</span><span class="RktPn">{</span><span class="RktSym">30%</span><span class="RktPn">}</span></td></tr><tr><td><span class="RktSym">&lt;body</span><span class="hspace">&nbsp;</span><span class="RktSym">style=</span><span class="RktVal">"margin: &#9674;my-inset; border:1px solid black"</span><span class="RktSym">&gt;</span></td></tr><tr><td><span class="RktSym">&#9674;my-inset</span><span class="hspace">&nbsp;</span><span class="RktSym">is</span><span class="hspace">&nbsp;</span><span class="RktSym">the</span><span class="hspace">&nbsp;</span><span class="RktSym">inset.</span></td></tr><tr><td><span class="RktSym">&lt;/body&gt;</span></td></tr></table></blockquote></blockquote></blockquote><p>Now reload <a href="http://localhost:8080/margin.html"><span class="stt">margin.html</span></a>. You&rsquo;ll see that the size of the margin has changed (because of the change to the <span class="stt">style</span> attribute) and so has the text of the HTML. If you like, try editing <span class="stt">my-inset</span> with different values and reloading the page. You can also try using <span class="RktSym">define</span> to create another variable (for instance, to change the color of the box border).</p><p>Still, this is the tiniest tip of the iceberg. The Pollen preprocessor gives you access to everything in the Racket programming language &#8212;&#160;including math functions, text manipulation, and so on.</p><h4>3.7<tt>&nbsp;</tt><a name="(part._.Markdown_mode)"></a>Markdown mode</h4><p>When used as a preprocessor, Pollen&rsquo;s rule is that what you write is what you get. But if you&rsquo;re targeting HTML, who wants to type out all those <span class="stt">&lt;tedious&gt;tags&lt;/tedious&gt;</span>? You can make Pollen do the heavy lifting by using it as a source decoder.</p><p>For instance, Markdown mode. Markdown is a simplified <a href="https://daringfireball.net/projects/markdown/">notation system</a> for HTML. You can use Pollen&rsquo;s Markdown decoder by inserting <span class="stt">#lang pollen</span> as the first line, and adding the <span class="stt">.pmd</span> file extension.</p><p>Try it. In DrRacket, create a file with the following lines and save it as <span class="stt">downtown.html.pmd</span>:</p><blockquote class="SCodeFlow"><blockquote class="Rfilebox"><p class="Rfiletitle"><span class="Rfilename"><span class="stt">"downtown.html.pmd"</span></span></p><blockquote class="Rfilecontent"><table cellspacing="0" class="RktBlk"><tr><td><font class="badlink"><span class="RktModLink"><span class="RktMod">#lang</span></span></font><span class="hspace">&nbsp;</span><a href="Source_formats.html" class="RktModLink" data-pltdoc="x"><span class="RktSym">pollen</span></a></td></tr><tr><td><span class="hspace">&nbsp;</span></td></tr><tr><td><span class="RktSym">Pollen</span><span class="hspace">&nbsp;</span><span class="RktSym">+</span><span class="hspace">&nbsp;</span><span class="RktSym">Markdown</span></td></tr><tr><td><span class="RktSym"><span class="nobreak">-----------------</span></span></td></tr><tr><td><span class="hspace">&nbsp;</span></td></tr><tr><td><span class="RktSym">+</span><span class="hspace">&nbsp;</span><span class="RktSym">You</span><span class="hspace">&nbsp;</span><span class="RktSym">**wanted**</span><span class="hspace">&nbsp;</span><span class="RktSym">it</span><span class="hspace">&nbsp;</span><span class="RktSym">&#8212;</span><span class="hspace">&nbsp;</span><span class="RktSym">you</span><span class="hspace">&nbsp;</span><span class="stt">_got_</span><span class="hspace">&nbsp;</span><span class="RktSym">it.</span></td></tr><tr><td><span class="hspace">&nbsp;</span></td></tr><tr><td><span class="RktSym">+</span><span class="hspace">&nbsp;</span><span class="RktPn">[</span><span class="RktSym">search</span><span class="hspace">&nbsp;</span><span class="RktSym">for</span><span class="hspace">&nbsp;</span><span class="RktSym">Racket</span><span class="RktPn">]</span><span class="RktPn">(</span><span class="RktSym">https://google.com/search?q=racket</span><span class="RktPn">)</span></td></tr></table></blockquote></blockquote></blockquote><p>As before, go to the <a href="http://localhost:8080/index.ptree">dashboard</a> for the project server. This time, click the link for <a href="http://localhost:8080/downtown.html"><span class="stt">downtown.html</span></a>. You&rsquo;ll see something like this:</p><blockquote class="SCodeFlow"><p><span style="font-weight: bold"><span class="Larger">Pollen + Markdown</span></span></p><blockquote><ul><li><p>You <span style="font-weight: bold">wanted</span> it &#8212;&#160;you <span style="font-style: italic">got</span> it.</p></li><li><p><a href="https://google.com/search?q=racket">search for Racket</a></p></li></ul></blockquote></blockquote><p>As usual, you&rsquo;re welcome to edit <span class="stt">downtown.html.pmd</span> and then refresh the web browser to see the changes.</p><p>In Markdown mode, you can still embed Pollen commands within the source as you did in preprocessor mode. Just keep in mind that your commands need to produce valid Markdown (as opposed to raw HTML). For instance, use <span class="stt">define</span> to create a variable called <span class="stt">metal</span>, and insert it into the Markdown:</p><blockquote class="SCodeFlow"><blockquote class="Rfilebox"><p class="Rfiletitle"><span class="Rfilename"><span class="stt">"downtown.html.pmd"</span></span></p><blockquote class="Rfilecontent"><table cellspacing="0" class="RktBlk"><tr><td><font class="badlink"><span class="RktModLink"><span class="RktMod">#lang</span></span></font><span class="hspace">&nbsp;</span><a href="Source_formats.html" class="RktModLink" data-pltdoc="x"><span class="RktSym">pollen</span></a></td></tr><tr><td><span class="RktSym">&#9674;define</span><span class="RktPn">[</span><span class="RktSym">metal</span><span class="RktPn">]</span><span class="RktPn">{</span><span class="RktSym">Plutonium</span><span class="RktPn">}</span></td></tr><tr><td><span class="hspace">&nbsp;</span></td></tr><tr><td><span class="RktSym">Pollen</span><span class="hspace">&nbsp;</span><span class="RktSym">+</span><span class="hspace">&nbsp;</span><span class="RktSym">&#9674;metal</span></td></tr><tr><td><span class="RktSym"><span class="nobreak">--------</span></span></td></tr><tr><td><span class="hspace">&nbsp;</span></td></tr><tr><td><span class="RktSym">+</span><span class="hspace">&nbsp;</span><span class="RktSym">You</span><span class="hspace">&nbsp;</span><span class="RktSym">**wanted**</span><span class="hspace">&nbsp;</span><span class="RktSym">&#9674;metal</span><span class="hspace">&nbsp;</span><span class="RktSym">&#8212;</span><span class="hspace">&nbsp;</span><span class="RktSym">you</span><span class="hspace">&nbsp;</span><span class="stt">_got_</span><span class="hspace">&nbsp;</span><span class="RktSym">it.</span></td></tr><tr><td><span class="hspace">&nbsp;</span></td></tr><tr><td><span class="RktSym">+</span><span class="hspace">&nbsp;</span><span class="RktPn">[</span><span class="RktSym">search</span><span class="hspace">&nbsp;</span><span class="RktSym">for</span><span class="hspace">&nbsp;</span><span class="RktSym">&#9674;metal</span><span class="RktPn">]</span><span class="RktPn">(</span><span class="RktSym">https://google.com/search?q=&#9674;metal</span><span class="RktPn">)</span></td></tr></table></blockquote></blockquote></blockquote><p>Refresh <a href="http://localhost:8080/downtown.html"><span class="stt">downtown.html</span></a> in the browser:</p><blockquote class="SCodeFlow"><p><span style="font-weight: bold"><span class="Larger">Pollen + Plutonium</span></span></p><blockquote><ul><li><p>You <span style="font-weight: bold">wanted</span> Plutonium &#8212;&#160;you <span style="font-style: italic">got</span> it.</p></li><li><p><a href="https://google.com/search?q=Plutonium">search for Plutonium</a></p></li></ul></blockquote></blockquote><p>Pollen is handling two tasks here: interpreting the commands in the source, and then converting the Markdown to HTML. But what if you wanted to use Pollen as a preprocessor that outputs a Markdown file? No problem &#8212;&#160;just change the source name from <span class="stt">downtown.html.pmd</span> to <span class="stt">downtown.md.pp</span>. Changing the extension from <span class="stt">.pmd</span> to <span class="stt">.pp</span> switches Pollen from Markdown mode back to preprocessor mode. And changing the base name from <span class="stt">downtown.html</span> to <span class="stt">downtown.md</span> updates the name of the output file.</p><h4>3.8<tt>&nbsp;</tt><a name="(part._.Markup_mode)"></a>Markup mode</h4><p>If all you need to do is produce basic HTML, Markdown is great. But if you need to do semantic markup or other kinds of custom markup, it&rsquo;s not flexible enough.</p><p>In that case, you can use Pollen markup mode. To use Pollen markup, insert <span class="stt">#lang pollen</span> as the first line of your source file, and add a <span class="stt">.pm</span> file extension.</p><p>Compared to Markdown mode, Pollen markup mode is wide open. Markdown mode gives you a limited set of formatting tools (i.e., the ones supported by Markdown). But in markup mode, you can use any tags you want. Markdown mode decodes the source in a fixed way (i.e., with the Markdown decoder). But markup mode lets you build any decoder you want.</p><p>Let&rsquo;s convert our Markdown example into Pollen markup. Marking up content is simple: insert the lozenge character (<span class="stt">&#9674;</span>) followed by the name of the tag (<span class="stt">&#9674;tag</span>), followed by the content of the tag in curly braces (<span class="stt">&#9674;tag{content}</span>). In DrRacket, create a new file called <span class="stt">uptown.html.pm</span> as follows:</p><blockquote class="SCodeFlow"><blockquote class="Rfilebox"><p class="Rfiletitle"><span class="Rfilename"><span class="stt">"uptown.html.pm"</span></span></p><blockquote class="Rfilecontent"><table cellspacing="0" class="RktBlk"><tr><td><font class="badlink"><span class="RktModLink"><span class="RktMod">#lang</span></span></font><span class="hspace">&nbsp;</span><a href="Source_formats.html" class="RktModLink" data-pltdoc="x"><span class="RktSym">pollen</span></a></td></tr><tr><td><span class="hspace">&nbsp;</span></td></tr><tr><td><span class="RktSym">&#9674;headline</span><span class="RktPn">{</span><span class="RktSym">Pollen</span><span class="hspace">&nbsp;</span><span class="RktSym">markup</span><span class="RktPn">}</span></td></tr><tr><td><span class="hspace">&nbsp;</span></td></tr><tr><td><span class="RktSym">&#9674;items</span><span class="RktPn">{</span></td></tr><tr><td><span class="hspace">&nbsp;</span></td></tr><tr><td><span class="RktSym">&#9674;item</span><span class="RktPn">{</span><span class="RktSym">You</span><span class="hspace">&nbsp;</span><span class="RktSym">&#9674;strong</span><span class="RktPn">{</span><span class="RktSym">wanted</span><span class="RktPn">}</span><span class="hspace">&nbsp;</span><span class="RktSym">it</span><span class="hspace">&nbsp;</span><span class="RktSym">&#8212;</span><span class="hspace">&nbsp;</span><span class="RktSym">you</span><span class="hspace">&nbsp;</span><span class="RktSym">&#9674;em</span><span class="RktPn">{</span><span class="RktSym">got</span><span class="RktPn">}</span><span class="hspace">&nbsp;</span><span class="RktSym">it.</span><span class="RktPn">}</span></td></tr><tr><td><span class="hspace">&nbsp;</span></td></tr><tr><td><span class="RktSym">&#9674;item</span><span class="RktPn">{</span><span class="RktSym">&#9674;link</span><span class="RktPn">[</span><span class="RktVal">"https://google.com/search?q=racket"</span><span class="RktPn">]</span><span class="RktPn">{</span><span class="RktSym">search</span><span class="hspace">&nbsp;</span><span class="RktSym">for</span><span class="hspace">&nbsp;</span><span class="RktSym">Racket</span><span class="RktPn">}</span><span class="RktPn">}</span><span class="RktPn">}</span></td></tr></table></blockquote></blockquote></blockquote><p>Go to the <a href="http://localhost:8080/index.ptree">project dashboard</a> and click on <a href="http://localhost:8080/uptown.html"><span class="stt">uptown.html</span></a>. You&rsquo;ll see something like this:</p><blockquote class="SCodeFlow"><p>Pollen markup You <span style="font-weight: bold">wanted</span> it &#8212; you <span style="font-style: italic">got</span> it. https://google.com/search?q=racketsearch for Racket</p></blockquote><p>That&rsquo;s not right. What happened?</p><p>We marked up the source using a combination of standard HTML tags (<span class="stt">strong</span>, <span class="stt">em</span>) and nonstandard ones (<span class="stt">headline</span>, <span class="stt">items</span>, <span class="stt">item</span>, <span class="stt">link</span>). This is valid Pollen markup. (In fact, if you look at <a href="http://localhost:8080/out/markup.html">the generated source</a>, you&rsquo;ll see that they didn&rsquo;t disappear.) But since we&rsquo;re targeting HTML, we need to convert our custom tags into valid HTML tags.</p><p>For that, we&rsquo;ll make a special file called <span class="stt">project-require.rkt</span>. This is a file in the standard Racket language that provides helper functions to decode the source. The definitions won&rsquo;t make sense yet. But this is the quick start, so all you need to do is copy, paste, and save:</p><blockquote class="SCodeFlow"><blockquote class="Rfilebox"><p class="Rfiletitle"><span class="Rfilename"><span class="stt">"project-require.rkt"</span></span></p><blockquote class="Rfilecontent"><table cellspacing="0" class="RktBlk"><tr><td><font class="badlink"><span class="RktModLink"><span class="RktMod">#lang</span></span></font><span class="hspace">&nbsp;</span><font class="badlink"><span class="RktModLink"><span class="RktSym">racket/base</span></span></font></td></tr><tr><td><span class="RktPn">(</span><span class="RktSym">require</span><span class="hspace">&nbsp;</span><span class="RktSym">pollen/tag</span><span class="RktPn">)</span></td></tr><tr><td><span class="RktPn">(</span><span class="RktSym">provide</span><span class="hspace">&nbsp;</span><span class="RktPn">(</span><span class="RktSym">all-defined-out</span><span class="RktPn">)</span><span class="RktPn">)</span></td></tr><tr><td><span class="RktPn">(</span><span class="RktSym">define</span><span class="hspace">&nbsp;</span><span class="RktSym">headline</span><span class="hspace">&nbsp;</span><span class="RktPn">(</span><span class="RktSym">make-tag-function</span><span class="hspace">&nbsp;</span><span class="RktVal">'</span><span class="RktVal">h2</span><span class="RktPn">)</span><span class="RktPn">)</span></td></tr><tr><td><span class="RktPn">(</span><span class="RktSym">define</span><span class="hspace">&nbsp;</span><span class="RktSym">items</span><span class="hspace">&nbsp;</span><span class="RktPn">(</span><span class="RktSym">make-tag-function</span><span class="hspace">&nbsp;</span><span class="RktVal">'</span><span class="RktVal">ul</span><span class="RktPn">)</span><span class="RktPn">)</span></td></tr><tr><td><span class="RktPn">(</span><span class="RktSym">define</span><span class="hspace">&nbsp;</span><span class="RktSym">item</span><span class="hspace">&nbsp;</span><span class="RktPn">(</span><span class="RktSym">make-tag-function</span><span class="hspace">&nbsp;</span><span class="RktVal">'</span><span class="RktVal">li</span><span class="hspace">&nbsp;</span><span class="RktVal">'</span><span class="RktVal">p</span><span class="RktPn">)</span><span class="RktPn">)</span></td></tr><tr><td><span class="RktPn">(</span><span class="RktSym">define</span><span class="hspace">&nbsp;</span><span class="RktPn">(</span><span class="RktSym">link</span><span class="hspace">&nbsp;</span><span class="RktSym">url</span><span class="hspace">&nbsp;</span><span class="RktSym">text</span><span class="RktPn">)</span><span class="hspace">&nbsp;</span><span class="RktVal">`</span><span class="RktVal">(</span><span class="RktVal">a</span><span class="hspace">&nbsp;</span><span class="RktVal">[</span><span class="RktVal">[</span><span class="RktVal">href</span><span class="hspace">&nbsp;</span><span class="RktRdr">,</span><span class="RktSym">url</span><span class="RktVal">]</span><span class="RktVal">]</span><span class="hspace">&nbsp;</span><span class="RktRdr">,</span><span class="RktSym">text</span><span class="RktVal">)</span><span class="RktPn">)</span></td></tr></table></blockquote></blockquote></blockquote><p>Return to the <a href="http://localhost:8080/index.ptree">project dashboard</a> and click on <a href="http://localhost:8080/uptown.html"><span class="stt">uptown.html</span></a>. Now you&rsquo;ll get the right result:</p><blockquote class="SCodeFlow"><p><span style="font-weight: bold"><span class="Larger">Pollen markup</span></span></p><blockquote><ul><li><p>You <span style="font-weight: bold">wanted</span> it &#8212;&#160;you <span style="font-style: italic">got</span> it.</p></li><li><p><a href="https://google.com/search?q=racket">search for Racket</a></p></li></ul></blockquote></blockquote><p>Markup mode takes a little more effort to set up. But it also allows you more flexibility. If you want to do semantic markup, or convert your source into multiple output formats, or handle complex page layouts &#8212;&#160;it&rsquo;s the way to go.</p><h4>3.9<tt>&nbsp;</tt><a name="(part._.Templates)"></a>Templates</h4><p>The final stop in the quick tour. The HTML pages we just made looked pretty dull. Let&rsquo;s fix that.</p><p>Pollen source files in Markdown or markup mode (i.e., <span class="stt">.pmd</span> or <span class="stt">.pm</span> files) are always rendered with a template. Pollen tries to find a template in the project directory whose name is <span class="stt">main.[output extension].pt</span>. So for <span class="stt">uptown.html.pm</span>, the output extension is <span class="stt">.html</span>, and Pollen will look for <span class="stt">main.html.pt</span> (the <span class="stt">.pt</span>, as you probably guessed, stands for Pollen template).</p><p>So let&rsquo;s make <span class="stt">main.html.pt</span>. Go back to DrRacket and enter this:</p><blockquote class="SCodeFlow"><blockquote class="Rfilebox"><p class="Rfiletitle"><span class="Rfilename"><span class="stt">"main.html.pt"</span></span></p><blockquote class="Rfilecontent"><table cellspacing="0" class="RktBlk"><tr><td><font class="badlink"><span class="RktModLink"><span class="RktMod">#lang</span></span></font><span class="hspace">&nbsp;</span><a href="Source_formats.html" class="RktModLink" data-pltdoc="x"><span class="RktSym">pollen</span></a></td></tr><tr><td><span class="RktSym">&lt;html&gt;&lt;head&gt;&lt;meta</span><span class="hspace">&nbsp;</span><span class="RktSym">charset=</span><span class="RktVal">"UTF-8"</span><span class="RktSym">/&gt;&lt;/head&gt;</span></td></tr><tr><td><span class="RktSym">&lt;body</span><span class="hspace">&nbsp;</span><span class="RktSym">style=</span><span class="RktVal">"margin: 30%; border:1px solid black; \nfont-size: 150%; font-family: sans-serif; background:yellow"</span></td></tr><tr><td><span class="hspace">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="RktSym">&gt;</span></td></tr><tr><td><span class="RktSym">&#9674;</span><span class="RktPn">(</span><span class="RktSym"><span class="nobreak">-&gt;</span>html</span><span class="hspace">&nbsp;</span><span class="RktSym">doc</span><span class="RktPn">)</span><span class="RktSym">&lt;/body&gt;</span></td></tr><tr><td><span class="RktSym">&lt;/html&gt;</span></td></tr></table></blockquote></blockquote></blockquote><p>OK, enough of the contrived examples. Let&rsquo;s make some magic.</p><div class="navsetbottom"><span class="navleft">&nbsp;&nbsp;</span><span class="navright">&nbsp;&nbsp;<a href="Acknowledgments.html" title="backward to &quot;2 Acknowledgments&quot;" data-pltdoc="x">&larr; prev</a>&nbsp;&nbsp;<a href="index.html" title="up to &quot;Pollen: the book is a program&quot;" data-pltdoc="x">up</a>&nbsp;&nbsp;<a href="Why_I_made_Pollen.html" title="forward to &quot;4 Why I made Pollen&quot;" data-pltdoc="x">next &rarr;</a></span>&nbsp;</div></div></div><div id="contextindicator">&nbsp;</div></body></html>