Module: IsoDoc::Function::Utils
- Included in:
- Common
- Defined in:
- lib/isodoc/function/utils.rb
Constant Summary collapse
- NOKOHEAD =
add namespaces for Word fragments
<<~HERE.freeze <!DOCTYPE html SYSTEM "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title></title> <meta charset="UTF-8" /> </head> <body> </body> </html> HERE
- CLAUSE_ANCESTOR =
".//ancestor::*[local-name() = 'annex' or "\ "local-name() = 'appendix' or local-name() = 'foreword' or "\ "local-name() = 'introduction' or local-name() = 'terms' or "\ "local-name() = 'clause' or local-name() = 'references']/@id".freeze
- NOTE_CONTAINER_ANCESTOR =
".//ancestor::*[local-name() = 'annex' or "\ "local-name() = 'foreword' or local-name() = 'appendix' or "\ "local-name() = 'introduction' or local-name() = 'terms' or "\ "local-name() = 'clause' or local-name() = 'references' or "\ "local-name() = 'figure' or local-name() = 'formula' or "\ "local-name() = 'table' or local-name() = 'example']/@id".freeze
Instance Method Summary collapse
- #attr_code(attributes) ⇒ Object
- #date_range(date) ⇒ Object
- #empty2nil(v) ⇒ Object
-
#extract_delims(text) ⇒ Object
avoid ‘; avoid {{ (Liquid Templates); avoid [[ (Javascript).
- #from_xhtml(xml) ⇒ Object
- #get_clause_id(node) ⇒ Object
- #get_note_container_id(node) ⇒ Object
- #header_strip(h) ⇒ Object
- #insert_tab(out, n) ⇒ Object
- #liquid(doc) ⇒ Object
-
#noko(&block) ⇒ Object
block for processing XML document fragments as XHTML, to allow for HTMLentities.
- #ns(xpath) ⇒ Object
- #populate_template(docxml, _format) ⇒ Object
- #save_dataimage(uri, relative_dir = true) ⇒ Object
- #sentence_join(array) ⇒ Object
- #to_xhtml(xml) ⇒ Object
- #to_xhtml_fragment(xml) ⇒ Object
Instance Method Details
#attr_code(attributes) ⇒ Object
36 37 38 39 40 41 |
# File 'lib/isodoc/function/utils.rb', line 36 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) ⇒ Object
4 5 6 |
# File 'lib/isodoc/function/utils.rb', line 4 def date_range(date) self.class.date_range(date) end |
#empty2nil(v) ⇒ Object
131 132 133 134 |
# File 'lib/isodoc/function/utils.rb', line 131 def empty2nil(v) return nil if !v.nil? && v.is_a?(String) && v.empty? v end |
#extract_delims(text) ⇒ Object
avoid ‘; avoid {{ (Liquid Templates); avoid [[ (Javascript)
96 97 98 99 100 101 102 103 104 |
# File 'lib/isodoc/function/utils.rb', line 96 def extract_delims(text) @openmathdelim = "(#(" @closemathdelim = ")#)" while text.include?(@openmathdelim) || text.include?(@closemathdelim) @openmathdelim += "(" @closemathdelim += ")" end [@openmathdelim, @closemathdelim] end |
#from_xhtml(xml) ⇒ Object
58 59 60 |
# File 'lib/isodoc/function/utils.rb', line 58 def from_xhtml(xml) xml.to_xml.sub(%r{ xmlns="http://www.w3.org/1999/xhtml"}, "") end |
#get_clause_id(node) ⇒ Object
68 69 70 71 |
# File 'lib/isodoc/function/utils.rb', line 68 def get_clause_id(node) clause = node.xpath(CLAUSE_ANCESTOR) clause&.last&.text || nil end |
#get_note_container_id(node) ⇒ Object
81 82 83 84 |
# File 'lib/isodoc/function/utils.rb', line 81 def get_note_container_id(node) container = node.xpath(NOTE_CONTAINER_ANCESTOR) container&.last&.text || nil end |
#header_strip(h) ⇒ Object
106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/isodoc/function/utils.rb', line 106 def header_strip(h) h = h.to_s.gsub(%r{<br/>}, " ").sub(/<\/?h[123456][^>]*>/, "") h1 = to_xhtml_fragment(h.dup) h1.traverse do |x| x.replace(" ") if x.name == "span" && /mso-tab-count/.match(x["style"]) x.remove if x.name == "span" && x["class"] == "MsoCommentReference" x.remove if x.name == "a" && x["epub:type"] == "footnote" x.replace(x.children) if x.name == "a" end from_xhtml(h1) end |
#insert_tab(out, n) ⇒ Object
12 13 14 |
# File 'lib/isodoc/function/utils.rb', line 12 def insert_tab(out, n) [1..n].each { out << " " } end |
#liquid(doc) ⇒ Object
118 119 120 |
# File 'lib/isodoc/function/utils.rb', line 118 def liquid(doc) self.class.liquid(doc) end |
#noko(&block) ⇒ Object
block for processing XML document fragments as XHTML, to allow for HTMLentities
27 28 29 30 31 32 33 34 |
# File 'lib/isodoc/function/utils.rb', line 27 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) ⇒ Object
8 9 10 |
# File 'lib/isodoc/function/utils.rb', line 8 def ns(xpath) self.class.ns(xpath) end |
#populate_template(docxml, _format) ⇒ Object
136 137 138 139 140 141 142 143 144 145 146 |
# File 'lib/isodoc/function/utils.rb', line 136 def populate_template(docxml, _format) = @meta.get.merge(@labels || {}) docxml = docxml. gsub(/\[TERMREF\]\s*/, l10n("[#{@source_lbl}: ")). gsub(/\s*\[MODIFICATION\]\s*\[\/TERMREF\]/, l10n(", #{@modified_lbl} [/TERMREF]")). gsub(/\s*\[\/TERMREF\]\s*/, l10n("]")). gsub(/\s*\[MODIFICATION\]/, l10n(", #{@modified_lbl} — ")) template = liquid(docxml) template.render(.map { |k, v| [k.to_s, empty2nil(v)] }.to_h). gsub('<', '<').gsub('>', '>').gsub('&', '&') end |
#save_dataimage(uri, relative_dir = true) ⇒ Object
148 149 150 151 152 153 154 155 156 |
# File 'lib/isodoc/function/utils.rb', line 148 def save_dataimage(uri, relative_dir = true) %r{^data:image/(?<imgtype>[^;]+);base64,(?<imgdata>.+)$} =~ uri uuid = UUIDTools::UUID.random_create.to_s fname = "#{uuid}.#{imgtype}" new_file = File.join(tmpimagedir, fname) @files_to_delete << new_file File.open(new_file, "wb") { |f| f.write(Base64.strict_decode64(imgdata)) } File.join(relative_dir ? rel_tmpimagedir : tmpimagedir, fname) end |
#sentence_join(array) ⇒ Object
86 87 88 89 90 91 92 93 |
# File 'lib/isodoc/function/utils.rb', line 86 def sentence_join(array) return "" if array.nil? || array.empty? if array.length == 1 array[0] else l10n("#{array[0..-2].join(', ')} #{@and_lbl} #{array.last}") end end |
#to_xhtml(xml) ⇒ Object
43 44 45 46 47 48 49 50 |
# File 'lib/isodoc/function/utils.rb', line 43 def to_xhtml(xml) xml.gsub!(/<\?xml[^>]*>/, "") unless /<!DOCTYPE /.match xml xml = '<!DOCTYPE html SYSTEM "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">' + xml end Nokogiri::XML.parse(xml) end |
#to_xhtml_fragment(xml) ⇒ Object
52 53 54 55 56 |
# File 'lib/isodoc/function/utils.rb', line 52 def to_xhtml_fragment(xml) doc = ::Nokogiri::XML.parse(NOKOHEAD) fragment = doc.fragment(xml) fragment end |