class PseudoHiki::Format

This class provides class methods for converting texts written in a Hiki-like notation into HTML or other formats.

Public Class Methods

format(hiki_data, format_type, options=nil, auto_linker=BlockParser.auto_linker, &block) click to toggle source

Converts <hiki_data> into a format specified by <format_type>

<hiki_data> should be a string or an array of strings

Options for <format_type> are:

:html

HTML4.1

:xhtml

XHTML1.0

:html5

HTML5

:plain

remove all of tags. certain information such as urls in link tags does not appear in the output

:plain_verbose

similar to :plain, but certain information such as urls in link tags will be kept in the output

:markdown

Markdown

:gfm

GitHub Flavored Markdown

# File lib/pseudohikiparser.rb, line 65
def self.format(hiki_data, format_type, options=nil,
                auto_linker=BlockParser.auto_linker, &block)
  tree = BlockParser.parse(hiki_data, auto_linker)
  formatter = select_formatter(format_type, options)

  if @html_types.include? format_type
    formatter.format(tree, { :auto_link_in_verbatim => auto_link_url?(auto_linker) })
  else
    formatter.format(tree)
  end.tap do |formatted|
    block.call(formatted) if block
  end.to_s
end
to_gfm(hiki_data, auto_linker=BlockParser.auto_linker, &block) click to toggle source

Converts <hiki_data> into GitHub Flavored Markdown

# File lib/pseudohikiparser.rb, line 148
def self.to_gfm(hiki_data, auto_linker=BlockParser.auto_linker, &block)
  format(hiki_data, :gfm, nil, auto_linker, &block)
end
to_html(hiki_data, auto_linker=BlockParser.auto_linker, &block) click to toggle source

Converts <hiki_data> into HTML4.1

When you give a block to this method, a tree of HtmlElement objects is passed as the parameter to the block, so you can traverse it, as in the following example:

hiki = <<HIKI
!! heading

paragraph 1 that contains [[a link to a html file|http://www.example.org/example.html]]

paragraph 2 that contains [[a link to a pdf file|http://www.example.org/example.pdf]]
HIKI

html_str = PseudoHiki::Format.to_html(hiki) do |html|
  html.traverse do |elm|
    if elm.kind_of? HtmlElement and elm.tagname == "a"
      elm["class"] = "pdf" if /\.pdf\Z/o =~ elm["href"]
    end
  end
end

and the value of html_str is

<div class="section h2">
<h2> heading
</h2>
<p>
paragraph 1 that contains <a href="http://www.example.org/example.html">a link to a html file</a>
</p>
<p>
paragraph 2 that contains <a class="pdf" href="http://www.example.org/example.pdf">a link to a pdf file</a>
</p>
<!-- end of section h2 -->
</div>
# File lib/pseudohikiparser.rb, line 114
def self.to_html(hiki_data, auto_linker=BlockParser.auto_linker, &block)
  format(hiki_data, :html, nil, auto_linker, &block)
end
to_html5(hiki_data, auto_linker=BlockParser.auto_linker, &block) click to toggle source

Converts <hiki_data> into HTML5

You can give a block to this method as in the case of ::to_html, but the parameter to the block is a tree of Xhtml5Element objects

# File lib/pseudohikiparser.rb, line 130
def self.to_html5(hiki_data, auto_linker=BlockParser.auto_linker, &block)
  format(hiki_data, :html5, nil, auto_linker, &block)
end
to_markdown(hiki_data, auto_linker=BlockParser.auto_linker, &block) click to toggle source

Converts <hiki_data> into Markdown

# File lib/pseudohikiparser.rb, line 142
def self.to_markdown(hiki_data, auto_linker=BlockParser.auto_linker, &block)
  format(hiki_data, :markdown, nil, auto_linker, &block)
end
to_plain(hiki_data, auto_linker=BlockParser.auto_linker, &block) click to toggle source

Converts <hiki_data> into plain texts without tags

# File lib/pseudohikiparser.rb, line 136
def self.to_plain(hiki_data, auto_linker=BlockParser.auto_linker, &block)
  format(hiki_data, :plain, nil, auto_linker, &block)
end
to_xhtml(hiki_data, auto_linker=BlockParser.auto_linker, &block) click to toggle source

Converts <hiki_data> into XHTML1.0

You can give a block to this method as in the case of ::to_html, but the parameter to the block is a tree of XhtmlElement objects

# File lib/pseudohikiparser.rb, line 122
def self.to_xhtml(hiki_data, auto_linker=BlockParser.auto_linker, &block)
  format(hiki_data, :xhtml, nil, auto_linker, &block)
end

Private Class Methods

select_formatter(format_type, options) click to toggle source
# File lib/pseudohikiparser.rb, line 37
def self.select_formatter(format_type, options)
  if options
    @formatter[[format_type, options]] ||= @type_to_formatter[format_type].create(options)
  else
    @formatter[@preset_options[format_type]]
  end
end