File: asciidoctor-unicodeconverter.rb

package info (click to toggle)
util-linux 2.41-5
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 95,208 kB
  • sloc: ansic: 179,016; sh: 22,689; yacc: 1,284; makefile: 528; xml: 422; python: 316; lex: 89; ruby: 75; csh: 37; exp: 19; sed: 16; perl: 15; sql: 9
file content (50 lines) | stat: -rw-r--r-- 1,445 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
# Copyright (C) 2023 Thomas Weißschuh <thomas@t-8ch.de>
# Extension for asciidoctor to remove unicode dash in first section of manpage

require 'asciidoctor/extensions'

module UnicodeConverter
  BEFORE_NAME_SECTION = 1
  IN_NAME_SECTION = 2
  AFTER_NAME_SECTION = 3

  class Preprocessor < Asciidoctor::Extensions::Preprocessor
    include Asciidoctor::Logging

    def process document, reader
      lines = reader.read_lines
      state = BEFORE_NAME_SECTION
      command = document.attributes['docname'].rpartition('.')[0]
      lines.map! do |line|
        if state == IN_NAME_SECTION
          if line.sub! " \u2013 ", " - " or line.sub! " \u2014 ", " - "
            logger.warn "replacing unicode dash in name section of #{document.attributes['docfile']}"
          end

          if line.start_with? command and not line.include? ',' and not line.start_with? "#{command} - "
            logger.warn "adding dash to name section of #{document.attributes['docfile']}"
            line.sub! command, "#{command} - "
          end
        end

        if line.start_with? '== '
          if state == BEFORE_NAME_SECTION
            state = IN_NAME_SECTION
          else if state == IN_NAME_SECTION
            state = AFTER_NAME_SECTION
          end
        end
      end

        line
      end
      reader.restore_lines lines
      reader
    end
  end

  Asciidoctor::Extensions.register do
    preprocessor Preprocessor
  end

end