Skip to content

Commit

Permalink
Merge pull request #351 from metanorma/features/external-log-init
Browse files Browse the repository at this point in the history
  • Loading branch information
opoudjis authored Mar 22, 2024
2 parents f64f7b9 + c97015c commit 2a2127e
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 17 deletions.
2 changes: 2 additions & 0 deletions Gemfile.devel
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
gem "metanorma-standoc", git: "https://github.com/metanorma/metanorma-standoc", branch: "main"
gem "metanorma-utils", git: "https://github.com/metanorma/metanorma-utils", branch: "features/late-filename-set"
19 changes: 16 additions & 3 deletions lib/metanorma/compile.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,31 @@ def initialize
@errors = []
@isodoc = IsoDoc::Convert.new({})
@fontist_installed = false
@log = Metanorma::Utils::Log.new
end

def compile(filename, options = {})
require_libraries(options)
options = options_extract(filename, options)
validate_options(options)
options_process(filename, options)
@processor = @registry.find_processor(options[:type].to_sym)
extensions = get_extensions(options) or return nil
(file, isodoc = process_input(filename, options)) or return nil
relaton_export(isodoc, options)
extract(isodoc, options[:extract], options[:extract_type])
process_exts(filename, extensions, file, isodoc, options)
clean_exit(options)
end

def options_process(filename, options)
require_libraries(options)
options = options_extract(filename, options)
validate_options(options)
@log.save_to(filename, options[:output_dir])
options[:log] = @log
end

def clean_exit(options)
options[:novalid] and return
@log.write
end

def process_input(filename, options)
Expand Down
1 change: 1 addition & 0 deletions lib/metanorma/compile_options.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ def options_extract(filename, options)
options[:format] ||= :asciidoc
options[:filename] = filename
options[:fontlicenseagreement] ||= "no-install-fonts"
options[:novalid] = o[:novalid] if o[:novalid]
options
end

Expand Down
17 changes: 9 additions & 8 deletions lib/metanorma/input/asciidoc.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ 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", "docfile=#{filename}",
"output_dir=#{options[:output_dir]}"]
}
out_opts = { to_file: false, safe: :safe, backend: type,
header_footer: true, log: options[:log],
novalid: options[:novalid],
attributes: ["nodoc", "stem", "docfile=#{filename}",
"output_dir=#{options[:output_dir]}"] }
unless asciidoctor_validate(file, filename, out_opts)
warn "Cannot continue compiling Asciidoctor document"
abort
Expand All @@ -24,7 +24,7 @@ def asciidoctor_validate(file, filename, options)
$stderr = StringIO.new
::Asciidoctor.load(file, options)
%r{(\n|^)asciidoctor: ERROR: ['"]?#{Regexp.escape(filename ||
"<empty>")}['"]?: line \d+: include file not found: }
'<empty>')}['"]?: line \d+: include file not found: }
.match($stderr.string) and err = $stderr.string
ensure
$stderr = previous_stderr
Expand All @@ -39,15 +39,16 @@ def extract_metanorma_options(file)
/\n:mn-output-extensions:\s+(?<extensions>[^\n]+)\n/ =~ headerextract
/\n:mn-relaton-output-file:\s+(?<relaton>[^\n]+)\n/ =~ headerextract
/\n(?<asciimath>:mn-keep-asciimath:[^\n]*)\n/ =~ headerextract
/\n(?<novalid>:novalid:[^\n]*)\n/ =~ headerextract
asciimath = if defined?(asciimath)
(!asciimath.nil? && asciimath != ":mn-keep-asciimath: false")
!asciimath.nil? && asciimath != ":mn-keep-asciimath: false"
end
asciimath = nil if asciimath == false
{
type: defined?(type) ? type&.strip : nil,
extensions: defined?(extensions) ? extensions&.strip : nil,
relaton: defined?(relaton) ? relaton&.strip : nil,
asciimath: asciimath,
asciimath: asciimath, novalid: !novalid.nil? || nil
}.compact
end

Expand Down
9 changes: 3 additions & 6 deletions spec/input_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -196,23 +196,20 @@
:mn-output-extensions: b
:mn-relaton-output-file: c
:mn-keep-asciimath:
:novalid:
INPUT
output = <<~OUTPUT
{:asciimath=>true, :extensions=>"b", :relaton=>"c", :type=>"a"}
{:asciimath=>true, :extensions=>"b", :novalid=>true, :relaton=>"c", :type=>"a"}
OUTPUT
expect(Metanorma::Input::Asciidoc.new
.extract_metanorma_options(input).sort.to_h.to_s + "\n").to eq output

input = <<~INPUT
= Document title
Author
:mn-document-class: a
:mn-output-extensions: b
:mn-relaton-output-file: c
:mn-keep-asciimath:
INPUT
expect(Metanorma::Input::Asciidoc.new
.extract_metanorma_options(input).sort.to_h.to_s + "\n").to eq output
.extract_metanorma_options(input).sort.to_h.to_s + "\n").to eq "{}\n"
end

it "extracts Asciidoctor document attributes for Metanorma" do
Expand Down

0 comments on commit 2a2127e

Please sign in to comment.