Module: Md2Man::HTML

Includes:
Document
Included in:
Engine
Defined in:
lib/md2man/html/engine.rb,
lib/md2man/html.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
)
HEADER_PARTS =

see “Title line” in man-pages(7) or “Top-level headings” in md2man(5)

%w[ title section date source manual ].freeze
SYNTAX_HIGHLIGHTER =
Class.new do
  require 'rouge'
  require 'rouge/plugins/redcarpet'
  extend Rouge::Plugins::Redcarpet
end

Constants included from Document

Document::PARAGRAPH_INDENT

Instance Method Summary collapse

Methods included from Document

#paragraph, #postprocess

Instance Method Details

#block_code(code, language) ⇒ Object



72
73
74
# File 'lib/md2man/html.rb', line 72

def block_code code, language
  SYNTAX_HIGHLIGHTER.block_code super, language
end

#codespan(code) ⇒ Object


span-level processing




80
81
82
# File 'lib/md2man/html.rb', line 80

def codespan code
  "<code>#{CGI.escape_html super}</code>"
end

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



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/md2man/html.rb', line 39

def header text, level, _=nil
  if level == 1 and not @h1_seen
    @h1_seen = true
    text = CGI.unescape_html(text) # unescape &quot; for Shellwords.split()
    text = Shellwords.split(text).zip(HEADER_PARTS).map do |value, part|
      part ? %{<span class="md2man-#{part}">#{value}</span>} : value
    end.compact.join(' ')
  end

  # decode here, since we will emit this heading below as raw HTML anyway
  text = decode_references(text)

  # strip all HTML tags, squeeze all non-word characters, and lowercase it
  id = text.gsub(/<.+?>/, '-').gsub(/\W+/, '-').gsub(/^-|-$/, '').downcase

  # make duplicate anchors unique by appending numerical suffixes to them
  count = @seen_count_by_id[id] += 1
  id += "-#{count - 1}" if count > 1

  [
    %{<h#{level} id="#{id}">},
      text,
      %{<a name="#{id}" href="##{id}" class="md2man-permalink" title="permalink"></a>},
    "</h#{level}>",
  ].join
end

#indented_paragraph(text) ⇒ Object



32
33
34
# File 'lib/md2man/html.rb', line 32

def indented_paragraph text
  "<dl><dd>#{text}</dd></dl>"
end

#normal_paragraph(text) ⇒ Object


block-level processing




23
24
25
# File 'lib/md2man/html.rb', line 23

def normal_paragraph text
  "<p>#{text}</p>"
end

#preprocess(document) ⇒ Object


document-level processing




13
14
15
16
17
# File 'lib/md2man/html.rb', line 13

def preprocess document
  @h1_seen = false
  @seen_count_by_id = Hash.new {|h,k| h[k] = 0 }
  super
end

#reference(input_match, output_match) ⇒ Object



84
85
86
87
88
89
90
91
# File 'lib/md2man/html.rb', line 84

def reference input_match, output_match
  if output_match.pre_match =~ /<[^>]*\z/
    input_match.to_s
  else
    url = reference_url(input_match[:page], input_match[:section])
    %{<a class="md2man-reference" href="#{url}">#{input_match}</a>}
  end + output_match[:addendum].to_s
end

#reference_url(page, section) ⇒ Object

You can override this in a derived class to compute URLs as you like!



94
95
96
# File 'lib/md2man/html.rb', line 94

def reference_url page, section
  "../man#{section}/#{page}.#{section}.html"
end

#tagged_paragraph(text) ⇒ Object



27
28
29
30
# File 'lib/md2man/html.rb', line 27

def tagged_paragraph text
  head, *body = text.lines.to_a
  "<dl><dt>#{head.chomp}</dt><dd>#{body.join}</dd></dl>"
end