3 The Wikitext extension is a fast wikitext-to-HTML translator written
4 in C and packaged as a Ruby extension.
6 Usage is straightforward:
10 >> Wikitext::Parser.new.parse("hello world!")
11 => "<p>hello world!</p>\n"
15 I needed a wikitext-to-HTML translator for a Rails application; a number
16 of design goals flowed on from this:
18 * _fast_: Rails has a reputation for being slow, so the translator had
19 to be part of the solution, not part of the problem
20 * _efficient_: again, given how much memory Rails likes to use, the
21 translator had to be very memory-efficient
22 * _robust_: on a public-facing web application that had to be up for long
23 periods, the translator had to be stable (no crashes, no resource leaks)
24 * _secure_: again, accepting input from untrusted sources meant that the
25 translator had to sanitize or reject unsafe input
26 * <em>easy to use</em>: for end users, the translator should provide a
27 simple, familiar markup as close as possible to what they already know
28 from other applications (such as MediaWiki, the wiki software that
30 * _forgiving_: wikitext is presentation markup, not source code, so the
31 translator should do a reasonable job of formatting even the most
32 invalid markup rather than giving up
33 * _informative_: when provided invalid markup the translator should
34 fail gracefully and emit HTML that provides useful visual feedback
35 about where the errors are in the input
36 * <em>multilingual-friendly</em>: the translator should handle input beyond
37 printable ASCII in a compatible fashion
38 * _attractive_: the emitted HTML source should be consistent and attractive
39 * <em>valid output</em>: regardless of the input, the translator should
40 always produce valid HTML5 output
41 * <em>well-tested</em>: the translator should have a comprehensive test
42 suite to ensure that its behaviour is not only correct but also stable
44 * <em>cross-platform</em>: should work identically on Mac OS X, Linux
45 (explicitly tested platforms) and perhaps others as well
47 Some notable things that were _not_ design goals:
49 * implement _all_ of the MediaWiki syntax (tables etc)
53 The markup is very close to that used by MediaWiki, the most popular wiki
54 software and the one that powers Wikipedia.
64 ====== Heading 6 ======
78 Consecutive linebreaks are converted into paragraph breaks.
81 This is one paragraph.
86 Would be marked up as:
89 <p>This is one paragraph. Another line.</p>
90 <p>And this is another.</p>
94 Emphasis is marked up as follows:
99 Which gets translated into:
104 Strong is marked up like this:
109 And transformed into:
112 <strong>strong text</strong>
114 You can nest spans inside one another, provided you don't try to produce
115 invalid HTML (for example, nesting strong inside strong). Here is a valid
119 '''''foo'' bar''' baz
124 <strong><em>foo</em> bar</strong> baz
126 Note that the translator emits HTML on the fly, so when it sees the
127 first run of five apostrophes it has no way of knowing what will come
128 afterwards and so doesn't know whether you mean to say "strong em" or
129 "em strong"; it therefore always assumes "strong em". If you wish to
130 force the alternative interpretation you can do one of the following:
133 '' '''foo''' bar'' baz (ie. use whitespace)
134 ''<nowiki></nowiki>'''foo''' bar'' baz (ie. insert an empty nowiki span)
135 <em><strong>foo</strong> bar</em> baz (ie. use explicit HTML tags instead)
136 <em>'''foo''' bar</em> baz (ie. use explicit HTML tags instead)
138 Note that to avoid ambiguity, the translator will not let you intermix
139 the shorthand style with the literal HTML tag style.
142 <em>foo'' (ie. intermixed, invalid)
146 The translator recognizes both standard HTML +tt+ tags and the
147 backtick (`) as a shorthand. These two are equivalent:
153 As of version 2.0, this markup is actually translated to +code+ tags
154 in the output because the +tt+ tag was removed from the HTML5 standard.
156 If you need to insert a literal backtick in your text you use a +nowiki+
160 here follows a literal <nowiki>`</nowiki> backtick
162 To avoid ambiguity, the translator will not let you intermix the two
167 Already mentioned above, you can use +nowiki+ tags to temporarily disable
168 wikitext markup. As soon as the translator sees the opening +nowiki+ tag
169 it starts emitting a literal copy of everything it sees up until the
170 closing +nowiki+ tag:
173 Hello <nowiki>''world''</nowiki>
189 <blockquote><p>Hello world! Bye for now.</p></blockquote>
191 You can nest blockquotes or any other kind of block or span inside
192 blockquotes. For example:
196 >> quote inside a quote
200 Any line indented with whitespace will be interpreted as part of a +pre+
201 block. Wikitext markup inside +pre+ blocks has no special meaning. For
202 example, consider the following block indented by a single space:
205 // source code listing
211 Would be translated into:
214 <pre>// source code listing
220 +pre+ blocks may be nested inside +blockquote+ blocks.
230 <a href="/wiki/article_title">article title</a>
240 <a href="/wiki/article">link text</a>
242 See the Wikitext::Parser attributes documentation for how you can override
243 the default link prefix (<em>/wiki/</em> as shown in the example), and how
244 "red links" can be implemented by applying custom CSS depending on the
245 link target (this can be used to make links to non-existent pages appear
246 in a different color).
248 == Alternative blockquote and preformatted block syntax
250 For +blockquote+ and +pre+ blocks that go on for many lines it may be
251 more convenient to use the alternative syntax which uses standard
252 HTML tags rather than special prefixes at the beginning of each line.
256 a blockquote!</blockquote>
259 preformatted text</pre>
261 +blockquote+ and +pre+ blocks may nest inside other +blockquote+
264 Note that to avoid ambiguity, the translator will not let you intermix
265 the two styles (HTML markup and wikitext markup).
267 +pre+ blocks may also contain a custom +lang+ attribute for the purposes
268 of marking up a block for syntax-highlighting (note that the highlighting
269 itself would be provided by JavaScript in the browser and is not actually
270 part of the Wikitext extension). For example:
273 <pre lang="ruby">puts @person.name</pre>
275 Would be translated into:
278 <pre class="ruby-syntax">puts @person.name</pre>
280 The +lang+ attribute may only contain letters, so "Objective-C", for
281 example would need to be written as "objc" or similar.
286 [http://example.com/ this site]
291 <a href="http://example.com/" class="external">this site</a>
293 See the {Wikitext::Parser} attributes documentation for information on
294 overriding the default external link class (+external+ in this
295 example), or including a +rel+ attribute of "nofollow" (which may be
296 useful for search-engine optimization).
298 Note that in addition to providing a fully-qualified URL including a
299 protocol (such as "http://" or "ftp://") you also have the option of
300 using an unqualified "path"-style URL. This is useful for making
301 links to other pages still on the same site, but outside of the wiki:
304 [/issues/1024 ticket #1024]
309 <a href="/issues/1024">ticket #1024</a>
311 Note that no "external" class is included in the generated link.
313 To avoid false positives, what constitutes a "path" is
314 narrowly-defined as a string that begins with a slash, optionally
315 followed by zero or more "path components" consisting of upper and
316 lowercase letters, numbers, underscores, hyphens or periods. Path
317 components are separated by a slash, and the trailing slash after
318 the last path component is optional.
325 When outputting using HTML syntax (the default), this would become:
328 <img src="/images/foo.png" alt="foo.png">
330 When outputting using XML syntax, this would become a self-closing tag:
333 <img src="/images/foo.png" alt="foo.png" />
335 See the Wikitext::Parser documentation for information on setting the
338 You can override the "/images/" prefix using the +img_prefix+ attribute
341 You can also specify "absolute" image "src" attributes regardless of
342 the current prefix setting by starting the image path with a forward
351 <img src="/foo.png" alt="/foo.png">
355 Lists come in both unordered ("ul"):
362 And ordered ("ol") forms:
369 These would produce, respectively:
387 Lists may be nested inside one another as needed. For example:
431 Version 4.0.0 and above target Ruby 2.0.0 or higher.
433 For older versions of Ruby, you may use the 3.1 release or older.
437 The Wikitext extension provides a template handler so that templates named
438 following the <tt>template_name.html.wikitext</tt> format will automatically be
439 translated from wikitext markup into HTML when rendered.
441 Additionally, an optional Haml filter is available if you <tt>require
442 "wikitext/haml_filter"</tt>, which enables you to write wikitext markup inline
446 = Here is some [[wikitext]] =
448 Likewise, a +to_wikitext+ method (aliased as +w+) is added to the +String+
449 class (and also +NilClass+, for convenience) so that content can be easily
450 translated from inside view templates following patterns like:
454 The +to_wikitext+ method will preprocess its string using the
455 String#wikitext_preprocess method, if it is defined, before feeding the string
456 through the parser. This can be used to add application-specific behavior such
457 as converting special strings like:
462 into links. An example preprocessor is included with the extension but it is
463 not active by default; it can be activated with:
465 require 'wikitext/preprocess'
467 Finally, a Wikitext::Parser#shared_parser method is added to provide
468 convenient access to a shared singleton instance of the parser so as to
469 avoid repeatedly instantiating and setting up new parser instances as part
474 For Rails 2.3.x support, use version 2.1.x of the Wikitext gem.
476 The plug-in can be activated with an appropriate <tt>config.gem</tt> statement
477 in your <tt>config/environment.rb</tt>:
479 config.gem 'wikitext', '2.1.1'
483 For Rails 3.0.x support, use version 2.1.x of the Wikitext gem.
485 Add a line like the following to your Gemfile:
487 gem 'wikitext', '~> 2.1.1'
489 Note that while older versions of Wikitext do work with Rails 3 to some degree,
490 for full compatibility Wikitext version 2.0 or higher should be used.
494 Add a line like the following to your Gemfile:
500 * RubyForge project page: http://rubyforge.org/projects/wikitext
501 * RDoc: http://wikitext.rubyforge.org
502 * Source: http://git.wincent.com/wikitext.git
506 Wikitext is written and maintained by Wincent Colaiuta (win@wincent.com).
507 Other contributors that have submitted patches include:
513 Copyright 2007-2013 Wincent Colaiuta. All rights reserved.
515 Redistribution and use in source and binary forms, with or without
516 modification, are permitted provided that the following conditions are met:
518 1. Redistributions of source code must retain the above copyright notice,
519 this list of conditions and the following disclaimer.
520 2. Redistributions in binary form must reproduce the above copyright notice,
521 this list of conditions and the following disclaimer in the documentation
522 and/or other materials provided with the distribution.
524 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
525 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
526 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
527 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
528 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
529 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
530 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
531 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
532 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
533 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
534 POSSIBILITY OF SUCH DAMAGE.
538 Please let me know if you're using the Wikitext extension in your project.
539 If you have any bug reports or feature requests please open a ticket in
540 the issue tracker at https://wincent.com/issues.
544 If you find this extension useful, please consider making a donation via
545 PayPal to win@wincent.com.