require_relative "image" require_relative "sourcecode" require "rouge" module IsoDoc class PresentationXMLConvert < ::IsoDoc::Convert def lower2cap(text) /^[[:upper:]][[:upper:]]/.match?(text) and return text text.capitalize end def block_delim " — " end def prefix_name(node, delim, number, elem) number.nil? || number.empty? and return unless name = node.at(ns("./#{elem}")) (node.children.empty? and node.add_child("<#{elem}>")) or node.children.first.previous = "<#{elem}>" name = node.children.first end if name.children.empty? then name.add_child(cleanup_entities(number.strip)) else (name.children.first.previous = "#{number.strip}#{delim}") end end def formula(docxml) docxml.xpath(ns("//formula")).each { |f| formula1(f) } end def formula1(elem) formula_where(elem.at(ns("./dl"))) lbl = @xrefs.anchor(elem["id"], :label, false) prefix_name(elem, "", lbl, "name") end def formula_where(dlist) dlist or return dlist["class"] = "formula_dl" where = dlist.xpath(ns("./dt")).size > 1 ? @i18n.where : @i18n.where_one dlist.previous = "

#{where}

" end def example(docxml) docxml.xpath(ns("//example")).each { |f| example1(f) } end def example1(elem) n = @xrefs.get[elem["id"]] lbl = if n.nil? || n[:label].nil? || n[:label].empty? @i18n.example else l10n("#{@i18n.example} #{n[:label]}") end prefix_name(elem, block_delim, lbl, "name") end def note(docxml) docxml.xpath(ns("//note")).each { |f| note1(f) } end def note1(elem) %w(bibdata bibitem).include?(elem.parent.name) || elem["notag"] == "true" and return n = @xrefs.get[elem["id"]] lbl = @i18n.note (n.nil? || n[:label].nil? || n[:label].empty?) or lbl = l10n("#{lbl} #{n[:label]}") prefix_name(elem, "", lbl, "name") end def admonition(docxml) docxml.xpath(ns("//admonition")).each { |f| admonition1(f) } end def admonition1(elem) if elem["type"] == "box" admonition_numbered1(elem) else elem["notag"] == "true" || elem.at(ns("./name")) and return prefix_name(elem, "", @i18n.admonition[elem["type"]]&.upcase, "name") end end def admonition_numbered1(elem) elem["unnumbered"] && !elem.at(ns("./name")) and return n = @xrefs.anchor(elem["id"], :label, false) prefix_name(elem, block_delim, l10n("#{@i18n.box} #{n}"), "name") end def table(docxml) table_long_strings_cleanup(docxml) docxml.xpath(ns("//table")).each { |f| table1(f) } end def table1(elem) labelled_ancestor(elem) and return elem["unnumbered"] && !elem.at(ns("./name")) and return n = @xrefs.anchor(elem["id"], :label, false) prefix_name(elem, block_delim, l10n("#{lower2cap @i18n.table} #{n}"), "name") end def table_long_strings_cleanup(docxml) @break_up_urls_in_tables or return docxml.xpath("//td | //th").each do |d| d.traverse do |n| n.text? or next ret = break_up_long_str(n.text) n.content = ret end end end LONGSTR_THRESHOLD = 10 LONGSTR_NOPUNCT = 2 def break_up_long_str(text) /^\s*$/.match?(text) and return text text.split(/(?=\s)/).map do |w| if /^\s*$/.match(text) || (w.size < LONGSTR_THRESHOLD) then w else w.scan(/.{,#{LONGSTR_THRESHOLD}}/o).map.with_index do |w1, i| w1.size < LONGSTR_THRESHOLD ? w1 : break_up_long_str1(w1, i + 1) end.join end end.join end STR_BREAKUP_RE = %r{ (?<=[=_—–\u2009→?+;]) | # break after any of these (?<=[,.:])(?!\d) | # break on punct only if not preceding digit (?<=[>])(?![>]) | # > not >-> (?<=[\]])(?![\]]) | # ] not ]-] (?<=//) | # // (?<=[/])(?![/]) | # / not /-/ (?