A marXup language to generate html files.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

421 lines
23 KiB

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel="stylesheet" href="css/styles.css"/>
</head>
<body>
<h1> Groucho </h1>
<p>
(A clunky tool I made to mar<span class="maths"><span class="regular"><span class="greek-letter">&Chi;</span></span></span> up blog posts)
</p>
<p>
<hr/>
</p>
<h2> Intro </h2>
<p>
Groucho is a janky little tool to generate an HTML file from a text containing simplified and non-intrusive markup codes.
</p>
<p>
It was written in order to ease writing blog posts for my website, aiming at the least friction.
</p>
<p>
As the content I'm writting on these posts is mainly technical, Groucho is built around three main goals&nbsp;:
</p>
<ul>
<li>Easy presentation of normal content, including sections titles, paragraphs, lists, images, links.</li>
<li>Formatting of basic maths or physics formulas, supporting maths symbols and notations.</li>
<li>Syntax highlighting of C source code.</li>
</ul>
<p>
Some examples :
</p>
<p>
<span class="code">*this is some text*</span> &emsp;&emsp;will print in bold : <b>this is some text</b>.<br>
<span class="code">/this is some text/</span> &emsp;&emsp;will print in italics : <i>this is some text</i>.<br>
<span class="code">_this is some text_</span> &emsp;&emsp;will print underlined : <u>this is some text</u>.
</p>
<p>
<span class="code">[m]x_+ = \frac{{-b+\sqrt{b^2-4ac}}{2a}}[/m]</span> &emsp;&emsp;will print this well-known maths formula&nbsp;:
</p>
<p>
<span class="maths">x<sub><span class="op">&thinsp;+&thinsp;</span></sub> <span class="op">&thinsp;=&thinsp;</span> <span class="fraction"><span class="numerator"><span class="op">&thinsp;&minus;&thinsp;</span>b<span class="op">&thinsp;+&thinsp;</span><span class="sqrt-symbol">&radic;</span><span class="sqrt-arg">&thinsp;b<sup>&thinsp;<span class="digit">2</span></sup><span class="op">&thinsp;&minus;&thinsp;</span><span class="digit">4</span>ac&nbsp;</span></span><span class="denominator"><span class="digit">2</span>a</span></span></span>
</p>
<br>
<h3> Download and Usage </h3>
<p>
The source code of Groucho is available under a Public domain or MIT license (choose whichever you prefer). The fonts provided for the example document are covered by their own licenses (X11 license for CMU and the GUST font license for latin-modern-maths), which you can find in the font directory.
</p>
<p>
You clone the repo from https://git.forkingpaths.dev/martinfouilleul/groucho.git. Please keep in mind though that this was written as a quick side-project for my personal use, and hence is far for production quality. It is made available online only in the interest of the reader's curiosity. It may or may not be updated in the future.
</p>
<p>
After cloning the repo, you should be able to compile in the source directory with&nbsp;:
</p>
<div class="code">cc -o groucho *.cpp</div>
<p>
To use it you can run :
</p>
<div class="code">./groucho in out</div>
<p>
Where in and out are your input and output files. If no files are specified groucho will get its input from the standard input and output to the standard output. You can test groucho by running it on the sample <span class="code">doc.groucho.txt</span> file. It should output the present quick documentation.
</p>
<p>
<hr/>
</p>
<h2> Basic principles </h2>
<p>
If not otherwise mentioned, a character in the input will produce the same character on the output. Some special characters and sequences will be interpreted as symbols and insert html entities in the html output. Some others will be interpreted as markups and insert html tags in the output stream. These tags contain <span class="code">class</span> attributes that are used by the browser, along with a css stylesheet, to style their content. A sample stylesheet is available with groucho, but it should be customizable at will (well, with css, you never know).
</p>
<p>
The symbols, the markups and their interpretations depends on the current mode of the interpreter which can be one of the four following modes&nbsp;:
</p>
<ul>
<li>Text mode which is the default and doesn't require a markup.</li>
<li>HTML mode, which is used to insert raw html.</li>
<li>Maths mode, which is used to present simple mathematical formulas.</li>
<li>Code mode, which is used to present syntax-highlighted C code.</li>
</ul>
<p>
By default, groucho will operate in text mode. You can switch to one of the other modes by using block tags, like <span class="code">[maths]...[/maths]</span>, and all text inserted between these two tags will be interpreted in this mode.
</p>
<br>
<p>
<hr/>
</p>
<h2> HTML mode </h2>
<p>
All text placed between the tags <span class="code">[html]</span> and <span class="code">[/html]</span> will be interpreted as HTML and will be outputed as-is.<br>
To produce the basic tags needed for an HTML document, you should insert the following block at the beginning of your input file&nbsp;:
</p>
<p>
<span class="code">
[html]
&lt;html&gt;
&lt;head&gt;
&lt;meta http-equiv="Content-Type" content="text/html; charset=utf-8" /&gt;
&lt;link rel="stylesheet" href="css/styles.css"/&gt;
&lt;/head&gt;
&lt;body&gt;
[/html]
</span>
</p>
<p>
Where you can also specify your CSS style sheet. To close those tags you should insert the following block at the end of your input file&nbsp;:
</p>
<p>
<span class="code">
[html]
&lt;/body&gt;
&lt;/html&gt;
[/html]
</span>
</p>
<br>
<p>
<hr/>
</p>
<h2> Markups common to Text and Maths mode </h2>
<p>
Text is the default mode, which is used when the source content is not contained between a pair of matching block markups.
</p>
<p>
Maths mode can be used by enclosing text between the markup tags <span class="code">[maths]</span> and <span class="code">[/maths]</span>, which will interpret its content as maths, and also place the output between <span class="code">&lt;div class="maths"&gt;&lt;/div&gt;</span> html tags. Alternatively, if one whishes to insert maths into the same line as normal text, the markup tags <span class="code">[m]</span> and <span class="code">[/m]</span> can be used, which will place the output between <span class="code">&lt;span class="maths"&gt;&lt;/span&gt;</span> html tags.
</p>
<p>
Text and maths mode share some of their markups, so we will present them together and then present their differences.
</p>
<h3> HTML entities </h3>
<p>
Some characters used by html tags will be replaced by html entities :
</p>
<ul>
<li><span class="code">&lt;</span> will be replaced by <span class="code">&amp;lt;</span> in maths mode, and in text mode if it is not followed by an alphabetic character, by a <span class="code">/</span> or by a <span class="code">!</span>. Otherwise, it will be interpreted as the beginning of an html tag and the following text until the next matching <span class="code">&gt;</span> will be output as-is.</li>
<li><span class="code">&gt;</span> will be replaced by <span class="code">&amp;gt;</span>.</li>
<li><span class="code">&amp;</span> will be replaced by <span class="code">&amp;amp;</span>.</li>
</ul>
<h3> Escape sequences </h3>
<p>
If a character is preceded by a <span class="code">\</span>, it will be interpreted as follows :
</p>
<ul>
<li>A <span class="code">\n</span> will be interpreted as an intentional line break and produce a <span class="code">&lt;br&gt;</span> tag.</li>
<li>An end of line preceded by <span class="code">\</span> will be interpreted as a source-only line break and will be suppressed in the output.</li>
<li>A <span class="code">\t</span> will output two em spaces <span class="code">&amp;emsp;&amp;emsp</span>.</li>
<li>A <span class="code">\s</span> will output a non breakable space <span class="code">&amp;nbsp;</span>.</li>
</ul>
<p>
In addition to that, there's a list of letters or words that will be interpreted as a special symbol or markup when preceded by <span class="code">\</span>&nbsp;:
</p>
<ul>
<li>Symbols and special letters. These symbols will likely be more useful in maths sections and not all fonts support them, but they're available in text mode as well&nbsp;:
<ul>
<li><span class="code">\star</span> will output a <span class="maths"><span class="op">&thinsp;&ast;&thinsp;</span></span></li>
<li><span class="code">\lowstar</span> will output a <span class="maths"><span class="op">&thinsp;&lowast;&thinsp;</span></span></li>
<li><span class="code">\div</span> will output a <span class="maths"><span class="op">&thinsp;&div;&thinsp;</span></span></li>
<li><span class="code">\minus</span> will output a <span class="maths"><span class="op">&thinsp;&minus;&thinsp;</span></span></li>
<li><span class="code">\cdot</span> will output a <span class="maths"><span class="op">&thinsp;&middot;&thinsp;</span></span></li>
<li><span class="code">\times</span> will output a <span class="maths"><span class="op">&thinsp;&times;&thinsp;</span></span></li>
<li><span class="code">\otimes</span> will output a <span class="maths"><span class="op">&thinsp;&otimes;&thinsp;</span></span></li>
<li><span class="code">\leq</span> will output a <span class="maths"><span class="rel">&leq;</span></span></li>
<li><span class="code">\geq</span> will output a <span class="maths"><span class="rel">&geq;</span></span></li>
<li><span class="code">\larr</span> will output a <span class="maths"><span class="delim">&larr;</span></span></li>
<li><span class="code">\rarr</span> will output a <span class="maths"><span class="delim">&rarr;</span></span></li>
<li><span class="code">\inf</span> will output the symbol <span class="maths"><span class="symbol">&infin;</span></span></li>
</ul>
</li>
</ul>
<ul>
<li>Greek letters:
<ul>
<li><span class="code">\alpha</span>, <span class="code">\beta</span>, <span class="code">\gamma</span>, <span class="code">\delta</span>, etc. : <span class="maths"><span class="regular"><span class="greek-letter">&alpha;</span> <span class="greek-letter">&beta;</span> <span class="greek-letter">&gamma;</span> <span class="greek-letter">&delta;</span></span></span></li>
<li><span class="code">\Alpha</span>, <span class="code">\Beta</span>, <span class="code">\Gamma</span>, <span class="code">\Delta</span>, etc. : <span class="maths"><span class="regular"><span class="greek-letter">&Alpha;</span> <span class="greek-letter">&Beta;</span> <span class="greek-letter">&Gamma;</span> <span class="greek-letter">&Delta;</span></span></span></li>
</ul>
</li>
</ul>
<ul>
<li>Font style markups, availables in both modes, change the style of text enclosed between a matching pair of them&nbsp;:
<ul>
<li><span class="code">\b</span> for <b>bold</b>.</li>
<li><span class="code">\i</span> for <i>italics</i>.</li>
<li><span class="code">\u</span> for <u>underlined</u></li>
<li><span class="code">\r</span> is used to render text in roman in a section that would otherwise be in italics. It can be useful for function in maths blocks, as in <span class="maths">a <span class="op">&thinsp;=&thinsp;</span> <span class="regular">log</span><span class="delim">(</span>b<span class="delim">)</span></span></li>
</ul>
</li>
</ul>
<ul>
<li>A single letter not matched by one of the above escape sequences will be output as is and not be interpreted as part of a markup.</li>
</ul>
<br>
<br>
<p>
<hr/>
</p>
<h2> Text mode </h2>
<h4> Additional markups </h4>
<p>
In text mode, a section title can be created by enclosing text in a matching pair of <span class="code">=</span> sequences. The section level corresponds to the number of <span class="code">=</span> signs&nbsp;:
</p>
<p>
<span class="code">===== Exemple title =====</span> in the input will produce the html <span class="code">&lt;h5&gt;Exemple title&lt;/h5&gt;</span>, resulting in&nbsp;:<br>
</p>
<h5> Exemple title </h5>
<p>
A separating line can be created by a sequence of five or more hyphens : <span class="code">-----</span> will output the html tag <span class="code">&lt;hr/&gt;</span> resulting in&nbsp;:<br>
<hr/>
</p>
<p>
Paragraphs can be created by simply having two line breaks in a row.
</p>
<p>
The use of \b, \i and \u markups can alternatively replaced by (respectively) *, /, and _.
</p>
<p>
The following character sequences can be used to create symbols&nbsp;:
</p>
<ul>
<li><span class="code">--</span> : two hyphens in a row will create a dash&nbsp;: &mdash;</li>
<li><span class="code">--&gt;</span> : one or more hyphens followed by a right angled bracket will produce a right arrow&nbsp;: &rarr;</li>
<li><span class="code">&lt;--</span> : a left angled bracket followed by one or more hyphens will produce a left arrow&nbsp;: &larr;</li>
<li><span class="code">"</span> : matching pairs of double quotes will be replaced by <span class="code">&amp;ldquo;</span> and <span class="code">&amp;rdquo;</span> as in &ldquo;quoted&rdquo;.</li>
</ul>
<h4> Lists </h4>
<p>
A number of tabs followed by an hyphen, followed by a space, will be interpreted as a list item. The number of leading tabs determine the list depth when creating nested lists&nbsp;:
</p>
<p>
<span class="code">- item 1
- item 2
- sub item 2.1
- sub item 2.2
- item 3
</span>
</p>
<p>
will produce the following list :
</p>
<ul>
<li>item 1</li>
<li>item 2
<ul>
<li>sub item 2.1</li>
<li>sub item 2.2</li>
</ul>
</li>
<li>item 3</li>
</ul>
<h3> External content </h3>
<h4> Links </h4>
<p>
he markup tags <span class="code">[url=][/url]</span> can be used to create a link. You specifiy the URL of your link by adding it after the <span class="code">=</span> sign. The text of the link goes between the two markup tags.<br>
For instance, <span class="code">[url=https://www.forkingpaths.garden]Home page[/url]</span> will create the following link : <a href="https://www.forkingpaths.garden">Home page</a>
that will land you on Forking Paths's website.
</p>
<br>
<h4> Images </h4>
<p>
n image can be created with the tag <span class="code">[img=]</span>. Similarly to the URL tag, you specify the URL to you image after the <span class="code">=</span> sign. Hence, the following tag <span class="code">[img=https://www.forkingpaths.garden/img/logo_black.png]</span> will display Forking Paths' logo&nbsp;:
</p>
<p>
<img src="https://www.forkingpaths.garden/img/logo_black.png">
</p>
<br>
<p>
<hr/>
</p>
<h2> Maths mode </h2>
<p>
By default, operators and capital letters will be printed in roman with a math-specific font, while lower-case letters will be printed in italics. You can always reverse that by using \r and \i markups.
</p>
<h3> Default symbol interpretation </h3>
<p>
In maths mode, the following (non-escaped) sequences are recognized as symbol equivalents&nbsp;:
</p>
<ul>
<li><span class="code">-</span> is equivalent to <span class="code">\minus</span></li>
<li><span class="code">*</span> is equivalent to <span class="code">\times</span></li>
<li><span class="code">/</span> is equivalent to <span class="code">\div</span></li>
<li><span class="code">&lt;=</span> is equivalent to <span class="code">\leq</span></li>
<li><span class="code">&gt;=</span> is equivalent to <span class="code">\geq</span></li>
</ul>
<h3> Exponents and indices </h3>
<p>
A character preceded by a <span class="code">_</span> will be printed as an index, as in <span class="maths">u<sub>k</sub></span>. You can use a pair of curled brackets after the underscode to have a longer index, ie. this code : <span class="code">C_{(i,j)}</span> will produce this result : <span class="maths"><span class="regular">C</span><sub><span class="delim">(</span>i<span class="delim">,</span>j<span class="delim">)</span></sub></span>.
</p>
<p>
Similarly, the character <span class="code">^</span> is used for exponent, as in <span class="maths"><span class="regular">e</span><sup>&#8239;<span class="regular">j<span class="delim">(</span><span class="digit">2</span><span class="greek-letter">&pi;</span></span>f&#8239;t<span class="op">&thinsp;+&thinsp;</span><span class="greek-letter">&phi;</span><span class="delim">)</span></sup></span>.
</p>
<h3> Vectors </h3>
<p>
<span class="code">\vec</span> : the character following this escape markup will be rendered with a vector arrow above it, as in <span class="maths">v<span class="vector">&#8407;</span></span>.
</p>
<h3> Square roots </h3>
<p>
<span class="code">\sqrt</span> : a single character, or a sequence of characters contained in curly brackets, following this escape markup, will be printed as the arguments of a square root.
</p>
<p>
<span class="maths"><span class="sqrt-symbol">&radic;</span><span class="sqrt-arg">&thinsp;a<span class="op">&thinsp;&times;&thinsp;</span><span class="delim">(</span><span class="digit">1</span><span class="op">&thinsp;&minus;&thinsp;</span>n<span class="delim">)</span><sup>&thinsp;<span class="digit">2</span></sup>&nbsp;</span></span>
</p>
<p>
More precisely, this markup outputs the following HTML&nbsp;:
</p>
<p>
<span class="code">&lt;span class="sqrt-symbol"&gt;&amp;radic;&lt;/span&gt;
&lt;span class="sqrt-arg"&gt;&amp;thinsp;YourExpressionHere;&amp;nbsp;&lt;/span&gt;</span>
</p>
<p>
The square root character is rendered as an HTML entity and the trailing top line of the square root is simulated by the top border of the <span class="code">sqrt-arg</span> span. Because HTML/CSS layout is such a disaster zone, it is extremely likely that the square root and the top bar won't join and you will have to adjust the stylesheet to make it happen. There's not much we can do about it, until MathML is largely supported, except resorting to TeX generated images and the like...
</p>
<h3> Fraction </h3>
<p>
You can use the markup <span class="code">\frac{{numerator}{denominator}}</span> to generate a fraction. For instance,
</p>
<p>
<span class="code">y = \frac{{x^2+1}{2}}</span>
</p>
<p>
will generate the following equation :
</p>
<p>
<span class="maths">y <span class="op">&thinsp;=&thinsp;</span> <span class="fraction"><span class="numerator">x<sup>&thinsp;<span class="digit">2</span></sup><span class="op">&thinsp;+&thinsp;</span><span class="digit">1</span></span><span class="denominator"><span class="digit">2</span></span></span></span>
</p>
<p>
As stated above, depending on your font, you might want to adjust the <span class="code">text-alignment</span> property of the <span class="code">fraction</span> class to align the fraction line with the equal sign.
</p>
<h3> Sums and Products </h3>
<p>
<span class="n-sum-product"></span>&thinsp;and <span class="n-sum-product"></span>&thinsp;ct can be used to create indexed sums or products. You need to specify the maximum index and the minimum index enclosed in curled brackets, like this&nbsp;:
</p>
<p>
<span class="code">x = \nsum{{p}{k=1}}u_k</span>
</p>
<p>
which produces this result :
</p>
<p>
<span class="maths">x <span class="op">&thinsp;=&thinsp;</span> <span class="n-sum-product"><span class="range-max">p</span><span class="n-op-symbol">&sum;</span><span class="range-min">k<span class="op">&thinsp;=&thinsp;</span><span class="digit">1</span></span></span>&thinsp;u<sub>k</sub></span>
</p>
<p>
<hr/>
</p>
<h2> Code mode </h2>
<p>
All text placed between the tags <span class="code">[html]</span> and <span class="code">[/html]</span> will be interpreted as C code, syntax highlighted and placed between the html tags <span class="code">&lt;div class="code"&gt;&lt;/div&gt;</span>. Alternatively, if one whishes to insert code into the same line as normal text, the markup tags <span class="code">[c]</span> and <span class="code">[/c]</span> can be used, which will place the output between the html tags <span class="code">&lt;span class="code"&gt;&lt;/span&gt;</span>. Inline code won't be syntax highlighted though.
</p>
<p>
You can specify that you don't want the syntax highlighter to be run on a code block by using the opening <span class="code">[code=none]</span> mark instead of <span class="code">[code]</span>
</p>
<p>
The CSS styling of code section uses <span class="code">white-space: pre;</span> to preserve indentation and line breaks. It also uses a monospace font by default. The syntax highlighter performs a simplified syntactical analysis of the code and encloses elements in <span class="code">&lt;span&gt;&lt;/span&gt;</span> tags, with one of the following classes&nbsp;:
</p>
<ul>
<li><span class="code">keyword</span> is used for C keywords, except for built-in types.</li>
<li><span class="code">type</span> is used for built-in or user-defined types.</li>
<li><span class="code">function-name</span> is used to highlight functions in declarations and expressions.</li>
<li><span class="code">comment</span> is used to highlight comments.</li>
<li><span class="code">label</span> is used for labels and <span class="code">goto</span> instructions.</li>
</ul>
<p>
The syntax highlighter may fail on some cases, either because the syntax is in fact incorrect or because the code uses some unrecognized dialect, or because the parser stumbles upon a macro, or simply because it lacks context. As a matter of fact, C not being completely context free, and to avoid complicating the parser for such a simple task, the parser sometimes falls back to some (hopefully) reasonable assumption on what's going on. If it was a wrong guess, it will try to recover and continue to output non highlighted code until it can resynchronize, often after the next semicolon or at the begining of the next compound statement.
</p>
<p>
Here is an exemple of a code snippet :
</p>
<div class="code"><span class="keyword">for</span>(<span class="type">int </span>i=<span class="constant">0</span>;<span class="expression-statement">i&lt;count;</span>i++)
{
<span class="comment">// Get all my items by name in the hash table and print their value
</span>
<span class="type">int </span>hash = <span class="function-name">HashFunction</span>(<span class="arguments">names[i]</span>);
<span class="type">my_struct</span>* a = <span class="function-name">GetItem</span>(<span class="arguments">hash</span>);
<span class="expression-statement"><span class="function-name">printf</span>(<span class="arguments"><span class="string">"item %s = %i %s\n"</span>, <span class="arguments">names[i], <span class="arguments">a-&gt;value, <span class="arguments">a-&gt;unit</span></span></span></span>);
</span>}
</div>
<br>
<p>
<hr/>
</p>
<h2> Planned features </h2>
<p>
This is a list of features that will eventually be added along the way, as I need them and/or find time to implement them&nbsp;:
</p>
<ul>
<li>Output some useful info to stderr on errors.</li>
<li>Clean-up the scanner code and reduce redundancies between modes.</li>
<li>Add markups to control over font size, text justification, etc.</li>
<li>Automatic non breakable space before colons.</li>
<li>Tables</li>
<li>Big parentheses for enclosing fractions</li>
<li>Exponents that should work with those big parentheses</li>
<li>More maths and logic symbols</li>
<li>TeX mode for more involved equations. It would allow to insert TeX code that would generate an image and a corresponding <span class="code">&lt;img&gt;</span> tag.</li>
<li>Add cast-expressions to the C parser, recognize <i>some</i> C++ features, etc...</li>
<li>Other langages for syntax highlighting : eg. bash scripts, Objective-C ?</li>
</ul>
<br>
<br>
<br>
</body>
</html>