module IsoDoc::Function::Utils

Constants

CLAUSE_ANCESTOR
DOCTYPE_HDR
NOKOHEAD

add namespaces for Word fragments

NOTE_CONTAINER_ANCESTOR

Public Instance Methods

attr_code(attributes) click to toggle source
# File lib/isodoc/function/utils.rb, line 39
def attr_code(attributes)
  attributes = attributes.reject { |_, val| val.nil? }.map
  attributes.map do |k, v|
    [k, v.is_a?(String) ? HTMLEntities.new.decode(v) : v]
  end.to_h
end
date_range(date) click to toggle source
# File lib/isodoc/function/utils.rb, line 6
def date_range(date)
  self.class.date_range(date)
end
empty2nil(str) click to toggle source
# File lib/isodoc/function/utils.rb, line 166
def empty2nil(str)
  return nil if !str.nil? && str.is_a?(String) && str.empty?

  str
end
extract_delims(text) click to toggle source

avoid `; avoid {{ (Liquid Templates); avoid [[ (Javascript)

# File lib/isodoc/function/utils.rb, line 121
def extract_delims(text)
  @openmathdelim = "(#("
  @closemathdelim = ")#)"
  while text.include?(@openmathdelim) || text.include?(@closemathdelim)
    @openmathdelim += "("
    @closemathdelim += ")"
  end
  [@openmathdelim, @closemathdelim]
end
from_xhtml(xml) click to toggle source
# File lib/isodoc/function/utils.rb, line 78
def from_xhtml(xml)
  xml.to_xml.sub(%r{ xmlns="http://www.w3.org/1999/xhtml"}, "")
end
get_clause_id(node) click to toggle source
# File lib/isodoc/function/utils.rb, line 90
def get_clause_id(node)
  clause = node.xpath(CLAUSE_ANCESTOR)
  clause&.last&.text || nil
end
get_note_container_id(node) click to toggle source
# File lib/isodoc/function/utils.rb, line 104
def get_note_container_id(node)
  container = node.xpath(NOTE_CONTAINER_ANCESTOR)
  container&.last&.text || nil
end
header_strip(hdr) click to toggle source
# File lib/isodoc/function/utils.rb, line 131
def header_strip(hdr)
  h1 = to_xhtml_fragment(hdr.to_s.gsub(%r{<br\s*/>}, " ")
    .gsub(/<\/?h[123456][^>]*>/, "").gsub(/<\/?b[^>]*>/, "").dup)
  h1.traverse do |x|
    if x.name == "span" && /mso-tab-count/.match(x["style"])
      x.replace(" ")
    elsif header_strip_elem?(x) then x.remove
    elsif x.name == "a" then x.replace(x.children)
    end
  end
  from_xhtml(h1)
end
header_strip_elem?(elem) click to toggle source
# File lib/isodoc/function/utils.rb, line 144
def header_strip_elem?(elem)
  elem.name == "img" ||
    elem.name == "span" && elem["class"] == "MsoCommentReference" ||
    elem.name == "a" && elem["class"] == "FootnoteRef" ||
    elem.name == "span" && /mso-bookmark/.match(elem["style"])
end
image_localfile(img) click to toggle source
# File lib/isodoc/function/utils.rb, line 195
def image_localfile(img)
  if /^data:/.match? img["src"]
    save_dataimage(img["src"], false)
  elsif %r{^([A-Z]:)?/}.match? img["src"]
    img["src"]
  else
    File.join(@localdir, img["src"])
  end
end
insert_tab(out, count) click to toggle source
# File lib/isodoc/function/utils.rb, line 14
def insert_tab(out, count)
  tab = %w(Hans Hant).include?(@script) ? "&#x3000;" : "&nbsp; "
  [1..count].each { out << tab }
end
labelled_ancestor(node) click to toggle source
# File lib/isodoc/function/utils.rb, line 205
def labelled_ancestor(node)
  !node.ancestors("example, requirement, recommendation, permission, "\
                  "note, table, figure, sourcecode").empty?
end
liquid(doc) click to toggle source

def liquid(doc)

self.class.liquid(doc)

end

# File lib/isodoc/function/utils.rb, line 157
def liquid(doc)
  # unescape HTML escapes in doc
  doc = doc.split(%r<(\{%|%\})>).each_slice(4).map do |a|
    a[2] = a[2].gsub(/&lt;/, "<").gsub(/&gt;/, ">") if a.size > 2
    a.join("")
  end.join("")
  Liquid::Template.parse(doc)
end
noko(&block) click to toggle source

block for processing XML document fragments as XHTML, to allow for HTMLentities

# File lib/isodoc/function/utils.rb, line 30
def noko(&block)
  doc = ::Nokogiri::XML.parse(NOKOHEAD)
  fragment = doc.fragment("")
  ::Nokogiri::XML::Builder.with fragment, &block
  fragment.to_xml(encoding: "US-ASCII").lines.map do |l|
    l.gsub(/\s*\n/, "")
  end
end
ns(xpath) click to toggle source
# File lib/isodoc/function/utils.rb, line 10
def ns(xpath)
  self.class.ns(xpath)
end
populate_template(docxml, _format = nil) click to toggle source
# File lib/isodoc/function/utils.rb, line 172
def populate_template(docxml, _format = nil)
  meta = @meta
    .get
    .merge(@labels ? { labels: @labels } : {})
    .merge(@meta.labels ? { labels: @meta.labels } : {})
    .merge(fonts_options || {})
  template = liquid(docxml)
  template.render(meta.map { |k, v| [k.to_s, empty2nil(v)] }.to_h)
    .gsub("&lt;", "&#x3c;").gsub("&gt;", "&#x3e;").gsub("&amp;", "&#x26;")
end
save_dataimage(uri, _relative_dir = true) click to toggle source
# File lib/isodoc/function/utils.rb, line 183
def save_dataimage(uri, _relative_dir = true)
  %r{^data:(image|application)/(?<imgtype>[^;]+);base64,(?<imgdata>.+)$} =~ uri
  imgtype.sub!(/\+[a-z0-9]+$/, "") # svg+xml
  imgtype = "png" unless /^[a-z0-9]+$/.match? imgtype
  Tempfile.open(["image", ".#{imgtype}"]) do |f|
    f.binmode
    f.write(Base64.strict_decode64(imgdata))
    @tempfile_cache << f # persist to the end
    f.path
  end
end
sentence_join(array) click to toggle source
# File lib/isodoc/function/utils.rb, line 109
def sentence_join(array)
  return "" if array.nil? || array.empty?

  if array.length == 1 then array[0]
  else
    @i18n.l10n("#{array[0..-2].join(', ')} "\
               "#{@i18n.and} #{array.last}",
               @lang, @script)
  end
end
to_xhtml(xml) click to toggle source
# File lib/isodoc/function/utils.rb, line 49
def to_xhtml(xml)
  xml = to_xhtml_prep(xml)
  begin
    Nokogiri::XML.parse(xml, &:strict)
  rescue Nokogiri::XML::SyntaxError => e
    File.open("#{@filename}.#{@format}.err", "w:UTF-8") do |f|
      f.write xml
    end
    abort "Malformed Output XML for #{@format}: #{e} "\
      "(see #{@filename}.#{@format}.err)"
  end
end
to_xhtml_fragment(xml) click to toggle source
# File lib/isodoc/function/utils.rb, line 73
def to_xhtml_fragment(xml)
  doc = ::Nokogiri::XML.parse(NOKOHEAD)
  doc.fragment(xml)
end
to_xhtml_prep(xml) click to toggle source
# File lib/isodoc/function/utils.rb, line 62
def to_xhtml_prep(xml)
  xml.gsub!(/<\?xml[^>]*>/, "")
  /<!DOCTYPE /.match(xml) || (xml = DOCTYPE_HDR + xml)
  xml.split(/(&[^ \r\n\t#;]+;)/).map do |t|
    if /^(&[^ \t\r\n#;]+;)/.match?(t)
      HTMLEntities.new.encode(HTMLEntities.new.decode(t), :hexadecimal)
    else t
    end
  end.join("")
end