Class: Metanorma::MPFA::Converter

Inherits:
Standoc::Converter
  • Object
show all
Defined in:
lib/metanorma/mpfa/section.rb,
lib/metanorma/mpfa/validate.rb,
lib/metanorma/mpfa/converter.rb

Overview

A Converter implementation that generates MPFD output, and a document schema encapsulation of the document for validation

Constant Summary collapse

XML_ROOT_TAG =
"mpfd-standard".freeze
XML_NAMESPACE =
"https://www.metanorma.org/ns/mpfd".freeze

Instance Method Summary collapse

Instance Method Details

#bibdata_validate(doc) ⇒ Object



9
10
11
12
# File 'lib/metanorma/mpfa/validate.rb', line 9

def bibdata_validate(doc)
  doctype_validate(doc)
  stage_validate(doc)
end

#clause_parse(attrs, xml, node) ⇒ Object



54
55
56
57
58
# File 'lib/metanorma/mpfa/section.rb', line 54

def clause_parse(attrs, xml, node)
  attrs[:guidance] = true if node.role == "guidance"
  attrs[:container] = true if node.role == "container"
  super
end

#content_validate(doc) ⇒ Object



4
5
6
7
# File 'lib/metanorma/mpfa/validate.rb', line 4

def content_validate(doc)
  super
  bibdata_validate(doc.root)
end

#default_publisherObject



21
22
23
# File 'lib/metanorma/mpfa/converter.rb', line 21

def default_publisher
  "Mandatory Provident Fund Schemes Authority"
end

#doc_converter(node) ⇒ Object



83
84
85
# File 'lib/metanorma/mpfa/converter.rb', line 83

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

#doctype_validate(xmldoc) ⇒ Object



14
15
16
17
18
19
# File 'lib/metanorma/mpfa/validate.rb', line 14

def doctype_validate(xmldoc)
  doctype = xmldoc&.at("//bibdata/ext/doctype")&.text
  %w(policy-and-procedures best-practices supporting-document
  report legal directives proposal standard).include? doctype or
  @log.add("Document Attributes", nil, "#{doctype} is not a recognised document type")
end

#html_converter(node) ⇒ Object



79
80
81
# File 'lib/metanorma/mpfa/converter.rb', line 79

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

#make_preface(x, s) ⇒ Object



45
46
47
48
49
50
51
52
# File 'lib/metanorma/mpfa/section.rb', line 45

def make_preface(x, s)
  if x.at("//foreword | //introduction | //terms | //acknowledgements |"\
      "//abstract[not(ancestor::bibitem)] | //clause[@preface]")
    preface = s.add_previous_sibling("<preface/>").first
    move_sections_into_preface(x, preface)
    make_abstract(x, s)
  end
end

#makexml(node) ⇒ Object



49
50
51
52
# File 'lib/metanorma/mpfa/converter.rb', line 49

def makexml(node)
  @draft = node.attributes.has_key?("draft")
  super
end

#metadata_committee(node, xml) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/metanorma/mpfa/converter.rb', line 25

def (node, xml)
  return unless node.attr("committee")

  xml.editorialgroup do |a|
    a.committee node.attr("committee"),
                **attr_code(type: node.attr("committee-type"))
    i = 2
    while node.attr("committee_#{i}")
      a.committee node.attr("committee_#{i}"),
                  **attr_code(type: node.attr("committee-type_#{i}"))
      i += 1
    end
  end
end

#metadata_id(node, xml) ⇒ Object



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

def (node, xml)
  xml.docidentifier { |i| i << node.attr("docnumber") }
  xml.docnumber { |i| i << node.attr("docnumber") }
end

#move_sections_into_preface(x, preface) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
# File 'lib/metanorma/mpfa/section.rb', line 33

def move_sections_into_preface(x, preface)
  foreword = x.at("//foreword")
  preface.add_child foreword.remove if foreword
  introduction = x.at("//introduction")
  preface.add_child introduction.remove if introduction
  terms = x.at("//sections/clause[descendant::terms]") || x.at("//terms")
  preface.add_child terms.remove if terms
  move_clauses_into_preface(x, preface)
  acknowledgements = x.at("//acknowledgements")
  preface.add_child acknowledgements.remove if acknowledgements
end

#outputs(node, ret) ⇒ Object



54
55
56
57
58
59
60
61
62
63
# File 'lib/metanorma/mpfa/converter.rb', line 54

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



87
88
89
90
91
# File 'lib/metanorma/mpfa/converter.rb', line 87

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

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

#presentation_xml_converter(node) ⇒ Object



75
76
77
# File 'lib/metanorma/mpfa/converter.rb', line 75

def presentation_xml_converter(node)
  IsoDoc::MPFA::PresentationXMLConvert.new(html_extract_attributes(node))
end

#sections_cleanup(xml) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
# File 'lib/metanorma/mpfa/section.rb', line 9

def sections_cleanup(xml)
  super
  xml.xpath("//*[@inline-header]").each do |h|
    h.delete("inline-header")
  end
  xml.xpath("//*[@guidance]").each do |h|
    c = h.xpath("./preceding-sibling::clause")
    c.empty? and next
    c.last.add_child h.remove
  end
end

#sectiontype_streamline(ret) ⇒ Object



21
22
23
24
25
26
27
# File 'lib/metanorma/mpfa/section.rb', line 21

def sectiontype_streamline(ret)
  case ret
  when "glossary" then "terms and definitions"
  else
    super
  end
end

#stage_validate(xmldoc) ⇒ Object



21
22
23
24
25
26
# File 'lib/metanorma/mpfa/validate.rb', line 21

def stage_validate(xmldoc)
  stage = xmldoc&.at("//bibdata/status/stage")&.text
  %w(proposal working-draft committee-draft draft-standard final-draft
  published withdrawn).include? stage or
  @log.add("Document Attributes", nil, "#{stage} is not a recognised status")
end

#style(_n, _t) ⇒ Object



71
72
73
# File 'lib/metanorma/mpfa/converter.rb', line 71

def style(_n, _t)
  nil
end

#term_def_title(_toplevel, node) ⇒ Object



29
30
31
# File 'lib/metanorma/mpfa/section.rb', line 29

def term_def_title(_toplevel, node)
  node.title
end

#title_validate(_root) ⇒ Object



45
46
47
# File 'lib/metanorma/mpfa/converter.rb', line 45

def title_validate(_root)
  nil
end

#validate(doc) ⇒ Object



65
66
67
68
69
# File 'lib/metanorma/mpfa/converter.rb', line 65

def validate(doc)
  content_validate(doc)
  schema_validate(formattedstr_strip(doc.dup),
                  File.join(File.dirname(__FILE__), "mpfd.rng"))
end