class Kramdown::Converter::Man

Converts markdown into a roff man-page.

Constants

GLYPHS
GLYPH_REGEXP

Regular expression to convert unicode characters into glyphs

Comment header

SMART_QUOTES

Smart Quotes and their UTF8 chars

TYPOGRAPHIC_SYMS

Typographic Symbols and their UTF8 chars

Public Class Methods

new(root,options) click to toggle source

Initializes the converter.

@param [Kramdown::Element] root

The root of the markdown document.

@param [Hash] options

Markdown options.
Calls superclass method
# File lib/kramdown/converter/man.rb, line 413
def initialize(root,options)
  super(root,options)

  @ol_index = 0
end

Public Instance Methods

convert(root) click to toggle source

Converts the markdown document into a man-page.

@param [Kramdown::Element] root

The root of a markdown document.

@return [String]

The roff output.
# File lib/kramdown/converter/man.rb, line 428
def convert(root)
  "#{HEADER}\n#{convert_root(root)}"
end
convert_a(a) click to toggle source

Converts a `kd:a` element.

@param [Kramdown::Element] a

A `kd:a` element.

@return [String]

The roff output.
# File lib/kramdown/converter/man.rb, line 761
def convert_a(a)
  href = escape(a.attr['href'])
  text = convert_children(a.children)

  case href
  when /^mailto:/
    email = href[7..-1]

    unless text == email then "#{text}\n.MT #{email}\n.ME"
    else                      "\n.MT #{email}\n.ME"
    end
  when /^man:/
    match = href.match(/man:([A-Za-z0-9_-]+)(?:\((\d[a-z]?)\))?/)

    if match[2] then "\n.BR #{match[1]} (#{match[2]})"
    else             "\n.BR #{match[1]}"
    end
  else
    "#{text}\n.UR #{href}\n.UE"
  end
end
convert_abbreviation(abbr) click to toggle source

Converts a `kd:abbreviation` element.

@param [Kramdown::Element] abbr

A `kd:abbreviation` element.

@return [String]

The roff output.
# File lib/kramdown/converter/man.rb, line 629
def convert_abbreviation(abbr)
  escape(abbr.value)
end
convert_blank(blank) click to toggle source

Converts a `kd:blank` element.

@param [Kramdown::Element] blank

A `kd:blank` element.

@return [String]

The roff output.
# File lib/kramdown/converter/man.rb, line 470
def convert_blank(blank)
  '.LP'
end
convert_blockquote(blockquote) click to toggle source

Converts a `kd:blockquote` element.

@param [Kramdown::Element] blockquote

A `kd:blockquote` element.

@return [String]

The roff output.
# File lib/kramdown/converter/man.rb, line 642
def convert_blockquote(blockquote)
  content = blockquote.children.map { |child|
    case child.type
    when :p then convert_children(child.children)
    else         convert_element(child)
    end
  }.join("\n")

  return ".PP\n.RS\n#{content}\n.RE"
end
convert_children(children) click to toggle source

Converts the children of an element.

@param [Array<Kramdown::Element>] children

The children of an element.

@return [String]

The roff output.
# File lib/kramdown/converter/man.rb, line 792
def convert_children(children)
  children.map { |child| convert_element(child) }.join.strip
end
convert_codeblock(codeblock) click to toggle source

Converts a `kd:codeblock` element.

@param [Kramdown::Element] codeblock

A `kd:codeblock` element.

@return [String]

The roff output.
# File lib/kramdown/converter/man.rb, line 662
def convert_codeblock(codeblock)
  ".nf\n#{escape(codeblock.value).rstrip}\n.fi"
end
convert_codespan(codespan) click to toggle source

Converts a `kd:codespan` element.

@param [Kramdown::Element] codespan

A `kd:codespan` element.

@return [String]

The roff output.
# File lib/kramdown/converter/man.rb, line 748
def convert_codespan(codespan)
  "\\fB#{codespan.value}\\fR"
end
convert_comment(comment) click to toggle source

Converts a `kd:comment` element.

@param [Kramdown::Element] comment

A `kd:comment` element.

@return [String]

The roff output.
# File lib/kramdown/converter/man.rb, line 675
def convert_comment(comment)
  comment.value.lines.map { |line|
    ".\\\" #{line}"
  }.join("\n")
end
convert_element(element) click to toggle source

Converts an element.

@param [Kramdown::Element] element

An arbitrary element within the markdown document.

@return [String]

The roff output.
# File lib/kramdown/converter/man.rb, line 456
def convert_element(element)
  method = "convert_#{element.type}"
  send(method,element) if respond_to?(method)
end
convert_em(em) click to toggle source

Converts a `kd:em` element.

@param [Kramdown::Element] em

A `kd:em` element.

@return [String]

The roff output.
# File lib/kramdown/converter/man.rb, line 722
def convert_em(em)
  "\\fI#{convert_children(em.children)}\\fP"
end
convert_header(header) click to toggle source

Converts a `kd:header` element.

@param [Kramdown::Element] header

A `kd:header` element.

@return [String]

The roff output.
# File lib/kramdown/converter/man.rb, line 522
def convert_header(header)
  text = header.options[:raw_text]

  case header.options[:level]
  when 1 then ".TH #{text}"
  when 2 then ".SH #{text}"
  else        ".SS #{text}"
  end
end
convert_hr(hr) click to toggle source

Converts a `kd:hr` element.

@param [Kramdown::Element] hr

A `kd:hr` element.

@return [String]

The roff output.
# File lib/kramdown/converter/man.rb, line 541
def convert_hr(hr)
  ".ti 0\n\\l'\\n(.lu'"
end
convert_ol(ol) click to toggle source

Converts a `kd:ol` element.

@param [Kramdown::Element] ol

A `kd:ol` element.

@return [String]

The roff output.
# File lib/kramdown/converter/man.rb, line 590
def convert_ol(ol)
  @ol_index += 1

  header  = ".nr step#{@ol_index} 0 1"
  content = ol.children.map { |li| convert_ol_li(li) }.join("\n")

  return "#{header}\n.RS\n#{content}\n.RE"
end
convert_ol_li(li) click to toggle source

Converts a `kd:li` element within a `kd:ol` list.

@param [Kramdown::Element] li

A `kd:li` element.

@return [String]

The roff output.
# File lib/kramdown/converter/man.rb, line 608
def convert_ol_li(li)
  li.children.each_with_index.map { |child,index|
    if child.type == :p
      content = convert_children(child.children)

      if index == 0 then ".IP \\n+[step#{@ol_index}]\n#{content}"
      else               ".IP \\n\n#{content}"
      end
    end
  }.compact.join("\n")
end
convert_p(p) click to toggle source

Converts a `kd:p` element.

@param [Kramdown::Element] p

A `kd:p` element.

@return [String]

The roff output.
# File lib/kramdown/converter/man.rb, line 690
def convert_p(p)
  children = p.children

  if ((children.length >= 2) && (children.first.type == :codespan ||
                                 children.first.type == :em       ||
                                 children.first.type == :strong))
    newline = children.find_index { |el|
      el.type == :text && el.value.start_with?("\n")
    }

    if newline
      first_line = convert_children(children[0...newline])
      rest       = convert_children(children[newline..-1]).strip

      ".TP\n#{first_line}\n#{rest}"
    else
      ".HP\n#{convert_children(children)}"
    end
  else
    ".PP\n#{convert_children(children)}"
  end
end
convert_root(root) click to toggle source

Converts the root of a markdown document.

@param [Kramdown::Element] root

The root of the markdown document.

@return [String]

The roff output.
# File lib/kramdown/converter/man.rb, line 441
def convert_root(root)
  root.children.map { |child|
    convert_element(child)
  }.compact.join("\n")
end
convert_smart_quote(quote) click to toggle source

Converts a `kd:smart_quote` element.

@param [Kramdown::Element] quote

A `kd:smart_quote` element.

@return [String]

The roff output.
# File lib/kramdown/converter/man.rb, line 509
def convert_smart_quote(quote)
  SMART_QUOTES[quote.value]
end
convert_strong(strong) click to toggle source

Converts a `kd:strong` element.

@param [Kramdown::Element] strong

A `kd:strong` element.

@return [String]

The roff output.
# File lib/kramdown/converter/man.rb, line 735
def convert_strong(strong)
  "\\fB#{convert_children(strong.children)}\\fP"
end
convert_text(text) click to toggle source

Converts a `kd:text` element.

@param [Kramdown::Element] text

A `kd:text` element.

@return [String]

The roff output.
# File lib/kramdown/converter/man.rb, line 483
def convert_text(text)
  escape(text.value.gsub(/^(  +|\t)/,''))
end
convert_typographic_sym(sym) click to toggle source

Converts a `kd:typographic_sym` element.

@param [Kramdown::Element] sym

A `kd:typographic_sym` element.

@return [String]

The roff output.
# File lib/kramdown/converter/man.rb, line 496
def convert_typographic_sym(sym)
  TYPOGRAPHIC_SYMS[sym.value]
end
convert_ul(ul) click to toggle source

Converts a `kd:ul` element.

@param [Kramdown::Element] ul

A `kd:ul` element.

@return [String]

The roff output.
# File lib/kramdown/converter/man.rb, line 554
def convert_ul(ul)
  content = ul.children.map { |li| convert_ul_li(li) }.join("\n")

  return ".RS\n#{content}\n.RE"
end
convert_ul_li(li) click to toggle source

Converts a `kd:li` element within a `kd:ul` list.

@param [Kramdown::Element] li

A `kd:li` element.

@return [String]

The roff output.
# File lib/kramdown/converter/man.rb, line 569
def convert_ul_li(li)
  li.children.each_with_index.map { |child,index|
    if child.type == :p
      content = convert_children(child.children)

      if index == 0 then ".IP \\(bu 2\n#{content}"
      else               ".IP \\( 2\n#{content}"
      end
    end
  }.compact.join("\n")
end
escape(text) click to toggle source

Escapes text for roff.

@param [String] text

The unescaped text.

@return [String]

The escaped text.
# File lib/kramdown/converter/man.rb, line 805
def escape(text)
  text.gsub(GLYPH_REGEXP) { |char| GLYPHS[char] }
end