Class: Metanorma::Input::Asciidoc

Inherits:
Base
  • Object
show all
Defined in:
lib/metanorma/input/asciidoc.rb

Instance Method Summary collapse

Instance Method Details

#asciidoctor_validate(file, filename, options) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/metanorma/input/asciidoc.rb', line 27

def asciidoctor_validate(file, filename, options)
  err = nil
  begin
    previous_stderr, $stderr = $stderr, StringIO.new
    ::Asciidoctor.load(file, options)
    %r{(\n|^)asciidoctor: ERROR: ['"]?#{Regexp.escape(filename || 
      "<empty>")}['"]?: line \d+: include file not found: }.match($stderr.string) and
        err = $stderr.string
  ensure
    $stderr = previous_stderr
  end
  warn err unless err.nil?
  err.nil?
end

#empty_attr(attr, name) ⇒ Object



59
60
61
# File 'lib/metanorma/input/asciidoc.rb', line 59

def empty_attr(attr, name)
  attr&.sub(/^#{name}:\s*$/, "#{name}: true")&.sub(/^#{name}:\s+/, "")
end

#extract_metanorma_options(file) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/metanorma/input/asciidoc.rb', line 42

def extract_metanorma_options(file)
  headerextract = file.sub(/\n\n.*$/m, "\n")
  /\n:mn-document-class: (?<type>[^\n]+)\n/ =~ headerextract
  /\n:mn-output-extensions: (?<extensions>[^\n]+)\n/ =~ headerextract
  /\n:mn-relaton-output-file: (?<relaton>[^\n]+)\n/ =~ headerextract
  /\n(?<asciimath>:mn-keep-asciimath:[^\n]*)\n/ =~ headerextract
  asciimath = defined?(asciimath) ?
    (!asciimath.nil? && asciimath != ":mn-keep-asciimath: false") : nil
  asciimath = nil if asciimath == false
  {
    type: defined?(type) ? type : nil,
    extensions: defined?(extensions) ? extensions : nil,
    relaton: defined?(relaton) ? relaton : nil,
    asciimath: asciimath,
  }.reject { |_, val| val.nil? }
end

#extract_options(file) ⇒ Object



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
# File 'lib/metanorma/input/asciidoc.rb', line 63

def extract_options(file)
  header = file.sub(/\n\n.*$/m, "\n")
  ret = %w(htmlstylesheet htmlcoverpage htmlintropage scripts
           scripts-pdf wordstylesheet
           standardstylesheet header wordcoverpage wordintropage i18nyaml
           ulstyle olstyle htmlstylesheet-override
           htmltoclevels doctoclevels sectionsplit
           body-font header-font monospace-font title-font
           wordstylesheet-override).each_with_object({}) do |w, acc|
    m = /\n:#{w}: ([^\n]+)\n/.match(header) or next
    acc[w.gsub(/-/, "").sub(/override$/, "_override")
      .sub(/pdf$/, "_pdf").to_sym] = m[1]
  end
  /\n:data-uri-image: (?<datauriimage>[^\n]+)\n/ =~ header
  /\n:(?<hierarchical_assets>hierarchical-assets:[^\n]*)\n/ =~ header
  /\n:(?<use_xinclude>use-xinclude:[^\n]*)\n/ =~ header
  /\n:(?<break_up>break-up-urls-in-tables:[^\n]*)\n/ =~ header

  defined?(hierarchical_assets) and
    hierarchical_assets = empty_attr(hierarchical_assets, "hierarchical-assets")
  defined?(use_xinclude) and
    use_xinclude = empty_attr(use_xinclude, "use-xinclude")
  defined?(break_up) and
    break_up = empty_attr(break_up, "break-up-urls-in-tables")
  ret.merge({
    datauriimage: defined?(datauriimage) ? datauriimage != "false" : nil,
    hierarchical_assets: defined?(hierarchical_assets) ? hierarchical_assets : nil,
    use_xinclude: defined?(use_xinclude) ? use_xinclude : nil,
    break_up_urls_in_tables: defined?(break_up) ? break_up : nil,
  }).reject { |_, val| val.nil? }
end

#process(file, filename, type, options = {}) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/metanorma/input/asciidoc.rb', line 8

def process(file, filename, type, options = {})
  require "asciidoctor"
  out_opts = {
    to_file: false,
    safe: :safe,
    backend: type,
    header_footer: true,
    attributes: [
      "nodoc", "stem", "xrefstyle=short", "docfile=#{filename}",
      "output_dir=#{options[:output_dir]}"
    ]
  }
  unless asciidoctor_validate(file, filename, out_opts)
    warn "Cannot continue compiling Asciidoctor document"
    abort
  end
  ::Asciidoctor.convert(file, out_opts)
end