Class: Metanorma::Standoc::NamedEscapePreprocessor

Inherits:
Asciidoctor::Extensions::Preprocessor
  • Object
show all
Defined in:
lib/metanorma/converter/macros.rb

Instance Method Summary collapse

Instance Method Details

#convert(line, esc) ⇒ Object



64
65
66
67
68
# File 'lib/metanorma/converter/macros.rb', line 64

def convert(line, esc)
  line.split(/(&[A-Za-z][^&;]*;)/).map do |s|
    /^&[A-Za-z]/.match?(s) ? esc.encode(esc.decode(s), :hexadecimal) : s
  end.join
end

#log(doc, text) ⇒ Object

debugging output of results of all preprocessing, including include files concatenation and Lutaml/Liquid processing.

Skipped for docfile-less (string/stdin) input: the target would degenerate to a single CWD-relative "./metanorma.asciidoc.log.txt" that the thousands of collection sub-compiles all truncate-rewrite (so it carries no useful diagnostics anyway). And a diagnostic write must never abort the build, so a write failure degrades to a warning rather than propagating out of the preprocessor. metanorma/metanorma-standoc#1209



79
80
81
82
83
84
85
86
87
# File 'lib/metanorma/converter/macros.rb', line 79

def log(doc, text)
  source = doc.attr("docfile") or return
  dirname  = File.dirname(source)
  basename = File.basename(source, ".*")
  fname = File.join(dirname, "#{basename}.asciidoc.log.txt")
  File.open(fname, "w:UTF-8") { |f| f.write(text.join("\n")) }
rescue SystemCallError => e
  warn "metanorma: could not write preprocessing log #{fname}: #{e.message}"
end

#process(document, reader) ⇒ Object



53
54
55
56
57
58
59
60
61
62
# File 'lib/metanorma/converter/macros.rb', line 53

def process(document, reader)
  c = HTMLEntities.new
  p = Metanorma::Utils::LineStatus.new
  lines = reader.lines.map do |l|
    p.process(l)
    p.pass ? l : convert(l, c)
  end
  log(document, lines)
  ::Asciidoctor::PreprocessorReader.new document, lines
end