File: roff.rb

package info (click to toggle)
ruby-ronn 0.10.1-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 764 kB
  • sloc: ruby: 1,587; sh: 22; makefile: 9
file content (393 lines) | stat: -rw-r--r-- 11,338 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
require 'nokogiri'
require 'ronn/utils'

module Ronn
  # Filter for converting HTML to ROFF
  class RoffFilter
    include Ronn::Utils

    # Convert Ronn HTML to roff.
    # The html input is an HTML fragment, not a complete document
    def initialize(html_fragment, name, section, tagline, manual = nil,
                   version = nil, date = nil)
      @buf = []
      title_heading name, section, tagline, manual, version, date
      doc = Nokogiri::HTML.fragment(html_fragment)
      remove_extraneous_elements! doc
      normalize_whitespace! doc
      block_filter doc
      write "\n"
    end

    def to_s
      @buf.join.gsub(/[ \t]+$/, '')
    end

    protected

    def previous(node)
      return unless node.respond_to?(:previous)
      prev = node.previous
      prev = prev.previous until prev.nil? || prev.elem?
      prev
    end

    def title_heading(name, section, _tagline, manual, version, date)
      comment "generated with Ronn-NG/v#{Ronn.version}"
      comment "http://github.com/apjanke/ronn-ng/tree/#{Ronn.revision}"
      return if name.nil?
      if manual
        macro 'TH', %("#{escape(name.upcase)}" "#{section}" "#{date.strftime('%B %Y')}" "#{version}" "#{manual}")
      else
        macro 'TH', %("#{escape(name.upcase)}" "#{section}" "#{date.strftime('%B %Y')}" "#{version}")
      end
    end

    def remove_extraneous_elements!(doc)
      doc.traverse do |node|
        node.parent.children.delete(node) if node.comment?
      end
    end

    def normalize_whitespace!(node)
      if node.is_a?(Array) || node.is_a?(Nokogiri::XML::NodeSet)
        node.to_a.dup.each { |ch| normalize_whitespace! ch }
      elsif node.text?
        preceding = node.previous
        following = node.next
        content = node.content.gsub(/[\n ]+/m, ' ')
        if preceding.nil? || block_element?(preceding.name) ||
           preceding.name == 'br'
          content.lstrip!
        end
        if following.nil? || block_element?(following.name) ||
           following.name == 'br'
          content.rstrip!
        end
        if content.empty?
          node.remove
        else
          node.content = content
        end
      elsif node.elem? && node.name == 'pre'
        # stop traversing
      elsif node.elem? && node.children
        normalize_whitespace! node.children
      elsif node.elem?
        # element has no children
      elsif node.document? || node.fragment?
        normalize_whitespace! node.children
      elsif node.is_a?(Nokogiri::XML::DTD) || node.is_a?(Nokogiri::XML::Comment)
        # ignore
        nop
      else
        warn 'unexpected node during whitespace normalization: %p', node
      end
    end

    def block_filter(node)
      return if node.nil?

      if node.is_a?(Array) || node.is_a?(Nokogiri::XML::NodeSet)
        node.each { |ch| block_filter(ch) }

      elsif node.document? || node.fragment?
        block_filter(node.children)

      elsif node.text?
        # This hack is necessary to support mixed-child-type dd's
        inline_filter(node)

      elsif node.elem?
        case node.name
        when 'html', 'body'
          block_filter(node.children)
        when 'div'
          block_filter(node.children)
        when 'h1'
          # discard
          nop
        when 'h2'
          macro 'SH', quote(escape(node.inner_html))
        when 'h3'
          macro 'SS', quote(escape(node.inner_html))
        when 'h4', 'h5', 'h6'
          # Ronn discourages use of this many heading levels, but if they are used,
          # we should make them legible instead of ignoring them.
          macro 'SS', quote(escape(node.inner_html))

        when 'p'
          prev = previous(node)
          if prev && %w[dd li blockquote].include?(node.parent.name)
            macro 'IP'
          elsif prev && !%w[h1 h2 h3].include?(prev.name)
            macro 'P'
          elsif node.previous&.text?
            macro 'IP'
          end
          inline_filter(node.children)

        when 'blockquote'
          prev = previous(node)
          indent = prev.nil? || !%w[h1 h2 h3].include?(prev.name)
          macro 'IP', %w["" 4] if indent
          block_filter(node.children)
          macro 'IP', %w["" 0] if indent

        when 'pre'
          prev = previous(node)
          indent = prev.nil? || !%w[h1 h2 h3].include?(prev.name)
          macro 'IP', %w["" 4] if indent
          macro 'nf'
          # HACK: strip an initial \n to avoid extra spacing
          if node.children && node.children[0].text?
            text = node.children[0].to_s
            node.children[0].replace(text[1..]) if text.start_with? "\n"
          end
          inline_filter(node.children)
          macro 'fi'
          macro 'IP', %w["" 0] if indent

        when 'dl'
          macro 'TP'
          block_filter(node.children)
        when 'dt'
          prev = previous(node)
          macro 'TP' unless prev.nil?
          inline_filter(node.children)
          write "\n"
        when 'dd'
          if node.at('p')
            block_filter(node.children)
          else
            inline_filter(node.children)
          end
          write "\n"

        when 'ol', 'ul'
          block_filter(node.children)
          macro 'IP', %w["" 0]
        when 'li'
          case node.parent.name
          when 'ol'
            macro 'IP', %W["#{node.parent.children.index(node) + 1}." 4]
          when 'ul'
            macro 'IP', ['"\(bu"', '4']
          else
            raise "List element found as a child of non-list parent element: #{node.inspect}"
          end
          if node.at('p,ol,ul,dl,div')
            block_filter(node.children)
          else
            inline_filter(node.children)
          end
          write "\n"

        when 'span', 'code', 'b', 'strong', 'kbd', 'samp', 'var', 'em', 'i',
             'u', 'br', 'a'
          inline_filter(node)

        when 'table'
          macro 'TS'
          write "allbox;\n"
          block_filter(node.children)
          macro 'TE'
        when 'thead'
          # Convert to format section and first row
          tr = node.children[0]
          header_contents = []
          cell_formats = []
          tr.children.each do |th|
            style = th['style']
            cell_format = case style
                          when 'text-align:left;'
                            'l'
                          when 'text-align:right;'
                            'r'
                          when 'text-align:center;'
                            'c'
                          else
                            'l'
                          end
            header_contents << th.inner_html
            cell_formats << cell_format
          end
          write cell_formats.join(' ') + ".\n"
          write header_contents.join("\t") + "\n"
        when 'th'
          raise 'internal error: unexpected <th> element'
        when 'tbody'
          # Let the 'tr' handle it
          block_filter(node.children)
        when 'tr'
          # Convert to a table data row
          node.children.each do |child|
            block_filter(child)
            write "\t"
          end
          write "\n"
        when 'td'
          inline_filter(node.children)

        else
          warn 'unrecognized block tag: %p', node.name
        end

      elsif node.is_a?(Nokogiri::XML::DTD)
        # Ignore
        nop
      elsif node.is_a?(Nokogiri::XML::Comment)
        # Ignore
        nop
      else
        raise "unexpected node: #{node.inspect}"
      end
    end

    def inline_filter(node)
      return unless node # is an empty node

      if node.is_a?(Array) || node.is_a?(Nokogiri::XML::NodeSet)
        node.each { |ch| inline_filter(ch) }

      elsif node.text?
        text = node.to_html.dup
        write escape(text)

      elsif node.comment?
        # ignore HTML comments

      elsif node.elem?
        case node.name
        when 'span'
          inline_filter(node.children)

        when 'code'
          if child_of?(node, 'pre')
            inline_filter(node.children)
          else
            write '\fB'
            inline_filter(node.children)
            write '\fR'
          end

        when 'b', 'strong', 'kbd', 'samp'
          write '\fB'
          inline_filter(node.children)
          write '\fR'

        when 'var', 'em', 'i', 'u'
          write '\fI'
          inline_filter(node.children)
          write '\fR'

        when 'br'
          macro 'br'

        when 'a'
          if node.classes.include?('man-ref')
            inline_filter(node.children)
          elsif node.has_attribute?('data-bare-link')
            write '\fI'
            inline_filter(node.children)
            write '\fR'
          else
            inline_filter(node.children)
            write ' '
            write '\fI'
            write escape(node.attributes['href'].content)
            write '\fR'
          end

        when 'sup'
          # This superscript equivalent is a big ugly hack.
          write '^('
          inline_filter(node.children)
          write ')'

        else
          warn 'unrecognized inline tag: %p', node.name
        end

      else
        raise "unexpected node: #{node.inspect}"
      end
    end

    def maybe_new_line
      write "\n" if @buf.last && @buf.last[-1] != "\n"
    end

    def macro(name, value = nil)
      maybe_new_line
      writeln ".#{[name, value].compact.join(' ')}"
    end

    HTML_ROFF_ENTITIES = {
      '•' => '\(bu',
      '&lt;' => '<',
      '&gt;' => '>',
      ' ' => '\~', # That's a literal non-breaking space character there
      '©' => '\(co',
      '”' => '\(rs',
      '—' => '\(em',
      '®' => '\(rg',
      '§' => '\(sc',
      '≥' => '\(>=',
      '≤' => '\(<=',
      '≠' => '\(!=',
      '≡' => '\(=='
    }.freeze

    def escape(text)
      return text.to_s if text.nil? || text.empty?
      ent = HTML_ROFF_ENTITIES
      text = text.dup
      text.gsub!(/&#x([0-9A-Fa-f]+);/) { $1.to_i(16).chr }  # hex entities
      text.gsub!(/&#(\d+);/) { $1.to_i.chr }                # dec entities
      text.gsub!('\\', '\e')                                # backslash
      text.gsub!('...', '\|.\|.\|.')                        # ellipses
      text.gsub!(/[.-]/) { |m| "\\#{m}" }                   # control chars
      ent.each do |key, val|
        text.gsub!(key, val)
      end
      text.gsub!('&amp;', '&')                              # amps
      text
    end

    def quote(text)
      "\"#{text.gsub('"', '\\"')}\""
    end

    # write text to output buffer
    def write(text)
      return if text.nil? || text.empty?
      # lines cannot start with a '.' or "'". insert zero-width character before.
      text = text.gsub("\n\\.", "\n\\\\&\\.")
      text = text.gsub("\n'", "\n\\&\\'")
      buf_ends_in_newline = @buf.last && @buf.last[-1] == "\n"
      @buf << '\&' if text[0, 2] == '\.' && buf_ends_in_newline
      @buf << '\&' if text[0, 1] == "'" && buf_ends_in_newline
      @buf << text
    end

    # write text to output buffer on a new line.
    def writeln(text)
      maybe_new_line
      write text
      write "\n"
    end

    def comment(text)
      writeln %(.\\" #{text})
    end

    def warn(text, *args)
      Kernel.warn format("warn: #{text}", args)
    end

    def nop
      # Do nothing
    end
  end
end