require "nokogiri" module Metanorma module Input class Asciidoc < Base 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 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 || "")}['"]?: 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 def extract_metanorma_options(file) headerextract = file.sub(/\n\n.*$/m, "\n") /\n:mn-document-class: (?[^\n]+)\n/ =~ headerextract /\n:mn-output-extensions: (?[^\n]+)\n/ =~ headerextract /\n:mn-relaton-output-file: (?[^\n]+)\n/ =~ headerextract /\n(?: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 def empty_attr(attr, name) attr&.sub(/^#{name}:\s*$/, "#{name}: true")&.sub(/^#{name}:\s+/, "") end def extract_options(file) headerextract = file.sub(/\n\n.*$/m, "\n") /\n:script: (?