rewrite of MARKMIN, thanks Vladyslav
This commit is contained in:
@@ -1 +1 @@
|
|||||||
Version 2.00.0 (2012-07-14 22:45:25) dev
|
Version 2.00.0 (2012-07-14 23:05:38) dev
|
||||||
|
|||||||
@@ -1,52 +1,82 @@
|
|||||||
<html><body><h1>Markmin markup language</h1><h2>About</h2><p>This is a new markup language that we call markmin designed to produce high quality scientific papers and books and also put them online. We provide serializers for html, latex and pdf. It is implemented in the <code class="">markmin2html</code> function in the <code class="">markmin2html.py</code>.</p><p>Example of usage:</p><pre><code class="">m = "Hello **world** [[link http://web2py.com]]"
|
<html><body>
|
||||||
|
<style>
|
||||||
|
blockquote { background-color: lime; }
|
||||||
|
thead { color: white; background-color: gray; text-align: center; }
|
||||||
|
tfoot { color: white; background-color: gray; }
|
||||||
|
|
||||||
|
.tableclass1 { background-color: yellow; }
|
||||||
|
.tableclass1 thead { color: yellow; background-color: green; }
|
||||||
|
.tableclass1 tfoot { color: yellow; background-color: green; }
|
||||||
|
|
||||||
|
td.num { text-align: right; }
|
||||||
|
pre { background-color: #E0E0E0; }
|
||||||
|
</style>
|
||||||
|
<h1>Markmin markup language</h1><h2>About</h2><p>This is a new markup language that we call markmin designed to produce high quality scientific papers and books and also put them online. We provide serializers for html, latex and pdf. It is implemented in the <code>markmin2html</code> function in the <code>markmin2html.py</code>.</p><p>Example of usage:</p><pre><code>m = "Hello **world** [[link http://web2py.com]]"
|
||||||
from markmin2html import markmin2html
|
from markmin2html import markmin2html
|
||||||
print markmin2html(m)
|
print markmin2html(m)
|
||||||
from markmin2latex import markmin2latex
|
from markmin2latex import markmin2latex
|
||||||
print markmin2latex(m)
|
print markmin2latex(m)
|
||||||
from markmin2pdf import markmin2pdf # requires pdflatex
|
from markmin2pdf import markmin2pdf # requires pdflatex
|
||||||
print markmin2pdf(m)</code></pre><h2>Why?</h2><p>We wanted a markup language with the following requirements:</p><ul><li>less than 200 lines of functional code</li><li>easy to read</li><li>secure</li><li>support table, ul, ol, code</li><li>support html5 video and audio elements (html serialization only)</li><li>can align images and resize them</li><li>can specify class for tables and code elements</li><li>can add anchors</li><li>does not use _ for markup (since it creates odd behavior)</li><li>automatically links urls</li><li>fast</li><li>easy to extend</li><li>supports latex and pdf including references</li><li>allows to describe the markup in the markup (this document is generated from markmin syntax)</li></ul><p>(results depend on text but in average for text ~100K markmin is 30% faster than markdown, for text ~10K it is 10x faster)</p><p>The <a href="http://www.lulu.com/product/paperback/web2py-%283rd-edition%29/12822827">web2py book</a> published by lulu, for example, was entirely generated with markmin2pdf from the online <a href="http://www.web2py.com/book">web2py wiki</a></p><h2>Download</h2><ul><li><a href="http://web2py.googlecode.com/hg/gluon/contrib/markmin/markmin2html.py">http://web2py.googlecode.com/hg/gluon/contrib/markmin/markmin2html.py</a></li><li><a href="http://web2py.googlecode.com/hg/gluon/contrib/markmin/markmin2latex.py">http://web2py.googlecode.com/hg/gluon/contrib/markmin/markmin2latex.py</a></li><li><a href="http://web2py.googlecode.com/hg/gluon/contrib/markmin/markmin2pdf.py">http://web2py.googlecode.com/hg/gluon/contrib/markmin/markmin2pdf.py</a></li></ul><p>markmin2html.py and markmin2latex.py are single files and have no web2py dependence. Their license is BSD.</p><h2>Examples</h2><h3>Bold, italic, code and links</h3><table class=""><tr><td><strong>SOURCE</strong> </td><td><strong>OUTPUT</strong></td></tr><tr><td><code class=""># title</code> </td><td><strong>title</strong></td></tr><tr><td><code class="">## section</code> </td><td><strong>section</strong></td></tr><tr><td><code class="">### subsection</code> </td><td><strong>subsection</strong></td></tr><tr><td><code class="">**bold**</code> </td><td><strong>bold</strong></td></tr><tr><td><code class="">''italic''</code> </td><td><em>italic</em></td></tr><tr><td><code class="">~~strikeout~~</code> </td><td><del>strikeout</del></td></tr><tr><td><code class="">``verbatim``</code> </td><td><code class="">verbatim</code></td></tr><tr><td><code class="">``color with **bold**``:red</code> </td><td><span style="color: red">color with <strong>bold</strong></span></td></tr><tr><td><code class="">``many colors``:color[blue:#ffff00]</code> </td><td><span style="color: blue;background-color: #ffff00;">many colors</span></td></tr><tr><td><code class="">http://google.com</code> </td><td><a href="http://google.com">http://google.com</a></td></tr><tr><td><code class="">[[**click** me #myanchor]]</code> </td><td><a href="#myanchor"><strong>click</strong> me</a></td></tr><tr><td><code class="">[[click me [extra info] #myanchor popup]]</code></td><td><a href="#myanchor" title="extra info" target="_blank">click me</a></td></tr></table><h3>More on links</h3><p>The format is always <code class="">[[title link]]</code> or <code class="">[[title [extra] link]]</code>. Notice you can nest bold, italic, strikeout and code inside the link <code class="">title</code>.</p><h3>Anchors <span id="myanchor"></span></h3><p>You can place an anchor anywhere in the text using the syntax <code class="">[[name]]</code> where <em>name</em> is the name of the anchor.
|
print markmin2pdf(m)</code></pre> ====================<h1>This is a test block with new features:</h1><p>This is a blockquote with a list with tables in it:</p><blockquote class="blockquoteclass" id="blockquoteid">This is a paragraph before list. You can continue paragraph on the next lines.<br />This is an ordered list with tables:<ol><li>Item 1</li><li>Item 2</li><li><table class="tableclass1" id="tableid1"><tbody><tr><td>aa</td><td>bb</td><td>cc</td></tr><tr><td class="num">11</td><td class="num">22</td><td class="num">33</td></tr></tbody></table></li><li>Item 4 <table class="tableclass1"><thead><tr><td>T1</td><td>T2</td><td>t3</td></tr></thead><tbody><tr><td>aaa</td><td>bbb</td><td>ccc</td></tr><tr><td>ddd</td><td>fff</td><td>ggg</td></tr><tr><td class="num">123</td><td class="num">0</td><td class="num">5.0</td></tr></tbody></table></li></ol></blockquote><p>This this a new paragraph with a table. Table has header and footer:</p><table class="tableclass1" id="tableid2"><thead><tr><td><strong>Title 1</strong></td><td><strong>Title 2</strong></td><td><strong>Title 3</strong></td></tr></thead><tbody><tr><td>data 1</td><td>data 2</td><td class="num">2.00</td></tr><tr><td>data 4</td><td>data5(long)</td><td class="num">23.00</td></tr><tr><td></td><td>data 8</td><td class="num">33.50</td></tr></tbody><tfoot><tr><td>Total:</td><td>3 items</td><td class="num">58.50</td></tr></tfoot></table><h2>Multilevel lists</h2><p>Now lists can be multilevel:</p><ol><li>Ordered item 1 on level 1. You can continue item text on next strings<ol><li><p>Ordered item 1 of sublevel 2 with a paragraph (paragraph can start with point after plus or minus characters, e.g. <strong>++.</strong> or <strong>--.</strong>)</p><li><p>This is another item. But with 3 paragraphs, blockquote and sublists:</p><p>This is the second paragraph in the item. You can add paragraphs to an item, using point notation, where first characters in the string are sequence of points with space between them and another string. For example, this paragraph (in sublevel 2) starts with two points:</p><code>.. This is the second paragraph...</code><blockquote><h3>this is a blockquote in a list</h3>You can use blockquote with headers, paragraphs, tables and lists in it:<br />Tables can have or have not header and footer. This table is defined without any header and footer in it:<table><tbody><tr><td>red</td><td>fox</td><td class="num">0</td></tr><tr><td>blue</td><td>dolphin</td><td class="num">1000</td></tr><tr><td>green</td><td>leaf</td><td class="num">10000</td></tr></tbody></table></blockquote><p>This is yet another paragraph in the item.</p><ul><li>This is an item of unordered list <strong>(sublevel 3)</strong></li><li>This is the second item of the unordered list <em>(sublevel 3)</em><ol><ol><ol><li>This is a single item of ordered list in sublevel 6</li></ol></ol><p>and this is a paragraph in sublevel 4</p></ol></li><li><p>This is a new item with paragraph in sublevel 3.</p><ol><li>Start ordered list in sublevel 4 with code block: <pre><code>line 1
|
||||||
You can then link the anchor with <a href="#myanchor">link</a>, i.e. <code class="">[[link #myanchor]]</code> or <a href="#myanchor" title="extra info">link with an extra info</a>, i.e.
|
line 2
|
||||||
<code class="">[[link with an extra info [extra info] #myanchor]]</code>.</p><h3>Images</h3><p><img src="http://www.web2py.com/examples/static/web2py_logo.png" alt="alt-string for the image" title="the image title" style="float:right" width="200px" />
|
line 3</code></pre></li><li><p>Yet another item with code block:</p><pre><code> line 1
|
||||||
This paragraph has an image aligned to the right with a width of 200px. Its is placed using the code</p><p><code class="">[[alt-string for the image [the image title] http://www.web2py.com/examples/static/web2py_logo.png right 200px]]</code>.</p><h3>Unordered Lists</h3><pre><code class="">- Dog
|
line 2
|
||||||
|
line 3</code></pre> This item finishes with this paragraph.</li></ol><p>Item in sublevel 3 can be continued with paragraphs.</p><pre><code> this is another
|
||||||
|
code block
|
||||||
|
in the
|
||||||
|
sublevel 3 item</code></pre></li></ul><ol><li>The last item in sublevel 3</li></ol><p>This is a continuous paragraph for item 2 in sublevel 2. You can use such structure to create difficult structured documents.</p><li>item 3 in sublevel 2</li></li></li></ol><ul><li>item 1 in sublevel 2 (new unordered list)</li><li>item 2 in sublevel 2</li><li>item 3 in sublevel 2</li></ul><ol><li>item 1 in sublevel 2 (new ordered list)</li><li>item 2 in sublevel 2</li><li>item 3 in sublevle 2</li></ol></li><li>item 2 in level 1</li><li>item 3 in level 1</li></ol><ul><li>new unordered list (item 1 in level 1)</li><li>level 2 in level 1</li><li>level 3 in level 1</li><li>level 4 in level 1</li></ul><h2>This is the last section of the test</h2><p>Single paragraph with '----' in it will be turned into separator:</p><hr /><p>And this is the last paragraph in the test. Be happy!</p><p>====================</p><h2>Why?</h2><p>We wanted a markup language with the following requirements:</p><ul><li>less than 300 lines of functional code</li><li>easy to read</li><li>secure</li><li>support table, ul, ol, code</li><li>support html5 video and audio elements (html serialization only)</li><li>can align images and resize them</li><li>can specify class for tables and code elements</li><li>can add anchors</li><li>does not use _ for markup (since it creates odd behavior)</li><li>automatically links urls</li><li>fast</li><li>easy to extend</li><li>supports latex and pdf including references</li><li>allows to describe the markup in the markup (this document is generated from markmin syntax)</li></ul><p>(results depend on text but in average for text ~100K markmin is 30% faster than markdown, for text ~10K it is 10x faster)</p><p>The <a href="http://www.lulu.com/product/paperback/web2py-%283rd-edition%29/12822827">web2py book</a> published by lulu, for example, was entirely generated with markmin2pdf from the online <a href="http://www.web2py.com/book">web2py wiki</a></p><h2>Download</h2><ul><li><a href="http://web2py.googlecode.com/hg/gluon/contrib/markmin/markmin2html.py">http://web2py.googlecode.com/hg/gluon/contrib/markmin/markmin2html.py</a></li><li><a href="http://web2py.googlecode.com/hg/gluon/contrib/markmin/markmin2latex.py">http://web2py.googlecode.com/hg/gluon/contrib/markmin/markmin2latex.py</a></li><li><a href="http://web2py.googlecode.com/hg/gluon/contrib/markmin/markmin2pdf.py">http://web2py.googlecode.com/hg/gluon/contrib/markmin/markmin2pdf.py</a></li></ul><p>markmin2html.py and markmin2latex.py are single files and have no web2py dependence. Their license is BSD.</p><h2>Examples</h2><h3>Bold, italic, code and links</h3><table><thead><tr><td><strong>SOURCE</strong></td><td><strong>OUTPUT</strong></td></tr></thead><tbody><tr><td><code># title</code></td><td><strong>title</strong></td></tr><tr><td><code>## section</code></td><td><strong>section</strong></td></tr><tr><td><code>### subsection</code></td><td><strong>subsection</strong></td></tr><tr><td><code>**bold**</code></td><td><strong>bold</strong></td></tr><tr><td><code>''italic''</code></td><td><em>italic</em></td></tr><tr><td><code>~~strikeout~~</code></td><td><del>strikeout</del></td></tr><tr><td><code>``verbatim``</code></td><td><code>verbatim</code></td></tr><tr><td><code>``color with **bold**``:red</code></td><td><span style="color: red">color with <strong>bold</strong></span></td></tr><tr><td><code>``many colors``:color[blue:#ffff00]</code></td><td><span style="color: blue;background-color: #ffff00;">many colors</span></td></tr><tr><td><code>http://google.com</code></td><td><a href="http://google.com">http://google.com</a></td></tr><tr><td><code>[[**click** me #myanchor]]</code></td><td><a href="#myanchor"><strong>click</strong> me</a></td></tr><tr><td><code>[[click me [extra info] #myanchor popup]]</code></td><td><a href="#myanchor" title="extra info" target="_blank">click me</a></td></tr></tbody></table><h3>More on links</h3><p>The format is always <code>[[title link]]</code> or <code>[[title [extra] link]]</code>. Notice you can nest bold, italic, strikeout and code inside the link <code>title</code>.</p><h3>Anchors <span id="myanchor"></span></h3><p>You can place an anchor anywhere in the text using the syntax <code>[[name]]</code> where <em>name</em> is the name of the anchor. You can then link the anchor with <a href="#myanchor">link</a>, i.e. <code>[[link #myanchor]]</code> or <a href="#myanchor" title="extra info">link with an extra info</a>, i.e. <code>[[link with an extra info [extra info] #myanchor]]</code>.</p><h3>Images</h3><p><img src="http://www.web2py.com/examples/static/web2py_logo.png" alt="alt-string for the image" title="the image title" style="float:right" width="200px" /> This paragraph has an image aligned to the right with a width of 200px. Its is placed using the code</p><p><code>[[alt-string for the image [the image title] http://www.web2py.com/examples/static/web2py_logo.png right 200px]]</code>.</p><h3>Unordered Lists</h3><pre><code>- Dog
|
||||||
- Cat
|
- Cat
|
||||||
- Mouse</code></pre><p>is rendered as</p><ul><li>Dog</li><li>Cat</li><li>Mouse</li></ul><p>Two new lines between items break the list in two lists.</p><h3>Ordered Lists</h3><pre><code class="">+ Dog
|
- Mouse</code></pre><p>is rendered as</p><ul><li>Dog</li><li>Cat</li><li>Mouse</li></ul><p>Two new lines between items break the list in two lists.</p><h3>Ordered Lists</h3><pre><code>+ Dog
|
||||||
+ Cat
|
+ Cat
|
||||||
+ Mouse</code></pre><p>is rendered as</p><ol><li>Dog</li><li>Cat</li><li>Mouse</li></ol><h3>Tables</h3><p>Something like this
|
+ Mouse</code></pre><p>is rendered as</p><ol><li>Dog</li><li>Cat</li><li>Mouse</li></ol><h3>Multilevel Lists</h3><pre><code>+ Dogs
|
||||||
<pre><code class="">---------
|
-- red
|
||||||
|
-- brown
|
||||||
|
-- black
|
||||||
|
+ Cats
|
||||||
|
-- fluffy
|
||||||
|
-- smooth
|
||||||
|
-- bald
|
||||||
|
+ Mice
|
||||||
|
-- small
|
||||||
|
-- big
|
||||||
|
-- huge</code></pre><p>is rendered as</p><ol><li>Dogs<ul><li>red</li><li>brown</li><li>black</li></ul></li><li>Cats<ul><li>fluffy</li><li>smooth</li><li>bald</li></ul></li><li>Mice<ul><li>small</li><li>big</li><li>huge</li></ul></li></ol><h3>Tables (with optional header and/or footer)</h3><p>Something like this</p><pre><code>-----------------
|
||||||
**A**|**B**|**C**
|
**A**|**B**|**C**
|
||||||
|
=================
|
||||||
0 | 0 | X
|
0 | 0 | X
|
||||||
0 | X | 0
|
0 | X | 0
|
||||||
X | 0 | 0
|
X | 0 | 0
|
||||||
-----:abc</code></pre>
|
=================
|
||||||
is a table and is rendered as
|
**D**|**F**|**G**
|
||||||
<table class="abc"><tr><td><strong>A</strong></td><td><strong>B</strong></td><td><strong>C</strong></td></tr><tr><td>0</td><td>0</td><td>X</td></tr><tr><td>0</td><td>X</td><td>0</td></tr><tr><td>X</td><td>0</td><td>0</td></tr></table>
|
-----------------:abc[id]</code></pre> is a table and is rendered as<table class="abc" id="id"><thead><tr><td><strong>A</strong></td><td><strong>B</strong></td><td><strong>C</strong></td></tr></thead><tbody><tr><td class="num">0</td><td class="num">0</td><td>X</td></tr><tr><td class="num">0</td><td>X</td><td class="num">0</td></tr><tr><td>X</td><td class="num">0</td><td class="num">0</td></tr></tbody><tfoot><tr><td><strong>D</strong></td><td><strong>F</strong></td><td><strong>G</strong></td></tr></tfoot></table> Four or more dashes delimit the table and | separates the columns. The <code>:abc</code>, <code>:id[abc_1]</code> or <code>:abc[abc_1]</code> at the end sets the class and/or id for the table and it is optional.<h3>Blockquote</h3><p>A table with a single cell is rendered as a blockquote:</p><blockquote>Hello world</blockquote><p>Blockquote can contain headers, paragraphs, lists and tables:</p><pre><code>-----
|
||||||
Four or more dashes delimit the table and | separates the columns.
|
This is a paragraph in a blockquote
|
||||||
The <code class="">:abc</code> at the end sets the class for the table and it is optional.</p><h3>Blockquote</h3><p>A table with a single cell is rendered as a blockquote:</p><blockquote class="">Hello world</blockquote><h3>Code, <code class=""><code></code>, escaping and extra stuff</h3><pre><code class="python">def test():
|
|
||||||
return "this is Python code"</code></pre><p>Optionally a ` inside a <code class="">``...``</code> block can be inserted escaped with !`!.</p><p><strong>NOTE:</strong> You can escape markmin constructions ('',``,**,~~,[,{,]},$,@) with '\' character:
|
+ item 1
|
||||||
so \`\` can replace !`!`! escape string</p><p>The <code class="">:python</code> after the markup is also optional. If present, by default, it is used to set the class of the <code> block.
|
+ item 2
|
||||||
The behavior can be overridden by passing an argument <code class="">extra</code> to the <code class="">render</code> function. For example:</p><pre><code class="python">markmin2html("``aaa``:custom",
|
-- item 2.1
|
||||||
extra=dict(custom=lambda text: 'x'+text+'x'))</code></pre><p>generates</p><code class="python">'xaaax'</code><p>(the <code class="">``...``:custom</code> block is rendered by the <code class="">custom=lambda</code> function passed to <code class="">render</code>).</p><h3>Html5 support</h3><p>Markmin also supports the <video> and <audio> html5 tags using the notation:
|
-- item 2.2
|
||||||
<pre><code class="">[[message link video]]
|
+ item 3
|
||||||
|
|
||||||
|
---------
|
||||||
|
0 | 0 | X
|
||||||
|
0 | X | 0
|
||||||
|
X | 0 | 0
|
||||||
|
---------:tableclass1
|
||||||
|
-----</code></pre><p>is rendered as:</p><blockquote>This is a paragraph in a blockquote<ol><li>item 1</li><li>item 2<ul><li>item 2.1</li><li>item 2.2</li></ul></li><li>item 3</li></ol><table class="tableclass1"><tbody><tr><td class="num">0</td><td class="num">0</td><td>X</td></tr><tr><td class="num">0</td><td>X</td><td class="num">0</td></tr><tr><td>X</td><td class="num">0</td><td class="num">0</td></tr></tbody></table></blockquote><h3>Code, <code><code></code>, escaping and extra stuff</h3><pre><code class="python">def test():
|
||||||
|
return "this is Python code"</code></pre><p>Optionally a ` inside a <code>``...``</code> block can be inserted escaped with !`!.</p><p><strong>NOTE:</strong> You can escape markmin constructions ('',``,**,~~,[,{,]},$,@) with '\' character: so \`\` can replace !`!`! escape string</p><p>The <code>:python</code> after the markup is also optional. If present, by default, it is used to set the class of the <code> block. The behavior can be overridden by passing an argument <code>extra</code> to the <code>render</code> function. For example:</p><pre><code class="python">markmin2html("``aaa``:custom",
|
||||||
|
extra=dict(custom=lambda text: 'x'+text+'x'))</code></pre><p>generates</p><code class="python">'xaaax'</code><p>(the <code>``...``:custom</code> block is rendered by the <code>custom=lambda</code> function passed to <code>render</code>).</p><h3>Html5 support</h3><p>Markmin also supports the <video> and <audio> html5 tags using the notation:</p><pre><code>[[message link video]]
|
||||||
[[message link audio]]
|
[[message link audio]]
|
||||||
|
|
||||||
[[message [title] link video]]
|
[[message [title] link video]]
|
||||||
[[message [title] link audio]]</code></pre>
|
[[message [title] link audio]]</code></pre> where <code>message</code> will be shown in brousers without HTML5 video/audio tags support.<h3>Latex and other extensions</h3><p>Formulas can be embedded into HTML with <em>$$<code>formula</code>$$</em>. You can use Google charts to render the formula:</p><pre><code>LATEX = '<img src="http://chart.apis.google.com/chart?cht=tx&chl=%s" />'
|
||||||
where <code class="">message</code> will be shown in brousers without HTML5 video/audio tags support.</p><h3>Latex and other extensions</h3><p>Formulas can be embedded into HTML with <em>$$<code class="">formula</code>$$</em>.
|
markmin2html(text,{'latex':lambda code: LATEX % code.replace('"','\"')})</code></pre><h3>Code with syntax highlighting</h3><p>This requires a syntax highlighting tool, such as the web2py CODE helper.</p><pre><code>extra={'code_cpp':lambda text: CODE(text,language='cpp').xml(),
|
||||||
You can use Google charts to render the formula:</p><pre><code class="">LATEX = '<img src="http://chart.apis.google.com/chart?cht=tx&chl=%s" />'
|
|
||||||
markmin2html(text,{'latex':lambda code: LATEX % code.replace('"','\"')})</code></pre><h3>Code with syntax highlighting</h3><p>This requires a syntax highlighting tool, such as the web2py CODE helper.</p><p><pre><code class="">extra={'code_cpp':lambda text: CODE(text,language='cpp').xml(),
|
|
||||||
'code_java':lambda text: CODE(text,language='java').xml(),
|
'code_java':lambda text: CODE(text,language='java').xml(),
|
||||||
'code_python':lambda text: CODE(text,language='python').xml(),
|
'code_python':lambda text: CODE(text,language='python').xml(),
|
||||||
'code_html':lambda text: CODE(text,language='html').xml()}</code></pre>
|
'code_html':lambda text: CODE(text,language='html').xml()}</code></pre> or simple:<pre><code>extra={'code':lambda text,lang='python': CODE(text,language=lang).xml()}</code></pre><pre><code>markmin2html(text,extra=extra)</code></pre><p>Code can now be marked up as in this example:</p><pre><code>``
|
||||||
or simple:
|
|
||||||
<pre><code class="">extra={'code':lambda text,lang='': CODE(text,language=lang).xml()}</code></pre>
|
|
||||||
<pre><code class="">markmin2html(text,extra=extra)</code></pre></p><p>Code can now be marked up as in this example:
|
|
||||||
<pre><code class="">``
|
|
||||||
<html><body>example</body></html>
|
<html><body>example</body></html>
|
||||||
``:code_html</code></pre>
|
``:code_html</code></pre> OR<pre><code>``
|
||||||
OR
|
|
||||||
<pre><code class="">``
|
|
||||||
<html><body>example</body></html>
|
<html><body>example</body></html>
|
||||||
``:code[html]</code></pre></p><h3>Citations and References</h3><p>Citations are treated as internal links in html and proper citations in latex if there is a final section called "References". Items like</p><pre><code class="">- [[key]] value</code></pre><p>in the References will be translated into Latex</p><pre><code class="">\bibitem{key} value</code></pre><p>Here is an example of usage:</p><pre><code class="">As shown in Ref.``mdipierro``:cite
|
``:code[html]</code></pre><h3>Citations and References</h3><p>Citations are treated as internal links in html and proper citations in latex if there is a final section called "References". Items like</p><pre><code>- [[key]] value</code></pre><p>in the References will be translated into Latex</p><pre><code>\bibitem{key} value</code></pre><p>Here is an example of usage:</p><pre><code>As shown in Ref.``mdipierro``:cite
|
||||||
|
|
||||||
## References
|
## References
|
||||||
- [[mdipierro]] web2py Manual, 3rd Edition, lulu.com</code></pre><h3>Caveats</h3><p><code class=""><ul/></code>, <code class=""><ol/></code>, <code class=""><code/></code>, <code class=""><table/></code>, <code class=""><blockquote/></code>, <code class=""><h1/></code>, ..., <code class=""><h6/></code> do not have <code class=""><p>...</p></code> around them.</p></body></html>
|
|
||||||
|
- [[mdipierro]] web2py Manual, 3rd Edition, lulu.com</code></pre><h3>Caveats</h3><p><code><ul/></code>, <code><ol/></code>, <code><code/></code>, <code><table/></code>, <code><blockquote/></code>, <code><h1/></code>, ..., <code><h6/></code> do not have <code><p>...</p></code> around them.</p></body></html>
|
||||||
|
|||||||
@@ -1,9 +1,11 @@
|
|||||||
#!/usr/bin/env python
|
#!/usr/bin/env python
|
||||||
|
# -*- coding: utf-8 -*-
|
||||||
# created by Massimo Di Pierro
|
# created by Massimo Di Pierro
|
||||||
# improved by Vladyslav Kozlovskyy
|
# recreated by Vladyslav Kozlovskyy
|
||||||
# license MIT/BSD/GPL
|
# license MIT/BSD/GPL
|
||||||
import re
|
import re
|
||||||
import cgi
|
from cgi import escape
|
||||||
|
from string import maketrans
|
||||||
|
|
||||||
"""
|
"""
|
||||||
TODO: next version should use MathJax
|
TODO: next version should use MathJax
|
||||||
@@ -41,11 +43,152 @@ print markmin2latex(m)
|
|||||||
from markmin2pdf import markmin2pdf # requires pdflatex
|
from markmin2pdf import markmin2pdf # requires pdflatex
|
||||||
print markmin2pdf(m)
|
print markmin2pdf(m)
|
||||||
``
|
``
|
||||||
|
====================
|
||||||
|
# This is a test block with new features:
|
||||||
|
|
||||||
|
This is a blockquote with
|
||||||
|
a list with tables in it:
|
||||||
|
-----------
|
||||||
|
This is a paragraph before list.
|
||||||
|
You can continue paragraph on the
|
||||||
|
next lines.
|
||||||
|
|
||||||
|
This is an ordered list with tables:
|
||||||
|
+ Item 1
|
||||||
|
+ Item 2
|
||||||
|
+ --------
|
||||||
|
aa|bb|cc
|
||||||
|
11|22|33
|
||||||
|
--------:tableclass1[tableid1]
|
||||||
|
+ Item 4
|
||||||
|
-----------
|
||||||
|
T1| T2| t3
|
||||||
|
===========
|
||||||
|
aaa|bbb|ccc
|
||||||
|
ddd|fff|ggg
|
||||||
|
123|0 |5.0
|
||||||
|
-----------:tableclass1
|
||||||
|
-----------:blockquoteclass[blockquoteid]
|
||||||
|
|
||||||
|
This this a new paragraph
|
||||||
|
with a table. Table has header and footer:
|
||||||
|
-------------------------------
|
||||||
|
**Title 1**|**Title 2**|**Title 3**
|
||||||
|
==============================
|
||||||
|
data 1 | data 2 | 2.00
|
||||||
|
data 4 |data5(long)| 23.00
|
||||||
|
|data 8 | 33.50
|
||||||
|
==============================
|
||||||
|
Total: | 3 items | 58.50
|
||||||
|
------------------------------:tableclass1[tableid2]
|
||||||
|
|
||||||
|
## Multilevel
|
||||||
|
lists
|
||||||
|
|
||||||
|
Now lists can be multilevel:
|
||||||
|
|
||||||
|
+ Ordered item 1 on level 1.
|
||||||
|
You can continue item text on
|
||||||
|
next strings
|
||||||
|
|
||||||
|
++. Ordered item 1 of sublevel 2 with
|
||||||
|
a paragraph (paragraph can start
|
||||||
|
with point after plus or minus
|
||||||
|
characters, e.g. **++.** or **--.**)
|
||||||
|
|
||||||
|
++. This is another item. But with 3 paragraphs,
|
||||||
|
blockquote and sublists:
|
||||||
|
|
||||||
|
.. This is the second paragraph in the item. You
|
||||||
|
can add paragraphs to an item, using point
|
||||||
|
notation, where first characters in the string
|
||||||
|
are sequence of points with space between
|
||||||
|
them and another string. For example, this
|
||||||
|
paragraph (in sublevel 2) starts with two points:
|
||||||
|
``.. This is the second paragraph...``
|
||||||
|
|
||||||
|
.. ----------
|
||||||
|
### this is a blockquote in a list
|
||||||
|
|
||||||
|
You can use blockquote with headers, paragraphs,
|
||||||
|
tables and lists in it:
|
||||||
|
|
||||||
|
Tables can have or have not header and footer.
|
||||||
|
This table is defined without any header
|
||||||
|
and footer in it:
|
||||||
|
---------------------
|
||||||
|
red |fox | 0
|
||||||
|
blue |dolphin | 1000
|
||||||
|
green|leaf | 10000
|
||||||
|
---------------------
|
||||||
|
----------
|
||||||
|
|
||||||
|
.. This is yet another paragraph in the item.
|
||||||
|
|
||||||
|
--- This is an item of unordered list **(sublevel 3)**
|
||||||
|
--- This is the second item of the unordered list ''(sublevel 3)''
|
||||||
|
|
||||||
|
++++++ This is a single item of ordered list in sublevel 6
|
||||||
|
.... and this is a paragraph in sublevel 4
|
||||||
|
---. This is a new item with paragraph in sublevel 3.
|
||||||
|
++++ Start ordered list in sublevel 4 with code block: ``
|
||||||
|
line 1
|
||||||
|
line 2
|
||||||
|
line 3
|
||||||
|
``
|
||||||
|
++++. Yet another item with code block:
|
||||||
|
``
|
||||||
|
line 1
|
||||||
|
line 2
|
||||||
|
line 3
|
||||||
|
``
|
||||||
|
This item finishes with this paragraph.
|
||||||
|
|
||||||
|
... Item in sublevel 3 can be continued with paragraphs.
|
||||||
|
|
||||||
|
... ``
|
||||||
|
this is another
|
||||||
|
code block
|
||||||
|
in the
|
||||||
|
sublevel 3 item
|
||||||
|
``
|
||||||
|
|
||||||
|
+++ The last item in sublevel 3
|
||||||
|
.. This is a continuous paragraph for item 2 in sublevel 2.
|
||||||
|
You can use such structure to create difficult structured
|
||||||
|
documents.
|
||||||
|
|
||||||
|
++ item 3 in sublevel 2
|
||||||
|
-- item 1 in sublevel 2 (new unordered list)
|
||||||
|
-- item 2 in sublevel 2
|
||||||
|
-- item 3 in sublevel 2
|
||||||
|
|
||||||
|
++ item 1 in sublevel 2 (new ordered list)
|
||||||
|
++ item 2 in sublevel 2
|
||||||
|
++ item 3 in sublevle 2
|
||||||
|
|
||||||
|
+ item 2 in level 1
|
||||||
|
+ item 3 in level 1
|
||||||
|
- new unordered list (item 1 in level 1)
|
||||||
|
- level 2 in level 1
|
||||||
|
|
||||||
|
- level 3 in level 1
|
||||||
|
- level 4 in level 1
|
||||||
|
## This is the last section of the test
|
||||||
|
|
||||||
|
Single paragraph with '----' in it will be turned into separator:
|
||||||
|
|
||||||
|
-----------
|
||||||
|
|
||||||
|
And this is the last paragraph in
|
||||||
|
the test. Be happy!
|
||||||
|
|
||||||
|
====================
|
||||||
|
|
||||||
## Why?
|
## Why?
|
||||||
|
|
||||||
We wanted a markup language with the following requirements:
|
We wanted a markup language with the following requirements:
|
||||||
- less than 200 lines of functional code
|
- less than 300 lines of functional code
|
||||||
- easy to read
|
- easy to read
|
||||||
- secure
|
- secure
|
||||||
- support table, ul, ol, code
|
- support table, ul, ol, code
|
||||||
@@ -78,6 +221,7 @@ markmin2html.py and markmin2latex.py are single files and have no web2py depende
|
|||||||
|
|
||||||
------------------------------------------------------------------------------
|
------------------------------------------------------------------------------
|
||||||
**SOURCE** | **OUTPUT**
|
**SOURCE** | **OUTPUT**
|
||||||
|
==============================================================================
|
||||||
``# title`` | **title**
|
``# title`` | **title**
|
||||||
``## section`` | **section**
|
``## section`` | **section**
|
||||||
``### subsection`` | **subsection**
|
``### subsection`` | **subsection**
|
||||||
@@ -138,26 +282,64 @@ is rendered as
|
|||||||
+ Mouse
|
+ Mouse
|
||||||
|
|
||||||
|
|
||||||
### Tables
|
### Multilevel Lists
|
||||||
|
|
||||||
|
``
|
||||||
|
+ Dogs
|
||||||
|
-- red
|
||||||
|
-- brown
|
||||||
|
-- black
|
||||||
|
+ Cats
|
||||||
|
-- fluffy
|
||||||
|
-- smooth
|
||||||
|
-- bald
|
||||||
|
+ Mice
|
||||||
|
-- small
|
||||||
|
-- big
|
||||||
|
-- huge
|
||||||
|
``
|
||||||
|
|
||||||
|
is rendered as
|
||||||
|
+ Dogs
|
||||||
|
-- red
|
||||||
|
-- brown
|
||||||
|
-- black
|
||||||
|
+ Cats
|
||||||
|
-- fluffy
|
||||||
|
-- smooth
|
||||||
|
-- bald
|
||||||
|
+ Mice
|
||||||
|
-- small
|
||||||
|
-- big
|
||||||
|
-- huge
|
||||||
|
|
||||||
|
|
||||||
|
### Tables (with optional header and/or footer)
|
||||||
|
|
||||||
Something like this
|
Something like this
|
||||||
``
|
``
|
||||||
---------
|
-----------------
|
||||||
**A**|**B**|**C**
|
**A**|**B**|**C**
|
||||||
|
=================
|
||||||
0 | 0 | X
|
0 | 0 | X
|
||||||
0 | X | 0
|
0 | X | 0
|
||||||
X | 0 | 0
|
X | 0 | 0
|
||||||
-----:abc
|
=================
|
||||||
|
**D**|**F**|**G**
|
||||||
|
-----------------:abc[id]
|
||||||
``
|
``
|
||||||
is a table and is rendered as
|
is a table and is rendered as
|
||||||
---------
|
-----------------
|
||||||
**A**|**B**|**C**
|
**A**|**B**|**C**
|
||||||
|
=================
|
||||||
0 | 0 | X
|
0 | 0 | X
|
||||||
0 | X | 0
|
0 | X | 0
|
||||||
X | 0 | 0
|
X | 0 | 0
|
||||||
-----:abc
|
=================
|
||||||
|
**D**|**F**|**G**
|
||||||
|
-----------------:abc[id]
|
||||||
Four or more dashes delimit the table and | separates the columns.
|
Four or more dashes delimit the table and | separates the columns.
|
||||||
The ``:abc`` at the end sets the class for the table and it is optional.
|
The ``:abc``, ``:id[abc_1]`` or ``:abc[abc_1]`` at the end sets the class and/or id for the table and it is optional.
|
||||||
|
|
||||||
### Blockquote
|
### Blockquote
|
||||||
|
|
||||||
@@ -167,6 +349,44 @@ A table with a single cell is rendered as a blockquote:
|
|||||||
Hello world
|
Hello world
|
||||||
-----
|
-----
|
||||||
|
|
||||||
|
Blockquote can contain headers, paragraphs, lists and tables:
|
||||||
|
|
||||||
|
``
|
||||||
|
-----
|
||||||
|
This is a paragraph in a blockquote
|
||||||
|
|
||||||
|
+ item 1
|
||||||
|
+ item 2
|
||||||
|
-- item 2.1
|
||||||
|
-- item 2.2
|
||||||
|
+ item 3
|
||||||
|
|
||||||
|
---------
|
||||||
|
0 | 0 | X
|
||||||
|
0 | X | 0
|
||||||
|
X | 0 | 0
|
||||||
|
---------:tableclass1
|
||||||
|
-----
|
||||||
|
``
|
||||||
|
|
||||||
|
is rendered as:
|
||||||
|
-----
|
||||||
|
This is a paragraph in a blockquote
|
||||||
|
|
||||||
|
+ item 1
|
||||||
|
+ item 2
|
||||||
|
-- item 2.1
|
||||||
|
-- item 2.2
|
||||||
|
+ item 3
|
||||||
|
|
||||||
|
---------
|
||||||
|
0 | 0 | X
|
||||||
|
0 | X | 0
|
||||||
|
X | 0 | 0
|
||||||
|
---------:tableclass1
|
||||||
|
-----
|
||||||
|
|
||||||
|
|
||||||
### Code, ``<code>``, escaping and extra stuff
|
### Code, ``<code>``, escaping and extra stuff
|
||||||
|
|
||||||
``
|
``
|
||||||
@@ -227,7 +447,7 @@ extra={'code_cpp':lambda text: CODE(text,language='cpp').xml(),
|
|||||||
``
|
``
|
||||||
or simple:
|
or simple:
|
||||||
``
|
``
|
||||||
extra={'code':lambda text,lang='': CODE(text,language=lang).xml()}
|
extra={'code':lambda text,lang='python': CODE(text,language=lang).xml()}
|
||||||
``
|
``
|
||||||
``
|
``
|
||||||
markmin2html(text,extra=extra)
|
markmin2html(text,extra=extra)
|
||||||
@@ -266,10 +486,12 @@ Here is an example of usage:
|
|||||||
As shown in Ref.!`!`mdipierro`!`!:cite
|
As shown in Ref.!`!`mdipierro`!`!:cite
|
||||||
|
|
||||||
## References
|
## References
|
||||||
|
|
||||||
- [[mdipierro]] web2py Manual, 3rd Edition, lulu.com
|
- [[mdipierro]] web2py Manual, 3rd Edition, lulu.com
|
||||||
``
|
``
|
||||||
|
|
||||||
### Caveats
|
### Caveats
|
||||||
|
|
||||||
``<ul/>``, ``<ol/>``, ``<code/>``, ``<table/>``, ``<blockquote/>``, ``<h1/>``, ..., ``<h6/>`` do not have ``<p>...</p>`` around them.
|
``<ul/>``, ``<ol/>``, ``<code/>``, ``<table/>``, ``<blockquote/>``, ``<h1/>``, ..., ``<h6/>`` do not have ``<p>...</p>`` around them.
|
||||||
|
|
||||||
"""
|
"""
|
||||||
@@ -277,57 +499,43 @@ html_colors=['aqua', 'black', 'blue', 'fuchsia', 'gray', 'green',
|
|||||||
'lime', 'maroon', 'navy', 'olive', 'purple', 'red',
|
'lime', 'maroon', 'navy', 'olive', 'purple', 'red',
|
||||||
'silver', 'teal', 'white', 'yellow']
|
'silver', 'teal', 'white', 'yellow']
|
||||||
|
|
||||||
META = 'META'
|
META = '\x06'
|
||||||
LINK = 'LINK'
|
LINK = '\x07'
|
||||||
DISABLED_META = META[::-1]
|
DISABLED_META = '\x08'
|
||||||
LATEX = '<img src="http://chart.apis.google.com/chart?cht=tx&chl=%s" />'
|
LATEX = '<img src="http://chart.apis.google.com/chart?cht=tx&chl=%s" />'
|
||||||
regex_URL=re.compile(r'(?P<b>(?<!\\)(?:\\\\)*)@\{(?P<f>\w+)/(?P<args>.+?)\}')
|
regex_URL=re.compile(r'@\{(?P<f>\w+)/(?P<args>.+?)\}')
|
||||||
regex_env=re.compile(r'(?P<b>(?<!\\)(?:\\\\)*)@\{(?P<a>\w+?)\}')
|
regex_env=re.compile(r'@\{(?P<a>\w+?)\}')
|
||||||
regex_expand_meta = re.compile('('+META+'|'+DISABLED_META+')')
|
regex_expand_meta = re.compile('('+META+'|'+DISABLED_META+')')
|
||||||
regex_newlines = re.compile(r'(\n\r)|(\r\n)')
|
|
||||||
regex_dd=re.compile(r'\$\$(?P<latex>.*?)\$\$')
|
regex_dd=re.compile(r'\$\$(?P<latex>.*?)\$\$')
|
||||||
regex_code = re.compile('('+META+'|'+DISABLED_META+r')|((?P<b>(?<!\\)(?:\\\\)*)``(?P<t>.*?(?<!\\)(?:\\\\)*)``(?:(?<!\\):(?P<c>\w+)(?:(?<!\\)\[(?P<p>\S+?)(?<!\\)\])?)?)',re.S)
|
regex_code = re.compile('('+META+'|'+DISABLED_META+r')|(``(?P<t>.+?)``(?::(?P<c>\w+)(?:\[(?P<p>\S+?)\])?)?)',re.S)
|
||||||
regex_maps = [
|
regex_strong=re.compile(r'\*\*(?P<t>[^\s*]+( +[^\s*]+)*)\*\*')
|
||||||
(re.compile(r'[ \t\r]+\n'),'\n'),
|
regex_del=re.compile(r'~~(?P<t>[^\s*]+( +[^\s*]+)*)~~')
|
||||||
(re.compile(r'[ \t\r]+\n'),'\n'),
|
regex_em=re.compile(r"''(?P<t>[^\s']+(?: +[^\s']+)*)''")
|
||||||
(re.compile(r'(?P<b1>(?<!\\)(?:\\\\)*)\*\*(?P<t>[^\s*]+( +[^\s*]+)*)(?P<b2>(?<!\\)(?:\\\\)*)\*\*'),'\g<b1><strong>\g<t>\g<b2></strong>'),
|
regex_num=re.compile(r"^\s*[+-]?((\d+(\.\d*)?)|\.\d+)([eE][+-]?[0-9]+)?\s*$")
|
||||||
(re.compile(r'(?P<b1>(?<!\\)(?:\\\\)*)~~(?P<t>[^\s*]+( +[^\s*]+)*)(?P<b2>(?<!\\)(?:\\\\)*)~~'),'\g<b1><del>\g<t>\g<b2></del>'),
|
regex_list=re.compile('^(?:(#{1,6}|\.+|\++|\-+)(\.)?\s+)?(.*)$')
|
||||||
(re.compile(r"(?P<b1>(?<!\\)(?:\\\\)*)''(?P<t>[^\s']+(?: +[^\s']+)*)(?P<b2>(?<!\\)(?:\\\\)*)''"),'\g<b1><em>\g<t>\g<b2></em>'),
|
regex_bq_headline=re.compile('^(?:(\.+|\++|\-+)(\.)?\s+)?(-{3}-*)$')
|
||||||
(re.compile(r'^#{6} (?P<t>[^\n]+)',re.M),'\n\n<<h6>\g<t></h6>\n'),
|
regex_tq=re.compile('^(-{3}-*)(?::(?P<c>\S+?)(?:\[(?P<p>\S+)\])?)?$')
|
||||||
(re.compile(r'^#{5} (?P<t>[^\n]+)',re.M),'\n\n<<h5>\g<t></h5>\n'),
|
|
||||||
(re.compile(r'^#{4} (?P<t>[^\n]+)',re.M),'\n\n<<h4>\g<t></h4>\n'),
|
|
||||||
(re.compile(r'^#{3} (?P<t>[^\n]+)',re.M),'\n\n<<h3>\g<t></h3>\n'),
|
|
||||||
(re.compile(r'^#{2} (?P<t>[^\n]+)',re.M),'\n\n<<h2>\g<t></h2>\n'),
|
|
||||||
(re.compile(r'^#{1} (?P<t>[^\n]+)',re.M),'\n\n<<h1>\g<t></h1>\n'),
|
|
||||||
(re.compile(r'^\- +(?P<t>.*)',re.M),'<<ul><li>\g<t></li></ul>'),
|
|
||||||
(re.compile(r'^\+ +(?P<t>.*)',re.M),'<<ol><li>\g<t></li></ol>'),
|
|
||||||
(re.compile(r'</ol>\n<<ol>'),''),
|
|
||||||
(re.compile(r'</ul>\n<<ul>'),''),
|
|
||||||
(re.compile(r'<<'),'\n\n<<'),
|
|
||||||
(re.compile(r'\n\s+\n'),'\n\n')]
|
|
||||||
regex_table = re.compile(r'^\-{4,}\n(?P<t>.*?)\n\-{4,}(:(?P<c>\w+))?\n',re.M|re.S)
|
|
||||||
regex_qr = re.compile(r'(?<!["\w>/=])qr:(?P<k>\w+://[\w\d\-+?&%/:.]+)',re.M)
|
regex_qr = re.compile(r'(?<!["\w>/=])qr:(?P<k>\w+://[\w\d\-+?&%/:.]+)',re.M)
|
||||||
regex_embed = re.compile(r'(?<!["\w>/=])embed:(?P<k>\w+://[\w\d\-+_=?%&/:.]+)', re.M)
|
regex_embed = re.compile(r'(?<!["\w>/=])embed:(?P<k>\w+://[\w\d\-+_=?%&/:.]+)', re.M)
|
||||||
regex_iframe = re.compile(r'(?<!["\w>/=])iframe:(?P<k>\w+://[\w\d\-+=?%&/:.]+)', re.M)
|
regex_iframe = re.compile(r'(?<!["\w>/=])iframe:(?P<k>\w+://[\w\d\-+=?%&/:.]+)', re.M)
|
||||||
regex_auto_image = re.compile(r'(?<!["\w>/=])(?P<k>\w+://[\w\d\-+_=%&/:.]+\.(jpeg|JPEG|jpg|JPG|gif|GIF|png|PNG)(\?[\w\d/\-+_=%&:.]+)?)',re.M)
|
regex_auto_image = re.compile(r'(?<!["\w>/=])(?P<k>\w+://[\w\d\-+_=%&/:.]+\.(jpeg|JPEG|jpg|JPG|gif|GIF|png|PNG)(\?[\w\d/\-+_=%&:.]+)?)',re.M)
|
||||||
regex_auto_video = re.compile(r'(?<!["\w>/=])(?P<k>\w+://[\w\d\-+_=%&/:.]+\.(mp4|MP4|mpeg|MPEG|mov|MOV)(\?[\w\d/\-+_=%&:.]+)?)',re.M)
|
regex_auto_video = re.compile(r'(?<!["\w>/=])(?P<k>\w+://[\w\d\-+_=%&/:.]+\.(mp4|MP4|mpeg|MPEG|mov|MOV|ogv|OGV)(\?[\w\d/\-+_=%&:.]+)?)',re.M)
|
||||||
regex_auto_audio = re.compile(r'(?<!["\w>/=])(?P<k>\w+://[\w\d\-+_=%&/:.]+\.(mp3|MP3|wav|WAV)(\?[\w\d/\-+_=%&:.]+)?)',re.M)
|
regex_auto_audio = re.compile(r'(?<!["\w>/=])(?P<k>\w+://[\w\d\-+_=%&/:.]+\.(mp3|MP3|wav|WAV|ogg|OGG)(\?[\w\d/\-+_=%&:.]+)?)',re.M)
|
||||||
regex_auto = re.compile(r'(?<!["\w>/=])(?P<k>\w+://[\w\d\-+_=?%&/:.]+)',re.M)
|
regex_auto = re.compile(r'(?<!["\w>/=])(?P<k>\w+://[\w\d\-+_=?%&/:.]+)',re.M)
|
||||||
|
|
||||||
regex_link=re.compile(r'('+LINK+r')|(?P<b>(?<!\\)(?:\\\\)*)\[\[(?P<s>.*?)(?<!\\)\]\]')
|
regex_link=re.compile(r'('+LINK+r')|\[\[(?P<s>.+?)\]\]')
|
||||||
regex_link_level2=re.compile(r'^(?P<t>\S.*?)?(?:\s+(?<!\\)\[(?P<a>.+?(?<!\\)(?:\\\\)*)\])?(?:\s+(?P<k>\S+))?(?:\s+(?P<p>popup))?\s*$')
|
regex_link_level2=re.compile(r'^(?P<t>\S.*?)?(?:\s+\[(?P<a>.+?)\])?(?:\s+(?P<k>\S+))?(?:\s+(?P<p>popup))?\s*$')
|
||||||
regex_media_level2=re.compile(r'^(?P<t>\S.*?)?(?:\s+(?<!\\)\[(?P<a>.+?(?<!\\)(?:\\\\)*)\])?(?:\s+(?P<k>\S+))?\s+(?P<p>img|IMG|left|right|center|video|audio)(?:\s+(?P<w>\d+px))?\s*$')
|
regex_media_level2=re.compile(r'^(?P<t>\S.*?)?(?:\s+\[(?P<a>.+?)\])?(?:\s+(?P<k>\S+))?\s+(?P<p>img|IMG|left|right|center|video|audio)(?:\s+(?P<w>\d+px))?\s*$')
|
||||||
|
|
||||||
regex_backslash = re.compile(r"(\\+)(['`:*~\\[\]{}@\$])")
|
regex_markmin_escape = re.compile(r"(\\*)(['`:*~\\[\]{}@\$+\-.#])")
|
||||||
regex_markmin_escape = re.compile(r"(\\*)(['`:*~\\[\]{}@\$])")
|
regex_backslash = re.compile(r"\\(['`:*~\\[\]{}@\$+\-.#])")
|
||||||
|
ttab_in = maketrans("'`:*~\\[]{}@$+-.#", '\x0b\x0c\x0e\x0f\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b')
|
||||||
|
ttab_out = maketrans('\x0b\x0c\x0e\x0f\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b',"'`:*~\\[]{}@$+-.#")
|
||||||
|
|
||||||
def markmin_escape(text):
|
def markmin_escape(text):
|
||||||
""" insert \\ before markmin control characters: '`:*~[]{}@$ """
|
""" insert \\ before markmin control characters: '`:*~[]{}@$ """
|
||||||
return regex_markmin_escape.sub(lambda m: '\\'+m.group(0).replace('\\','\\\\'), text)
|
return regex_markmin_escape.sub(lambda m: '\\'+m.group(0).replace('\\','\\\\'), text)
|
||||||
|
|
||||||
def remove_backslashes(text):
|
|
||||||
return regex_backslash.sub(lambda m: m.group(0)[m.group(0).find('\\')+1:].replace('\\\\','\\') , text)
|
|
||||||
|
|
||||||
def render(text,extra={},allowed={},sep='p',URL=None,environment=None,latex='google',auto=True):
|
def render(text,extra={},allowed={},sep='p',URL=None,environment=None,latex='google',auto=True):
|
||||||
"""
|
"""
|
||||||
Arguments:
|
Arguments:
|
||||||
@@ -341,20 +549,28 @@ def render(text,extra={},allowed={},sep='p',URL=None,environment=None,latex='goo
|
|||||||
- auto is a True/False value (default is True) -
|
- auto is a True/False value (default is True) -
|
||||||
enables auto links processing for iframe,embed,qr,url,image,video,audio
|
enables auto links processing for iframe,embed,qr,url,image,video,audio
|
||||||
|
|
||||||
>>> render('this is\\n# a section\\nparagraph')
|
>>> render('this is\\n# a section\\n\\nparagraph')
|
||||||
'<p>this is</p><h1>a section</h1><p>paragraph</p>'
|
'<p>this is</p><h1>a section</h1><p>paragraph</p>'
|
||||||
>>> render('this is\\n## a subsection\\nparagraph')
|
>>> render('this is\\n## a subsection\\n\\nparagraph')
|
||||||
'<p>this is</p><h2>a subsection</h2><p>paragraph</p>'
|
'<p>this is</p><h2>a subsection</h2><p>paragraph</p>'
|
||||||
>>> render('this is\\n### a subsubsection\\nparagraph')
|
>>> render('this is\\n### a subsubsection\\n\\nparagraph')
|
||||||
'<p>this is</p><h3>a subsubsection</h3><p>paragraph</p>'
|
'<p>this is</p><h3>a subsubsection</h3><p>paragraph</p>'
|
||||||
>>> render('**hello world**')
|
>>> render('**hello world**')
|
||||||
'<p><strong>hello world</strong></p>'
|
'<p><strong>hello world</strong></p>'
|
||||||
>>> render('``hello world``')
|
>>> render('``hello world``')
|
||||||
'<code class="">hello world</code>'
|
'<code>hello world</code>'
|
||||||
>>> render('``hello world``:python')
|
>>> render('``hello world``:python')
|
||||||
'<code class="python">hello world</code>'
|
'<code class="python">hello world</code>'
|
||||||
>>> render('``\\nhello\\nworld\\n``:python')
|
>>> render('``\\nhello\\nworld\\n``:python')
|
||||||
'<pre><code class="python">hello\\nworld</code></pre>'
|
'<pre><code class="python">hello\\nworld</code></pre>'
|
||||||
|
>>> render('``hello world``:python[test_id]')
|
||||||
|
'<code class="python" id="test_id">hello world</code>'
|
||||||
|
>>> render('``hello world``:id[test_id]')
|
||||||
|
'<code id="test_id">hello world</code>'
|
||||||
|
>>> render('``\\nhello\\nworld\\n``:python[test_id]')
|
||||||
|
'<pre><code class="python" id="test_id">hello\\nworld</code></pre>'
|
||||||
|
>>> render('``\\nhello\\nworld\\n``:id[test_id]')
|
||||||
|
'<pre><code id="test_id">hello\\nworld</code></pre>'
|
||||||
>>> render("''hello world''")
|
>>> render("''hello world''")
|
||||||
'<p><em>hello world</em></p>'
|
'<p><em>hello world</em></p>'
|
||||||
>>> render('** hello** **world**')
|
>>> render('** hello** **world**')
|
||||||
@@ -367,10 +583,10 @@ def render(text,extra={},allowed={},sep='p',URL=None,environment=None,latex='goo
|
|||||||
'<ol><li>this</li><li>is</li><li>a list</li></ol><p>and this</p><ol><li>is</li><li>another</li></ol>'
|
'<ol><li>this</li><li>is</li><li>a list</li></ol><p>and this</p><ol><li>is</li><li>another</li></ol>'
|
||||||
|
|
||||||
>>> render("----\\na | b\\nc | d\\n----\\n")
|
>>> render("----\\na | b\\nc | d\\n----\\n")
|
||||||
'<table class=""><tr><td>a</td><td>b</td></tr><tr><td>c</td><td>d</td></tr></table>'
|
'<table><tbody><tr><td>a</td><td>b</td></tr><tr><td>c</td><td>d</td></tr></tbody></table>'
|
||||||
|
|
||||||
>>> render("----\\nhello world\\n----\\n")
|
>>> render("----\\nhello world\\n----\\n")
|
||||||
'<blockquote class="">hello world</blockquote>'
|
'<blockquote>hello world</blockquote>'
|
||||||
|
|
||||||
>>> render('[[this is a link http://example.com]]')
|
>>> render('[[this is a link http://example.com]]')
|
||||||
'<p><a href="http://example.com">this is a link</a></p>'
|
'<p><a href="http://example.com">this is a link</a></p>'
|
||||||
@@ -511,63 +727,54 @@ def render(text,extra={},allowed={},sep='p',URL=None,environment=None,latex='goo
|
|||||||
'<p><strong>this is a test</strong></p>'
|
'<p><strong>this is a test</strong></p>'
|
||||||
"""
|
"""
|
||||||
text = str(text or '')
|
text = str(text or '')
|
||||||
|
text = regex_backslash.sub(lambda m: m.group(1).translate(ttab_in), text)
|
||||||
|
|
||||||
if environment:
|
if environment:
|
||||||
def u2(match, environment=environment):
|
def u2(match, environment=environment):
|
||||||
b,a = match.group('b','a')
|
return str(environment.get(match.group('a'), match.group(0)))
|
||||||
return b + str(environment.get(a, match.group(0)))
|
|
||||||
text = regex_env.sub(u2, text)
|
text = regex_env.sub(u2, text)
|
||||||
|
|
||||||
if URL is not None:
|
if URL is not None:
|
||||||
# this is experimental @{controller/index/args}
|
# this is experimental @{function/args}
|
||||||
# turns into a digitally signed URL
|
# turns into a digitally signed URL
|
||||||
def u1(match,URL=URL):
|
def u1(match,URL=URL):
|
||||||
b,f,args = match.group('b','f','args')
|
f,args = match.group('f','args')
|
||||||
return b + URL(f,args=args.split('/'),scheme=True,host=True)
|
return URL(f,args=args.split('/'), scheme=True, host=True)
|
||||||
text = regex_URL.sub(u1,text)
|
text = regex_URL.sub(u1,text)
|
||||||
|
|
||||||
if latex == 'google':
|
if latex == 'google':
|
||||||
text = regex_dd.sub('``\g<latex>``:latex ', text)
|
text = regex_dd.sub('``\g<latex>``:latex ', text)
|
||||||
text = regex_newlines.sub('\n',text)
|
|
||||||
|
|
||||||
#############################################################
|
#############################################################
|
||||||
# replace all blocks marked with ``...``:class with META
|
# replace all blocks marked with ``...``:class[id] with META
|
||||||
# store them into segments they will be treated as code
|
# store them into segments they will be treated as code
|
||||||
#############################################################
|
#############################################################
|
||||||
segments = []
|
segments = []
|
||||||
def mark_code(m):
|
def mark_code(m):
|
||||||
|
g = m.group(0)
|
||||||
if m.group() in ( META, DISABLED_META ):
|
if m.group() in ( META, DISABLED_META ):
|
||||||
segments.append((None, None, None, m.group(0)))
|
segments.append((None, None, None, g))
|
||||||
return m.group()
|
return m.group()
|
||||||
else:
|
else:
|
||||||
c = m.group('c') or ''
|
c = m.group('c') or ''
|
||||||
p = m.group('p') or ''
|
p = m.group('p') or ''
|
||||||
b = m.group('b') or ''
|
|
||||||
if 'code' in allowed and not c in allowed['code']: c = ''
|
if 'code' in allowed and not c in allowed['code']: c = ''
|
||||||
code = m.group('t').replace('!`!','`')
|
code = m.group('t').replace('!`!','`')
|
||||||
segments.append((code, c, p, m.group(0)))
|
segments.append((code, c, p, m.group(0)))
|
||||||
return b + META
|
return META
|
||||||
text = regex_code.sub(mark_code, text)
|
text = regex_code.sub(mark_code, text)
|
||||||
|
|
||||||
#############################################################
|
#############################################################
|
||||||
# replace all blocks marked with [[...]] with LINK
|
# replace all blocks marked with [[...]] with LINK
|
||||||
# store them into links|medias they will be treated as link
|
# store them into links they will be treated as link
|
||||||
#############################################################
|
#############################################################
|
||||||
links = []
|
links = []
|
||||||
def mark_link(m):
|
def mark_link(m):
|
||||||
if m.group() == LINK:
|
links.append( None if m.group() == LINK
|
||||||
links.append(None)
|
else m.group('s') )
|
||||||
b = ''
|
return LINK
|
||||||
else:
|
|
||||||
s = m.group('s') or ''
|
|
||||||
b = m.group('b') or ''
|
|
||||||
links.append(s)
|
|
||||||
return b + LINK
|
|
||||||
text = regex_link.sub(mark_link, text)
|
text = regex_link.sub(mark_link, text)
|
||||||
|
text = escape(text)
|
||||||
#############################################################
|
|
||||||
# normalize spaces
|
|
||||||
#############################################################
|
|
||||||
text = '\n'.join(t.strip() for t in text.split('\n'))
|
|
||||||
text = cgi.escape(text)
|
|
||||||
|
|
||||||
if auto:
|
if auto:
|
||||||
text = regex_iframe.sub('<iframe src="\g<k>" frameborder="0" allowfullscreen></iframe>',text)
|
text = regex_iframe.sub('<iframe src="\g<k>" frameborder="0" allowfullscreen></iframe>',text)
|
||||||
@@ -579,81 +786,342 @@ def render(text,extra={},allowed={},sep='p',URL=None,environment=None,latex='goo
|
|||||||
text = regex_auto.sub('<a href="\g<k>">\g<k></a>', text)
|
text = regex_auto.sub('<a href="\g<k>">\g<k></a>', text)
|
||||||
|
|
||||||
#############################################################
|
#############################################################
|
||||||
# do h1,h2,h3,h4,h5,h6,b,i,ol,ul
|
# normalize spaces
|
||||||
#############################################################
|
#############################################################
|
||||||
for regex, sub in regex_maps:
|
strings=[t.strip() for t in text.split('\n')]
|
||||||
text = regex.sub(sub,text)
|
|
||||||
|
|
||||||
#############################################################
|
def parse_title(t, s): #out, lev, etags, tag, s):
|
||||||
# process tables and blockquotes
|
hlevel=str(len(t))
|
||||||
#############################################################
|
out.extend(etags[::-1])
|
||||||
while True:
|
out.append("<h%s>%s"%(hlevel,s))
|
||||||
item = regex_table.search(text)
|
etags[:]=["</h%s>"%hlevel]
|
||||||
if not item: break
|
lev=0
|
||||||
c = item.group('c') or ''
|
ltags[:]=[]
|
||||||
if 'table' in allowed and not c in allowed['table']: c = ''
|
tlev[:]=[]
|
||||||
content = item.group('t')
|
return (lev, 'h')
|
||||||
if ' | ' in content:
|
|
||||||
rows = content.replace('\n','</td></tr><tr><td>').replace(' | ','</td><td>')
|
def parse_list(t, p, s, tag, lev, mtag, lineno):
|
||||||
text = text[:item.start()] + '<<table class="%s"><tr><td>'%c + rows + '</td></tr></table>\n' + text[item.end():]
|
lent=len(t)
|
||||||
|
if lent<lev: # current item level < previous item level
|
||||||
|
while ltags[-1]>lent:
|
||||||
|
ltags.pop()
|
||||||
|
out.append(etags.pop())
|
||||||
|
lev=lent
|
||||||
|
tlev[lev:]=[]
|
||||||
|
|
||||||
|
if lent>lev: # current item level > previous item level
|
||||||
|
if lev==0: # previous line is not a list (paragraph or title)
|
||||||
|
out.extend(etags[::-1])
|
||||||
|
ltags[:]=[]
|
||||||
|
tlev[:]=[]
|
||||||
|
etags[:]=[]
|
||||||
|
if pend and mtag == '.': # paragraph in a list:
|
||||||
|
out.append(etags.pop())
|
||||||
|
ltags.pop()
|
||||||
|
for i in xrange(lent-lev):
|
||||||
|
out.append('<'+tag+'>')
|
||||||
|
etags.append('</'+tag+'>')
|
||||||
|
lev+=1
|
||||||
|
ltags.append(lev)
|
||||||
|
tlev.append(tag)
|
||||||
|
elif lent == lev:
|
||||||
|
if tlev[-1] != tag:
|
||||||
|
# type of list is changed (ul<=>ol):
|
||||||
|
for i in xrange(ltags.count(lent)):
|
||||||
|
ltags.pop()
|
||||||
|
out.append(etags.pop())
|
||||||
|
tlev[-1]=tag
|
||||||
|
out.append('<'+tag+'>')
|
||||||
|
etags.append('</'+tag+'>')
|
||||||
|
ltags.append(lev)
|
||||||
else:
|
else:
|
||||||
text = text[:item.start()] + '<<blockquote class="%s">'%c + content + '</blockquote>\n' + text[item.end():]
|
if ltags.count(lev)>1:
|
||||||
|
out.append(etags.pop())
|
||||||
|
ltags.pop()
|
||||||
|
mtag='l'
|
||||||
|
out.append('<li>')
|
||||||
|
etags.append('</li>')
|
||||||
|
ltags.append(lev)
|
||||||
|
if s[:1] == '-':
|
||||||
|
(s, mtag, lineno) = parse_table_or_blockquote(s, mtag, lineno)
|
||||||
|
if p and mtag=='l':
|
||||||
|
(lev,mtag,lineno)=parse_point(t, s, lev, '', lineno)
|
||||||
|
else:
|
||||||
|
out.append(s)
|
||||||
|
|
||||||
|
return (lev, mtag, lineno)
|
||||||
|
|
||||||
|
def parse_point(t, s, lev, mtag, lineno):
|
||||||
|
""" paragraphs in lists """
|
||||||
|
lent=len(t)
|
||||||
|
if lent>lev:
|
||||||
|
return parse_list(t, '.', s, 'ul', lev, mtag)
|
||||||
|
elif lent<lev:
|
||||||
|
while ltags[-1]>lent:
|
||||||
|
ltags.pop()
|
||||||
|
out.append(etags.pop())
|
||||||
|
lev=lent
|
||||||
|
tlev[lev:]=[]
|
||||||
|
mtag=''
|
||||||
|
elif lent==lev:
|
||||||
|
if pend and mtag == '.':
|
||||||
|
out.append(etags.pop())
|
||||||
|
ltags.pop()
|
||||||
|
if br and mtag in ('l','.'):
|
||||||
|
out.append(br)
|
||||||
|
if s == META:
|
||||||
|
mtag = ''
|
||||||
|
else:
|
||||||
|
mtag = '.'
|
||||||
|
if s[:1] == '-':
|
||||||
|
(s, mtag, lineno) = parse_table_or_blockquote(s, mtag, lineno)
|
||||||
|
if mtag == '.':
|
||||||
|
out.append(pbeg)
|
||||||
|
if pend:
|
||||||
|
etags.append(pend)
|
||||||
|
ltags.append(lev)
|
||||||
|
out.append(s)
|
||||||
|
return (lev, mtag, lineno)
|
||||||
|
|
||||||
|
def parse_table_or_blockquote(s, mtag, lineno):
|
||||||
|
# check next line. If next line :
|
||||||
|
# - is empty -> this is an <hr /> tag
|
||||||
|
# - consists '|' -> table
|
||||||
|
# - consists other characters -> blockquote
|
||||||
|
if ( lineno+1 >= strings_len or
|
||||||
|
not (s.count('-') == len(s) and len(s)>3) ):
|
||||||
|
return (s, mtag, lineno)
|
||||||
|
|
||||||
|
lineno+=1
|
||||||
|
s = strings[lineno]
|
||||||
|
if s:
|
||||||
|
if '|' in s:
|
||||||
|
# table
|
||||||
|
tout=[]
|
||||||
|
thead=[]
|
||||||
|
tbody=[]
|
||||||
|
t_id = ''
|
||||||
|
t_cls = ''
|
||||||
|
|
||||||
|
# parse table:
|
||||||
|
while lineno < strings_len:
|
||||||
|
s = strings[lineno]
|
||||||
|
if s[:1] == '=':
|
||||||
|
if s.count('=')==len(s) and len(s)>3: # header or footer
|
||||||
|
if not thead: # if thead list is empty:
|
||||||
|
thead = tout
|
||||||
|
else: # if tbody list is empty:
|
||||||
|
tbody.extend(tout)
|
||||||
|
tout = []
|
||||||
|
lineno+=1
|
||||||
|
continue
|
||||||
|
|
||||||
|
m = regex_tq.match(s)
|
||||||
|
if m:
|
||||||
|
t_cls = m.group('c') or ''
|
||||||
|
t_id = m.group('p') or ''
|
||||||
|
break
|
||||||
|
|
||||||
|
tout.append('<tr>'+''.join(['<td%s>%s</td>'% \
|
||||||
|
(' class="num"'
|
||||||
|
if regex_num.match(f)
|
||||||
|
else '',
|
||||||
|
f.strip()
|
||||||
|
) for f in s.split('|')])+'</tr>')
|
||||||
|
lineno+=1
|
||||||
|
|
||||||
|
t_cls = ' class="%s"'%t_cls if t_cls and t_cls != 'id' else ''
|
||||||
|
t_id = ' id="%s"'%t_id if t_id else ''
|
||||||
|
s = ''
|
||||||
|
if thead:
|
||||||
|
s += '<thead>'+''.join([l for l in thead])+'</thead>'
|
||||||
|
if not tbody: # tbody strings are in tout list
|
||||||
|
tbody = tout
|
||||||
|
tout = []
|
||||||
|
if tbody: # if tbody list is not empty:
|
||||||
|
s += '<tbody>'+''.join([l for l in tbody])+'</tbody>'
|
||||||
|
if tout: # tfoot is not empty:
|
||||||
|
s += '<tfoot>'+''.join([l for l in tout])+'</tfoot>'
|
||||||
|
s = '<table%s%s>%s</table>' % (t_cls, t_id, s)
|
||||||
|
mtag='t'
|
||||||
|
else:
|
||||||
|
# parse blockquote:
|
||||||
|
bq_begin=lineno
|
||||||
|
t_mode = False # embidded table
|
||||||
|
t_cls = ''
|
||||||
|
t_id = ''
|
||||||
|
|
||||||
|
# search blockquote closing line:
|
||||||
|
while lineno < strings_len:
|
||||||
|
s = strings[lineno]
|
||||||
|
if not t_mode:
|
||||||
|
m = regex_tq.match(s)
|
||||||
|
if m:
|
||||||
|
if lineno+1 == strings_len or '|' not in strings[lineno+1]:
|
||||||
|
t_cls = m.group('c') or ''
|
||||||
|
t_id = m.group('p') or ''
|
||||||
|
break
|
||||||
|
|
||||||
|
if regex_bq_headline.match(s):
|
||||||
|
if lineno+1 < strings_len and strings[lineno+1]:
|
||||||
|
t_mode = True
|
||||||
|
lineno+=1
|
||||||
|
continue
|
||||||
|
elif regex_tq.match(s):
|
||||||
|
t_mode=False
|
||||||
|
lineno+=1
|
||||||
|
continue
|
||||||
|
|
||||||
|
lineno+=1
|
||||||
|
|
||||||
|
t_cls = ' class="%s"'%t_cls if t_cls and t_cls != 'id' else ''
|
||||||
|
t_id = ' id="%s"'%t_id if t_id else ''
|
||||||
|
s = '<blockquote%s%s>%s</blockquote>' \
|
||||||
|
% (t_cls,
|
||||||
|
t_id,
|
||||||
|
render('\n'.join(strings[bq_begin:lineno]),
|
||||||
|
extra,
|
||||||
|
allowed,
|
||||||
|
'br',
|
||||||
|
URL,
|
||||||
|
environment,
|
||||||
|
latex,
|
||||||
|
auto)
|
||||||
|
)
|
||||||
|
mtag='q'
|
||||||
|
else:
|
||||||
|
s = '<hr />'
|
||||||
|
lineno-=1
|
||||||
|
mtag='q'
|
||||||
|
return (s, 'q', lineno)
|
||||||
|
|
||||||
#############################################################
|
|
||||||
# deal with paragraphs (trick <<ul, <<ol, <<table, <<h1, etc)
|
|
||||||
# the << indicates that there should NOT be a new paragraph
|
|
||||||
# META indicates a code block therefore no new paragraph
|
|
||||||
#############################################################
|
|
||||||
items = [item.strip() for item in text.split('\n\n')]
|
|
||||||
if sep == 'p':
|
if sep == 'p':
|
||||||
text = ''.join(
|
pbeg = "<p>"
|
||||||
(p[:2]!='<<' and p!=META and '<p>%s</p>'%p or '%s'%p) \
|
pend = "</p>"
|
||||||
for p in items if p.strip())
|
br = ''
|
||||||
elif sep=='br':
|
else:
|
||||||
text = '<br />'.join(items)
|
pbeg = pend = ''
|
||||||
|
br = "<br />" if sep=='br' else ''
|
||||||
|
|
||||||
|
lev = 0 # рівень вкладеності списків
|
||||||
|
c0 = '' # перший символ поточного рядка
|
||||||
|
out = [] # результуючий список рядків
|
||||||
|
etags = [] # завершуючі таги
|
||||||
|
ltags = [] # номер рівня відповідний завершуючому тагу
|
||||||
|
tlev = [] # таг рівня ('ul' або 'ol')
|
||||||
|
mtag = '' # marked tag (~last tag) ('l','.','h','p','t'). Used for set <br/>
|
||||||
|
# and for avoid <p></p> around tables and blockquotes
|
||||||
|
lineno = 0
|
||||||
|
strings_len = len(strings)
|
||||||
|
while lineno < strings_len:
|
||||||
|
s = strings[lineno]
|
||||||
|
""" # + - . ---------------------
|
||||||
|
## ++ -- .. ------- field | field | field <-title
|
||||||
|
### +++ --- ... quote =====================
|
||||||
|
#### ++++ ---- .... ------- field | field | field <-body
|
||||||
|
##### +++++ ----- ..... ---------------------:class[id]
|
||||||
|
"""
|
||||||
|
pc0=c0 # перший символ попереднього рядка
|
||||||
|
c0=s[:1]
|
||||||
|
if c0: # for non empty strings
|
||||||
|
if c0 in "#+-.": # first character is one of: # + - .
|
||||||
|
(t,p,s) = regex_list.findall(s)[0] # t - tag ("###", "+++", "---", "...")
|
||||||
|
# p - paragraph point ('.')->for "++." or "--."
|
||||||
|
# s - other part of string
|
||||||
|
if t:
|
||||||
|
# headers and lists:
|
||||||
|
if c0 == '#': # headers
|
||||||
|
(lev, mtag) = parse_title(t, s)
|
||||||
|
elif c0 == '+': # ordered list
|
||||||
|
(lev, mtag, lineno)= parse_list(t, p, s, 'ol', lev, mtag, lineno)
|
||||||
|
elif c0 == '-': # unordered list
|
||||||
|
(lev, mtag, lineno) = parse_list(t, p, s, 'ul', lev, mtag, lineno)
|
||||||
|
else: # c0 == '.' # paragraph in lists
|
||||||
|
(lev, mtag, lineno) = parse_point(t, s, lev, mtag, lineno)
|
||||||
|
lineno+=1
|
||||||
|
continue
|
||||||
|
else:
|
||||||
|
if c0 == '-': # table or blockquote?
|
||||||
|
(s, mtag, lineno) = parse_table_or_blockquote(s, mtag, lineno)
|
||||||
|
|
||||||
|
if lev == 0 and (mtag == 'q' or s == META):
|
||||||
|
# new paragraph
|
||||||
|
pc0=''
|
||||||
|
|
||||||
|
if pc0 == '':
|
||||||
|
# paragraph
|
||||||
|
out.extend(etags[::-1])
|
||||||
|
etags=[]
|
||||||
|
ltags=[]
|
||||||
|
tlev=[]
|
||||||
|
lev=0
|
||||||
|
if br and mtag == 'p': out.append(br)
|
||||||
|
if mtag != 'q' and s != META:
|
||||||
|
if pend: etags=[pend]
|
||||||
|
out.append(pbeg)
|
||||||
|
mtag = 'p'
|
||||||
|
else:
|
||||||
|
mtag = ''
|
||||||
|
out.append(s)
|
||||||
|
else:
|
||||||
|
if lev>0 and mtag=='.' and s == META:
|
||||||
|
out.append(etags.pop())
|
||||||
|
ltags.pop()
|
||||||
|
out.append(s)
|
||||||
|
mtag = ''
|
||||||
|
else:
|
||||||
|
out.append(' '+s)
|
||||||
|
lineno+=1
|
||||||
|
out.extend(etags[::-1])
|
||||||
|
text = ''.join(out)
|
||||||
|
|
||||||
#############################################################
|
#############################################################
|
||||||
# finally get rid of <<
|
# do strong,em,del
|
||||||
#############################################################
|
#############################################################
|
||||||
text=text.replace('<<','<')
|
text = regex_strong.sub('<strong>\g<t></strong>', text)
|
||||||
|
text = regex_del.sub('<del>\g<t></del>', text)
|
||||||
|
text = regex_em.sub('<em>\g<t></em>', text)
|
||||||
|
|
||||||
#############################################################
|
#############################################################
|
||||||
# deal with images, videos, audios and links
|
# deal with images, videos, audios and links
|
||||||
#############################################################
|
#############################################################
|
||||||
def sub_media(m):
|
def sub_media(m):
|
||||||
d=m.groupdict()
|
t,a,k,p,w = m.group('t','a','k','p','w')
|
||||||
if not d['k']:
|
if not k:
|
||||||
return m.group(0)
|
return m.group(0)
|
||||||
d['k'] = cgi.escape(d['k'])
|
k = escape(k)
|
||||||
d['t'] = d['t'] or ''
|
t = t or ''
|
||||||
d['width'] = ' width="%s"'%d['w'] if d['w'] else ''
|
width = ' width="%s"' % w if w else ''
|
||||||
d['title'] = ' title="%s"'%cgi.escape(d['a']).replace(META, DISABLED_META) if d['a'] else ''
|
title = ' title="%s"' % escape(a).replace(META, DISABLED_META) if a else ''
|
||||||
d['style'] = d['p_begin'] = d['p_end'] = ''
|
style = p_begin = p_end = ''
|
||||||
if d['p'] == 'center':
|
if p == 'center':
|
||||||
d['p_begin'] = '<p style="text-align:center">'
|
p_begin = '<p style="text-align:center">'
|
||||||
d['p_end'] = '</p>'
|
p_end = '</p>'
|
||||||
elif d['p'] in ('left','right'):
|
elif p in ('left','right'):
|
||||||
d['style'] = ' style="float:%s"'%d['p']
|
style = ' style="float:%s"' % p
|
||||||
if d['p'] in ('video','audio'):
|
if p in ('video','audio'):
|
||||||
d['t']=render(d['t'],{},{},'',URL,environment,latex,auto)
|
t = render(t, {}, {}, 'br', URL, environment, latex, auto)
|
||||||
return '<%(p)s controls="controls"%(title)s%(width)s><source src="%(k)s" />%(t)s</%(p)s>'%d
|
return '<%(p)s controls="controls"%(title)s%(width)s><source src="%(k)s" />%(t)s</%(p)s>' \
|
||||||
d['alt'] = ' alt="%s"'%cgi.escape(d['t']).replace(META, DISABLED_META) if d['t'] else ''
|
% dict(p=p, title=title, width=width, k=k, t=t)
|
||||||
return '%(p_begin)s<img src="%(k)s"%(alt)s%(title)s%(style)s%(width)s />%(p_end)s'%d
|
alt = ' alt="%s"'%escape(t).replace(META, DISABLED_META) if t else ''
|
||||||
|
return '%(begin)s<img src="%(k)s"%(alt)s%(title)s%(style)s%(width)s />%(end)s' \
|
||||||
|
% dict(begin=p_begin, k=k, alt=alt, title=title,
|
||||||
|
style=style, width=width, end=p_end)
|
||||||
|
|
||||||
def sub_link(m):
|
def sub_link(m):
|
||||||
d=m.groupdict()
|
t,a,k,p = m.group('t','a','k','p')
|
||||||
if not d['k'] and not d['t']:
|
if not k and not t:
|
||||||
return m.group(0)
|
return m.group(0)
|
||||||
d['t'] = d['t'] or ''
|
t = t or ''
|
||||||
d['a'] = cgi.escape(d['a']) if d['a'] else ''
|
a = escape(a) if a else ''
|
||||||
if d['k']:
|
if k:
|
||||||
d['k'] = cgi.escape(d['k'])
|
k = escape(k)
|
||||||
d['title'] = ' title="%s"' % d['a'].replace(META, DISABLED_META) if d['a'] else ''
|
title = ' title="%s"' % a.replace(META, DISABLED_META) if a else ''
|
||||||
d['target'] = ' target="_blank"' if d['p'] == 'popup' else ''
|
target = ' target="_blank"' if p == 'popup' else ''
|
||||||
d['t'] = render(d['t'],{},{},'',URL,environment,latex,auto) if d['t'] else d['k']
|
t = render(t, {}, {}, 'br', URL, environment, latex, auto) if t else k
|
||||||
return '<a href="%(k)s"%(title)s%(target)s>%(t)s</a>'%d
|
return '<a href="%(k)s"%(title)s%(target)s>%(t)s</a>' \
|
||||||
d['t'] = cgi.escape(d['t'])
|
% dict(k=k, title=title, target=target, t=t)
|
||||||
return '<span id="%(t)s">%(a)s</span>'%d
|
return '<span id="%s">%s</span>' % (escape(t),a)
|
||||||
|
|
||||||
parts = text.split(LINK)
|
parts = text.split(LINK)
|
||||||
text = parts[0]
|
text = parts[0]
|
||||||
@@ -667,7 +1135,7 @@ def render(text,extra={},allowed={},sep='p',URL=None,environment=None,latex='goo
|
|||||||
if html == s:
|
if html == s:
|
||||||
# return unprocessed string as a signal of an error
|
# return unprocessed string as a signal of an error
|
||||||
html = '[[%s]]'%s
|
html = '[[%s]]'%s
|
||||||
text = text+html+parts[i+1]
|
text += html + parts[i+1]
|
||||||
|
|
||||||
#############################################################
|
#############################################################
|
||||||
# process all code text
|
# process all code text
|
||||||
@@ -675,7 +1143,7 @@ def render(text,extra={},allowed={},sep='p',URL=None,environment=None,latex='goo
|
|||||||
def expand_meta(m):
|
def expand_meta(m):
|
||||||
code,b,p,s = segments.pop(0)
|
code,b,p,s = segments.pop(0)
|
||||||
if code==None or m.group() == DISABLED_META:
|
if code==None or m.group() == DISABLED_META:
|
||||||
return cgi.escape(s)
|
return escape(s)
|
||||||
if b in extra:
|
if b in extra:
|
||||||
if code[:1]=='\n': code=code[1:]
|
if code[:1]=='\n': code=code[1:]
|
||||||
if code[-1:]=='\n': code=code[:-1]
|
if code[-1:]=='\n': code=code[:-1]
|
||||||
@@ -686,24 +1154,27 @@ def render(text,extra={},allowed={},sep='p',URL=None,environment=None,latex='goo
|
|||||||
elif b=='cite':
|
elif b=='cite':
|
||||||
return '['+','.join('<a href="#%s" class="%s">%s</a>' \
|
return '['+','.join('<a href="#%s" class="%s">%s</a>' \
|
||||||
% (d,b,d) \
|
% (d,b,d) \
|
||||||
for d in cgi.escape(code).split(','))+']'
|
for d in escape(code).split(','))+']'
|
||||||
elif b=='latex':
|
elif b=='latex':
|
||||||
return LATEX % code.replace('"','\"').replace('\n',' ')
|
return LATEX % code.replace('"','\"').replace('\n',' ')
|
||||||
elif b in html_colors:
|
elif b in html_colors:
|
||||||
return '<span style="color: %s">%s</span>' \
|
return '<span style="color: %s">%s</span>' \
|
||||||
% (b, render(code,{},{},'',URL,environment,latex,auto))
|
% (b, render(code,{},{},'br',URL,environment,latex,auto))
|
||||||
elif b in ('c', 'color') and p:
|
elif b in ('c', 'color') and p:
|
||||||
c=p.split(':')
|
c=p.split(':')
|
||||||
fg='color: %s;' % c[0] if c[0] else ''
|
fg='color: %s;' % c[0] if c[0] else ''
|
||||||
bg='background-color: %s;' % c[1] if len(c)>1 and c[1] else ''
|
bg='background-color: %s;' % c[1] if len(c)>1 and c[1] else ''
|
||||||
return '<span style="%s%s">%s</span>' \
|
return '<span style="%s%s">%s</span>' \
|
||||||
% (fg, bg, render(code,{},{},'',URL,environment,latex,auto))
|
% (fg, bg, render(code,{},{},'br', URL, environment, latex, auto))
|
||||||
elif code[:1]=='\n' and code[-1:]=='\n':
|
cls = ' class="%s"'%b if b and b != 'id' else ''
|
||||||
return '<pre><code class="%s">%s</code></pre>' % (b,cgi.escape(code[1:-1]))
|
id = ' id="%s"'%escape(p) if p else ''
|
||||||
return '<code class="%s">%s</code>' % (b,cgi.escape(code[ (code[:1]=='\n')
|
if code[:1]=='\n' and code[-1:]=='\n':
|
||||||
|
return '<pre><code%s%s>%s</code></pre>' % (cls, id, escape(code[1:-1]))
|
||||||
|
return '<code%s%s>%s</code>' \
|
||||||
|
% (cls, id, escape(code[ (code[:1]=='\n')
|
||||||
: [None,-1][code[-1:]=='\n']]))
|
: [None,-1][code[-1:]=='\n']]))
|
||||||
text = regex_expand_meta.sub(expand_meta, text)
|
text = regex_expand_meta.sub(expand_meta, text)
|
||||||
text = remove_backslashes(text)
|
text = text.translate(ttab_out)
|
||||||
return text
|
return text
|
||||||
|
|
||||||
def markmin2html(text, extra={}, allowed={}, sep='p', auto=True):
|
def markmin2html(text, extra={}, allowed={}, sep='p', auto=True):
|
||||||
@@ -713,7 +1184,27 @@ if __name__ == '__main__':
|
|||||||
import sys
|
import sys
|
||||||
import doctest
|
import doctest
|
||||||
if sys.argv[1:2] == ['-h']:
|
if sys.argv[1:2] == ['-h']:
|
||||||
print '<html><body>'+markmin2html(__doc__)+'</body></html>'
|
print """<html><body>
|
||||||
|
<style>
|
||||||
|
blockquote { background-color: lime; }
|
||||||
|
thead { color: white; background-color: gray; text-align: center; }
|
||||||
|
tfoot { color: white; background-color: gray; }
|
||||||
|
|
||||||
|
.tableclass1 { background-color: yellow; }
|
||||||
|
.tableclass1 thead { color: yellow; background-color: green; }
|
||||||
|
.tableclass1 tfoot { color: yellow; background-color: green; }
|
||||||
|
|
||||||
|
td.num { text-align: right; }
|
||||||
|
pre { background-color: #E0E0E0; }
|
||||||
|
</style>
|
||||||
|
"""+markmin2html(__doc__)+'</body></html>'
|
||||||
|
elif sys.argv[1:2] == ['-t']:
|
||||||
|
from timeit import Timer
|
||||||
|
loops=1000
|
||||||
|
ts = Timer("markmin2html(__doc__)","from markmin2html import markmin2html")
|
||||||
|
print 'timeit "markmin2html(__doc__)":'
|
||||||
|
t = min([ts.timeit(loops) for i in range(3)])
|
||||||
|
print "%s loops, best of 3: %.3f ms per loop" % (loops, t/1000*loops)
|
||||||
elif len(sys.argv) > 1:
|
elif len(sys.argv) > 1:
|
||||||
fargv = open(sys.argv[1],'r')
|
fargv = open(sys.argv[1],'r')
|
||||||
try:
|
try:
|
||||||
|
|||||||
Reference in New Issue
Block a user