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

def extract_options(file)
  headerextract = file.sub(/\n\n.*$/m, "\n")

  /\n:script: (?<script>[^\n]+)\n/ =~ headerextract
  /\n:body-font: (?<bodyfont>[^\n]+)\n/ =~ headerextract
  /\n:header-font: (?<headerfont>[^\n]+)\n/ =~ headerextract
  /\n:monospace-font: (?<monospacefont>[^\n]+)\n/ =~ headerextract
  /\n:title-font: (?<titlefont>[^\n]+)\n/ =~ headerextract
  /\n:i18nyaml: (?<i18nyaml>[^\n]+)\n/ =~ headerextract
  /\n:htmlstylesheet: (?<htmlstylesheet>[^\n]+)\n/ =~ headerextract
  /\n:htmlcoverpage: (?<htmlcoverpage>[^\n]+)\n/ =~ headerextract
  /\n:htmlintropage: (?<htmlintropage>[^\n]+)\n/ =~ headerextract
  /\n:scripts: (?<scripts>[^\n]+)\n/ =~ headerextract
  /\n:scripts-pdf: (?<scripts_pdf>[^\n]+)\n/ =~ headerextract
  /\n:wordstylesheet: (?<wordstylesheet>[^\n]+)\n/ =~ headerextract
  /\n:standardstylesheet: (?<standardstylesheet>[^\n]+)\n/ =~ headerextract
  /\n:header: (?<header>[^\n]+)\n/ =~ headerextract
  /\n:wordcoverpage: (?<wordcoverpage>[^\n]+)\n/ =~ headerextract
  /\n:wordintropage: (?<wordintropage>[^\n]+)\n/ =~ headerextract
  /\n:ulstyle: (?<ulstyle>[^\n]+)\n/ =~ headerextract
  /\n:olstyle: (?<olstyle>[^\n]+)\n/ =~ headerextract
  /\n:data-uri-image: (?<datauriimage>[^\n]+)\n/ =~ headerextract
  /\n:htmltoclevels: (?<htmltoclevels>[^\n]+)\n/ =~ headerextract
  /\n:doctoclevels: (?<doctoclevels>[^\n]+)\n/ =~ headerextract
  /\n:(?<hierarchical_assets>hierarchical-assets:[^\n]*)\n/ =~ headerextract
  /\n:(?<use_xinclude>use-xinclude:[^\n]*)\n/ =~ headerextract
  /\n:(?<break_up_urls_in_tables>break-up-urls-in-tables:[^\n]*)\n/ =~ headerextract

  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_urls_in_tables) and
    break_up_urls_in_tables = empty_attr(break_up_urls_in_tables, "break-up-urls-in-tables")
  {
    script: defined?(script) ? script : nil,
    bodyfont: defined?(bodyfont) ? bodyfont : nil,
    headerfont: defined?(headerfont) ? headerfont : nil,
    monospacefont: defined?(monospacefont) ? monospacefont : nil,
    titlefont: defined?(titlefont) ? titlefont : nil,
    i18nyaml: defined?(i18nyaml) ? i18nyaml : nil,
    htmlstylesheet: defined?(htmlstylesheet) ? htmlstylesheet : nil,
    htmlcoverpage: defined?(htmlcoverpage) ? htmlcoverpage : nil,
    htmlintropage: defined?(htmlintropage) ? htmlintropage : nil,
    scripts: defined?(scripts) ? scripts : nil,
    scripts_pdf: defined?(scripts_pdf) ? scripts_pdf : nil,
    wordstylesheet: defined?(wordstylesheet) ? wordstylesheet : nil,
    standardstylesheet: defined?(standardstylesheet) ? standardstylesheet : nil,
    header: defined?(header) ? header : nil,
    wordcoverpage: defined?(wordcoverpage) ? wordcoverpage : nil,
    wordintropage: defined?(wordintropage) ? wordintropage : nil,
    ulstyle: defined?(ulstyle) ? ulstyle : nil,
    olstyle: defined?(olstyle) ? olstyle : nil,
    datauriimage: defined?(datauriimage) ? datauriimage != "false" : nil,
    htmltoclevels: defined?(htmltoclevels) ? htmltoclevels : nil,
    doctoclevels: defined?(doctoclevels) ? doctoclevels : 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_urls_in_tables) ? break_up_urls_in_tables : 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