Class: Stepmod::Utils::StepmodFileAnnotator

Inherits:
Object
  • Object
show all
Defined in:
lib/stepmod/utils/stepmod_file_annotator.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(express_file:, stepmod_dir: nil) ⇒ StepmodFileAnnotator

Returns a new instance of StepmodFileAnnotator.

Parameters:

  • express_file (String)

    path to the exp file needed to annotate

  • resource_docs_cache (String)

    output of ./stepmod-build-resource-docs-cache



19
20
21
22
23
24
25
26
27
28
29
# File 'lib/stepmod/utils/stepmod_file_annotator.rb', line 19

def initialize(express_file:, stepmod_dir: nil)
  @express_file = express_file
  @resource_docs_cache = resource_docs_schemas(stepmod_dir)
  @stepmod_dir = stepmod_dir || Dir.pwd
  @added_bibdata = {}

  @schema_name = Expressir::Express::Parser.from_file(express_file)
                                           .schemas
                                           .first
                                           .id
end

Instance Attribute Details

#express_fileObject (readonly)

Returns the value of attribute express_file.



15
16
17
# File 'lib/stepmod/utils/stepmod_file_annotator.rb', line 15

def express_file
  @express_file
end

#resource_docs_cacheObject (readonly)

Returns the value of attribute resource_docs_cache.



15
16
17
# File 'lib/stepmod/utils/stepmod_file_annotator.rb', line 15

def resource_docs_cache
  @resource_docs_cache
end

#stepmod_dirObject (readonly)

Returns the value of attribute stepmod_dir.



15
16
17
# File 'lib/stepmod/utils/stepmod_file_annotator.rb', line 15

def stepmod_dir
  @stepmod_dir
end

Instance Method Details

#callObject



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/stepmod/utils/stepmod_file_annotator.rb', line 48

def call
  match = File.basename(express_file).match('^(arm|mim|bom)\.exp$')
  descriptions_base = match ? "#{match.captures[0]}_descriptions.xml" : "descriptions.xml"

  descriptions_file = File.join(File.dirname(express_file),
                                descriptions_base)

  output_express = File.read(express_file)
  converted_description = ""
  base_linked = ""

  if File.exist?(descriptions_file)
    descriptions = Nokogiri::XML(File.read(descriptions_file)).root
    added_resource_descriptions = {}

    descriptions.xpath("ext_description").each do |description|
      # Add base resource from linked path if exists, eg "language_schema.language.wr:WR1" -> "language_schema"
      base_linked = description["linkend"].to_s.split(".").first

      if added_resource_descriptions[base_linked].nil?
        base_reource_doc_dir = resource_docs_cache[description["linkend"].to_s.split(".").first]
        if base_reource_doc_dir
          converted_description << convert_from_resource_file(
            base_reource_doc_dir, stepmod_dir, base_linked, descriptions_file
          )
        end
        added_resource_descriptions[base_linked] = true
      end
      resource_docs_dir = resource_docs_cache[description["linkend"]]
      # Convert content description
      # when a schema description is available from resource.xml and also descriptions.xml, the description from resource.xml is only used.
      # https://github.com/metanorma/annotated-express/issues/32#issuecomment-792609078
      if description.text.strip.length.positive? && resource_docs_dir.nil?
        converted_description << convert_from_description_text(
          descriptions_file, description
        )
      end
      # Add converted description from exact linked path
      if resource_docs_dir && added_resource_descriptions[description["linkend"]].nil?
        output_express << convert_from_resource_file(resource_docs_dir,
                                                     stepmod_dir, description["linkend"], descriptions_file)
        added_resource_descriptions[description["linkend"]] = true
      end
    end
  end

  bib_file_name = extract_bib_file_name(match, resource_docs_cache[@schema_name || ""])
  bib_file = if match
               File.join(File.dirname(express_file), bib_file_name)
             else
               resource_docs_file_path(stepmod_dir, bib_file_name)
             end

  output_express << if bib_file && File.exist?(bib_file)
                      prepend_bibdata(
                        converted_description || "",
                        # bib_file will not be present for resouces
                        # that are not in resource_docs cache.
                        # e.g hierarchy_schema
                        bib_file,
                        @schema_name,
                        match,
                      )
                    else
                      converted_description
                    end

  sanitize(output_express)
rescue StandardError => e
  puts "[ERROR]!!! #{e.message}"
  puts e.backtrace
end

#resource_docs_schemas(stepmod_dir) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/stepmod/utils/stepmod_file_annotator.rb', line 31

def resource_docs_schemas(stepmod_dir)
  filepath = File.join(stepmod_dir, "data/resource_docs/*/resource.xml")

  schemas = {}
  Dir.glob(filepath).each do |resource_docs_file|
    match = resource_docs_file.match("data/resource_docs/([^/]+)/resource.xml")
    resource_docs_dir = match.captures[0]

    resource_docs = Nokogiri::XML(File.read(resource_docs_file)).root
    resource_docs.xpath("schema").each do |schema|
      schemas[schema["name"]] = resource_docs_dir
    end
  end

  schemas
end