Class: Kramdown::Man::Converter Private

Inherits:
Converter::Base
  • Object
show all
Defined in:
lib/kramdown/man/converter.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Converts markdown into a roff man-page.

Since:

  • 1.0.0

Constant Summary collapse

HEADER =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Comment header

Since:

  • 1.0.0

<<~ROFF
  .\\\" Generated by kramdown-man #{VERSION}
  .\\\" https://github.com/postmodern/kramdown-man#readme
ROFF
TYPOGRAPHIC_SYMS =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Typographic Symbols and their UTF8 chars

Since:

  • 1.0.0

{
  :ndash       => '\-\-',
  :mdash       => '\[em]',
  :hellip      => '\.\.\.',
  :laquo       => '\[Fo]',
  :raquo       => '\[Fc]',
  :laquo_space => '\[Fo]',
  :raquo_space => '\[Fc]'
}
SMART_QUOTES =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Smart Quotes and their UTF8 chars

Since:

  • 1.0.0

{
  :lsquo => '\[oq]',
  :rsquo => '\[cq]',
  :ldquo => '\[lq]',
  :rdquo => '\[rq]'
}
GLYPHS =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Since:

  • 1.0.0

{
  'Ð' => '\[-D]',
  'ð' => '\[Sd]',
  'Þ' => '\[TP]',
  'þ' => '\[Tp]',
  'ß' => '\[ss]',
  # Ligatures and Other Latin Glyphs
  '' => '\[ff]',
  '' => '\[fi]',
  '' => '\[fl]',
  '' => '\[Fi]',
  '' => '\[Fl]',
  'Ł' => '\[/L]',
  'ł' => '\[/l]',
  'Ø' => '\[/O]',
  'ø' => '\[/o]',
  'Æ' => '\[AE]',
  'æ' => '\[ae]',
  'Œ' => '\[OE]',
  'œ' => '\[oe]',
  'IJ' => '\[IJ]',
  'ij' => '\[ij]',
  'ı' => '\[.i]',
  'ȷ' => '\[.j]',
  # Accented Characters
  'Á' => '\[\'A]',
  'Ć' => '\[\'C]',
  'É' => '\[\'E]',
  'Í' => '\[\'I]',
  'Ó' => '\[\'O]',
  'Ú' => '\[\'U]',
  'Ý' => '\[\'Y]',
  'á' => '\[\'a]',
  'ć' => '\[\'c]',
  'é' => '\[\'e]',
  'í' => '\[\'i]',
  'ó' => '\[\'o]',
  'ú' => '\[\'u]',
  'ý' => '\[\'y]',
  'Ä' => '\[:A]',
  'Ë' => '\[:E]',
  'Ï' => '\[:I]',
  'Ö' => '\[:O]',
  'Ü' => '\[:U]',
  'Ÿ' => '\[:Y]',
  'ä' => '\[:a]',
  'ë' => '\[:e]',
  'ï' => '\[:i]',
  'ö' => '\[:o]',
  'ü' => '\[:u]',
  'ÿ' => '\[:y]',
  'Â' => '\[^A]',
  'Ê' => '\[^E]',
  'Î' => '\[^I]',
  'Ô' => '\[^O]',
  'Û' => '\[^U]',
  'â' => '\[^a]',
  'ê' => '\[^e]',
  'î' => '\[^i]',
  'ô' => '\[^o]',
  'û' => '\[^u]',
  'À' => '\[`A]',
  'È' => '\[`E]',
  'Ì' => '\[`I]',
  'Ò' => '\[`O]',
  'Ù' => '\[`U]',
  'à' => '\[`a]',
  'è' => '\[`e]',
  'ì' => '\[`i]',
  'ò' => '\[`o]',
  'ù' => '\[`u]',
  'Ã' => '\[~A]',
  'Ñ' => '\[~N]',
  'Õ' => '\[~O]',
  'ã' => '\[~a]',
  'ñ' => '\[~n]',
  'õ' => '\[~o]',
  'Š' => '\[vS]',
  'š' => '\[vs]',
  'Ž' => '\[vZ]',
  'ž' => '\[vz]',
  'Ç' => '\[,C]',
  'ç' => '\[,c]',
  'Å' => '\[oA]',
  'å' => '\[oa]',
  # Accents
  '˝' => '\[a"]',
  '¯' => '\[a-]',
  '˙' => '\[a.]',
  # '^' => '\[a^]',
  '´' => "\\´",
  '`' => '\`',
  '˘' => '\[ab]',
  '¸' => '\[ac]',
  '¨' => '\[ad]',
  'ˇ' => '\[ah]',
  '˚' => '\[ao]',
  # '~' => '\(ti',
  '˛' => '\[ho]',
  '^' => '\(ha',
  '~' => '\[ti]',
  # Quotes
  '' => '\[Bq]',
  '' => '\[bq]',
  '' => '\[lq]',
  '' => '\[rq]',
  '' => '\[oq]',
  '' => '\[cq]',
  "'" => '\(aq',
  '"' => '\[dq]',
  '«' => '\[Fo]',
  '»' => '\[Fc]',
  '' => '\[fo]',
  '' => '\[fc]',
  # Punctuation
  '.' => '\.',
  '¡' => '\[r!]',
  '¿' => '\[r?]',
  '' => '\[em]',
  '' => '\[en]',
  '' => '\[hy]',
  # Brackets
  '[' => '\[lB]',
  ']' => '\[rB]',
  '{' => '\[lC]',
  '}' => '\[rC]',
  '' => '\[la]',
  '' => '\[ra]',
  # '⎪' => '\[bv]',
  # '⎪' => '\[braceex]',
  '' => '\[bracketlefttp]',
  '' => '\[bracketleftbt]',
  '' => '\[bracketleftex]',
  '' => '\[bracketrighttp]',
  '' => '\[bracketrightbt]',
  '' => '\[bracketrightex]',
  '' => '\[lt]',
  '' => '\[bracelefttp]',
  '' => '\[lk]',
  '' => '\[braceleftmid]',
  '' => '\[lb]',
  '' => '\[braceleftbt]',
  # '⎪' => '\[braceleftex]',
  '' => '\[rt]',
  '' => '\[bracerighttp]',
  '' => '\[rk]',
  '' => '\[bracerightmid]',
  '' => '\[rb]',
  '' => '\[bracerightbt]',
  '' => '\[bracerightex]',
  '' => '\[parenlefttp]',
  '' => '\[parenleftbt]',
  '' => '\[parenleftex]',
  '' => '\[parenrighttp]',
  '' => '\[parenrightbt]',
  '' => '\[parenrightex]',
  # Arrows
  '' => '\[<-]',
  '' => '\[->]',
  '' => '\[<>]',
  '' => '\[da]',
  '' => '\[ua]',
  '' => '\[va]',
  '' => '\[lA]',
  '' => '\[rA]',
  '' => '\[hA]',
  '' => '\[dA]',
  '' => '\[uA]',
  '' => '\[vA]',
  '' => '\[an]',
  # Lines
  # '|' => '\[ba]',
  '' => '\[br]',
  # '_' => '\[ul]',
  '' => '\[rn]',
  '_' => '\[ru]',
  '¦' => '\[bb]',
  '/' => '\[sl]',
  '\\' => '\e',
  # Text markers
  '' => '\[ci]',
  # '·' => '\[bu]',
  '' => '\[dd]',
  '' => '\[dg]',
  '' => '\[lz]',
  '' => '\[sq]',
  '' => '\[ps]',
  '§' => '\[sc]',
  '' => '\[lh]',
  '' => '\[rh]',
  '@' => '\[at]',
  '#' => '\[sh]',
  '' => '\[CR]',
  '' => '\[OK]',
  # Legal Symbols
  '©' => '\[co]',
  '®' => '\[rg]',
  '' => '\[tm]',
  # Currency symbols
  '$' => '\[Do]',
  '¢' => '\[ct]',
  # '€' => '\[eu]',
  '' => '\[Eu]',
  '¥' => '\[Ye]',
  '£' => '\[Po]',
  '¤' => '\[Cs]',
  'ƒ' => '\[Fn]',
  # Units
  '°' => '\[de]',
  '' => '\[%0]',
  '' => '\[fm]',
  '' => '\[sd]',
  'µ' => '\[mc]',
  'ª' => '\[Of]',
  'º' => '\[Om]',
  # Logical Symbols
  '' => '\[AN]',
  '' => '\[OR]',
  # '¬' => '\[no]',
  '¬' => '\[tno]',
  '' => '\[te]',
  '' => '\[fa]',
  '' => '\[st]',
  # '∴' => '\[3d]',
  '' => '\[tf]',
  '|' => '\[or]',
  # Mathematical Symbols
  '½' => '\[12]',
  '¼' => '\[14]',
  '¾' => '\[34]',
  '' => '\[18]',
  '' => '\[38]',
  '' => '\[58]',
  '' => '\[78]',
  '¹' => '\[S1]',
  '²' => '\[S2]',
  '³' => '\[S3]',
  '+' => '\[pl]',
  '-' => '\-',
  '' => '\[mi]',
  '' => '\[-+]',
  # '±' => '\[+-]',
  '±' => '\[t+-]',
  '·' => '\[pc]',
  '' => '\[md]',
  # '×' => '\[mu]',
  '×' => '\[tmu]',
  '' => '\[c*]',
  '' => '\[c+]',
  # '÷' => '\[di]',
  '÷' => '\[tdi]',
  '' => '\[f/]',
  '' => '\[**]',
  '' => '\[<=]',
  '' => '\[>=]',
  '' => '\[<<]',
  '' => '\[>>]',
  '=' => '\[eq]',
  '' => '\[!=]',
  '' => '\[==]',
  '' => '\[ne]',
  '' => '\[=~]',
  '' => '\[|=]',
  '' => '\[ap]',
  # '≈' => '\[~~]',
  '' => '\[~=]',
  '' => '\[pt]',
  '' => '\[es]',
  '' => '\[mo]',
  '' => '\[nm]',
  '' => '\[sb]',
  '' => '\[nb]',
  '' => '\[sp]',
  '' => '\[nc]',
  '' => '\[ib]',
  '' => '\[ip]',
  '' => '\[ca]',
  '' => '\[cu]',
  '' => '\[/_]',
  '' => '\[pp]',
  # '∫' => '\[is]',
  '' => '\[integral]',
  '' => '\[sum]',
  '' => '\[product]',
  '' => '\[coproduct]',
  '' => '\[gr]',
  # '√' => '\[sr]',
  '' => '\[sqrt]',
  '' => '\[lc]',
  '' => '\[rc]',
  '' => '\[lf]',
  '' => '\[rf]',
  '' => '\[if]',
  '' => '\[Ah]',
  '' => '\[Im]',
  '' => '\[Re]',
  '' => '\[wp]',
  '' => '\[pd]',
  # 'ℏ' => '\[-h]',
  '' => '\[hbar]',
  # Greek glyphs
  'Α' => '\[*A]',
  'Β' => '\[*B]',
  'Γ' => '\[*G]',
  'Δ' => '\[*D]',
  'Ε' => '\[*E]',
  'Ζ' => '\[*Z]',
  'Η' => '\[*Y]',
  'Θ' => '\[*H]',
  'Ι' => '\[*I]',
  'Κ' => '\[*K]',
  'Λ' => '\[*L]',
  'Μ' => '\[*M]',
  'Ν' => '\[*N]',
  'Ξ' => '\[*C]',
  'Ο' => '\[*O]',
  'Π' => '\[*P]',
  'Ρ' => '\[*R]',
  'Σ' => '\[*S]',
  'Τ' => '\[*T]',
  'Υ' => '\[*U]',
  'Φ' => '\[*F]',
  'Χ' => '\[*X]',
  'Ψ' => '\[*Q]',
  'Ω' => '\[*W]',
  'α' => '\[*a]',
  'β' => '\[*b]',
  'γ' => '\[*g]',
  'δ' => '\[*d]',
  'ε' => '\[*e]',
  'ζ' => '\[*z]',
  'η' => '\[*y]',
  'θ' => '\[*h]',
  'ι' => '\[*i]',
  'κ' => '\[*k]',
  'λ' => '\[*l]',
  'μ' => '\[*m]',
  'ν' => '\[*n]',
  'ξ' => '\[*c]',
  'ο' => '\[*o]',
  'π' => '\[*p]',
  'ρ' => '\[*r]',
  'ς' => '\[ts]',
  'σ' => '\[*s]',
  'τ' => '\[*t]',
  'υ' => '\[*u]',
  'ϕ' => '\[*f]',
  'χ' => '\[*x]',
  'ψ' => '\[*q]',
  'ω' => '\[*w]',
  'ϑ' => '\[+h]',
  'φ' => '\[+f]',
  'ϖ' => '\[+p]',
  'ϵ' => '\[+e]',
  # Card symbols
  '' => '\[CL]',
  '' => '\[SP]',
  '' => '\[HE]',
  '' => '\[u2661]',
  '' => '\[DI]',
  '' => '\[u2662]'
}
GLYPH_REGEXP =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Regular expression to convert unicode characters into glyphs

Since:

  • 1.0.0

Regexp.union(GLYPHS.keys)

Instance Method Summary collapse

Constructor Details

#initialize(root, options) ⇒ Converter

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Initializes the converter.

Parameters:

  • root (Kramdown::Element)

    The root of the markdown document.

  • options (Hash)

    Markdown options.

Since:

  • 1.0.0



418
419
420
421
422
# File 'lib/kramdown/man/converter.rb', line 418

def initialize(root,options)
  super(root,options)

  @ol_index = 0
end

Instance Method Details

#convert(root) ⇒ String

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Converts the markdown document into a man-page.

Parameters:

  • root (Kramdown::Element)

    The root of a markdown document.

Returns:

  • (String)

    The roff output.

Since:

  • 1.0.0



433
434
435
# File 'lib/kramdown/man/converter.rb', line 433

def convert(root)
  "#{HEADER}#{convert_root(root)}"
end

#convert_a(a) ⇒ String

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Converts a kd:a element.

Parameters:

  • a (Kramdown::Element)

    A kd:a element.

Returns:

  • (String)

    The roff output.

Since:

  • 1.0.0



931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
# File 'lib/kramdown/man/converter.rb', line 931

def convert_a(a)
  href = a.attr['href']
  scheme, path = href.split(':',2)

  text = convert_text_elements(a.children)

  case scheme
  when 'mailto'
    email = escape(path)

    unless text == email
      <<~ROFF
        #{text.chomp}
        .MT #{email}
        .ME
      ROFF
    else
      <<~ROFF
        .MT #{email}
        .ME
      ROFF
    end
  when 'man'
    if (match = path.match(/\A(?<page>[A-Za-z0-9_-]+)(?:\((?<section>\d[a-z]?)\)|\.(?<section>\d[a-z]?))\z/))
      man_page_link(match[:page],match[:section])
    else
      page = escape(path)

      <<~ROFF
        .BR #{page}
      ROFF
    end
  else
    if (match = href.match(/(?<page>[A-Za-z0-9_-]+)\.(?<section>\d[a-z]?)\.md\z/))
      man_page_link(match[:page],match[:section])
    else
      <<~ROFF
        #{text.chomp}
        .UR #{escape(href)}
        .UE
      ROFF
    end
  end
end

#convert_abbreviation(abbr) ⇒ String

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Converts a kd:abbreviation element.

Parameters:

  • abbr (Kramdown::Element)

    A kd:abbreviation element.

Returns:

  • (String)

    The roff output.

Since:

  • 1.0.0



784
785
786
# File 'lib/kramdown/man/converter.rb', line 784

def convert_abbreviation(abbr)
  escape(abbr.value)
end

#convert_blank(blank) ⇒ nil

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Converts a kd:blank element.

Parameters:

  • blank (Kramdown::Element)

    A kd:blank element.

Returns:

  • (nil)

Since:

  • 1.0.0



473
474
# File 'lib/kramdown/man/converter.rb', line 473

def convert_blank(blank)
end

#convert_blockquote(blockquote) ⇒ String

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Converts a kd:blockquote element.

Parameters:

  • blockquote (Kramdown::Element)

    A kd:blockquote element.

Returns:

  • (String)

    The roff output.

Since:

  • 1.0.0



797
798
799
800
801
802
803
804
805
# File 'lib/kramdown/man/converter.rb', line 797

def convert_blockquote(blockquote)
  contents = convert_children_of(blockquote)

  return <<~ROFF
    .RS
    #{contents.chomp}
    .RE
  ROFF
end

#convert_children_of(element) ⇒ String

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Converts the children of an element.

Parameters:

  • element (Array<Kramdown::Element>)

    The elements to convert.

Returns:

  • (String)

    The combined roff output.

Since:

  • 1.0.0



1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
# File 'lib/kramdown/man/converter.rb', line 1009

def convert_children_of(element)
  roff = String.new(encoding: Encoding::UTF_8)

  element.children.each do |child|
    if (contents = convert_element(child))
      roff << contents
    end
  end

  return roff
end

#convert_codeblock(codeblock) ⇒ String

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Converts a kd:codeblock element.

Parameters:

  • codeblock (Kramdown::Element)

    A kd:codeblock element.

Returns:

  • (String)

    The roff output.

Since:

  • 1.0.0



816
817
818
819
820
821
822
823
824
825
826
827
# File 'lib/kramdown/man/converter.rb', line 816

def convert_codeblock(codeblock)
  contents = escape(codeblock.value)

  return <<~ROFF
    .PP
    .RS 4
    .EX
    #{contents.chomp}
    .EE
    .RE
  ROFF
end

#convert_codespan(codespan) ⇒ String

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Converts a kd:codespan element.

Parameters:

  • codespan (Kramdown::Element)

    A kd:codespan element.

Returns:

  • (String)

    The roff output.

Since:

  • 1.0.0



903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
# File 'lib/kramdown/man/converter.rb', line 903

def convert_codespan(codespan)
  # ``` code fence blocks are parsed as kd:codespans
  if codespan.options[:codespan_delimiter] == '```'
    # ignore the first and last newlines
    contents = escape(codespan.value[1..-2])

    <<~ROFF
      .PP
      .RS 4
      .EX
      #{contents}
      .EE
      .RE
    ROFF
  else
    "\\fB#{escape(codespan.value)}\\fR"
  end
end

#convert_comment(comment) ⇒ String

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Converts a kd:comment element.

Parameters:

  • comment (Kramdown::Element)

    A kd:comment element.

Returns:

  • (String)

    The roff output.

Since:

  • 1.0.0



838
839
840
841
842
843
844
845
846
847
848
# File 'lib/kramdown/man/converter.rb', line 838

def convert_comment(comment)
  roff = String.new(encoding: Encoding::UTF_8)

  comment.value.lines.each do |line|
    roff << <<~ROFF
              .\\" #{line}
            ROFF
  end

  return roff
end

#convert_dd(dd, index: 0) ⇒ String

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Converts a kd:dd element within a kd:dd.

Parameters:

  • dd (Kramdown::Element)

    A kd:dd element.

  • index (Integer) (defaults to: 0)

    The index of the kd:dd element. Used to indicate whether this is the first kd:dd element following a kd;dt element or additional kd:dt elements.

Returns:

  • (String)

    The roff output.

Since:

  • 1.0.0



751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
# File 'lib/kramdown/man/converter.rb', line 751

def convert_dd(dd, index: 0)
  roff = String.new(encoding: Encoding::UTF_8)

  dd.children.each_with_index do |child,child_index|
    if index == 0 && child_index == 0 && child.type == :p
      contents = convert_children_of(child)

      # omit the .PP macro for the first paragraph
      roff << "#{contents.chomp}\n"
    else
      if (contents = convert_element(child))
        # indent all other following paragraphs or other elements
        roff << <<~ROFF
                  .RS
                  #{contents.chomp}
                  .RE
                ROFF
      end
    end
  end

  return roff
end

#convert_dl(dl) ⇒ String

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Converts a kd:dl element.

Parameters:

  • dl (Kramdown::Element)

    A kd:dl element.

Returns:

  • (String)

    The roff output.

Since:

  • 1.0.0



678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
# File 'lib/kramdown/man/converter.rb', line 678

def convert_dl(dl)
  roff = String.new(encoding: Encoding::UTF_8)

  dt_index = 0
  dd_index = 0

  dl.children.each do |element|
    case element.type
    when :dt
      roff << convert_dt(element, index: dt_index)

      dt_index += 1 # increment the dt count
      dd_index  = 0 # reset the dd count
    when :dd
      roff << convert_dd(element, index: dd_index)

      dd_index += 1 # increment the dd count
      dt_index  = 0 # reset the dt count
    else
      roff << convert(element)

      # reset both the dt_index and dd_index counters
      dt_index = 0
      dd_index = 0
    end
  end

  return roff
end

#convert_dt(dt, index: 0) ⇒ String

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Converts a kd:dt element within a kd:dl.

Parameters:

  • dt (Kramdown::Element)

    A kd:dt element.

  • index (Integer) (defaults to: 0)

    The index of the kd:dt element. Used to indicate whether this is the first kd:dt element or additional kd:dt elements.

Returns:

  • (String)

    The roff output.

Since:

  • 1.0.0



721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
# File 'lib/kramdown/man/converter.rb', line 721

def convert_dt(dt, index: 0)
  text = convert_text_elements(dt.children)

  if index == 0
    <<~ROFF
      .TP
      #{text}
    ROFF
  else
    <<~ROFF
      .TQ
      #{text}
    ROFF
  end
end

#convert_element(element) ⇒ String

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Converts an element.

Parameters:

  • element (Kramdown::Element)

    An arbitrary element within the markdown document.

Returns:

  • (String)

    The roff output.

Since:

  • 1.0.0



459
460
461
462
463
# File 'lib/kramdown/man/converter.rb', line 459

def convert_element(element)
  method = "convert_#{element.type}"

  send(method,element) if respond_to?(method)
end

#convert_em(em) ⇒ String

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Converts a kd:em element.

Parameters:

  • em (Kramdown::Element)

    A kd:em element.

Returns:

  • (String)

    The roff output.

Since:

  • 1.0.0



877
878
879
# File 'lib/kramdown/man/converter.rb', line 877

def convert_em(em)
  "\\fI#{convert_text_elements(em.children)}\\fP"
end

#convert_header(header) ⇒ String

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Converts a kd:header element.

Parameters:

  • header (Kramdown::Element)

    A kd:header element.

Returns:

  • (String)

    The roff output.

Since:

  • 1.0.0



524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
# File 'lib/kramdown/man/converter.rb', line 524

def convert_header(header)
  text = header.options[:raw_text]

  case header.options[:level]
  when 1
    <<~ROFF
      .TH #{text}
    ROFF
  when 2
    <<~ROFF
      .SH #{text}
    ROFF
  else
    <<~ROFF
      .SS #{text}
    ROFF
  end
end

#convert_hr(hr) ⇒ nil

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Ignore kd:hr elements.

Parameters:

  • hr (Kramdown::Element)

    A kd:hr element.

Returns:

  • (nil)

Since:

  • 1.0.0



551
552
# File 'lib/kramdown/man/converter.rb', line 551

def convert_hr(hr)
end

#convert_ol(ol) ⇒ String

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Converts a kd:ol element.

Parameters:

  • ol (Kramdown::Element)

    A kd:ol element.

Returns:

  • (String)

    The roff output.

Since:

  • 1.0.0



619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
# File 'lib/kramdown/man/converter.rb', line 619

def convert_ol(ol)
  @ol_index += 1

  contents = String.new(encoding: Encoding::UTF_8)

  ol.children.each do |li|
    contents << convert_ol_li(li)
  end

  return <<~ROFF
    .nr step#{@ol_index} 0 1
    .RS
    #{contents.chomp}
    .RE
  ROFF
end

#convert_ol_li(li) ⇒ String

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

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

Parameters:

  • li (Kramdown::Element)

    A kd:li element.

Returns:

  • (String)

    The roff output.

Since:

  • 1.0.0



645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
# File 'lib/kramdown/man/converter.rb', line 645

def convert_ol_li(li)
  roff = String.new(encoding: Encoding::UTF_8)

  li.children.each_with_index do |child,index|
    if child.type == :p
      contents = convert_children_of(child)

      roff << if index == 0
                <<~ROFF
                  .IP \\n+[step#{@ol_index}]
                  #{contents.chomp}
                ROFF
              else
                <<~ROFF
                  .IP \\n
                  #{contents.chomp}
                ROFF
              end
    end
  end

  return roff
end

#convert_p(p) ⇒ String

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Converts a kd:p element.

Parameters:

  • p (Kramdown::Element)

    A kd:p element.

Returns:

  • (String)

    The roff output.

Since:

  • 1.0.0



859
860
861
862
863
864
865
866
# File 'lib/kramdown/man/converter.rb', line 859

def convert_p(p)
  contents = convert_text_elements(p.children)

  return <<~ROFF
    .PP
    #{contents.chomp}
  ROFF
end

#convert_root(root) ⇒ String

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Converts the root of a markdown document.

Parameters:

  • root (Kramdown::Element)

    The root of the markdown document.

Returns:

  • (String)

    The roff output.

Since:

  • 1.0.0



446
447
448
# File 'lib/kramdown/man/converter.rb', line 446

def convert_root(root)
  convert_children_of(root)
end

#convert_smart_quote(quote) ⇒ String

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Converts a kd:smart_quote element.

Parameters:

  • quote (Kramdown::Element)

    A kd:smart_quote element.

Returns:

  • (String)

    The roff output.

Since:

  • 1.0.0



511
512
513
# File 'lib/kramdown/man/converter.rb', line 511

def convert_smart_quote(quote)
  SMART_QUOTES[quote.value]
end

#convert_strong(strong) ⇒ String

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Converts a kd:strong element.

Parameters:

  • strong (Kramdown::Element)

    A kd:strong element.

Returns:

  • (String)

    The roff output.

Since:

  • 1.0.0



890
891
892
# File 'lib/kramdown/man/converter.rb', line 890

def convert_strong(strong)
  "\\fB#{convert_text_elements(strong.children)}\\fP"
end

#convert_text(text) ⇒ String

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Converts a kd:text element.

Parameters:

  • text (Kramdown::Element)

    A kd:text element.

Returns:

  • (String)

    The roff output.

Since:

  • 1.0.0



485
486
487
# File 'lib/kramdown/man/converter.rb', line 485

def convert_text(text)
  escape(text.value)
end

#convert_text_elements(elements) ⇒ String

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Converts the children of an element.

Parameters:

  • elements (Array<Kramdown::Element>)

    The text elements to convert.

Returns:

  • (String)

    The roff output.

Since:

  • 1.0.0



1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
# File 'lib/kramdown/man/converter.rb', line 1030

def convert_text_elements(elements)
  roff = String.new(encoding: Encoding::UTF_8)

  elements.each do |element|
    if (contents = convert_element(element))
      if contents.start_with?('.') && !roff.empty? && !roff.end_with?("\n")
        # roff macross must exist on their own line
        roff << "\n#{contents}"
      elsif contents.start_with?(' ') && roff.end_with?("\n")
        # remove leadning whitespace following a newline
        roff << contents.lstrip
      else
        roff << contents
      end
    end
  end

  return roff
end

#convert_typographic_sym(sym) ⇒ String

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Converts a kd:typographic_sym element.

Parameters:

  • sym (Kramdown::Element)

    A kd:typographic_sym element.

Returns:

  • (String)

    The roff output.

Since:

  • 1.0.0



498
499
500
# File 'lib/kramdown/man/converter.rb', line 498

def convert_typographic_sym(sym)
  TYPOGRAPHIC_SYMS[sym.value]
end

#convert_ul(ul) ⇒ String

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Converts a kd:ul element.

Parameters:

  • ul (Kramdown::Element)

    A kd:ul element.

Returns:

  • (String)

    The roff output.

Since:

  • 1.0.0



563
564
565
566
567
568
569
570
571
572
573
574
575
# File 'lib/kramdown/man/converter.rb', line 563

def convert_ul(ul)
  contents = String.new(encoding: Encoding::UTF_8)

  ul.children.each do |li|
    contents << convert_ul_li(li)
  end

  return <<~ROFF
    .RS
    #{contents.chomp}
    .RE
  ROFF
end

#convert_ul_li(li) ⇒ String

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

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

Parameters:

  • li (Kramdown::Element)

    A kd:li element.

Returns:

  • (String)

    The roff output.

Since:

  • 1.0.0



586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
# File 'lib/kramdown/man/converter.rb', line 586

def convert_ul_li(li)
  roff = String.new(encoding: Encoding::UTF_8)

  li.children.each_with_index do |child,index|
    if child.type == :p
      contents = convert_children_of(child)

      roff << if index == 0
                <<~ROFF
                  .IP \\(bu 2
                  #{contents.chomp}
                ROFF
              else
                <<~ROFF
                 .IP \\( 2
                 #{contents.chomp}
                ROFF
              end
    end
  end

  return roff
end

#escape(text) ⇒ String

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Escapes text for roff.

Parameters:

  • text (String)

    The unescaped text.

Returns:

  • (String)

    The escaped text.

Since:

  • 1.0.0



1059
1060
1061
# File 'lib/kramdown/man/converter.rb', line 1059

def escape(text)
  text.gsub(GLYPH_REGEXP,GLYPHS)
end

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Outputs a man page link.

Parameters:

  • page (String)

    The man page name.

  • section (String, nil) (defaults to: nil)

    The optional section of the man page.

Returns:

  • (String)

    The roff output.

Since:

  • 1.0.0



988
989
990
991
992
993
994
995
996
997
998
# File 'lib/kramdown/man/converter.rb', line 988

def man_page_link(page,section=nil)
  if section
    <<~ROFF
      .BR #{escape(page)} (#{escape(section)})
    ROFF
  else
    <<~ROFF
      .BR #{escape(page)}
    ROFF
  end
end