Skip to content

Commit

Permalink
Merge pull request #101 from metanorma/fix/preprocessing-readlines
Browse files Browse the repository at this point in the history
Fix/preprocessing readlines
  • Loading branch information
opoudjis authored Mar 1, 2024
2 parents 7f2e0d0 + f9aa27e commit b03f144
Show file tree
Hide file tree
Showing 19 changed files with 314 additions and 215 deletions.
7 changes: 6 additions & 1 deletion Gemfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
Encoding.default_external = Encoding::UTF_8
Encoding.default_internal = Encoding::UTF_8

source "https://rubygems.org"
git_source(:github) { |repo| "https://github.com/#{repo}" }

# Specify your gem's dependencies in metanorma-plugin-lutaml.gemspec
gemspec

eval_gemfile("Gemfile.devel") rescue nil
4 changes: 4 additions & 0 deletions Gemfile.devel
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
gem "metanorma-plugin-datastruct", git: "https://github.com/metanorma/metanorma-plugin-datastruct", branch: "fix/preprocessing-readlines"
#gem "metanorma-plugin-lutaml", git: "https://github.com/metanorma/metanorma-plugin-lutaml", branch: "fix/preprocessing-readlines"
gem "metanorma-plugin-glossarist", git: "https://github.com/metanorma/metanorma-plugin-glossarist", branch: "fix/preprocessing-readlines"
gem "metanorma-standoc", git: "https://github.com/metanorma/metanorma-standoc", branch: "fix/preprocessing-readlines"
11 changes: 0 additions & 11 deletions lib/metanorma-plugin-lutaml.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,4 @@ module Plugin
module Lutaml
end
end

Asciidoctor::Extensions.register do
preprocessor Metanorma::Plugin::Lutaml::LutamlPreprocessor
preprocessor Metanorma::Plugin::Lutaml::LutamlUmlAttributesTablePreprocessor
preprocessor Metanorma::Plugin::Lutaml::LutamlUmlDatamodelDescriptionPreprocessor
preprocessor Metanorma::Plugin::Lutaml::LutamlUmlClassPreprocessor
inline_macro Metanorma::Plugin::Lutaml::LutamlFigureInlineMacro
inline_macro Metanorma::Plugin::Lutaml::LutamlTableInlineMacro
block_macro Metanorma::Plugin::Lutaml::LutamlDiagramBlockMacro
block Metanorma::Plugin::Lutaml::LutamlDiagramBlock
end
end
14 changes: 14 additions & 0 deletions lib/metanorma/plugin/lutaml/asciidoctor/preprocessor.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
module Metanorma
module Plugin
module Lutaml
module Asciidoctor
class PreprocessorNoIfdefsReader < ::Asciidoctor::PreprocessorReader
def preprocess_conditional_directive(_keyword, _target, _delimiter,
_text)
false # decline to resolve idefs
end
end
end
end
end
end
3 changes: 2 additions & 1 deletion lib/metanorma/plugin/lutaml/express_remarks_decorator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ def update_relative_paths(string, path_prefix)
def prefix_relative_paths(line, path_prefix)
line.gsub(RELATIVE_PREFIX_MACRO_REGEXP) do |_match|
prefixed_path = File.join(path_prefix, $3.strip)
# When we are dealing with arelative path of a template: ../path/to/file we need to transform it into
# When we are dealing with a relative path of a template:
# ../path/to/file we need to transform it into
# the absolute one because `image::` macro wont understand it other way
prefixed_path = File.absolute_path(prefixed_path) if prefixed_path.start_with?('../')
full_path = File.expand_path(prefixed_path)
Expand Down
17 changes: 10 additions & 7 deletions lib/metanorma/plugin/lutaml/lutaml_diagram_base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ module Plugin
module Lutaml
module LutamlDiagramBase
def process(parent, reader, attrs)
uml_document = ::Lutaml::Uml::Parsers::Dsl.parse(lutaml_file(parent.document, reader))
uml_document = ::Lutaml::Uml::Parsers::Dsl
.parse(lutaml_file(parent.document, reader))
filename = generate_file(parent, reader, uml_document)
through_attrs = generate_attrs(attrs)
through_attrs["target"] = filename
Expand All @@ -23,8 +24,8 @@ def process(parent, reader, attrs)
abort(parent, reader, attrs, e.message)
end

def lutaml_file(reader)
raise 'Implement me!'
def lutaml_file(_reader)
raise "Implement me!"
end

protected
Expand All @@ -36,7 +37,7 @@ def abort(parent, reader, attrs, msg)
create_listing_block(
parent,
source,
attrs.reject { |k, _v| k == 1 }
attrs.reject { |k, _v| k == 1 },
)
end

Expand All @@ -47,14 +48,16 @@ def generate_file(parent, _reader, uml_document)

path_lutaml = "lutaml"
imagesdir = if parent.document.attr("imagesdir")
File.join(parent.document.attr("imagesdir"), path_lutaml)
File.join(parent.document.attr("imagesdir"),
path_lutaml)
else
path_lutaml
end
result_path = Utils.relative_file_path(parent.document, imagesdir)
result_pathname = Pathname.new(result_path)
result_pathname.mkpath
File.writable?(result_pathname) || raise("Destination path #{result_path} not writable for Lutaml!")
File.writable?(result_pathname) ||
raise("Destination path #{result_path} not writable for Lutaml!")

outfile = Tempfile.new(["lutaml", ".png"])
outfile.binmode
Expand All @@ -67,7 +70,7 @@ def generate_file(parent, _reader, uml_document)
filename = File.basename(outfile.path)
FileUtils.cp(outfile, result_pathname) && outfile.unlink

#File.join(result_pathname, filename)
# File.join(result_pathname, filename)
File.join(path_lutaml, filename)
end

Expand Down
6 changes: 3 additions & 3 deletions lib/metanorma/plugin/lutaml/lutaml_diagram_block.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
module Metanorma
module Plugin
module Lutaml
class LutamlDiagramBlock < Asciidoctor::Extensions::BlockProcessor
class LutamlDiagramBlock < ::Asciidoctor::Extensions::BlockProcessor
include LutamlDiagramBase

use_dsl
Expand All @@ -14,14 +14,14 @@ class LutamlDiagramBlock < Asciidoctor::Extensions::BlockProcessor
parse_content_as :raw

def lutaml_file(document, reader)

lutaml_temp(document, reader)
end

private

def lutaml_temp(document, reader)
temp_file = Tempfile.new(["lutaml", ".lutaml"], Utils.relative_file_path(document, ''))
temp_file = Tempfile.new(["lutaml", ".lutaml"],
Utils.relative_file_path(document, ""))
temp_file.puts(reader.read)
temp_file.rewind
temp_file
Expand Down
2 changes: 1 addition & 1 deletion lib/metanorma/plugin/lutaml/lutaml_diagram_block_macro.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
module Metanorma
module Plugin
module Lutaml
class LutamlDiagramBlockMacro < Asciidoctor::Extensions::BlockMacroProcessor
class LutamlDiagramBlockMacro < ::Asciidoctor::Extensions::BlockMacroProcessor
include LutamlDiagramBase

use_dsl
Expand Down
9 changes: 5 additions & 4 deletions lib/metanorma/plugin/lutaml/lutaml_figure_inline_macro.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,20 @@
module Metanorma
module Plugin
module Lutaml
class LutamlFigureInlineMacro < Asciidoctor::Extensions::InlineMacroProcessor
class LutamlFigureInlineMacro < ::Asciidoctor::Extensions::InlineMacroProcessor
include LutamlDiagramBase

use_dsl
named :lutaml_figure

def process(parent, _target, attrs)
diagram_key = [attrs["package"], attrs["name"]].compact.join(":")
return if parent.document.attributes['lutaml_figure_id'].nil?
xmi_id = parent.document.attributes['lutaml_figure_id'][diagram_key]
return if parent.document.attributes["lutaml_figure_id"].nil?

xmi_id = parent.document.attributes["lutaml_figure_id"][diagram_key]
return unless xmi_id

%Q(<xref target="figure-#{xmi_id}"></xref>)
%(<xref target="figure-#{xmi_id}"></xref>)
end
end
end
Expand Down
11 changes: 6 additions & 5 deletions lib/metanorma/plugin/lutaml/lutaml_preprocessor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,26 @@
require "lutaml"
require "metanorma/plugin/lutaml/utils"
require "metanorma/plugin/lutaml/express_remarks_decorator"
require "metanorma/plugin/lutaml/asciidoctor/preprocessor"

module Metanorma
module Plugin
module Lutaml
# Class for processing Lutaml files
class LutamlPreprocessor < Asciidoctor::Extensions::Preprocessor
class LutamlPreprocessor < ::Asciidoctor::Extensions::Preprocessor
REMARKS_ATTRIBUTE = "remarks"

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

protected
Expand Down
14 changes: 8 additions & 6 deletions lib/metanorma/plugin/lutaml/lutaml_table_inline_macro.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,23 @@
module Metanorma
module Plugin
module Lutaml
class LutamlTableInlineMacro < Asciidoctor::Extensions::InlineMacroProcessor
class LutamlTableInlineMacro < ::Asciidoctor::Extensions::InlineMacroProcessor
include LutamlDiagramBase
SUPPORTED_OPTIONS = %w[class enum data_type]
SUPPORTED_OPTIONS = %w[class enum data_type].freeze

use_dsl
named :lutaml_table

def process(parent, _target, attrs)
keyword = SUPPORTED_OPTIONS.find { |n| attrs[n] }
entity_key = [keyword, attrs["package"], attrs[keyword]].compact.join(":")
return if parent.document.attributes['lutaml_entity_id'].nil?
xmi_id = parent.document.attributes['lutaml_entity_id'][entity_key]
entity_key = [keyword, attrs["package"],
attrs[keyword]].compact.join(":")
return if parent.document.attributes["lutaml_entity_id"].nil?

xmi_id = parent.document.attributes["lutaml_entity_id"][entity_key]
return unless xmi_id

%Q(<xref target="section-#{xmi_id}"/>)
%(<xref target="section-#{xmi_id}"/>)
end
end
end
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# frozen_string_literal: true

require_relative "./lutaml_uml_class_preprocessor.rb"
require "metanorma/plugin/lutaml/asciidoctor/preprocessor"

module Metanorma
module Plugin
Expand All @@ -16,36 +16,35 @@ def template(options)
skip_headers = options[:skip_headers]

<<~TEMPLATE
#{"=== {{ definition.name }}" unless skip_headers}
{{ definition.definition }}
{% if definition.attributes %}
{% if definition.keyword == 'enumeration' %}
.{{ definition.name }} values
|===
|Name |Definition
{% for item in definition.attributes %}
|{{ item.name }} |{{ item.definition }}
{% endfor %}
|===
{% else %}
.{{ definition.name }} attributes
|===
|Name |Definition |Mandatory / Optional / Conditional |Max Occur |Data Type
{% for item in definition.attributes %}
|{{ item.name }} |{% if item.definition %}{{ item.definition }}{% endif %} |{% if item.cardinality.min == "0" %}O{% else %}M{% endif %} |{% if item.cardinality.max == "*" %}N{% else %}1{% endif %} |{% if item.origin %}<<{{ item.origin }}>>{% endif %} `{{ item.type }}`
{% endfor %}
|===
{% endif %}
{% endif %}
#{'=== {{ definition.name }}' unless skip_headers}
{{ definition.definition }}
{% if definition.attributes %}
{% if definition.keyword == 'enumeration' %}
.{{ definition.name }} values
|===
|Name |Definition
{% for item in definition.attributes %}
|{{ item.name }} |{{ item.definition }}
{% endfor %}
|===
{% else %}
.{{ definition.name }} attributes
|===
|Name |Definition |Mandatory / Optional / Conditional |Max Occur |Data Type
{% for item in definition.attributes %}
|{{ item.name }} |{% if item.definition %}{{ item.definition }}{% endif %} |{% if item.cardinality.min == "0" %}O{% else %}M{% endif %} |{% if item.cardinality.max == "*" %}N{% else %}1{% endif %} |{% if item.origin %}<<{{ item.origin }}>>{% endif %} `{{ item.type }}`
{% endfor %}
|===
{% endif %}
{% endif %}
TEMPLATE
end
# rubocop:enable Layout/IndentHeredoc
end

end
end
end
Loading

0 comments on commit b03f144

Please sign in to comment.