Skip to content

Commit

Permalink
add log of generated Lutaml Preprocessor output: #100
Browse files Browse the repository at this point in the history
  • Loading branch information
opoudjis committed Dec 14, 2023
1 parent 697e623 commit 97734dc
Show file tree
Hide file tree
Showing 5 changed files with 132 additions and 50 deletions.
41 changes: 28 additions & 13 deletions lib/metanorma/plugin/lutaml/lutaml_preprocessor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,24 +12,38 @@ module Plugin
module Lutaml
# Class for processing Lutaml files
class LutamlPreprocessor < Asciidoctor::Extensions::Preprocessor
REMARKS_ATTRIBUTE = "remarks".freeze
REMARKS_ATTRIBUTE = "remarks"

def process(document, reader)
input_lines = reader.readlines.to_enum
has_lutaml = !input_lines.select { |x| lutaml?(x) }.empty?
express_indexes = Utils.parse_document_express_indexes(
document,
input_lines
input_lines,
)
result_content = processed_lines(document, input_lines, express_indexes)
result_reader = Asciidoctor::PreprocessorReader.new(document, result_content)
result_reader
result_content = processed_lines(document, input_lines,
express_indexes)
has_lutaml and log(document, result_content)
Asciidoctor::PreprocessorReader.new(document, result_content)
end

protected

def log(document, result)
File.open("#{document.attr('docfile')}.lutaml.log.txt",
"w:UTF-8") do |f|
f.write(result.join("\n"))
end
end

def lutaml?(line)
line.match(/^\[(?:\blutaml\b|\blutaml_express\b),([^,]+)?,?([^,]+)?,?([^,]+)?\]/)
end

def content_from_files(document, file_paths)
file_list = file_paths.map do |file_path|
File.new(Utils.relative_file_path(document, file_path), encoding: "UTF-8")
File.new(Utils.relative_file_path(document, file_path),
encoding: "UTF-8")
end
::Lutaml::Parser.parse(file_list)
end
Expand All @@ -43,15 +57,15 @@ def processed_lines(document, input_lines, express_indexes)
.push(*process_text_blocks(
document,
input_lines,
express_indexes
express_indexes,
))
end
result
end

def process_text_blocks(document, input_lines, express_indexes)
line = input_lines.next
block_match = line.match(/^\[(?:\blutaml\b|\blutaml_express\b),([^,]+)?,?([^,]+)?,?([^,]+)?\]/)
block_match = lutaml?(line)
return [line] if block_match.nil?

end_mark = input_lines.next
Expand Down Expand Up @@ -97,7 +111,8 @@ def contexts_items(block_match, document, express_indexes)
result
end

def parse_template(document, current_block, block_match, express_indexes)
def parse_template(document, current_block, block_match,
express_indexes)
options = parse_options(block_match[3])
contexts_items(block_match, document, express_indexes)
.map do |items|
Expand All @@ -112,9 +127,9 @@ def parse_template(document, current_block, block_match, express_indexes)
context_items: items,
context_name: block_match[2].strip)
end.flatten
rescue StandardError => e
document.logger.warn("Failed to parse lutaml block: #{e.message}")
[]
rescue StandardError => e
document.logger.warn("Failed to parse lutaml block: #{e.message}")
[]
end

def parse_options(options_string)
Expand Down Expand Up @@ -155,7 +170,7 @@ def parse_context_block(context_lines:,
template_string: context_lines.join("\n"),
context_items: context_items,
context_name: context_name,
document: document
document: document,
)
Utils.notify_render_errors(document, errors)
render_result.split("\n")
Expand Down
2 changes: 1 addition & 1 deletion lib/metanorma/plugin/lutaml/version.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module Metanorma
module Plugin
module Lutaml
VERSION = "0.4.19".freeze
VERSION = "0.4.20".freeze
end
end
end
7 changes: 4 additions & 3 deletions metanorma-plugin-lutaml.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ Gem::Specification.new do |spec|
# Specify which files should be added to the gem when it is released.
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
spec.files = Dir.chdir(File.expand_path(__dir__)) do
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
`git ls-files -z`.split("\x0").reject do |f|
f.match(%r{^(test|spec|features)/})
end
end
spec.bindir = "exe"
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
Expand All @@ -30,10 +32,9 @@ Gem::Specification.new do |spec|
spec.add_dependency "relaton-cli"
spec.add_dependency "reverse_adoc"

spec.add_development_dependency "byebug"
spec.add_development_dependency "debug"
spec.add_development_dependency "equivalent-xml"
spec.add_development_dependency "metanorma-standoc"
spec.add_development_dependency "pry", "~> 0.12.2"
spec.add_development_dependency "rake", "~> 13"
spec.add_development_dependency "rspec", "~> 3.6"
spec.add_development_dependency "rubocop", "= 0.54.0"
Expand Down
Loading

0 comments on commit 97734dc

Please sign in to comment.