Module: IsoDoc::Function::Utils
Constant Summary collapse
- DOCTYPE_HDR =
"<!DOCTYPE html SYSTEM " \ '"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">'.freeze
- CLAUSE_ANCESTOR =
".//ancestor::*[local-name() = 'annex' or " \ "local-name() = 'definitions' or " \ "local-name() = 'acknowledgements' or local-name() = 'term' 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() = 'acknowledgements' or local-name() = 'term' 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
- #cleanup_entities(text, is_xml: true) ⇒ Object
- #date_range(date) ⇒ Object
- #emf?(type) ⇒ Boolean
- #empty2nil(str) ⇒ Object
- #eps?(type) ⇒ Boolean
- #external_path(path) ⇒ 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, type) ⇒ Object
no recursion on references.
- #header_strip(hdr) ⇒ Object
- #header_strip_elem?(elem) ⇒ Boolean
- #image_localfile(img) ⇒ Object
- #insert_tab(out, count) ⇒ Object
- #labelled_ancestor(node) ⇒ Object
- #liquid(doc) ⇒ Object
- #noko(&block) ⇒ Object
- #ns(xpath) ⇒ Object
- #numeric_escapes(xml) ⇒ Object
- #populate_template(docxml, _format = nil) ⇒ Object
- #save_dataimage(uri, _relative_dir = true) ⇒ Object
- #save_svg(img) ⇒ Object
- #sentence_join(array) ⇒ Object
- #start_of_sentence(node) ⇒ Object
- #to_xhtml(xml) ⇒ Object
- #to_xhtml_fragment(xml) ⇒ Object
- #to_xhtml_prep(xml) ⇒ Object
- #to_xml(node) ⇒ Object
Instance Method Details
#attr_code(attributes) ⇒ Object
31 32 33 34 35 |
# File 'lib/isodoc/function/utils.rb', line 31 def attr_code(attributes) attributes.compact.transform_values do |v| v.is_a?(String) ? HTMLEntities.new.decode(v) : v end end |
#cleanup_entities(text, is_xml: true) ⇒ Object
215 216 217 218 219 220 221 222 223 224 225 |
# File 'lib/isodoc/function/utils.rb', line 215 def cleanup_entities(text, is_xml: true) c = HTMLEntities.new if is_xml text.split(/([<>])/).each_slice(4).map do |a| a[0] = c.encode(c.decode(a[0]), :hexadecimal) a end.join else c.encode(c.decode(text), :hexadecimal) end end |
#date_range(date) ⇒ Object
10 11 12 |
# File 'lib/isodoc/function/utils.rb', line 10 def date_range(date) self.class.date_range(date) end |
#emf?(type) ⇒ Boolean
206 207 208 209 |
# File 'lib/isodoc/function/utils.rb', line 206 def emf?(type) %w(application/emf application/x-emf image/x-emf image/x-mgx-emf application/x-msmetafile image/x-xbitmap image/emf).include? type end |
#empty2nil(str) ⇒ Object
154 155 156 157 |
# File 'lib/isodoc/function/utils.rb', line 154 def empty2nil(str) !str.nil? && str.is_a?(String) && str.empty? and return nil str end |
#eps?(type) ⇒ Boolean
211 212 213 |
# File 'lib/isodoc/function/utils.rb', line 211 def eps?(type) %w(application/postscript image/x-eps).include? type end |
#external_path(path) ⇒ Object
227 228 229 230 231 232 233 234 235 |
# File 'lib/isodoc/function/utils.rb', line 227 def external_path(path) win = !!((RUBY_PLATFORM =~ /(win|w)(32|64)$/) || (RUBY_PLATFORM =~ /mswin|mingw/)) if win path.gsub!(%{/}, "\\") path[/\s/] ? "\"#{path}\"" : path else path end end |
#extract_delims(text) ⇒ Object
avoid ‘; avoid {{ (Liquid Templates); avoid [[ (Javascript)
114 115 116 117 118 119 120 121 122 |
# File 'lib/isodoc/function/utils.rb', line 114 def extract_delims(text) @openmathdelim = "(#(" @closemathdelim = ")#)" while text.include?(@openmathdelim) || text.include?(@closemathdelim) @openmathdelim += "(" @closemathdelim += ")" end [@openmathdelim, @closemathdelim] end |
#from_xhtml(xml) ⇒ Object
67 68 69 70 |
# File 'lib/isodoc/function/utils.rb', line 67 def from_xhtml(xml) numeric_escapes(xml.to_xml .sub(%r{ xmlns="http://www.w3.org/1999/xhtml"}, "")) end |
#get_clause_id(node) ⇒ Object
80 81 82 |
# File 'lib/isodoc/function/utils.rb', line 80 def get_clause_id(node) node.xpath(CLAUSE_ANCESTOR)&.last&.text || nil end |
#get_note_container_id(node, type) ⇒ Object
no recursion on references
94 95 96 97 98 99 100 |
# File 'lib/isodoc/function/utils.rb', line 94 def get_note_container_id(node, type) xpath = NOTE_CONTAINER_ANCESTOR.dup %w(figure table example).include?(type) and xpath.sub!(%r[ or local-name\(\) = '#{type}'], "") container = node.xpath(xpath) container&.last&.text || nil end |
#header_strip(hdr) ⇒ Object
124 125 126 127 128 129 130 131 132 133 134 135 136 |
# File 'lib/isodoc/function/utils.rb', line 124 def header_strip(hdr) h1 = to_xhtml_fragment(hdr.to_s.gsub(%r{<br\s*/>}, " ") .gsub(%r{</?p(\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) ⇒ Boolean
138 139 140 141 142 143 |
# File 'lib/isodoc/function/utils.rb', line 138 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) ⇒ Object
191 192 193 194 195 196 197 198 199 |
# File 'lib/isodoc/function/utils.rb', line 191 def image_localfile(img) img.name == "svg" && !img["src"] and return save_svg(img) case img["src"] when /^data:/ then save_dataimage(img["src"], false) when %r{^([A-Z]:)?/} then img["src"] else File.join(@localdir, img["src"]) end end |
#insert_tab(out, count) ⇒ Object
22 23 24 25 |
# File 'lib/isodoc/function/utils.rb', line 22 def insert_tab(out, count) tab = %w(Hans Hant).include?(@script) ? " " : "  " [1..count].each { out << tab } end |
#labelled_ancestor(node) ⇒ Object
201 202 203 204 |
# File 'lib/isodoc/function/utils.rb', line 201 def labelled_ancestor(node) !node.ancestors("example, requirement, recommendation, permission, " \ "note, table, figure, sourcecode").empty? end |
#liquid(doc) ⇒ Object
145 146 147 148 149 150 151 152 |
# File 'lib/isodoc/function/utils.rb', line 145 def liquid(doc) # unescape HTML escapes in doc doc = doc.split(%r<(\{%|%\})>).each_slice(4).map do |a| a[2] = a[2].gsub(/</, "<").gsub(/>/, ">") if a.size > 2 a.join end.join Liquid::Template.parse(doc) end |
#noko(&block) ⇒ Object
27 28 29 |
# File 'lib/isodoc/function/utils.rb', line 27 def noko(&block) Metanorma::Utils::noko_html(&block) end |
#ns(xpath) ⇒ Object
14 15 16 |
# File 'lib/isodoc/function/utils.rb', line 14 def ns(xpath) self.class.ns(xpath) end |
#numeric_escapes(xml) ⇒ Object
53 54 55 |
# File 'lib/isodoc/function/utils.rb', line 53 def numeric_escapes(xml) Metanorma::Utils::numeric_escapes(xml) end |
#populate_template(docxml, _format = nil) ⇒ Object
159 160 161 162 163 164 165 166 167 168 |
# File 'lib/isodoc/function/utils.rb', line 159 def populate_template(docxml, _format = nil) = .get .merge(@labels ? { labels: @labels } : {}) .merge(.labels ? { labels: .labels } : {}) .merge( || {}) 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
170 171 172 173 174 175 176 177 178 179 180 181 |
# File 'lib/isodoc/function/utils.rb', line 170 def save_dataimage(uri, _relative_dir = true) %r{^data:(?<imgclass>image|application)/(?<imgtype>[^;]+);(?:charset=[^;]+;)?base64,(?<imgdata>.+)$} =~ uri imgtype = "emf" if emf?("#{imgclass}/#{imgtype}") imgtype = 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 |
#save_svg(img) ⇒ Object
183 184 185 186 187 188 189 |
# File 'lib/isodoc/function/utils.rb', line 183 def save_svg(img) Tempfile.open(["image", ".svg"]) do |f| f.write(img.to_xml) @tempfile_cache << f # persist to the end f.path end end |
#sentence_join(array) ⇒ Object
102 103 104 105 106 107 108 109 110 111 |
# File 'lib/isodoc/function/utils.rb', line 102 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 |
#start_of_sentence(node) ⇒ Object
18 19 20 |
# File 'lib/isodoc/function/utils.rb', line 18 def start_of_sentence(node) self.class.start_of_sentence(node) end |
#to_xhtml(xml) ⇒ Object
40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/isodoc/function/utils.rb', line 40 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) ⇒ Object
63 64 65 |
# File 'lib/isodoc/function/utils.rb', line 63 def to_xhtml_fragment(xml) Metanorma::Utils::to_xhtml_fragment(xml) end |
#to_xhtml_prep(xml) ⇒ Object
57 58 59 60 61 |
# File 'lib/isodoc/function/utils.rb', line 57 def to_xhtml_prep(xml) xml.gsub!(/<\?xml[^>]*>/, "") /<!DOCTYPE /.match(xml) || (xml = DOCTYPE_HDR + xml) numeric_escapes(xml) end |
#to_xml(node) ⇒ Object
6 7 8 |
# File 'lib/isodoc/function/utils.rb', line 6 def to_xml(node) self.class.to_xml(node) end |