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



174
175
176
177
178
179
180
# File 'lib/asciidoctor/iso/base.rb', line 174

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



22
23
24
# File 'lib/asciidoctor/iso/base.rb', line 22

def content(node)
  node.content
end

#default_fonts(node) ⇒ Object



106
107
108
109
110
111
112
113
114
115
# File 'lib/asciidoctor/iso/base.rb', line 106

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



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

def doc_converter(node)
  IsoDoc::Iso::WordConvert.new(
    script: node.attr("script"),
    bodyfont: node.attr("body-font"),
    headerfont: node.attr("header-font"),
    monospacefont: node.attr("monospace-font"),
    i18nyaml: node.attr("i18nyaml"),
  )
end

#doctype(node) ⇒ Object



151
152
153
# File 'lib/asciidoctor/iso/base.rb', line 151

def doctype(node)
  node.attr("doctype")
end

#document(node) ⇒ Object



117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/asciidoctor/iso/base.rb', line 117

def document(node)
  init(node)
  ret = makexml(node).to_xml(indent: 2)
  unless node.attr("nodoc") || !node.attr("docfile")
    File.open(@filename + ".xml", "w:UTF-8") { |f| f.write(ret) }
    html_converter_alt(node).convert(@filename + ".xml")
    system "mv #{@filename}.html #{@filename}_alt.html"
    html_converter(node).convert(@filename + ".xml")
    doc_converter(node).convert(@filename + ".xml")
  end
  @files_to_delete.each { |f| system "rm #{f}" }
  ret
end

#draft?Boolean

Returns:

  • (Boolean)


147
148
149
# File 'lib/asciidoctor/iso/base.rb', line 147

def draft?
  @draft
end

#extract_termsource_refs(text, node) ⇒ Object



191
192
193
194
195
196
197
# File 'lib/asciidoctor/iso/base.rb', line 191

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



155
156
157
158
159
160
# File 'lib/asciidoctor/iso/base.rb', line 155

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

#html_converter(node) ⇒ Object



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

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

#html_converter_alt(node) ⇒ Object



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

def html_converter_alt(node)
  IsoDoc::Iso::HtmlConvert.new(
    script: node.attr("script"),
    bodyfont: node.attr("body-font"),
    headerfont: node.attr("header-font"),
    monospacefont: node.attr("monospace-font"),
    i18nyaml: node.attr("i18nyaml"),
    alt: true,
  )
end

#init(node) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/asciidoctor/iso/base.rb', line 64

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 = []
  @filename = node.attr("docfile") ?
    node.attr("docfile").gsub(/\.adoc$/, "").gsub(%r{^.*/}, "") : ""
  @no_isobib_cache = node.attr("no-isobib-cache")
  @no_isobib = node.attr("no-isobib")
  @bibdb = nil
  init_bib_caches(node)
  init_iev_caches(node)
end

#init_bib_caches(node) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
# File 'lib/asciidoctor/iso/base.rb', line 82

def init_bib_caches(node)
  unless (@no_isobib_cache || @no_isobib)
    globalname = bibliocache_name(true)
    localname = bibliocache_name(false) if node.attr("local-cache")
    if node.attr("flush-caches")
      system("rm -f #{globalname}") unless globalname.nil?
      system("rm -f #{localname}") unless localname.nil?
    end
  end        
  @bibdb = Relaton::Db.new(globalname, localname) unless @no_isobib
end

#init_iev_caches(node) ⇒ Object



94
95
96
97
98
99
100
101
102
103
104
# File 'lib/asciidoctor/iso/base.rb', line 94

def init_iev_caches(node)
  unless (@no_isobib_cache || @no_isobib)
    globalname = ievcache_name(true)
    localname = ievcache_name(false) if node.attr("local-cache")
    if node.attr("flush-caches")
      system("rm -f #{globalname}") unless globalname.nil?
      system("rm -f #{localname}") unless localname.nil?
    end
  end
  @iev = Iev::Db.new(globalname, localname) unless @no_isobib
end

#makexml(node) ⇒ Object



139
140
141
142
143
144
145
# File 'lib/asciidoctor/iso/base.rb', line 139

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



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

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



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

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

#skip(node, name = nil) ⇒ Object



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

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



168
169
170
171
172
# File 'lib/asciidoctor/iso/base.rb', line 168

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

#termsource(node) ⇒ Object



199
200
201
202
203
204
205
206
207
208
# File 'lib/asciidoctor/iso/base.rb', line 199

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