#!/usr/bin/env ruby #!/usr/bin/env ruby # encoding: UTF-8 # resolve bin path, ignoring symlinks require "pathname" bin_file = Pathname.new(__FILE__).realpath # add self to libpath $:.unshift File.expand_path("../../lib", bin_file) # Fixes https://github.com/rubygems/rubygems/issues/1420 require "rubygems/specification" class Gem::Specification def this; self; end end require 'bundler/setup' require 'stepmod/utils/stepmod_definition_converter' require 'stepmod/utils/bibdata' require 'stepmod/utils/concept' ReverseAdoc.config.unknown_tags = :bypass parsed_terms = [] parsed_bibliography = [] encountered_terms = {} stepmod_dir = ARGV.first || Dir.pwd def log message puts "[stepmod-utils] #{message}" end log "STEPmod directory set to #{stepmod_dir}. Detecting paths..." files = %w( resource.xml application_protocol.xml business_object_model.xml module.xml ).inject([]) do |acc, t| acc << Dir["#{stepmod_dir}/**/#{t}"] end.flatten.sort.uniq files.each do |file_path| log "Processing XML file #{file_path}" current_document = Nokogiri::XML(File.read(file_path)).root bibdata = nil begin bibdata = Stepmod::Utils::Bibdata.new(document: current_document) rescue log "WARNING: Unknown file #{file_path}, skipped" next end # TODO: we may want a command line option to override this in the future unless %w(IS DIS TS).include? bibdata.doctype log "WARNING: skipping #{bibdata.docid} as it is not IS, DIS or TS" next end if bibdata.part.to_s.empty? log "FATAL: missing `part` attribute: #{file_path}" end # read definitions current_document.xpath('//definition').each.with_index(1) do |definition, index| term_id = definition['id'] unless term_id.nil? if encountered_terms[term_id] log "FATAL: Duplicated term with id: #{term_id}, #{file_path}" end encountered_terms[term_id] = true end concept = Stepmod::Utils::Concept.parse( definition, reference_anchor: bibdata.anchor, # Assume that definition is located in clause 3 of the ISO document # in order. We really don't have a good reference here. reference_clause: "3.#{index}" ) parsed_terms << concept parsed_bibliography << bibdata end end parsed_bibliography.uniq! File.open('031-generated-terms.adoc', 'w') { |file| file.puts(parsed_terms.map(&:to_mn_adoc).join("\n")) } log "written to: 031-generated-terms.adoc" File.open('991-generated-bibliography.adoc', 'w') { |file| file.puts(parsed_bibliography.map(&:to_mn_adoc).join("\n")) } log "written to: 991-generated-bibliography.adoc"