summaryrefslogtreecommitdiff
path: root/vendor/bundle/ruby/3.4.0/gems/kramdown-2.5.2/lib/kramdown/converter/man.rb
blob: dd84540dacc40fc3eb47b56def9d804dfe3fbb39 (plain)
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
# -*- coding: utf-8; frozen_string_literal: true -*-
#
#--
# Copyright (C) 2009-2026 Thomas Leitner <t_leitner@gmx.at>
#
# This file is part of kramdown which is licensed under the MIT.
#++
#

require 'kramdown/converter'

module Kramdown

  module Converter

    # Converts a Kramdown::Document to a manpage in groff format. See man(7), groff_man(7) and
    # man-pages(7) for information regarding the output.
    class Man < Base

      def convert(el, opts = {indent: 0, result: +''}) # :nodoc:
        send("convert_#{el.type}", el, opts)
      end

      private

      def inner(el, opts, use = :all)
        arr = el.children.reject {|e| e.type == :blank }
        arr.each_with_index do |inner_el, index|
          next if use == :rest && index == 0
          break if use == :first && index > 0
          options = opts.dup
          options[:parent] = el
          options[:index] = index
          options[:prev] = (index == 0 ? nil : arr[index - 1])
          options[:next] = (index == arr.length - 1 ? nil : arr[index + 1])
          convert(inner_el, options)
        end
      end

      def convert_root(el, opts)
        @title_done = false
        opts[:result] = +".\\\" generated by kramdown\n"
        inner(el, opts)
        opts[:result]
      end

      def convert_blank(*)
      end
      alias convert_hr convert_blank
      alias convert_xml_pi convert_blank

      def convert_p(el, opts)
        if (opts[:index] != 0 && opts[:prev].type != :header) ||
            (opts[:parent].type == :blockquote && opts[:index] == 0)
          opts[:result] << macro("P")
        end
        inner(el, opts)
        newline(opts[:result])
      end

      def convert_header(el, opts)
        return unless opts[:parent].type == :root
        case el.options[:level]
        when 1
          unless @title_done
            @title_done = true
            data = el.options[:raw_text].scan(/([^(]+)\s*\((\d\w*)\)(?:\s*-+\s*(.*))?/).first ||
              el.options[:raw_text].scan(/([^\s]+)\s*(?:-*\s+)?()(.*)/).first
            return unless data && data[0]
            name = data[0]
            section = (data[1].to_s.empty? ? el.attr['data-section'] || '7' : data[1])
            description = (data[2].to_s.empty? ? nil : " - #{data[2]}")
            date = el.attr['data-date'] ? quote(el.attr['data-date']) : nil
            extra = (el.attr['data-extra'] ? quote(escape(el.attr['data-extra'].to_s)) : nil)
            opts[:result] << macro("TH", quote(escape(name.upcase)), quote(section), date, extra)
            if description
              opts[:result] << macro("SH", "NAME") << escape("#{name}#{description}") << "\n"
            end
          end
        when 2
          opts[:result] << macro("SH", quote(escape(el.options[:raw_text])))
        when 3
          opts[:result] << macro("SS", quote(escape(el.options[:raw_text])))
        else
          warning("Header levels greater than three are not supported")
        end
      end

      def convert_codeblock(el, opts)
        opts[:result] << macro("sp") << macro("RS", 4) << macro("EX")
        opts[:result] << newline(escape(el.value, true))
        opts[:result] << macro("EE") << macro("RE")
      end

      def convert_blockquote(el, opts)
        opts[:result] << macro("RS")
        inner(el, opts)
        opts[:result] << macro("RE")
      end

      def convert_ul(el, opts)
        compact = (el.attr['class'] =~ /\bcompact\b/)
        opts[:result] << macro("sp") << macro("PD", 0) if compact
        inner(el, opts)
        opts[:result] << macro("PD") if compact
      end
      alias convert_dl convert_ul
      alias convert_ol convert_ul

      def convert_li(el, opts)
        sym = (opts[:parent].type == :ul ? '\(bu' : "#{opts[:index] + 1}.")
        opts[:result] << macro("IP", sym, 4)
        inner(el, opts, :first)
        if el.children.size > 1
          opts[:result] << macro("RS")
          inner(el, opts, :rest)
          opts[:result] << macro("RE")
        end
      end

      def convert_dt(el, opts)
        opts[:result] << macro(opts[:prev] && opts[:prev].type == :dt ? "TQ" : "TP")
        inner(el, opts)
        opts[:result] << "\n"
      end

      def convert_dd(el, opts)
        inner(el, opts, :first)
        if el.children.size > 1
          opts[:result] << macro("RS")
          inner(el, opts, :rest)
          opts[:result] << macro("RE")
        end
        opts[:result] << macro("sp") if opts[:next] && opts[:next].type == :dd
      end

      TABLE_CELL_ALIGNMENT = {left: 'l', center: 'c', right: 'r', default: 'l'}

      def convert_table(el, opts)
        opts[:alignment] = el.options[:alignment].map {|a| TABLE_CELL_ALIGNMENT[a] }
        table_options = ["box"]
        table_options << "center" if el.attr['class']&.match?(/\bcenter\b/)
        opts[:result] << macro("TS") << "#{table_options.join(' ')} ;\n"
        inner(el, opts)
        opts[:result] << macro("TE") << macro("sp")
      end

      def convert_thead(el, opts)
        opts[:result] << opts[:alignment].map {|a| "#{a}b" }.join(' ') << " .\n"
        inner(el, opts)
        opts[:result] << "=\n"
      end

      def convert_tbody(el, opts)
        opts[:result] << ".T&\n" if opts[:index] != 0
        opts[:result] << opts[:alignment].join(' ') << " .\n"
        inner(el, opts)
        opts[:result] << (opts[:next].type == :tfoot ? "=\n" : "_\n") if opts[:next]
      end

      def convert_tfoot(el, opts)
        inner(el, opts)
      end

      def convert_tr(el, opts)
        inner(el, opts)
        opts[:result] << "\n"
      end

      def convert_td(el, opts)
        result = opts[:result]
        opts[:result] = +''
        inner(el, opts)
        if opts[:result].include?("\n")
          warning("Table cells using links are not supported")
          result << "\t"
        else
          result << opts[:result] << "\t"
        end
      end

      def convert_html_element(*)
        warning("HTML elements are not supported")
      end

      def convert_xml_comment(el, opts)
        newline(opts[:result]) << ".\"#{escape(el.value, true).rstrip.gsub(/\n/, "\n.\"")}\n"
      end
      alias convert_comment convert_xml_comment

      def convert_a(el, opts)
        if el.children.size == 1 && el.children[0].type == :text &&
            el.attr['href'] == el.children[0].value
          newline(opts[:result]) << macro("UR", escape(el.attr['href'])) << macro("UE")
        elsif el.attr['href'].start_with?('mailto:')
          newline(opts[:result]) << macro("MT", escape(el.attr['href'].sub(/^mailto:/, ''))) <<
            macro("UE")
        else
          newline(opts[:result]) << macro("UR", escape(el.attr['href']))
          inner(el, opts)
          newline(opts[:result]) << macro("UE")
        end
      end

      def convert_img(_el, _opts)
        warning("Images are not supported")
      end

      def convert_em(el, opts)
        opts[:result] << '\fI'
        inner(el, opts)
        opts[:result] << '\fP'
      end

      def convert_strong(el, opts)
        opts[:result] << '\fB'
        inner(el, opts)
        opts[:result] << '\fP'
      end

      def convert_codespan(el, opts)
        opts[:result] << "\\fB#{escape(el.value)}\\fP"
      end

      def convert_br(_el, opts)
        newline(opts[:result]) << macro("br")
      end

      def convert_abbreviation(el, opts)
        opts[:result] << escape(el.value)
      end

      def convert_math(el, opts)
        if el.options[:category] == :block
          convert_codeblock(el, opts)
        else
          convert_codespan(el, opts)
        end
      end

      def convert_footnote(*)
        warning("Footnotes are not supported")
      end

      def convert_raw(*)
        warning("Raw content is not supported")
      end

      def convert_text(el, opts)
        text = escape(el.value)
        text.lstrip! if opts[:result][-1] == "\n"
        opts[:result] << text
      end

      def convert_entity(el, opts)
        opts[:result] << unicode_char(el.value.code_point)
      end

      def convert_smart_quote(el, opts)
        opts[:result] << unicode_char(::Kramdown::Utils::Entities.entity(el.value.to_s).code_point)
      end

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

      def convert_typographic_sym(el, opts)
        opts[:result] << TYPOGRAPHIC_SYMS_MAP[el.value]
      end

      def macro(name, *args)
        ".#{[name, *args].compact.join(' ')}\n"
      end

      def newline(text)
        text << "\n" unless text[-1] == "\n"
        text
      end

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

      def escape(text, preserve_whitespace = false)
        text = (preserve_whitespace ? text.dup : text.gsub(/\s+/, ' '))
        text.gsub!('\\', "\\e")
        text.gsub!(/^\./, '\\\\&.')
        text.gsub!(/[.'-]/) {|m| "\\#{m}" }
        text
      end

      def unicode_char(codepoint)
        "\\[u#{codepoint.to_s(16).rjust(4, '0')}]"
      end

    end

  end
end