Contents:
Synopsis
This code converts input_string, which is in the format described in markup/simple_markup.rb, to HTML. The conversion takes place in the convert method, so you can use the same SimpleMarkup object to convert multiple input strings.
require 'rdoc/markup/simple_markup' require 'rdoc/markup/simple_markup/to_html' p = SM::SimpleMarkup.new h = SM::ToHtml.new puts p.convert(input_string, h)
You can extend the SimpleMarkup parser to recognise new markup sequences, and to add special processing for text that matches a regular epxression. Here we make WikiWords significant to the parser, and also make the sequences {word} and <no>text…</no> signify strike-through text. When then subclass the HTML output class to deal with these:
require 'rdoc/markup/simple_markup' require 'rdoc/markup/simple_markup/to_html' class WikiHtml < SM::ToHtml def handle_special_WIKIWORD(special) "<font color="red">" + special.text + "</font>" end end p = SM::SimpleMarkup.new p.add_word_pair("{", "}", :STRIKE) p.add_html("no", :STRIKE) p.add_special(/\b([A-Z][a-z]+[A-Z]\w+)/, :WIKIWORD) h = WikiHtml.new h.add_tag(:STRIKE, "<strike>", "</strike>") puts "<body>" + p.convert(ARGF.read, h) + "</body>"
Output Formatters
missing
| Constants | |
|---|---|
| LABEL_ |
|
| SIMPLE_ |
List entries look like: |
| SPACE | |
| Public Methods | |
|---|---|
| add_ |
Add to the sequences recognized as general markup |
| add_ |
Add to other inline sequences. For example, we could add WikiWords using something like: |
| add_ |
Add to the sequences used to add formatting to an individual word (such as bold). Matching entries will generate attibutes that the output formatters can recognize by their name |
| content | for debugging, we allow access to our line contents as text |
| convert | We take a string, split it into lines, work out the type of each line, and from there deduce groups of lines (for example all lines in a paragraph). We then invoke the output formatter using a Visitor to display the result |
| get_ |
for debugging, return the list of line types |
| new | take a block of text and use various heuristics to determine it’s structure (paragraphs, lists, and so on). Invoke an event handler as we identify significant chunks. |
| Private Methods | |
|---|---|
| assign_ |
Look through the text at line indentation. We flag each line as being Blank, a paragraph, a list element, or verbatim text |
| group_ |
Return a block consisting of fragments which are paragraphs, list entries or verbatim text. We merge consecutive lines of the same type and level together. We are also slightly tricky with lists: the lines following a list introduction look like paragraph lines at the next level, and we remap them into list entries instead |
| handled_ |
Handle labeled list entries, We have a special case to deal with. Because the labels can be long, they force the remaining block of text over the to right: |
<code/>and<pre/>for code samples.