The Textiler class is accessible via the webhelpers.textile module.
This is the base class for the PyTextile text processor.
Instantiate the class, passing the text to be formatted.
Here we pre-process the text and collect all the link lookups for later.
Grab link lookups.
Check the text for link lookups, store them in a dictionary, and clean them up.
Process the text.
Here we actually process the text, splitting the text in blocks and applying the corresponding function to each one of them.
Fix single tags.
Fix tags like <img />, <br /> and <hr />.
--- h1. Sanitizing
Textile can help you generate valid XHTML(eXtensible HyperText Markup Language). It will fix any single tags that are not properly closed, like @<img />@, @<br />@ and @<hr />@.
If you have "mx.Tidy":http://www.egenix.com/files/python/mxTidy.html and/or "µTidyLib":http://utidylib.sourceforge.net/ installed, it also can optionally validade the generated code with these wrappers to ensure 100% valid XHTML(eXtensible HyperText Markup Language).
Process the blocks from the text.
Split the blocks according to the signatures, join extended blocks and associate each one of them with a function to process them.
--- h1. Blocks
Textile process your text by dividing it in blocks. Each block is identified by a signature and separated from other blocks by an empty line.
All signatures should end with a period followed by a space. A header @<h1></h1>@ can be done this way:
pre. h1. This is a header 1.
Blocks may continue for multiple paragraphs of text. If you want a block signature to stay "active", use two periods after the signature instead of one. For example:
pre.. bq.. This is paragraph one of a block quote.
This is paragraph two of a block quote.
=p. Now we're back to a regular paragraph.
pre.. <blockquote> <p>This is paragraph one of a block quote.</p>
<p>This is paragraph two of a block quote.</p> </blockquote>
<p>Now we’re back to a regular paragraph.</p>
p. The blocks can be customised by adding parameters between the signature and the period. These include:
dl. {style rule}:A CSS(Cascading Style Sheets) style rule. [ll]:A language identifier (for a "lang" attribute). (class) or (#id) or (class#id):For CSS(Cascading Style Sheets) class and id attributes. >, <, =, <>:Modifier characters for alignment. Right-justification, left-justification, centered, and full-justification. The paragraph will also receive the class names "right", "left", "center" and "justify", respectively. ( (one or more):Adds padding on the left. 1em per "(" character is applied. When combined with the align-left or align-right modifier, it makes the block float. ) (one or more):Adds padding on the right. 1em per ")" character is applied. When combined with the align-left or align-right modifier, it makes the block float.
Here's an overloaded example:
pre. p(())>(class#id)[en]{color:red}. A simple paragraph.
Becomes:
pre. <p lang="en" style="color:red;padding-left:2em;padding-right:2em;float:right;" class="class right" id="id">A simple paragraph.</p>
Parse the parameters from a block signature.
This function parses the parameters from a block signature, splitting the information about class, id, language and style. The positioning (indentation and alignment) is parsed and stored in the style.
A paragraph like:
p>(class#id){color:red}[en]. Paragraph.
or:
p{color:red}[en](class#id)>. Paragraph.
will have its parameters parsed to:
- output = {'lang' : 'en',
- 'class': 'class', 'id' : 'id', 'style': 'color:red;text-align:right;'}
Note that order is not important.
Build the open tag with specified attributes.
This function is used by all block builders to generate the opening tags with the attributes of the block.
Process a paragraph.
This function processes the paragraphs, enclosing the text in a <p> tag and breaking lines with <br />. Paragraphs are formatted with all the inline rules.
--- h1. Paragraph
This is how you write a paragraph:
pre. p. This is a paragraph, although a short one.
Since the paragraph is the default block, you can safely omit its signature ([@p@]). Simply write:
pre. This is a paragraph, although a short one.
Text in a paragraph block is wrapped in @<p></p>@ tags, and newlines receive a <br /> tag. In both cases Textile will process the text to:
pre. <p>This is a paragraph, although a short one.</p>
Text in a paragraph block is processed with all the inline rules.
Process pre-formatted text.
This function processes pre-formatted text into a <pre> tag. No HTML is added for the lines, but @<@ and @>@ are translated into HTML entities.
--- h1. Pre-formatted text
Pre-formatted text can be specified using the @pre@ signature. Inside a "pre" block, whitespace is preserved and @<@ and @>@ are translated into HTML(HyperText Markup Language) entities automatically.
Text in a "pre" block is _not processed_ with any inline rule.
Here's a simple example:
pre. pre. This text is pre-formatted. Nothing interesting happens inside here...
Will become:
pre. <pre> This text is pre-formatted. Nothing interesting happens inside here... </pre>
Process block code.
This function processes block code into a <code> tag inside a <pre>. No HTML is added for the lines, but @<@ and @>@ are translated into HTML entities.
--- h1. Block code
A block code, specified by the @bc@ signature, is a block of pre-formatted text which also receives a @<code></code>@ tag. As with "pre", whitespace is preserved and @<@ and @>@ are translated into HTML(HyperText Markup Language) entities automatically.
Text in a "bc" code is _not processed_ with the inline rules.
If you have "Twisted":http://www.twistedmatrix.com/ installed, Textile can automatically colorize your Python code if you specify its language as "Python":
pre. bc[python]. from twisted.python import htmlizer
This will become:
pre. <pre> <code lang="python"> <span class="py-src-keyword">from</span> <span class="py-src-variable">twisted</span><span class="py-src-op">.</span><span class="py-src-variable">python</span> <span class="py-src-keyword">import</span> <span class="py-src-variable">htmlizer</span> </code> </pre>
The colors can be specified in your CSS(Cascading Style Sheets) file. If you don't want to install Twisted, you can download just the @htmlizer@ module "independently":http://dealmeida.net/code/htmlizer.py.txt.
Process definition list.
This function process definition lists. The text inside the <dt> and <dd> tags is processed for inline formatting.
--- h1. Definition list
A definition list starts with the signature @dl@, and has its items separated by a @:@. Here's a simple example:
pre. dl. name:Sir Lancelot of Camelot. quest:To seek the Holy Grail. color:Blue.
Becomes:
pre. <dl> <dt>name</dt> <dd>Sir Lancelot of Camelot.</dd> <dt>quest</dt> <dd>To seek the Holy Grail.</dd> <dt>color</dt> <dd>Blue.</dd> </dl>
Process block quote.
The block quote is inserted into a <blockquote> tag, and processed as a paragraph. An optional cite attribute can be appended on the last line after two dashes (--), or after the period following ':' for compatibility with the Perl version.
--- h1. Blockquote
A blockquote is denoted by the signature @bq@. The text in this block will be enclosed in @<blockquote></blockquote>@ and @<p></p>@, receiving the same formatting as a paragraph. For example:
pre. bq. This is a blockquote.
Becomes:
pre. <blockquote> <p>This is a blockquote.</p> </blockquote>
You can optionally specify the @cite@ attribute of the blockquote, using the following syntax:
pre. bq.:http://example.com Some text.
pre. bq.:"John Doe" Some other text.
Becomes:
pre. <blockquote cite="http://example.com"> <p>Some text.</p> </blockquote>
pre. <blockquote cite="John Doe"> <p>Some other text.</p> </blockquote>
You can also specify the @cite@ using a pair of dashes on the last line of the blockquote:
pre. bq. Some text. -- http://example.com
Process a header.
The header number is captured by the regular expression and lives in header. If head_offset is set, it is adjusted accordingly.
--- h1. Header
A header is produced by the signature @hn@, where @n@ goes from 1 to 6. You can adjust the relative output of the headers passing a @head_offset@ attribute when calling @textile()@.
To make a header:
pre. h1. This is a header.
Becomes:
pre. <h1>This is a header.</h1>
Process a footnote.
A footnote is formatted as a paragraph of class 'footnote' and id 'fn%d', starting with the footnote number in a <sup> tag. Here we just build the attributes and pass them directly to self.paragraph().
--- h1. Footnote
A footnote is produced by the signature @fn@ followed by a number. Footnotes are paragraphs of a special CSS(Cascading Style Sheets) class. An example:
pre. fn1. This is footnote number one.
Will produce this:
pre. <p class="footnote" id="fn1"><sup>1</sup> This is footnote number one.</p>
This footnote can be referenced anywhere on the text by the following way:
pre. This is a reference[1] to footnote number one.
Which becomes:
pre. <p>This is a reference<sup class="footnote"><a href="#fn1" title="This is footnote number one.">1</a></sup> to footnote number 1.</p>
Note that the text from the footnote appears in the @title@ of the link pointing to it.
Build the list item.
This function build the list item of an (un)ordered list. It works by peeking at the next list item, and searching for a multi-list. If a multi-list is found, it is processed and appended inside the list item tags, as it should be.
Build an ordered list.
This function basically just sets the <ol></ol> with the right attributes, and then pass everything inside to _build_li, which does the real tough recursive job.
--- h1. Ordered lists
Ordered lists can be constructed this way:
pre. # Item number 1. # Item number 2. # Item number 3.
And you get:
pre. <ol> <li>Item number 1.</li> <li>Item number 2.</li> <li>Item number 3.</li> </ol>
If you want a list to "break" an extended block, you should add a period after the hash. This is useful for writing Python code:
pre.. bc[python].. #!/usr/bin/env python
# This is a comment, not an ordered list! # So this won't break the extended "bc".
pre. # Item number 1. ## Item number 1a. ## Item number 1b. # Item number 2. ## Item number 2a.
Textile will transform this to:
pre. <ol> <li>Item number 1. <ol> <li>Item number 1a.</li> <li>Item number 1b.</li> </ol> </li> <li>Item number 2. <ol> <li>Item number 2a.</li> </ol> </li> </ol>
You can also mix ordered and unordered lists:
pre. * To write well you need: *# to read every day *# to write every day *# and X
You'll get this:
pre. <ul> <li>To write well you need: <ol> <li>to read every day</li> <li>to write every day</li> <li>and X</li> </ol> </li> </ul>
To style a list, the parameters should go before the hash if you want to set the attributes on the @<ol>@ tag:
pre. (class#id)# one # two # three
If you want to customize the firsr @<li>@ tag, apply the parameters after the hash:
pre. #(class#id) one # two # three
Build an unordered list.
This function basically just sets the <ul></ul> with the right attributes, and then pass everything inside to _build_li, which does the real tough recursive job.
--- h1. Unordered lists
Unordered lists behave exactly like the ordered lists, and are defined using a star:
pre. * Python * Perl * PHP
Becomes:
pre. <ul> <li>Python</li> <li>Perl</li> <li><span class="caps">PHP</span></li> </ul>
Build a table.
To build a table we split the text in lines to get the rows, and split the rows between '|' to get the individual cells.
--- h1. Tables
Making a simple table is as easy as possible:
Will be processed into:
pre. <table> <tr> <td>a</td> <td>b</td> <td>c</td> </tr> <tr> <td>1</td> <td>2</td> <td>3</td> </tr> </table>
If you want to customize the @<table>@ tag, you must use the @table@ signature:
pre. table(class#id)[en]. |a|b|c| |1|2|3|
To customize a row, apply the modifier _before_ the first @|@:
pre. table. (class)<>|a|b|c| |1|2|3|
Individual cells can by customized by adding the parameters _after_ the @|@, proceded by a period and a space:
pre. |(#id). a|b|c| |1|2|3|
The allowed modifiers are:
dl. {style rule}:A CSS(Cascading Style Sheets) style rule. (class) or (#id) or (class#id):A CSS(Cascading Style Sheets) class and/or id attribute. ( (one or more):Adds 1em of padding to the left for each '(' character. ) (one or more):Adds 1em of padding to the right for each ')' character. <:Aligns to the left (floats to left for tables if combined with the ')' modifier). >:Aligns to the right (floats to right for tables if combined with the '(' modifier). =:Aligns to center (sets left, right margins to 'auto' for tables). <>:For cells only. Justifies text. ^:For rows and cells only. Aligns to the top. ~ (tilde):For rows and cells only. Aligns to the bottom. _ (underscore):Can be applied to a table row or cell to indicate a header row or cell. 2 or 3 or 4, etc.:Used within cells to indicate a colspan of 2, 3, 4, etc. columns. When you see "", think "push forward". /2 or /3 or /4, etc.:Used within cells to indicate a rowspan of 2, 3, 4, etc. rows. When you see "/", think "push downward".
When a cell is identified as a header cell and an alignment is specified, that becomes the default alignment for cells below it. You can always override this behavior by specifying an alignment for one of the lower cells.
Do nothing.
This is used to match escaped text. Nothing to see here!
--- h1. Escaping
If you don't want Textile processing a block, you can simply enclose it inside @==@:
pre. p. Regular paragraph
pre. == Escaped portion -- will not be formatted by Textile at all ==
pre. p. Back to normal.
This can also be used inline, disabling the formatting temporarily:
pre. p. This is ==*a test*== of escaping.
Convert itex to MathML.
If the itex2mml binary is set, we use it to convert the itex to MathML. Otherwise, the text is unprocessed and return as is.
--- h1. itex
Textile can automatically convert itex code to MathML(Mathematical Markup Language) for you, if you have the itex2MML binary (you can download it from the "Movable Type plugin":http://golem.ph.utexas.edu/~distler/blog/files/itexToMML.tar.gz).
Block equations should be enclosed inbetween @[@ and @]@:
pre. [ e^{ipi} + 1 = 0 ]
Will be translated to:
pre. <math xmlns='http://www.w3.org/1998/Math/MathML' mode='display'> <msup><mi>e</mi> <mrow><mi>i</mi> <mi>&pi;</mi></mrow></msup> <mo>+</mo><mn>1</mn><mo>=</mo><mn>0</mn> </math>
Equations can also be displayed inline:
pre. Euler's formula, $e^{ipi}+1=0$, ...
(Note that if you want to display MathML(Mathematical Markup Language) your content must be served as @application/xhtml+xml@, which is not accepted by all browsers.)
Show PyTextile's functionalities.
An introduction to PyTextile. Can be called when running the main script or if you write the following line:
'tell me about textile.'
But keep it a secret!
Process acronyms.
Acronyms can have letters in upper and lower caps, or even numbers, provided that the numbers and upper caps are the same in the abbreviation and in the description. For example:
XHTML(eXtensible HyperText Markup Language) OPeNDAP(Open source Project for a Network Data Access Protocol) L94(Levitus 94)
are all valid acronyms.
--- h1. Acronyms
You can define acronyms in your text the following way:
pre. This is XHTML(eXtensible HyperText Markup Language).
The resulting code is:
pre. <p><acronym title="eXtensible HyperText Markup Language"><span class="caps">XHTML</span></acronym></p>
Acronyms can have letters in upper and lower caps, or even numbers, provided that the numbers and upper caps are the same in the abbreviation and in the description. For example:
pre. XHTML(eXtensible HyperText Markup Language) OPeNDAP(Open source Project for a Network Data Access Protocol) L94(Levitus 94)
are all valid acronyms.
Add titles to footnotes references.
This function searches for footnotes references like this [1], and adds a title to the link containing the first paragraph of the footnote.
Quick macros.
This function replaces macros inside brackets using a built-in dictionary, and also unicode names if the key doesn't exist.
--- h1. Macros
Textile has support for character macros, which should be enclosed in curly braces. A few useful ones are:
pre. {C=} or {=C}: euro sign {+-} or {-+}: plus-minus sign {L-} or {-L}: pound sign.
You can also make accented characters:
pre. Expos{e'}
Becomes:
pre. <p>Expos&#233;</p>
You can also specify Unicode names like:
pre. {umbrella} {white smiling face}
Glyph formatting.
This function replaces quotations marks, dashes and a few other symbol for numerical entities. The em/en dashes use definitions comes from http://alistapart.com/articles/emen/.
--- h1. Glyphs
Textile replaces some of the characters in your text with their equivalent numerical entities. These include:
Quick tags formatting.
This function does the inline formatting of text, like bold, italic, strong and also itex code.
--- h1. Quick tags
Quick tags allow you to format your text, making it bold, emphasized or small, for example. The quick tags operators include:
dl. ==*strong*==:Translates into @<strong>strong</strong>@. ==_emphasis_==:Translates into @<em>emphasis</em>@. ==**bold**==:Translates into @<b>bold</b>@. ==__italics__==:Translates into @<i>italics</i>@. ==++bigger++==:Translates into @<big>bigger</big>@. ==--smaller--==:Translates into: @<small>smaller</small>@. ==-deleted text-==:Translates into @<del>deleted text</del>@. ==+inserted text+==:Translates into @<ins>inserted text</ins>@. ==^superscript^==:Translates into @<sup>superscript</sup>@. ==~subscript~==:Translates into @<sub>subscript</sub>@. ==%span%==:Translates into @<span>span</span>@. ==@code@==:Translates into @<code>code</code>@.
Note that within a "==@==...==@==" section, @<@ and @>@ are translated into HTML entities automatically.
Inline formatting operators accept the following modifiers:
dl. {style rule}:A CSS(Cascading Style Sheets) style rule. [ll]:A language identifier (for a "lang" attribute). (class) or (#id) or (class#id):For CSS(Cascading Style Sheets) class and id attributes.
Process images.
This function process images tags, with or without links. Images can have vertical and/or horizontal alignment, and can be resized unefficiently using width and height tags.
--- h1. Images
An image is generated by enclosing the image source in @!@:
pre. !/path/to/image!
You may optionally specify an alternative text for the image, which will also be used as its title:
pre. !image.jpg (Nice picture)!
Becomes:
pre. <p><img src="image.jpg" alt="Nice picture" title="Nice picture" /></p>
If you want to make the image point to a link, simply append a comma and the URL(Universal Republic of Love) to the image:
pre. !image.jpg!:http://diveintopython.org
Images can also be resized. These are all equivalent:
pre. !image.jpg 10x20! !image.jpg 10w 20h! !image.jpg 20h 10w!
The image @image.jpg@ will be resized to width 10 and height 20.
Modifiers to the @<img>@ tag go after the opening @!@:
pre. !(class#id)^image.jpg!
Allowed modifiers include:
dl. <:Align the image to the left (causes the image to float if CSS options are enabled). >:Align the image to the right (causes the image to float if CSS options are enabled). - (dash):Aligns the image to the middle. ^:Aligns the image to the top. ~ (tilde):Aligns the image to the bottom. {style rule}:Applies a CSS style rule to the image. (class) or (#id) or (class#id):Applies a CSS class and/or id to the image. ( (one or more):Pads 1em on the left for each '(' character. ) (one or more):Pads 1em on the right for each ')' character.
Images receive the class "top" when using top alignment, "bottom" for bottom alignment and "middle" for middle alignment.
Process each image.
This method builds the <img> tag for each image in the text. It's separated from the 'images' method so it can be easily overriden when subclassing Textiler. Useful if you want to download and/or process the images, for example.
Process links.
This function is responsible for processing links. It has some nice shortcuts to Google, Amazon and IMDB queries.
--- h1. Links
A links is done the following way:
pre. "This is the text link":http://example.com
The result from this markup is:
pre. <p><a href="http://example.com">This is the text link</a></p>
You can add an optional @title@ attribute:
pre. "This is the text link(This is the title)":http://example.com
The link can be customised as well:
pre. "(nospam)E-mail me please":mailto:someone@example.com
You can use either single or double quotes. They must be enclosed in whitespace, punctuation or brackets:
pre. You["gotta":http://example.com]seethis!
If you are going to reference the same link a couple of times, you can define a lookup list anywhere on your document:
pre. [python]http://www.python.org
Links to the Python website can then be defined the following way:
pre. "Check this":python
There are also shortcuts for Amazon, IMDB(Internet Movie DataBase) and Google queries:
pre. "Has anyone seen this guy?":imdb:Stephen+Fry "Really nice book":amazon:Goedel+Escher+Bach "PyBlosxom":google ["Using Textile and Blosxom with Python":google:python blosxom textile]
Becomes:
pre. <a href="http://www.imdb.com/Find?for=Stephen+Fry">Has anyone seen this guy?</a> <a href="http://www.amazon.com/exec/obidos/external-search?index=blended&keyword=Goedel+Escher+Bach">Really nice book</a> <a href="http://www.google.com/search?q=PyBlosxom">PyBlosxom</a> <a href="http://www.google.com/search?q=python+blosxom+textile">Using Textile and Blosxom with Python</a>
Text formatting.
This function basically defines the order on which the formatting is applied.
Inline formatting.
This function calls the formatting on the inline text, taking care to avoid the escaped parts.
--- h1. Inline
Inline formatting is applied within a block of text.
See the source for more information.