Class: Asciidoctor::Mpfd::Converter

Inherits:
Standoc::Converter
  • Object
show all
Defined in:
lib/asciidoctor/mpfd/section.rb,
lib/asciidoctor/mpfd/validate.rb,
lib/asciidoctor/mpfd/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/asciidoctor/mpfd/validate.rb', line 9

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

#clause_parse(attrs, xml, node) ⇒ Object



59
60
61
62
63
# File 'lib/asciidoctor/mpfd/section.rb', line 59

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/asciidoctor/mpfd/validate.rb', line 4

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

#doctype_validate(xmldoc) ⇒ Object



14
15
16
17
18
19
# File 'lib/asciidoctor/mpfd/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

#document(node) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/asciidoctor/mpfd/converter.rb', line 83

def document(node)
  init(node)
  ret1 = makexml(node)
  ret = ret1.to_xml(indent: 2)
  unless node.attr("nodoc") || !node.attr("docfile")
    filename = node.attr("docfile").gsub(/\.adoc/, ".xml").
      gsub(%r{^.*/}, "")
    File.open(filename, "w") { |f| f.write(ret) }
    html_converter(node).convert filename unless node.attr("nodoc")
    word_converter(node).convert filename unless node.attr("nodoc")
  end
  @log.write(@localdir + @filename + ".err") unless @novalid
  @files_to_delete.each { |f| FileUtils.rm f }
  ret
end

#html_converter(node) ⇒ Object



109
110
111
# File 'lib/asciidoctor/mpfd/converter.rb', line 109

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

#make_preface(x, s) ⇒ Object



50
51
52
53
54
55
56
57
# File 'lib/asciidoctor/mpfd/section.rb', line 50

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



78
79
80
81
# File 'lib/asciidoctor/mpfd/converter.rb', line 78

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

#metadata_author(node, xml) ⇒ Object



22
23
24
25
26
27
28
29
30
# File 'lib/asciidoctor/mpfd/converter.rb', line 22

def (node, xml)
  xml.contributor do |c|
    c.role **{ type: "author" }
    c.organization do |a|
      a.name "Mandatory Provident Fund Schemes Authority"
      a.abbreviation "MPFA"
    end
  end
end

#metadata_committee(node, xml) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/asciidoctor/mpfd/converter.rb', line 42

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}") do
      a.committee node.attr("committee_#{i}"),
        **attr_code(type: node.attr("committee-type_#{i}"))
      i += 1
    end
  end
end


61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/asciidoctor/mpfd/converter.rb', line 61

def (node, xml)
  from = node.attr("copyright-year") || Date.today.year
  xml.copyright do |c|
    c.from from
    c.owner do |owner|
      owner.organization do |o|
        o.name "Mandatory Provident Fund Schemes Authority"
        o.abbreviation "MPFA"
      end
    end
  end
end

#metadata_id(node, xml) ⇒ Object



56
57
58
59
# File 'lib/asciidoctor/mpfd/converter.rb', line 56

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

#metadata_publisher(node, xml) ⇒ Object



32
33
34
35
36
37
38
39
40
# File 'lib/asciidoctor/mpfd/converter.rb', line 32

def (node, xml)
  xml.contributor do |c|
    c.role **{ type: "publisher" }
    c.organization do |a|
      a.name "Mandatory Provident Fund Schemes Authority"
      a.abbreviation "MPFA"
    end
  end
end

#move_sections_into_preface(x, preface) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
# File 'lib/asciidoctor/mpfd/section.rb', line 38

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

#sections_cleanup(x) ⇒ Object



15
16
17
18
19
20
21
22
23
24
# File 'lib/asciidoctor/mpfd/section.rb', line 15

def sections_cleanup(x)
  super
  x.xpath("//*[@inline-header]").each do |h|
    h.delete("inline-header")
  end
  x.xpath("//*[@guidance]").each do |h|
    c = h.previous_element || next
    c.add_child h.remove
  end
end

#sectiontype_streamline(ret) ⇒ Object



26
27
28
29
30
31
32
# File 'lib/asciidoctor/mpfd/section.rb', line 26

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/asciidoctor/mpfd/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



105
106
107
# File 'lib/asciidoctor/mpfd/converter.rb', line 105

def style(n, t)
  return
end

#term_def_title(_toplevel, node) ⇒ Object



34
35
36
# File 'lib/asciidoctor/mpfd/section.rb', line 34

def term_def_title(_toplevel, node)
  return node.title
end

#title_validate(root) ⇒ Object



74
75
76
# File 'lib/asciidoctor/mpfd/converter.rb', line 74

def title_validate(root)
  nil
end

#validate(doc) ⇒ Object



99
100
101
102
103
# File 'lib/asciidoctor/mpfd/converter.rb', line 99

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

#word_converter(node) ⇒ Object



113
114
115
# File 'lib/asciidoctor/mpfd/converter.rb', line 113

def word_converter(node)
  IsoDoc::Mpfd::WordConvert.new(doc_extract_attributes(node))
end