Class: Asciidoctor::Mpfd::Converter

Inherits:
ISO::Converter
  • Object
show all
Defined in:
lib/asciidoctor/mpfd/section.rb,
lib/asciidoctor/mpfd/converter.rb

Overview

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

Instance Method Summary collapse

Instance Method Details

#clause_parse(attrs, xml, node) ⇒ Object



85
86
87
88
89
90
# File 'lib/asciidoctor/mpfd/section.rb', line 85

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

#doctype(node) ⇒ Object



91
92
93
94
95
96
97
98
99
100
# File 'lib/asciidoctor/mpfd/converter.rb', line 91

def doctype(node)
  d = node.attr("doctype")
=begin
  unless %w{policy-and-procedures best-practices supporting-document report legal directives proposal standard}.include? d
    warn "#{d} is not a legal document type: reverting to 'standard'"
    d = "standard"
  end
=end
  d
end

#document(node) ⇒ Object



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/asciidoctor/mpfd/converter.rb', line 109

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")
    pdf_convert(filename.sub(/\.xml$/, "")) unless node.attr("nodoc")
  end
  @files_to_delete.each { |f| system "rm #{f}" }
  ret
end

#html_converter(node) ⇒ Object



155
156
157
158
159
160
161
162
163
164
165
# File 'lib/asciidoctor/mpfd/converter.rb', line 155

def html_converter(node)
  IsoDoc::Mpfd::HtmlConvert.new(
    script: node.attr("script"),
    bodyfont: node.attr("body-font"),
    headerfont: node.attr("header-font"),
    monospacefont: node.attr("monospace-font"),
    titlefont: node.attr("title-font"),
    i18nyaml: node.attr("i18nyaml"),
    scope: node.attr("scope"),
  )
end

#inline_quoted(node) ⇒ Object



179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
# File 'lib/asciidoctor/mpfd/converter.rb', line 179

def inline_quoted(node)
  noko do |xml|
    case node.type
    when :emphasis then xml.em node.text
    when :strong then xml.strong node.text
    when :monospaced then xml.tt node.text
    when :double then xml << "\"#{node.text}\""
    when :single then xml << "'#{node.text}'"
    when :superscript then xml.sup node.text
    when :subscript then xml.sub node.text
    when :asciimath then stem_parse(node.text, xml)
    else
      case node.role
      when "strike" then xml.strike node.text
      when "smallcap" then xml.smallcap node.text
      when "keyword" then xml.keyword node.text
      else
        xml << node.text
      end
    end
  end.join
end

#literal(node) ⇒ Object



135
136
137
138
139
140
141
142
# File 'lib/asciidoctor/mpfd/converter.rb', line 135

def literal(node)
  noko do |xml|
    xml.figure **id_attr(node) do |f|
      figure_title(node, f)
      f.pre node.lines.join("\n")
    end
  end
end

#make_preface(x, s) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/asciidoctor/mpfd/section.rb', line 69

def make_preface(x, s)
  if x.at("//foreword | //introduction | //terms")
    preface = s.add_previous_sibling("<preface/>").first
    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("//terms")
    preface.add_child terms.remove if terms
  end
  x.xpath("//clause[@preface]").each do |c|
    c.delete("preface")
    preface.add_child c.remove
  end
end

#makexml(node) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/asciidoctor/mpfd/converter.rb', line 78

def makexml(node)
  result = ["<?xml version='1.0' encoding='UTF-8'?>\n<mpfd-standard>"]
  @draft = node.attributes.has_key?("draft")
  result << noko { |ixml| front node, ixml }
  result << noko { |ixml| middle node, ixml }
  result << "</mpfd-standard>"
  result = textcleanup(result.flatten * "\n")
  ret1 = cleanup(Nokogiri::XML(result))
  validate(ret1)
  ret1.root.add_namespace(nil, MPFD_NAMESPACE)
  ret1
end

#metadata_author(node, xml) ⇒ Object



18
19
20
21
22
23
24
25
26
# File 'lib/asciidoctor/mpfd/converter.rb', line 18

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



38
39
40
41
42
43
# File 'lib/asciidoctor/mpfd/converter.rb', line 38

def (node, xml)
  xml.editorialgroup do |a|
    a.committee node.attr("committee"),
      **attr_code(type: node.attr("committee-type"))
  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



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

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

#metadata_publisher(node, xml) ⇒ Object



28
29
30
31
32
33
34
35
36
# File 'lib/asciidoctor/mpfd/converter.rb', line 28

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

#metadata_status(node, xml) ⇒ Object



53
54
55
# File 'lib/asciidoctor/mpfd/converter.rb', line 53

def (node, xml)
  xml.status(**{ format: "plain" }) { |s| s << node.attr("status") }
end

#pdf_convert(filename) ⇒ Object



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

def pdf_convert(filename)
  url = "#{Dir.pwd}/#{filename}.html"
  pdfjs = File.join(File.dirname(__FILE__), 'pdf.js')
  system "export NODE_PATH=$(npm root --quiet -g);
          node #{pdfjs} file://#{url} #{filename}.pdf"
end

#rsd_html_path(file) ⇒ Object



131
132
133
# File 'lib/asciidoctor/mpfd/converter.rb', line 131

def rsd_html_path(file)
  File.join(File.dirname(__FILE__), File.join("html", file))
end

#section(node) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/asciidoctor/mpfd/section.rb', line 27

def section(node)
  a = { id: Asciidoctor::ISO::Utils::anchor_or_uuid(node) }
  noko do |xml|
    case sectiontype(node)
    when "introduction" then
      if node.level == 1 then introduction_parse(a, xml, node)
      else
        clause_parse(a, xml, node)
      end
    when "terms and definitions",
      "terms, definitions, symbols and abbreviated terms",
      "terms, definitions, symbols and abbreviations",
      "terms, definitions and symbols",
      "terms, definitions and abbreviations",
      "terms, definitions and abbreviated terms",
      "glossary"
      @term_def = true
      term_def_parse(a, xml, node, true)
      @term_def = false
    when "symbols and abbreviated terms"
      symbols_parse(a, xml, node)
    when "bibliography" then bibliography_parse(a, xml, node)
    else
      if @term_def then term_def_subclause_parse(a, xml, node)
      elsif @biblio then bibliography_parse(a, xml, node)
      elsif node.attr("style") == "bibliography" && node.level == 1
        bibliography_parse(a, xml, node)
      elsif node.attr("style") == "appendix" && node.level == 1
        annex_parse(a, xml, node)
      elsif node.option? "appendix"
        appendix_parse(a, xml, node)
      else
        clause_parse(a, xml, node)
      end
    end
  end.join("\n")
end

#sections_cleanup(x) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
# 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|
    require "byebug"; byebug
    c = h.previous_element || next
    c.add_child h.remove
  end
end

#style(n, t) ⇒ Object



151
152
153
# File 'lib/asciidoctor/mpfd/converter.rb', line 151

def style(n, t)
  return
end

#term_def_title(_toplevel, node) ⇒ Object



65
66
67
# File 'lib/asciidoctor/mpfd/section.rb', line 65

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

#title(node, xml) ⇒ Object



45
46
47
48
49
50
51
# File 'lib/asciidoctor/mpfd/converter.rb', line 45

def title(node, xml)
  ["en"].each do |lang|
    xml.title **{ language: lang, format: "plain" } do |t|
      t << asciidoc_sub(node.attr("title"))
    end
  end
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



125
126
127
128
129
# File 'lib/asciidoctor/mpfd/converter.rb', line 125

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

#word_converter(node) ⇒ Object



167
168
169
170
171
172
173
174
175
176
177
# File 'lib/asciidoctor/mpfd/converter.rb', line 167

def word_converter(node)
  IsoDoc::Mpfd::WordConvert.new(
    script: node.attr("script"),
    bodyfont: node.attr("body-font"),
    headerfont: node.attr("header-font"),
    monospacefont: node.attr("monospace-font"),
    titlefont: node.attr("title-font"),
    i18nyaml: node.attr("i18nyaml"),
    scope: node.attr("scope"),
  )
end