Module: Asciidoctor::ISO::Base

Included in:
Converter
Defined in:
lib/asciidoctor/iso/base.rb

Constant Summary collapse

TERM_REFERENCE_RE_STR =
<<~REGEXP.freeze
  ^(?<xref><xref[^>]+>([^<]*</xref>)?)
         (,\s(?<text>.*))?
  $
REGEXP
TERM_REFERENCE_RE =
Regexp.new(TERM_REFERENCE_RE_STR.gsub(/\s/, "").gsub(/_/, "\\s"),
Regexp::IGNORECASE | Regexp::MULTILINE)

Instance Method Summary collapse

Instance Method Details

#add_term_source(xml_t, seen_xref, m) ⇒ Object



142
143
144
145
146
147
148
# File 'lib/asciidoctor/iso/base.rb', line 142

def add_term_source(xml_t, seen_xref, m)
  xml_t.origin seen_xref.children[0].content,
    **attr_code(term_source_attr(seen_xref))
  m[:text] && xml_t.modification do |mod|
    mod.p { |p| p << m[:text].sub(/^\s+/, "") }
  end
end

#content(node) ⇒ Object



20
21
22
# File 'lib/asciidoctor/iso/base.rb', line 20

def content(node)
  node.content
end

#default_fonts(node) ⇒ Object



79
80
81
82
83
84
85
86
87
88
# File 'lib/asciidoctor/iso/base.rb', line 79

def default_fonts(node)
  b = node.attr("body-font") ||
    (node.attr("script") == "Hans" ? '"SimSun",serif' :
     '"Cambria",serif')
  h = node.attr("header-font") ||
    (node.attr("script") == "Hans" ? '"SimHei",sans-serif' :
     '"Cambria",serif')
  m = node.attr("monospace-font") || '"Courier New",monospace'
  "$bodyfont: #{b};\n$headerfont: #{h};\n$monospacefont: #{m};\n"
end

#doc_converter(node) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/asciidoctor/iso/base.rb', line 55

def doc_converter(node)
  IsoDoc::WordConvert.new(
    wordstylesheet:  generate_css(html_doc_path("wordstyle.scss"), false),
    standardstylesheet: generate_css(html_doc_path("isodoc.scss"), false),
    header: html_doc_path("header.html"),
    wordcoverpage: html_doc_path("word_iso_titlepage.html"),
    wordintropage: html_doc_path("word_iso_intro.html"),
    i18nyaml: node.attr("i18nyaml"),
    ulstyle: "l3",
    olstyle: "l2",
  )
end

#document(node) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/asciidoctor/iso/base.rb', line 90

def document(node)
  init(node)
  ret1 = makexml(node)
  ret = ret1.to_xml(indent: 2)
  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")
  doc_converter(node).convert filename unless node.attr("nodoc")
  @files_to_delete.each { |f| system "rm #{f}" }
  ret
end

#draft?Boolean

Returns:

  • (Boolean)


119
120
121
# File 'lib/asciidoctor/iso/base.rb', line 119

def draft?
  @draft
end

#extract_termsource_refs(text, node) ⇒ Object



159
160
161
162
163
164
165
# File 'lib/asciidoctor/iso/base.rb', line 159

def extract_termsource_refs(text, node)
  matched = TERM_REFERENCE_RE.match text
  if matched.nil?
    Utils::warning(node, "term reference not in expected format", text)
  end
  matched
end

#front(node, xml) ⇒ Object



123
124
125
126
127
128
# File 'lib/asciidoctor/iso/base.rb', line 123

def front(node, xml)
  xml.bibdata **attr_code(type: node.attr("doctype")) do |b|
     node, b
  end
  (node, xml)
end

#generate_css(filename, stripwordcss) ⇒ Object



35
36
37
38
39
40
41
42
43
# File 'lib/asciidoctor/iso/base.rb', line 35

def generate_css(filename, stripwordcss)
  stylesheet = File.read(filename, encoding: "UTF-8")
  stylesheet = stylesheet.gsub(/(\s|\{)mso-[^:]+:[^;]+;/m, "\\1") if stripwordcss
  engine = Sass::Engine.new(@fontheader + stylesheet, syntax: :scss)
  outname = File.basename(filename, ".*") + ".css"
  File.open(outname, "w") { |f| f.write(engine.render) }
  @files_to_delete << outname
  outname
end

#html_converter(node) ⇒ Object



45
46
47
48
49
50
51
52
53
# File 'lib/asciidoctor/iso/base.rb', line 45

def html_converter(node)
  IsoDoc::Convert.new(
    htmlstylesheet: generate_css(html_doc_path("htmlstyle.scss"), true),
    standardstylesheet: generate_css(html_doc_path("isodoc.scss"), true),
    htmlcoverpage: html_doc_path("html_iso_titlepage.html"),
    htmlintropage: html_doc_path("html_iso_intro.html"),
    i18nyaml: node.attr("i18nyaml"),
  )
end

#html_doc_path(file) ⇒ Object



31
32
33
# File 'lib/asciidoctor/iso/base.rb', line 31

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

#init(node) ⇒ Object



68
69
70
71
72
73
74
75
76
77
# File 'lib/asciidoctor/iso/base.rb', line 68

def init(node)
  @fn_number = 0
  @draft = false
  @refids = Set.new
  @anchors = {}
  @draft = node.attributes.has_key?("draft")
  @novalid = node.attr("novalid")
  @fontheader = default_fonts(node)
  @files_to_delete = []
end

#makexml(node) ⇒ Object



111
112
113
114
115
116
117
# File 'lib/asciidoctor/iso/base.rb', line 111

def makexml(node)
  result = makexml1(node)
  ret1 = cleanup(Nokogiri::XML(result))
  ret1.root.add_namespace(nil, "http://riboseinc.com/isoxml")
  validate(ret1) unless @novalid
  ret1
end

#makexml1(node) ⇒ Object



103
104
105
106
107
108
109
# File 'lib/asciidoctor/iso/base.rb', line 103

def makexml1(node)
  result = ["<?xml version='1.0' encoding='UTF-8'?>\n<iso-standard>"]
  result << noko { |ixml| front node, ixml }
  result << noko { |ixml| middle node, ixml }
  result << "</iso-standard>"
  textcleanup(result.flatten * "\n")
end

#middle(node, xml) ⇒ Object



130
131
132
133
134
# File 'lib/asciidoctor/iso/base.rb', line 130

def middle(node, xml)
  xml.sections do |s|
    s << node.content if node.blocks?
  end
end

#skip(node, name = nil) ⇒ Object



24
25
26
27
28
29
# File 'lib/asciidoctor/iso/base.rb', line 24

def skip(node, name = nil)
  name = name || node.node_name
  w = "converter missing for #{name} node in ISO backend"
  Utils::warning(node, w, nil)
  nil
end

#term_source_attr(seen_xref) ⇒ Object



136
137
138
139
140
# File 'lib/asciidoctor/iso/base.rb', line 136

def term_source_attr(seen_xref)
  { bibitemid: seen_xref.children[0]["target"],
    format: seen_xref.children[0]["format"],
    type: "inline" }
end

#termsource(node) ⇒ Object



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

def termsource(node)
  matched = extract_termsource_refs(node.content, node) || return
  noko do |xml|
    attrs = { status: matched[:text] ? "modified" : "identical" }
    xml.termsource **attrs do |xml_t|
      seen_xref = Nokogiri::XML.fragment(matched[:xref])
      add_term_source(xml_t, seen_xref, matched)
    end
  end.join("\n")
end