Class: Metanorma::Cc::Converter

Inherits:
Generic::Converter
  • Object
show all
Defined in:
lib/metanorma/cc/log.rb,
lib/metanorma/cc/converter.rb,
lib/metanorma/cc/validate_section.rb

Constant Summary collapse

CC_LOG_MESSAGES =
{
  # rubocop:disable Naming/VariableNumber
  "CC_1": { category: "Style",
            error: "Only one Symbols and Abbreviated Terms section in the standard",
            severity: 2 },
  "CC_2": { category: "Style",
            error: "Symbols and Abbreviated Terms can only contain a definition list",
            severity: 2 },
  "CC_3": { category: "Style",
            error: "(section sequencing) %s",
            severity: 2 },
  "CC_4": { category: "Style",
            error: "Document must contain at least one clause",
            severity: 2 },
  "CC_5": { category: "Style",
            error: "Document must contain clause after Terms and Definitions",
            severity: 2 },
  "CC_6": { category: "Style",
            error: "Scope must occur before Terms and Definitions",
            severity: 2 },
  "CC_8": { category: "Style",
            error: "Only annexes and references can follow clauses",
            severity: 2 },
  "CC_9": { category: "Style",
            error: "Document must include (references) Normative References",
            severity: 2 },
  "CC_11": { category: "Style",
             error: "Final section must be (references) Bibliography",
             severity: 2 },
  "CC_12": { category: "Style",
             error: "There are sections after the final Bibliography",
             severity: 2 },

}.freeze
SEQ =

spec of permissible section sequence we skip normative references, it goes to end of list

[
  {
    msg: "Initial section must be (content) Foreword",
    val: ["./self::foreword"],
  },
  {
    msg: "Prefatory material must be followed by (clause) Scope",
    val: ["./self::introduction", "./self::clause[@type = 'scope']"],
  },
  {
    msg: "Prefatory material must be followed by (clause) Scope",
    val: ["./self::clause[@type = 'scope']"],
  },
  {
    msg: "Normative References must be followed by "\
         "Terms and Definitions",
    val: ["./self::terms | .//terms"],
  },
].freeze
SECTIONS_XPATH =
"//foreword | //introduction | //sections/terms | .//annex | "\
"//sections/definitions | //sections/clause | "\
"//references[not(parent::clause)] | "\
"//clause[descendant::references][not(parent::clause)]".freeze

Instance Method Summary collapse

Instance Method Details

#configurationObject



15
16
17
# File 'lib/metanorma/cc/converter.rb', line 15

def configuration
  Metanorma::Cc.configuration
end

#doc_converter(node) ⇒ Object



40
41
42
# File 'lib/metanorma/cc/converter.rb', line 40

def doc_converter(node)
  IsoDoc::Cc::WordConvert.new(doc_extract_attributes(node))
end

#html_converter(node) ⇒ Object



30
31
32
# File 'lib/metanorma/cc/converter.rb', line 30

def html_converter(node)
  IsoDoc::Cc::HtmlConvert.new(html_extract_attributes(node))
end

#log_messagesObject

rubocop:enable Naming/VariableNumber



40
41
42
# File 'lib/metanorma/cc/log.rb', line 40

def log_messages
  super.merge(CC_LOG_MESSAGES)
end

#outputs(node, ret) ⇒ Object



19
20
21
22
23
24
25
26
27
28
# File 'lib/metanorma/cc/converter.rb', line 19

def outputs(node, ret)
  File.open("#{@filename}.xml", "w:UTF-8") { |f| f.write(ret) }
  presentation_xml_converter(node).convert("#{@filename}.xml")
  html_converter(node).convert("#{@filename}.presentation.xml",
                               nil, false, "#{@filename}.html")
  doc_converter(node).convert("#{@filename}.presentation.xml",
                              nil, false, "#{@filename}.doc")
  pdf_converter(node)&.convert("#{@filename}.presentation.xml",
                               nil, false, "#{@filename}.pdf")
end

#pdf_converter(node) ⇒ Object



34
35
36
37
38
# File 'lib/metanorma/cc/converter.rb', line 34

def pdf_converter(node)
  return if node.attr("no-pdf")

  IsoDoc::Cc::PdfConvert.new(pdf_extract_attributes(node))
end

#presentation_xml_converter(node) ⇒ Object



44
45
46
47
48
# File 'lib/metanorma/cc/converter.rb', line 44

def presentation_xml_converter(node)
  IsoDoc::Cc::PresentationXMLConvert
    .new(doc_extract_attributes(node)
    .merge(output_formats: ::Metanorma::Cc::Processor.new.output_formats))
end

#section_validate(doc) ⇒ Object



7
8
9
10
11
12
# File 'lib/metanorma/cc/validate_section.rb', line 7

def section_validate(doc)
  advisory = doc.root.at("//bibdata/ext[doctype = 'advisory']")
  symbols_validate(doc.root) unless advisory
  sections_sequence_validate(doc.root) unless advisory
  super
end

#sections_sequence_validate(root) ⇒ Object



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
# File 'lib/metanorma/cc/validate_section.rb', line 66

def sections_sequence_validate(root)
  names = root.xpath(SECTIONS_XPATH)
  names = seqcheck(names, SEQ[0][:msg], SEQ[0][:val])
  n = names[0]
  names = seqcheck(names, SEQ[1][:msg], SEQ[1][:val])
  if n&.at("./self::introduction")
    names = seqcheck(names, SEQ[2][:msg], SEQ[2][:val])
  end
  names = seqcheck(names, SEQ[3][:msg], SEQ[3][:val])
  n = names.shift
  if n&.at("./self::definitions")
    n = names.shift
  end
  if n.nil? || n.name != "clause"
    @log.add("CC_4", nil)
  end
  n&.at("./self::clause") ||
    @log.add("CC_5", nil)
  n&.at("./self::clause[@type = 'scope']") &&
    @log.add("CC_6", nil)
  n = names.shift
  while n&.name == "clause"
    n&.at("./self::clause[@type = 'scope']")
    @log.add("CC_6", nil)
    n = names.shift
  end
  unless %w(annex references).include? n&.name
    @log.add("CC_8", nil)
  end
  while n&.name == "annex"
    n = names.shift
    if n.nil?
      @log.add("CC_9", nil)

    end
  end
  n&.at("./self::references[@normative = 'true']") ||
    @log.add("CC_9", nil)
  n = names&.shift
  n&.at("./self::references[@normative = 'false']") ||
    @log.add("CC_11", nil)
  names.empty? ||
    @log.add("CC_12", nil)
end

#seqcheck(names, msg, accepted) ⇒ Object



26
27
28
29
30
31
32
33
34
35
# File 'lib/metanorma/cc/validate_section.rb', line 26

def seqcheck(names, msg, accepted)
  n = names.shift
  return [] if n.nil?

  test = accepted.map { |a| n.at(a) }
  if test.all?(&:nil?)
    @log.add("CC_3", nil, params: [msg])
  end
  names
end

#style_warning(node, msg, text = nil) ⇒ Object



111
112
113
114
115
116
117
# File 'lib/metanorma/cc/validate_section.rb', line 111

def style_warning(node, msg, text = nil)
  return if @novalid

  w = msg
  w += ": #{text}" if text
  @log.add("STANDOC_48", node, params: [w])
end

#symbols_validate(root) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
# File 'lib/metanorma/cc/validate_section.rb', line 14

def symbols_validate(root)
  f = root.xpath("//definitions")
  f.empty? && return
  f.size == 1 or @log.add("CC_1", f.first)
  f.first.elements.each do |e|
    unless e.name == "dl"
      @log.add("CC_2", f.first)
      return
    end
  end
end