|
1 | 1 | require 'asciidoctor'
|
2 | 2 | require 'asciidoctor/extensions'
|
| 3 | +require 'asciidoctor/converter/docbook5' |
| 4 | +require 'asciidoctor/converter/html5' |
3 | 5 |
|
4 | 6 | module Git
|
5 | 7 | module Documentation
|
@@ -39,10 +41,95 @@ def process document, output
|
39 | 41 | output
|
40 | 42 | end
|
41 | 43 | end
|
| 44 | + |
| 45 | + class SynopsisBlock < Asciidoctor::Extensions::BlockProcessor |
| 46 | + |
| 47 | + use_dsl |
| 48 | + named :synopsis |
| 49 | + parse_content_as :simple |
| 50 | + |
| 51 | + def process parent, reader, attrs |
| 52 | + outlines = reader.lines.map do |l| |
| 53 | + l.gsub(/(\.\.\.?)([^\]$.])/, '`\1`\2') |
| 54 | + .gsub(%r{([\[\] |()>]|^)([-a-zA-Z0-9:+=~@,/_^\$]+)}, '\1{empty}`\2`{empty}') |
| 55 | + .gsub(/(<[-a-zA-Z0-9.]+>)/, '__\\1__') |
| 56 | + .gsub(']', ']{empty}') |
| 57 | + end |
| 58 | + create_block parent, :verse, outlines, attrs |
| 59 | + end |
| 60 | + end |
| 61 | + |
| 62 | + class GitDBConverter < Asciidoctor::Converter::DocBook5Converter |
| 63 | + |
| 64 | + extend Asciidoctor::Converter::Config |
| 65 | + register_for 'docbook5' |
| 66 | + |
| 67 | + def convert_inline_quoted node |
| 68 | + if (type = node.type) == :asciimath |
| 69 | + # NOTE fop requires jeuclid to process mathml markup |
| 70 | + asciimath_available? ? %(<inlineequation>#{(::AsciiMath.parse node.text).to_mathml 'mml:', 'xmlns:mml' => 'http://www.w3.org/1998/Math/MathML'}</inlineequation>) : %(<inlineequation><mathphrase><![CDATA[#{node.text}]]></mathphrase></inlineequation>) |
| 71 | + elsif type == :latexmath |
| 72 | + # unhandled math; pass source to alt and required mathphrase element; dblatex will process alt as LaTeX math |
| 73 | + %(<inlineequation><alt><![CDATA[#{equation = node.text}]]></alt><mathphrase><![CDATA[#{equation}]]></mathphrase></inlineequation>) |
| 74 | + elsif type == :monospaced |
| 75 | + node.text.gsub(/(\.\.\.?)([^\]$.])/, '<literal>\1</literal>\2') |
| 76 | + .gsub(%r{([\[\s|()>.]|^|\]|>)(\.?([-a-zA-Z0-9:+=~@,/_^\$]+\.{0,2})+)}, '\1<literal>\2</literal>') |
| 77 | + .gsub(/(<[-a-zA-Z0-9.]+>)/, '<emphasis>\1</emphasis>') |
| 78 | + else |
| 79 | + open, close, supports_phrase = QUOTE_TAGS[type] |
| 80 | + text = node.text |
| 81 | + if node.role |
| 82 | + if supports_phrase |
| 83 | + quoted_text = %(#{open}<phrase role="#{node.role}">#{text}</phrase>#{close}) |
| 84 | + else |
| 85 | + quoted_text = %(#{open.chop} role="#{node.role}">#{text}#{close}) |
| 86 | + end |
| 87 | + else |
| 88 | + quoted_text = %(#{open}#{text}#{close}) |
| 89 | + end |
| 90 | + node.id ? %(<anchor#{common_attributes node.id, nil, text}/>#{quoted_text}) : quoted_text |
| 91 | + end |
| 92 | + end |
| 93 | + end |
| 94 | + |
| 95 | + # register a html5 converter that takes in charge to convert monospaced text into Git style synopsis |
| 96 | + class GitHTMLConverter < Asciidoctor::Converter::Html5Converter |
| 97 | + |
| 98 | + extend Asciidoctor::Converter::Config |
| 99 | + register_for 'html5' |
| 100 | + |
| 101 | + def convert_inline_quoted node |
| 102 | + if node.type == :monospaced |
| 103 | + node.text.gsub(/(\.\.\.?)([^\]$.])/, '<code>\1</code>\2') |
| 104 | + .gsub(%r{([\[\s|()>.]|^|\]|>)(\.?([-a-zA-Z0-9:+=~@,/_^\$]+\.{0,2})+)}, '\1<code>\2</code>') |
| 105 | + .gsub(/(<[-a-zA-Z0-9.]+>)/, '<em>\1</em>') |
| 106 | + |
| 107 | + else |
| 108 | + open, close, tag = QUOTE_TAGS[node.type] |
| 109 | + if node.id |
| 110 | + class_attr = node.role ? %( class="#{node.role}") : '' |
| 111 | + if tag |
| 112 | + %(#{open.chop} id="#{node.id}"#{class_attr}>#{node.text}#{close}) |
| 113 | + else |
| 114 | + %(<span id="#{node.id}"#{class_attr}>#{open}#{node.text}#{close}</span>) |
| 115 | + end |
| 116 | + elsif node.role |
| 117 | + if tag |
| 118 | + %(#{open.chop} class="#{node.role}">#{node.text}#{close}) |
| 119 | + else |
| 120 | + %(<span class="#{node.role}">#{open}#{node.text}#{close}</span>) |
| 121 | + end |
| 122 | + else |
| 123 | + %(#{open}#{node.text}#{close}) |
| 124 | + end |
| 125 | + end |
| 126 | + end |
| 127 | + end |
42 | 128 | end
|
43 | 129 | end
|
44 | 130 |
|
45 | 131 | Asciidoctor::Extensions.register do
|
46 | 132 | inline_macro Git::Documentation::LinkGitProcessor, :linkgit
|
| 133 | + block Git::Documentation::SynopsisBlock |
47 | 134 | postprocessor Git::Documentation::DocumentPostProcessor
|
48 | 135 | end
|
0 commit comments