Extended maintenance of Ruby versions 1.8.7 and 1.9.2 will end on July 31, 2014. Read more

In Files

  • rdoc/markup/simple_markup.rb

Files

Class/Module Index [+]

Quicksearch

SM::SimpleMarkup

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_LIST_RE
SIMPLE_LIST_RE

List entries look like:

*       text
1.      text
[label] text
label:: text

Flag it as a list entry, and work out the indent for subsequent lines

SPACE

Public Class Methods

new() click to toggle source

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.

 
               # File rdoc/markup/simple_markup.rb, line 207
def initialize
  @am = AttributeManager.new
  @output = nil
end
            

Public Instance Methods

add_html(tag, name) click to toggle source

Add to the sequences recognized as general markup

 
               # File rdoc/markup/simple_markup.rb, line 225
def add_html(tag, name)
  @am.add_html(tag, name)
end
            
add_special(pattern, name) click to toggle source

Add to other inline sequences. For example, we could add WikiWords using something like:

parser.add_special(/\b([A-Z][a-z]+[A-Z]\w+)/, :WIKIWORD)

Each wiki word will be presented to the output formatter via the accept_special method

 
               # File rdoc/markup/simple_markup.rb, line 239
def add_special(pattern, name)
  @am.add_special(pattern, name)
end
            
add_word_pair(start, stop, name) click to toggle source

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

 
               # File rdoc/markup/simple_markup.rb, line 217
def add_word_pair(start, stop, name)
  @am.add_word_pair(start, stop, name)
end
            
content() click to toggle source

for debugging, we allow access to our line contents as text

 
               # File rdoc/markup/simple_markup.rb, line 464
def content
  @lines.as_text
end
            
convert(str, op) click to toggle source

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

 
               # File rdoc/markup/simple_markup.rb, line 249
def convert(str, op)
  @lines = Lines.new(str.split(/\r?\n/).collect { |aLine| 
                       Line.new(aLine) })
  return "" if @lines.empty?
  @lines.normalize
  assign_types_to_lines
  group = group_lines
  # call the output formatter to handle the result
  #      group.to_a.each {|i| p i}
  group.accept(@am, op)
end
            
get_line_types() click to toggle source

for debugging, return the list of line types

 
               # File rdoc/markup/simple_markup.rb, line 470
def get_line_types
  @lines.line_types
end
            

Commenting is here to help enhance the documentation. For example, code samples, or clarification of the documentation.

If you have questions about Ruby or the documentation, please post to one of the Ruby mailing lists. You will get better, faster, help that way.

If you wish to post a correction of the docs, please do so, but also file bug report so that it can be corrected for the next release. Thank you.

If you want to help improve the Ruby documentation, please visit Documenting-ruby.org.

blog comments powered by Disqus