Module: Md2Man::Roff

Includes:
Document
Included in:
Engine
Defined in:
lib/md2man/roff/engine.rb,
lib/md2man/roff.rb

Defined Under Namespace

Classes: Engine

Constant Summary collapse

ENGINE =
Redcarpet::Markdown.new(Engine,
  :tables => true,
  :autolink => true,
  :superscript => true,
  :strikethrough => true,
  :no_intra_emphasis => false,
  :fenced_code_blocks => true
)

Constants included from Document

Document::PARAGRAPH_INDENT

Instance Method Summary collapse

Methods included from Document

#paragraph

Instance Method Details



181
182
183
# File 'lib/md2man/roff.rb', line 181

def autolink link, link_type
  "\\[la]#{link.chomp}\\[ra]"
end

#block_code(code, language) ⇒ Object



45
46
47
48
# File 'lib/md2man/roff.rb', line 45

def block_code code, language
  code = escape(super, true)
  block_quote "\n.nf\n#{code.chomp}\n.fi\n"
end

#block_html(html) ⇒ Object



54
55
56
# File 'lib/md2man/roff.rb', line 54

def block_html html
  warn "md2man/roff: block_html not implemented: #{html.inspect}"
end

#block_quote(quote) ⇒ Object



50
51
52
# File 'lib/md2man/roff.rb', line 50

def block_quote quote
  "\n.PP\n.RS\n#{remove_leading_pp(quote).chomp}\n.RE\n"
end

#codespan(code) ⇒ Object



165
166
167
168
169
170
# File 'lib/md2man/roff.rb', line 165

def codespan code
  code = escape(super, true)
  # NOTE: this double font sequence gives us the best of both worlds:
  # man(1) shows it in bold and `groff -Thtml` shows it in monospace
  "\\fB\\fC#{code}\\fR"
end

#double_emphasis(text) ⇒ Object



149
150
151
# File 'lib/md2man/roff.rb', line 149

def double_emphasis text
  "\\fB#{text}\\fP"
end

#emphasis(text) ⇒ Object



145
146
147
# File 'lib/md2man/roff.rb', line 145

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

#entity(text) ⇒ Object



201
202
203
204
205
206
207
# File 'lib/md2man/roff.rb', line 201

def entity text
  if unicode = entity_to_unicode(text)
    unicode_to_glyph unicode
  else
    warn "md2man/roff: entity not implemented: #{text.inspect}"
  end
end

#header(text, level, _ = nil) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/md2man/roff.rb', line 58

def header text, level, _=nil
  macro =
    case level
    when 1
      if @h1_seen
        :SH
      else
        @h1_seen = true
        :TH
      end
    when 2 then :SH
    else :SS
    end
  "\n.#{macro} #{text.chomp}\n"
end

#hruleObject



74
75
76
# File 'lib/md2man/roff.rb', line 74

def hrule
  "\n.ti 0\n\\l'\\n(.lu'\n"
end

#image(link, title, alt_text) ⇒ Object



185
186
187
# File 'lib/md2man/roff.rb', line 185

def image link, title, alt_text
  warn "md2man/roff: image not implemented: #{link.inspect}"
end

#indented_paragraph(text) ⇒ Object


block-level processing




33
34
35
# File 'lib/md2man/roff.rb', line 33

def indented_paragraph text
  "\n.IP\n#{text}\n"
end

#linebreakObject



141
142
143
# File 'lib/md2man/roff.rb', line 141

def linebreak
  "\n.br\n"
end


172
173
174
175
176
177
178
179
# File 'lib/md2man/roff.rb', line 172

def link link, title, content
  content + ' ' +
  if link =~ /^mailto:/
    autolink $', :email
  else
    autolink link, :url
  end
end

#list(contents, list_type) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/md2man/roff.rb', line 78

def list contents, list_type
  result = []

  if list_type == :ordered
    result << ".nr step#{@ordered_list_id} 0 1"
    contents.gsub!(/^\.IP \\n\+\[step_tbd\]$/, ".IP \\n+[step#{@ordered_list_id}]")
    @ordered_list_id += 1
  end

  result << ".RS\n#{contents}\n.RE\n"
  result.join("\n")
end

#list_item(text, list_type) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
# File 'lib/md2man/roff.rb', line 91

def list_item text, list_type
  designator =
    case list_type
    when :ordered
      "\\n+[step_tbd]"
    when :unordered
      "\\(bu 2"
    end

  ".IP #{designator}\n#{remove_leading_pp(text).lstrip.chomp}\n"
end

#normal_paragraph(text) ⇒ Object



41
42
43
# File 'lib/md2man/roff.rb', line 41

def normal_paragraph text
  "\n.PP\n#{text}\n"
end

#normal_text(text) ⇒ Object


low-level processing




197
198
199
# File 'lib/md2man/roff.rb', line 197

def normal_text text
  escape text, false if text
end

#postprocess(document) ⇒ Object



18
19
20
21
22
23
24
25
26
27
# File 'lib/md2man/roff.rb', line 18

def postprocess document
  super.

  # strip newlines from the beginning, before the very first roff directive
  lstrip.

  # squeeze newlines between roff directives to prevent double-spaced output
  # and also at the end of the document, after the very last roff directive
  gsub(/(\n){2,}(?=\.|\z)/, '\1')
end

#preprocess(document) ⇒ Object


document-level processing




11
12
13
14
15
16
# File 'lib/md2man/roff.rb', line 11

def preprocess document
  @ordered_list_id = 0
  @table_cells = {}
  @h1_seen = false
  super
end

#raw_html(html) ⇒ Object



189
190
191
# File 'lib/md2man/roff.rb', line 189

def raw_html html
  warn "md2man/roff: raw_html not implemented: #{html.inspect}"
end

#reference(input_match, output_match) ⇒ Object


span-level processing




137
138
139
# File 'lib/md2man/roff.rb', line 137

def reference input_match, output_match
  "\n.BR #{input_match[:page]} (#{input_match[:section]})#{output_match[:addendum]}\n"
end

#strikethrough(text) ⇒ Object



157
158
159
# File 'lib/md2man/roff.rb', line 157

def strikethrough text
  warn "md2man/roff: strikethrough not implemented: #{text.inspect}"
end

#superscript(text) ⇒ Object



161
162
163
# File 'lib/md2man/roff.rb', line 161

def superscript text
  warn "md2man/roff: superscript not implemented: #{text.inspect}"
end

#table(header, body) ⇒ Object



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/md2man/roff.rb', line 103

def table header, body
  head_rows = decode_table_rows(header)
  body_rows = decode_table_rows(body)

  ".TS\nallbox;\n#{
    [
      head_rows.map do |row|
        (['cb'] * row.length).join(TABLE_COL_DELIM)
      end,
      body_rows.map do |row|
        row.map do |content, alignment|
          (alignment || :left).to_s[0,1]
        end.join(TABLE_COL_DELIM)
      end
    ].join(TABLE_ROW_DELIM)
  }\n.\n#{
    (head_rows + body_rows).map do |row|
      row.map(&:first).join(TABLE_CELL_DELIM)
    end.join(TABLE_ROW_DELIM)
  }\n.TE\n"
end

#table_cell(content, alignment) ⇒ Object



129
130
131
# File 'lib/md2man/roff.rb', line 129

def table_cell content, alignment
  encode_table_cell [content, alignment]
end

#table_row(content) ⇒ Object



125
126
127
# File 'lib/md2man/roff.rb', line 125

def table_row content
  encode_table_row content
end

#tagged_paragraph(text) ⇒ Object



37
38
39
# File 'lib/md2man/roff.rb', line 37

def tagged_paragraph text
  "\n.TP\n#{text}\n"
end

#triple_emphasis(text) ⇒ Object



153
154
155
# File 'lib/md2man/roff.rb', line 153

def triple_emphasis text
  "\\s+2#{double_emphasis text}\\s-2"
end