看看

Txtmark - Java markdown processor

Copyright (C) 2011 René Jeschke rene_jeschke@yahoo.de
See LICENSE.txt for licensing information.


Txtmark is yet another markdown processor for the JVM.

  • It is easy to use:

    String result = txtmark.Processor.process("This is ***TXTMARK***");
    
  • It is fast (see below)
    ... well, it is the fastest markdown processor on the JVM right now.

  • It does not depend on other libraries, so classpathing txtmark.jar is
    sufficient to use Txtmark in your project.

For an in-depth explanation of markdown have a look at the original Markdown Syntax.


Maven repository

Txtmark is now available as a maven artifact without additional repository entries. Have a look [here] (http://search.maven.org/#search|ga|1|txtmark).


Txtmark extensions

To enable Txtmark's extended markdown parsing you can use the $PROFILE$ mechanism:

[$PROFILE$]: extended

This seemed to me as the easiest and safest way to enable different behaviours.
Just put this line into your Txtmark file like you would use reference links.

Behavior changes when using [$PROFILE$]: extended

  • Lists and code blocks end a paragraph

    In normal markdown the following:

    This is a paragraph
    * and this is not a list
    

    Will produce:

    <p>This is a paragraph
    * and this is not a list</p>
    

    When using Txtmark extensions this changes to:

    <p>This is a paragraph</p>
    <ul>
    <li>and this is not a list</li>
    </ul>
    
  • Text anchors

    Headlines and list items may recieve an ID which
    you can refer to using links.

    ## Headline with ID ##     {#headid}
    
    Another headline with ID   {#headid2}
    ------------------------
    
    * List with ID             {#listid}
    
    Links: [Foo] (#headid)
    

    this will produce:

    <h2 id="headid">Headline with ID</h2>
    <h2 id="headid2">Another headline with ID</h2>
    <ul>
    <li id="listid">List with ID</li>
    </ul>
    <p>Links: <a href="#headid">Foo</a></p>
    

    The ID must be the last thing on the first line.

    All spaces before {# get removed, so you can't
    use an ID and a manual line break in the same line.

  • Auto HTML entities

    • (C) becomes © - ©
    • (R) becomes ® - ®
    • (TM) becomes - ™
    • -- becomes - –
    • --- becomes - —
    • ... becomes - …
    • << becomes « - «
    • >> becomes » - »
    • "Hello" becomes “Hello” - “Hello”
  • Underscores (Emphasis)

    Underscores in the middle of a word don't result in emphasis.

    Con_cat_this
    

    normally produces this:

    Con<em>cat</em>this
    
  • Superscript

    You can use ^ to mark a span as superscript.

    2^2^ = 4
    

    turns into

    2<sup>2</sup> = 4
    
  • Abbreviations

    Abbreviations are defined like reference links, but using a *
    instead of a link and must be single-line only.

    [Git]: * "Fast distributed revision control system"
    

    and used like this

    This is [Git]!
    

    which will produce

    This is <abbr title="Fast distributed revision control system">Git</abbr>!
    

Markdown conformity

Txtmark passes all tests inside MarkdownTest_1.0_2007-05-09
except of two:

  1. Images.text

    Fails because Txtmark doesn't produce empty 'title' image attributes.
    (IMHO: Images ... OK)

  2. Literal quotes in titles.text

    What the frell ... this test will continue to FAIL.
    Sorry, but using unescaped " in a title which should be surrounded
    by " is unacceptable for me ;)

    Change:

    Foo [bar](/url/ "Title with "quotes" inside").
    [bar]: /url/ "Title with "quotes" inside"
    

    to:

    Foo [bar](/url/ "Title with \"quotes\" inside").
    [bar]: /url/ "Title with \"quotes\" inside"
    

    and Txtmark will produce the correct result.
    (IMHO: Literal quotes in titles ... OK)


Where Txtmark is not like Markdown

  • Txtmark does not produce empty title attributes in link and image tags.

  • Unescaped " in link titles starting with " are not recognized and result
    in unexpected behaviour.

  • Due to a different list parsing approach some things get interpreted differently:

    * List
    > Quote
    

    will produce when processed with Markdown:

    <p><ul>
    <li>List</p>
    
    <blockquote>
     <p>Quote</li>
    </ul></p>
    </blockquote>
    

    and this when produced with Txtmark:

    <ul>
    <li>List<blockquote><p>Quote</p>
    </blockquote>
    </li>
    </ul>
    

    Another one:

    * List
    ====
    

    will produce when processed with Markdown:

    <h1>* List</h1>
    

    and this when produced with Txtmark:

    <ul>
    <li><h1>List</h1>
    </li>
    </ul>
    
  • List of escapeable characters:

    \   [   ]   (   )   {   }   #
    "   '   .   <   >   +   -   _
    !   `   ^
    

Performance comparison of markdown processors for the JVM

Based on this benchmark suite.

Excerpt from the original post concerning this benchmark suite:

Most of these tests are of course unrealistic: Who would write a
text where each word is a link? Yet they serve an important use:
It makes it possible for the developer to pinpoint the parts of
the parser where there is most room for improvement. Also, it
explains why certain texts might render much faster in one
Processor than in another.

Benchmark system:

  • Ubuntu Linux 10.04 32 Bit
  • Intel(R) Core(TM) 2 Duo T7500 @ 2.2GHz
  • Java(TM) SE Runtime Environment (build 1.6.0_24-b07)
  • Java HotSpot(TM) Server VM (build 19.1-b02, mixed mode)

<table>
<tr><th>Test</th><th colspan="2">Actuarius</th><th colspan="2">PegDown</th><th colspan="2">Knockoff</th><th colspan="2">Txtmark</th></tr>
<tr><td></td><td>1st Run (ms)</td><td>2nd Run (ms)</td><td>1st Run (ms)</td><td>2nd Run (ms)</td><td>1st Run (ms)</td><td>2nd Run (ms)</td><td>1st Run (ms)</td><td>2nd Run (ms)</td></tr>
<tr><td>Plain Paragraphs</td><td>1127</td><td>577</td><td>1273</td><td>1037</td><td>740</td><td>400</td><td>157</td><td>64</td></tr>
<tr><td>Every Word Emphasized</td><td>1562</td><td>1001</td><td>1523</td><td>1513</td><td>13982</td><td>13221</td><td>54</td><td>46</td></tr>
<tr><td>Every Word Strong</td><td>1125</td><td>997</td><td>1115</td><td>1114</td><td>9543</td><td>9647</td><td>44</td><td>41</td></tr>
<tr><td>Every Word Inline Code</td><td>382</td><td>277</td><td>1058</td><td>1052</td><td>9116</td><td>9074</td><td>51</td><td>39</td></tr>
<tr><td>Every Word a Fast Link</td><td>2257</td><td>1600</td><td>537</td><td>531</td><td>3980</td><td>3410</td><td>109</td><td>55</td></tr>
<tr><td>Every Word Consisting of Special XML Chars</td><td>4045</td><td>4270</td><td>2985</td><td>3044</td><td>312</td><td>377</td><td>778</td><td>775</td></tr>
<tr><td>Every Word wrapped in manual HTML tags</td><td>3334</td><td>2919</td><td>901</td><td>896</td><td>3863</td><td>3736</td><td>73</td><td>62</td></tr>
<tr><td>Every Line with a manual line break</td><td>510</td><td>588</td><td>1445</td><td>1440</td><td>1527</td><td>1130</td><td>56</td><td>56</td></tr>
<tr><td>Every word with a full link</td><td>452</td><td>246</td><td>1045</td><td>996</td><td>1884</td><td>1819</td><td>86</td><td>55</td></tr>
<tr><td>Every word with a full image</td><td>268</td><td>150</td><td>1140</td><td>1132</td><td>1985</td><td>1908</td><td>38</td><td>36</td></tr>
<tr><td>Every word with a reference link</td><td>9847</td><td>9082</td><td>18956</td><td>18719</td><td>121136</td><td>115416</td><td>1525</td><td>1380</td></tr>
<tr><td>Every block a quote</td><td>445</td><td>206</td><td>1312</td><td>1301</td><td>478</td><td>457</td><td>50</td><td>45</td></tr>
<tr><td>Every block a codeblock</td><td>70</td><td>87</td><td>373</td><td>376</td><td>161</td><td>175</td><td>60</td><td>22</td></tr>
<tr><td>Every block a list</td><td>920</td><td>912</td><td>1720</td><td>1725</td><td>622</td><td>651</td><td>55</td><td>55</td></tr>
<tr><td>All tests together</td><td>3281</td><td>2885</td><td>5184</td><td>5196</td><td>10130</td><td>10460</td><td>206</td><td>196</td></tr>
</table>

Benchmarked versions:

Actuarius version: 0.2
PegDown version: 0.8.5.4
Knockoff version: 0.7.3-15


TODO

  • Inline HTML control (configurable escaping of unallowed HTML tags)
  • Code clean-ups

Mentioned/related projects

Markdown is Copyright (C) 2004 by John Gruber
SmartyPants is Copyright (C) 2003 by John Gruber
Actuarius is Copyright (C) 2010 by Christoph Henkelmann
Knockoff is Copyright (C) 2009-2011 by Tristan Juricek
PegDown is Copyright (C) 2010 by Mathias Doenitz
PHP Markdown & Extra is Copyright (C) 2009 Michel Fortin


Project link: https://github.com/rjeschke/txtmark

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 203,324评论 5 476
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 85,303评论 2 381
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 150,192评论 0 337
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,555评论 1 273
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,569评论 5 365
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,566评论 1 281
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,927评论 3 395
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,583评论 0 257
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,827评论 1 297
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,590评论 2 320
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,669评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,365评论 4 318
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,941评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,928评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,159评论 1 259
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 42,880评论 2 349
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,399评论 2 342

推荐阅读更多精彩内容