lib/metanorma/compile.rb in metanorma-0.3.8 vs lib/metanorma/compile.rb in metanorma-0.3.9
- old
+ new
@@ -1,7 +1,8 @@
require "fileutils"
require "nokogiri"
+require "htmlentities"
module Metanorma
class Compile
def initialize
@registry = Metanorma::Registry.instance
@@ -13,10 +14,11 @@
require_libraries(options)
@processor = @registry.find_processor(options[:type].to_sym)
extensions = get_extensions(options) or return nil
(file, isodoc = process_input(filename, options)) or return nil
relaton_export(isodoc, options)
+ sourcecode_export(isodoc, options[:sourcecode])
process_extensions(extensions, file, isodoc, options)
end
def require_libraries(options)
if options[:require]
@@ -29,10 +31,11 @@
def options_extract(filename, options)
o = Metanorma::Input::Asciidoc.new.extract_metanorma_options(File.read(filename, encoding: "utf-8"))
options[:type] ||= o[:type]&.to_sym
dir = filename.sub(%r(/[^/]+$), "/")
options[:relaton] ||= "#{dir}/#{o[:relaton]}" if o[:relaton]
+ options[:sourcecode] ||= "#{dir}/#{o[:sourcecode]}" if o[:sourcecode]
options[:extension_keys] ||= o[:extensions]&.split(/,[ ]*/)&.map(&:to_sym)
options[:extension_keys] = nil if options[:extension_keys] == [:all]
options[:format] ||= :asciidoc
options[:filename] = filename
options
@@ -111,9 +114,27 @@
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(options[:relaton], "w:UTF-8") { |f| f.write bibdata.to_xml }
+ end
+
+ 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
+
+ def sourcecode_export(isodoc, dirname)
+ return unless dirname
+ xml = Nokogiri::XML(isodoc)
+ FileUtils.rm_rf dirname
+ FileUtils.mkdir_p dirname
+ xml.xpath("//sourcecode | //xmlns:sourcecode").each_with_index do |s, i|
+ File.open("#{dirname}/#{i}", "w:UTF-8") { |f| f.write clean_sourcecode(s.dup) }
+ end
end
def process_extensions(extensions, file, isodoc, options)
extensions.each do |ext|
isodoc_options = @processor.extract_options(file)