Class: Metanorma::Compile
- Inherits:
-
Object
- Object
- Metanorma::Compile
- Defined in:
- lib/metanorma/compile.rb
Constant Summary collapse
- REQUIREMENT_XPATH =
"//requirement | //xmlns:requirement | "\ "//recommendation | //xmlns:recommendation | //permission | "\ "//xmlns:permission".freeze
Instance Attribute Summary collapse
- #errors ⇒ Array<String> readonly
- #processor ⇒ Array<String> readonly
Instance Method Summary collapse
- #clean_sourcecode(xml) ⇒ Object
- #compile(filename, options = {}) ⇒ Object
- #extract(isodoc, dirname, extract_types) ⇒ Object
- #get_extensions(options) ⇒ Object
- #image_export(xml, dirname) ⇒ Object
-
#initialize ⇒ Compile
constructor
A new instance of Compile.
- #install_fonts(options) ⇒ Object
- #options_extract(filename, options) ⇒ Object
-
#process_extensions(extensions, file, isodoc, options) ⇒ Object
isodoc is Raw Metanorma XML.
- #process_input(filename, options) ⇒ Object
- #read_file(filename) ⇒ Object
- #relaton_export(isodoc, options) ⇒ Object
- #require_libraries(options) ⇒ Object
- #requirement_export(xml, dirname) ⇒ Object
-
#sort_extensions_execution(ext) ⇒ Object
dependency ordering.
- #sourcecode_export(xml, dirname) ⇒ Object
- #validate_format(options) ⇒ Object
- #validate_type(options) ⇒ Object
- #wrap_html(options, file_extension, outfilename) ⇒ Object
- #xml_options_extract(file) ⇒ Object
Constructor Details
Instance Attribute Details
#errors ⇒ Array<String> (readonly)
11 12 13 |
# File 'lib/metanorma/compile.rb', line 11 def errors @errors end |
#processor ⇒ Array<String> (readonly)
11 12 13 |
# File 'lib/metanorma/compile.rb', line 11 def processor @processor end |
Instance Method Details
#clean_sourcecode(xml) ⇒ Object
162 163 164 165 166 167 168 |
# File 'lib/metanorma/compile.rb', line 162 def clean_sourcecode(xml) xml.xpath(".//callout | .//annotation | .//xmlns:callout | .//xmlns:annotation").each do |x| x.remove end xml.xpath(".//br | .//xmlns:br").each { |x| x.replace("\n") } HTMLEntities.new.decode(xml.children.to_xml) end |
#compile(filename, options = {}) ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/metanorma/compile.rb', line 18 def compile(filename, = {}) require_libraries() = (filename, ) validate_type() && validate_format() || (return nil) @processor = @registry.find_processor([:type].to_sym) extensions = get_extensions() or return nil (file, isodoc = process_input(filename, )) or return nil relaton_export(isodoc, ) extract(isodoc, [:extract], [:extract_type]) install_fonts() process_extensions(extensions, file, isodoc, ) end |
#extract(isodoc, dirname, extract_types) ⇒ Object
170 171 172 173 174 175 176 177 178 179 180 181 |
# File 'lib/metanorma/compile.rb', line 170 def extract(isodoc, dirname, extract_types) return unless dirname if extract_types.nil? || extract_types.empty? extract_types = [:sourcecode, :image, :requirement] end FileUtils.rm_rf dirname FileUtils.mkdir_p dirname xml = Nokogiri::XML(isodoc) sourcecode_export(xml, dirname) if extract_types.include? :sourcecode image_export(xml, dirname) if extract_types.include? :image requirement_export(xml, dirname) if extract_types.include? :requirement end |
#get_extensions(options) ⇒ Object
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 |
# File 'lib/metanorma/compile.rb', line 107 def get_extensions() [:extension_keys] ||= @processor.output_formats.reduce([]) do |memo, (k, _)| memo << k end extensions = [:extension_keys].reduce([]) do |memo, e| if @processor.output_formats[e] memo << e else = "[metanorma] Error: #{e} format is not supported for this standard." @errors << Util.log(, :error) memo end end if !extensions.include?(:presentation) and extensions.any? { |e| @processor.use_presentation_xml(e) } extensions << :presentation end extensions end |
#image_export(xml, dirname) ⇒ Object
194 195 196 197 198 199 200 201 202 203 204 205 |
# File 'lib/metanorma/compile.rb', line 194 def image_export(xml, dirname) xml.at("//image | //xmlns:image") or return FileUtils.mkdir_p "#{dirname}/image" xml.xpath("//image | //xmlns:image").each_with_index do |s, i| next unless /^data:image/.match s["src"] %r{^data:image/(?<imgtype>[^;]+);base64,(?<imgdata>.+)$} =~ s["src"] filename = s["filename"] || sprintf("image-%04d.%s", i, imgtype) File.open("#{dirname}/image/#{filename}", "wb") do |f| f.write(Base64.strict_decode64(imgdata)) end end end |
#install_fonts(options) ⇒ Object
271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 |
# File 'lib/metanorma/compile.rb', line 271 def install_fonts() if [:no_install_fonts] Util.log("[fontist] Skip font installation because" \ " --no-install-fonts argument passed", :debug) return end if !@processor.respond_to?(:fonts_manifest) || @processor.fonts_manifest.nil? Util.log("[fontist] Skip font installation because font_manifest is missing", :debug) return end @updated_formulas_repo = false manifest = @processor.fonts_manifest agree_to_terms = [:agree_to_terms] || false continue_without_fonts = [:continue_without_fonts] || false install_fonts_safe(manifest, agree_to_terms, continue_without_fonts) end |
#options_extract(filename, options) ⇒ Object
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/metanorma/compile.rb', line 49 def (filename, ) content = read_file(filename) o = Metanorma::Input::Asciidoc.new.(content) o = o.merge((content)) [:type] ||= o[:type]&.to_sym t = @registry.alias([:type]) and [:type] = t dir = filename.sub(%r(/[^/]+$), "/") [:relaton] ||= "#{dir}/#{o[:relaton]}" if o[:relaton] [:sourcecode] ||= "#{dir}/#{o[:sourcecode]}" if o[:sourcecode] [:extension_keys] ||= o[:extensions]&.split(/,[ ]*/)&.map(&:to_sym) [:extension_keys] = nil if [:extension_keys] == [:all] [:format] ||= :asciidoc [:filename] = filename end |
#process_extensions(extensions, file, isodoc, options) ⇒ Object
isodoc is Raw Metanorma XML
243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 |
# File 'lib/metanorma/compile.rb', line 243 def process_extensions(extensions, file, isodoc, ) f = change_output_dir xml_name = f.sub(/\.[^.]+$/, ".xml") presentationxml_name = f.sub(/\.[^.]+$/, ".presentation.xml") extensions.sort do |a, b| sort_extensions_execution(a) <=> sort_extensions_execution(b) end.each do |ext| = @processor.(file) [:datauriimage] = true if [:datauriimage] file_extension = @processor.output_formats[ext] outfilename = f.sub(/\.[^.]+$/, ".#{file_extension}") if ext == :rxl [:relaton] = outfilename relaton_export(isodoc, ) else begin @processor.use_presentation_xml(ext) ? @processor.output(nil, presentationxml_name, outfilename, ext, ) : @processor.output(isodoc, xml_name, outfilename, ext, ) rescue StandardError => e puts e. puts e.backtrace.join("\n") end end wrap_html(, file_extension, outfilename) end end |
#process_input(filename, options) ⇒ Object
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 |
# File 'lib/metanorma/compile.rb', line 127 def process_input(filename, ) case extname = File.extname(filename) when ".adoc" Util.log("[metanorma] Processing: AsciiDoc input.", :info) file = read_file(filename) [:asciimath] and file.sub!(/^(=[^\n]+\n)/, "\\1:mn-keep-asciimath:\n") dir = File.dirname(filename) dir != '.' and file.gsub!(/^include::/, "include::#{dir}/") [file, @processor.input_to_isodoc(file, filename, )] when ".xml" Util.log("[metanorma] Processing: Metanorma XML input.", :info) # TODO NN: this is a hack -- we should provide/bridge the # document attributes in Metanorma XML ["", read_file(filename)] else Util.log("[metanorma] Error: file extension #{extname} is not supported.", :error) nil end end |
#read_file(filename) ⇒ Object
149 150 151 |
# File 'lib/metanorma/compile.rb', line 149 def read_file(filename) File.read(filename, encoding: "utf-8").gsub("\r\n", "\n") end |
#relaton_export(isodoc, options) ⇒ Object
153 154 155 156 157 158 159 160 |
# File 'lib/metanorma/compile.rb', line 153 def relaton_export(isodoc, ) return unless [:relaton] xml = Nokogiri::XML(isodoc) bibdata = xml.at("//bibdata") || xml.at("//xmlns:bibdata") #docid = bibdata&.at("./xmlns:docidentifier")&.text || options[:filename] #outname = docid.sub(/^\s+/, "").sub(/\s+$/, "").gsub(/\s+/, "-") + ".xml" File.open([:relaton], "w:UTF-8") { |f| f.write bibdata.to_xml } end |
#require_libraries(options) ⇒ Object
31 32 33 34 35 36 37 |
# File 'lib/metanorma/compile.rb', line 31 def require_libraries() if [:require] [:require].each do |r| require r end end end |
#requirement_export(xml, dirname) ⇒ Object
211 212 213 214 215 216 217 218 219 220 |
# File 'lib/metanorma/compile.rb', line 211 def requirement_export(xml, dirname) xml.at(REQUIREMENT_XPATH) or return FileUtils.mkdir_p "#{dirname}/requirement" xml.xpath(REQUIREMENT_XPATH).each_with_index do |s, i| filename = s["filename"] || sprintf("%s-%04d.xml", s.name, i) File.open("#{dirname}/requirement/#{filename}", "w:UTF-8") do |f| f.write s end end end |
#sort_extensions_execution(ext) ⇒ Object
dependency ordering
223 224 225 226 227 228 229 230 231 |
# File 'lib/metanorma/compile.rb', line 223 def sort_extensions_execution(ext) case ext when :xml then 0 when :rxl then 1 when :presentation then 2 else 99 end end |
#sourcecode_export(xml, dirname) ⇒ Object
183 184 185 186 187 188 189 190 191 192 |
# File 'lib/metanorma/compile.rb', line 183 def sourcecode_export(xml, dirname) xml.at("//sourcecode | //xmlns:sourcecode") or return FileUtils.mkdir_p "#{dirname}/sourcecode" xml.xpath("//sourcecode | //xmlns:sourcecode").each_with_index do |s, i| filename = s["filename"] || sprintf("sourcecode-%04d.txt", i) File.open("#{dirname}/sourcecode/#{filename}", "w:UTF-8") do |f| f.write clean_sourcecode(s.dup) end end end |
#validate_format(options) ⇒ Object
99 100 101 102 103 104 105 |
# File 'lib/metanorma/compile.rb', line 99 def validate_format() unless [:format] == :asciidoc Util.log("[metanorma] Error: Only source file format currently supported is 'asciidoc'.", :error) return false end true end |
#validate_type(options) ⇒ Object
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/metanorma/compile.rb', line 65 def validate_type() unless [:type] Util.log("[metanorma] Error: Please specify a standard type: #{@registry.supported_backends}.", :error) return nil end stdtype = [:type].to_sym = "metanorma-#{stdtype}" unless @registry.supported_backends.include? stdtype Util.log("[metanorma] Info: Loading `#{}` gem for standard type `#{stdtype}`.", :info) end begin require "metanorma-#{stdtype}" Util.log("[metanorma] Info: gem `#{}` loaded.", :info) rescue Gem::ConflictError Util.log("[metanorma] Error: Couldn't resolve dependencies for `metanorma-#{stdtype}`, Please add it to your Gemfile and run bundle install first", :error) return false rescue LoadError Util.log("[metanorma] Error: loading gem `#{}` failed. Exiting.", :error) return false end unless @registry.supported_backends.include? stdtype Util.log("[metanorma] Error: The `#{}` gem still doesn't support `#{stdtype}`. Exiting.", :error) return false end true end |
#wrap_html(options, file_extension, outfilename) ⇒ Object
233 234 235 236 237 238 239 240 |
# File 'lib/metanorma/compile.rb', line 233 def wrap_html(, file_extension, outfilename) if [:wrapper] and /html$/.match file_extension outfilename = outfilename.sub(/\.html$/, "") FileUtils.mkdir_p outfilename FileUtils.mv "#{outfilename}.html", outfilename FileUtils.mv "#{outfilename}_images", outfilename, force: true end end |
#xml_options_extract(file) ⇒ Object
39 40 41 42 43 44 45 46 47 |
# File 'lib/metanorma/compile.rb', line 39 def (file) xml = Nokogiri::XML(file) if xml.root @registry..each do |k, v| return { type: k } if v == xml.root.name end end {} end |