lib/kramdown/converter/man.rb in kramdown-man-0.1.4 vs lib/kramdown/converter/man.rb in kramdown-man-0.1.5

- old
+ new

@@ -11,32 +11,398 @@ class Man < Base # Comment header HEADER = [ ".\\\" Generated by kramdown-man #{::Kramdown::Man::VERSION}", - ".\\\" https://github.com/postmodern/kramdown-roff#readme" + ".\\\" https://github.com/postmodern/kramdown-man#readme" ].join("\n") # Typographic Symbols and their UTF8 chars TYPOGRAPHIC_SYMS = { - :ndash => "--", - :mdash => "—", - :hellip => "…", - :laquo => "«", - :raquo => "»", - :laquo_space => "«", - :raquo_space => "»" + :ndash => '\-\-', + :mdash => '\[em]', + :hellip => '...', + :laquo => '\[Fo]', + :raquo => '\[Fc]', + :laquo_space => '\[Fo]', + :raquo_space => '\[Fc]' } # Smart Quotes and their UTF8 chars SMART_QUOTES = { - :lsquo => "‘", - :rsquo => "’", - :ldquo => "“", - :rdquo => "”" + :lsquo => '\[oq]', + :rsquo => '\[cq]', + :ldquo => '\[lq]', + :rdquo => '\[rq]' } + GLYPHS = { + 'Ð' => '\[-D]', + 'ð' => '\[Sd]', + 'Þ' => '\[TP]', + 'þ' => '\[Tp]', + 'ß' => '\[ss]', + # Ligatures and Other Latin Glyphs + 'ff' => '\[ff]', + 'fi' => '\[fi]', + 'fl' => '\[fl]', + 'ffi' => '\[Fi]', + 'ffl' => '\[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]' + } + + # Regular expression to convert unicode characters into glyphs + GLYPH_REGEXP = Regexp.union(GLYPHS.keys) + # # Initializes the converter. # # @param [Kramdown::Element] root # The root of the markdown document. @@ -322,12 +688,13 @@ # The roff output. # def convert_p(p) children = p.children - if (children.length >= 2) && - (children.first.type == :em || children.first.type == :codespan) + if ((children.length >= 2) && (children.first.type == :codespan || + children.first.type == :em || + children.first.type == :strong)) newline = children.find_index { |el| el.type == :text && el.value.start_with?("\n") } if newline @@ -377,11 +744,11 @@ # # @return [String] # The roff output. # def convert_codespan(codespan) - "\\fB\\fC#{codespan.value}\\fR" + "\\fB#{codespan.value}\\fR" end # # Converts a `kd:a` element. # @@ -390,11 +757,11 @@ # # @return [String] # The roff output. # def convert_a(a) - href = a.attr['href'] + href = escape(a.attr['href']) text = convert_children(a.children) case href when /^mailto:/ email = href[7..-1] @@ -434,10 +801,10 @@ # # @return [String] # The escaped text. # def escape(text) - text.gsub('\\','\&\&').gsub('-','\\-') + text.gsub(GLYPH_REGEXP) { |char| GLYPHS[char] } end end end end