class Kramdown::Converter::Man
Converts a Kramdown::Document
to a manpage in groff format. See man(7), groff_man(7) and man-pages(7) for information regarding the output.
Constants
- TABLE_CELL_ALIGNMENT
- TYPOGRAPHIC_SYMS_MAP
Private Instance Methods
convert_a(el, opts)
click to toggle source
# File lib/kramdown/converter/man.rb 190 def convert_a(el, opts) 191 if el.children.size == 1 && el.children[0].type == :text && 192 el.attr['href'] == el.children[0].value 193 newline(opts[:result]) << macro("UR", escape(el.attr['href'])) << macro("UE") 194 elsif el.attr['href'].start_with?('mailto:') 195 newline(opts[:result]) << macro("MT", escape(el.attr['href'].sub(/^mailto:/, ''))) << 196 macro("UE") 197 else 198 newline(opts[:result]) << macro("UR", escape(el.attr['href'])) 199 inner(el, opts) 200 newline(opts[:result]) << macro("UE") 201 end 202 end
convert_abbreviation(el, opts)
click to toggle source
# File lib/kramdown/converter/man.rb 228 def convert_abbreviation(el, opts) 229 opts[:result] << escape(el.value) 230 end
convert_blank(*)
click to toggle source
# File lib/kramdown/converter/man.rb 46 def convert_blank(*) 47 end
Also aliased as: convert_hr, convert_xml_pi
convert_blockquote(el, opts)
click to toggle source
# File lib/kramdown/converter/man.rb 94 def convert_blockquote(el, opts) 95 opts[:result] << macro("RS") 96 inner(el, opts) 97 opts[:result] << macro("RE") 98 end
convert_br(_el, opts)
click to toggle source
# File lib/kramdown/converter/man.rb 224 def convert_br(_el, opts) 225 newline(opts[:result]) << macro("br") 226 end
convert_codeblock(el, opts)
click to toggle source
# File lib/kramdown/converter/man.rb 88 def convert_codeblock(el, opts) 89 opts[:result] << macro("sp") << macro("RS", 4) << macro("EX") 90 opts[:result] << newline(escape(el.value, true)) 91 opts[:result] << macro("EE") << macro("RE") 92 end
convert_codespan(el, opts)
click to toggle source
# File lib/kramdown/converter/man.rb 220 def convert_codespan(el, opts) 221 opts[:result] << "\\fB#{escape(el.value)}\\fP" 222 end
convert_dd(el, opts)
click to toggle source
# File lib/kramdown/converter/man.rb 126 def convert_dd(el, opts) 127 inner(el, opts, :first) 128 if el.children.size > 1 129 opts[:result] << macro("RS") 130 inner(el, opts, :rest) 131 opts[:result] << macro("RE") 132 end 133 opts[:result] << macro("sp") if opts[:next] && opts[:next].type == :dd 134 end
convert_dt(el, opts)
click to toggle source
# File lib/kramdown/converter/man.rb 120 def convert_dt(el, opts) 121 opts[:result] << macro(opts[:prev] && opts[:prev].type == :dt ? "TQ" : "TP") 122 inner(el, opts) 123 opts[:result] << "\n" 124 end
convert_em(el, opts)
click to toggle source
# File lib/kramdown/converter/man.rb 208 def convert_em(el, opts) 209 opts[:result] << '\fI' 210 inner(el, opts) 211 opts[:result] << '\fP' 212 end
convert_entity(el, opts)
click to toggle source
# File lib/kramdown/converter/man.rb 254 def convert_entity(el, opts) 255 opts[:result] << unicode_char(el.value.code_point) 256 end
convert_footnote(*)
click to toggle source
# File lib/kramdown/converter/man.rb 240 def convert_footnote(*) 241 warning("Footnotes are not supported") 242 end
convert_header(el, opts)
click to toggle source
# File lib/kramdown/converter/man.rb 60 def convert_header(el, opts) 61 return unless opts[:parent].type == :root 62 case el.options[:level] 63 when 1 64 unless @title_done 65 @title_done = true 66 data = el.options[:raw_text].scan(/([^(]+)\s*\((\d\w*)\)(?:\s*-+\s*(.*))?/).first || 67 el.options[:raw_text].scan(/([^\s]+)\s*(?:-*\s+)?()(.*)/).first 68 return unless data && data[0] 69 name = data[0] 70 section = (data[1].to_s.empty? ? el.attr['data-section'] || '7' : data[1]) 71 description = (data[2].to_s.empty? ? nil : " - #{data[2]}") 72 date = el.attr['data-date'] ? quote(el.attr['data-date']) : nil 73 extra = (el.attr['data-extra'] ? quote(escape(el.attr['data-extra'].to_s)) : nil) 74 opts[:result] << macro("TH", quote(escape(name.upcase)), quote(section), date, extra) 75 if description 76 opts[:result] << macro("SH", "NAME") << escape("#{name}#{description}") << "\n" 77 end 78 end 79 when 2 80 opts[:result] << macro("SH", quote(escape(el.options[:raw_text]))) 81 when 3 82 opts[:result] << macro("SS", quote(escape(el.options[:raw_text]))) 83 else 84 warning("Header levels greater than three are not supported") 85 end 86 end
convert_html_element(*)
click to toggle source
# File lib/kramdown/converter/man.rb 181 def convert_html_element(*) 182 warning("HTML elements are not supported") 183 end
convert_img(_el, _opts)
click to toggle source
# File lib/kramdown/converter/man.rb 204 def convert_img(_el, _opts) 205 warning("Images are not supported") 206 end
convert_li(el, opts)
click to toggle source
# File lib/kramdown/converter/man.rb 109 def convert_li(el, opts) 110 sym = (opts[:parent].type == :ul ? '\(bu' : "#{opts[:index] + 1}.") 111 opts[:result] << macro("IP", sym, 4) 112 inner(el, opts, :first) 113 if el.children.size > 1 114 opts[:result] << macro("RS") 115 inner(el, opts, :rest) 116 opts[:result] << macro("RE") 117 end 118 end
convert_math(el, opts)
click to toggle source
# File lib/kramdown/converter/man.rb 232 def convert_math(el, opts) 233 if el.options[:category] == :block 234 convert_codeblock(el, opts) 235 else 236 convert_codespan(el, opts) 237 end 238 end
convert_p(el, opts)
click to toggle source
# File lib/kramdown/converter/man.rb 51 def convert_p(el, opts) 52 if (opts[:index] != 0 && opts[:prev].type != :header) || 53 (opts[:parent].type == :blockquote && opts[:index] == 0) 54 opts[:result] << macro("P") 55 end 56 inner(el, opts) 57 newline(opts[:result]) 58 end
convert_raw(*)
click to toggle source
# File lib/kramdown/converter/man.rb 244 def convert_raw(*) 245 warning("Raw content is not supported") 246 end
convert_root(el, opts)
click to toggle source
# File lib/kramdown/converter/man.rb 39 def convert_root(el, opts) 40 @title_done = false 41 opts[:result] = +".\\\" generated by kramdown\n" 42 inner(el, opts) 43 opts[:result] 44 end
convert_smart_quote(el, opts)
click to toggle source
# File lib/kramdown/converter/man.rb 258 def convert_smart_quote(el, opts) 259 opts[:result] << unicode_char(::Kramdown::Utils::Entities.entity(el.value.to_s).code_point) 260 end
convert_strong(el, opts)
click to toggle source
# File lib/kramdown/converter/man.rb 214 def convert_strong(el, opts) 215 opts[:result] << '\fB' 216 inner(el, opts) 217 opts[:result] << '\fP' 218 end
convert_table(el, opts)
click to toggle source
# File lib/kramdown/converter/man.rb 138 def convert_table(el, opts) 139 opts[:alignment] = el.options[:alignment].map {|a| TABLE_CELL_ALIGNMENT[a] } 140 table_options = ["box"] 141 table_options << "center" if el.attr['class']&.match?(/\bcenter\b/) 142 opts[:result] << macro("TS") << "#{table_options.join(' ')} ;\n" 143 inner(el, opts) 144 opts[:result] << macro("TE") << macro("sp") 145 end
convert_tbody(el, opts)
click to toggle source
# File lib/kramdown/converter/man.rb 153 def convert_tbody(el, opts) 154 opts[:result] << ".T&\n" if opts[:index] != 0 155 opts[:result] << opts[:alignment].join(' ') << " .\n" 156 inner(el, opts) 157 opts[:result] << (opts[:next].type == :tfoot ? "=\n" : "_\n") if opts[:next] 158 end
convert_td(el, opts)
click to toggle source
# File lib/kramdown/converter/man.rb 169 def convert_td(el, opts) 170 result = opts[:result] 171 opts[:result] = +'' 172 inner(el, opts) 173 if opts[:result].include?("\n") 174 warning("Table cells using links are not supported") 175 result << "\t" 176 else 177 result << opts[:result] << "\t" 178 end 179 end
convert_text(el, opts)
click to toggle source
# File lib/kramdown/converter/man.rb 248 def convert_text(el, opts) 249 text = escape(el.value) 250 text.lstrip! if opts[:result][-1] == "\n" 251 opts[:result] << text 252 end
convert_tfoot(el, opts)
click to toggle source
# File lib/kramdown/converter/man.rb 160 def convert_tfoot(el, opts) 161 inner(el, opts) 162 end
convert_thead(el, opts)
click to toggle source
# File lib/kramdown/converter/man.rb 147 def convert_thead(el, opts) 148 opts[:result] << opts[:alignment].map {|a| "#{a}b" }.join(' ') << " .\n" 149 inner(el, opts) 150 opts[:result] << "=\n" 151 end
convert_tr(el, opts)
click to toggle source
# File lib/kramdown/converter/man.rb 164 def convert_tr(el, opts) 165 inner(el, opts) 166 opts[:result] << "\n" 167 end
convert_typographic_sym(el, opts)
click to toggle source
# File lib/kramdown/converter/man.rb 267 def convert_typographic_sym(el, opts) 268 opts[:result] << TYPOGRAPHIC_SYMS_MAP[el.value] 269 end
convert_ul(el, opts)
click to toggle source
# File lib/kramdown/converter/man.rb 100 def convert_ul(el, opts) 101 compact = (el.attr['class'] =~ /\bcompact\b/) 102 opts[:result] << macro("sp") << macro("PD", 0) if compact 103 inner(el, opts) 104 opts[:result] << macro("PD") if compact 105 end
Also aliased as: convert_dl, convert_ol
convert_xml_comment(el, opts)
click to toggle source
# File lib/kramdown/converter/man.rb 185 def convert_xml_comment(el, opts) 186 newline(opts[:result]) << ".\"#{escape(el.value, true).rstrip.gsub(/\n/, "\n.\"")}\n" 187 end
Also aliased as: convert_comment
escape(text, preserve_whitespace = false)
click to toggle source
# File lib/kramdown/converter/man.rb 284 def escape(text, preserve_whitespace = false) 285 text = (preserve_whitespace ? text.dup : text.gsub(/\s+/, ' ')) 286 text.gsub!('\\', "\\e") 287 text.gsub!(/^\./, '\\\\&.') 288 text.gsub!(/[.'-]/) {|m| "\\#{m}" } 289 text 290 end
inner(el, opts, use = :all)
click to toggle source
# File lib/kramdown/converter/man.rb 25 def inner(el, opts, use = :all) 26 arr = el.children.reject {|e| e.type == :blank } 27 arr.each_with_index do |inner_el, index| 28 next if use == :rest && index == 0 29 break if use == :first && index > 0 30 options = opts.dup 31 options[:parent] = el 32 options[:index] = index 33 options[:prev] = (index == 0 ? nil : arr[index - 1]) 34 options[:next] = (index == arr.length - 1 ? nil : arr[index + 1]) 35 convert(inner_el, options) 36 end 37 end
macro(name, *args)
click to toggle source
# File lib/kramdown/converter/man.rb 271 def macro(name, *args) 272 ".#{[name, *args].compact.join(' ')}\n" 273 end
newline(text)
click to toggle source
# File lib/kramdown/converter/man.rb 275 def newline(text) 276 text << "\n" unless text[-1] == "\n" 277 text 278 end
quote(text)
click to toggle source
# File lib/kramdown/converter/man.rb 280 def quote(text) 281 "\"#{text.gsub(/"/, '\\"')}\"" 282 end
unicode_char(codepoint)
click to toggle source
# File lib/kramdown/converter/man.rb 292 def unicode_char(codepoint) 293 "\\[u#{codepoint.to_s(16).rjust(4, '0')}]" 294 end