Class: RedcarpetManpage::Renderer

Inherits:
Redcarpet::Render::Base
  • Object
show all
Defined in:
lib/redcarpet-manpage.rb

Constant Summary collapse

DEFINITION_INDENT =

two spaces

'  '

Instance Method Summary collapse

Instance Method Details



30
31
32
# File 'lib/redcarpet-manpage.rb', line 30

def autolink(link, link_type)
  emphasis(link)
end

#block_code(code, language) ⇒ Object



20
21
22
# File 'lib/redcarpet-manpage.rb', line 20

def block_code(code, language)
  "\n.nf\n#{normal_text(code)}\n.fi\n"
end

#emphasis(text) ⇒ Object



16
17
18
# File 'lib/redcarpet-manpage.rb', line 16

def emphasis(text)
  "\\fI#{text}\\fP"
end

#header(title, level) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/redcarpet-manpage.rb', line 34

def header(title, level)
  case level
  when 1
    "\n.TH #{title}\n"

  when 2
    "\n.SH #{title}\n"

  when 3
    "\n.SS #{title}\n"
  end
end

#linebreakObject



51
52
53
# File 'lib/redcarpet-manpage.rb', line 51

def linebreak
  "\n.LP\n"
end


26
27
28
# File 'lib/redcarpet-manpage.rb', line 26

def link(link, title, content)
  "#{triple_emphasis(content)} #{emphasis(link)}"
end

#list(content, list_type) ⇒ Object



55
56
57
58
59
60
61
62
# File 'lib/redcarpet-manpage.rb', line 55

def list(content, list_type)
  case list_type
  when :ordered
    "\n\n.nr step 0 1\n#{content}\n"
  when :unordered
    "\n.\n#{content}\n"
  end
end

#list_item(content, list_type) ⇒ Object



64
65
66
67
68
69
70
71
# File 'lib/redcarpet-manpage.rb', line 64

def list_item(content, list_type)
  case list_type
  when :ordered
    ".IP \\n+[step]\n#{content.strip}\n"
  when :unordered
    ".IP \\[bu] 2 \n#{content.strip}\n"
  end
end

#normal_text(text) ⇒ Object



6
7
8
# File 'lib/redcarpet-manpage.rb', line 6

def normal_text(text)
  text.gsub(/(?<=\W)-(?=\W)/, '\\-') if text
end

#paragraph(text) ⇒ Object



47
48
49
# File 'lib/redcarpet-manpage.rb', line 47

def paragraph(text)
  "\n.PP\n#{text}\n"
end

#postprocess(document) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/redcarpet-manpage.rb', line 75

def postprocess document
  document.
    # squeeze blank lines to prevent double-spaced output
    gsub(/^\n/, '').

    # first paragraphs inside list items
    gsub(/^(\.IP.*)\n\.PP/, '\1').

    # paragraphs beginning with bold/italic and followed by
    # at least one definition-indented line are definitions
    gsub(/^\.PP(?=\n\\f.+\n#{DEFINITION_INDENT}\S)/, '.TP').

    # make indented paragraphs occupy less space on screen:
    # roff will fit the second line of the paragraph along
    # side the first line if it has enough room to do so!
    gsub(/^#{DEFINITION_INDENT}(?=\S)/, '').

    # encode references to other man pages as "hyperlinks"
    gsub(/(\w+)(\([1-9nol]\)[[:punct:]]?\s*)/, "\n.BR \\1 \\2\n").
    # keep the SEE ALSO sequence of references in-line
    gsub(/(?:^\.BR.+\n)+/m){ |sequence| sequence.squeeze("\n") }
end

#triple_emphasis(text) ⇒ Object Also known as: double_emphasis



10
11
12
# File 'lib/redcarpet-manpage.rb', line 10

def triple_emphasis(text)
  "\\fB#{text}\\fP"
end