Class: Metanorma::Ietf::Processor

Inherits:
Processor
  • Object
show all
Defined in:
lib/metanorma/ietf/processor.rb

Instance Method Summary collapse

Constructor Details

#initializeProcessor

Returns a new instance of Processor.



8
9
10
11
12
# File 'lib/metanorma/ietf/processor.rb', line 8

def initialize
  @short = :ietf
  @input_format = :asciidoc
  @asciidoctor_backend = :rfc2
end

Instance Method Details

#extract_options(isodocxml) ⇒ Object

TODO: we’re not yet using IsoDoc here



37
38
39
# File 'lib/metanorma/ietf/processor.rb', line 37

def extract_options(isodocxml)
  {}
end

#input_to_isodoc(file, filename) ⇒ Object



27
28
29
30
31
32
33
34
# File 'lib/metanorma/ietf/processor.rb', line 27

def input_to_isodoc(file, filename)
  # TODO: storing these two variables for xmlrfc2. Remove when we have IsoDoc
  @file = file
  @filename = filename

  # This is XML RFC v3 output in text
  Metanorma::Input::Asciidoc.new.process(file, filename, @asciidoctor_backend)
end

#output(isodoc_node, outname, format, options = {}) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/metanorma/ietf/processor.rb', line 54

def output(isodoc_node, outname, format, options={})
  case format
  when :xmlrfc2
    # TODO: we're using XML RFC v2 as the central format because our processor
    # is not updated to the new schema

    # xmlrfcdoc = Metanorma::Input::Asciidoc.new.process(@file, @filename, :rfc2)
    # File.open(outname, 'w') { |f| f << xmlrfcdoc }

    # TODO: When we have IsoDoc, we just translate IsoDoc to XML RFC v2
    File.open(outname, 'w') { |f| f << isodoc_node }
  when :xmlrfc3
    # TODO: When we have IsoDoc, we just translate IsoDoc to XML RFC v3
    xmlrfcdoc = Metanorma::Input::Asciidoc.new.process(@file, @filename, :rfc3)
    File.open(outname, 'w') { |f| f << xmlrfcdoc }

  when :txt, :html
    Tempfile.open(outname) do |f|
      f << isodoc_node

      unless which("xml2rfc")
        warn "[metanorma-ietf] Error: unable to generate #{format}, the command `xml2rfc` is not found in path."
        return
      end

      # In xml2rfc, --text and --html are used
      format = :text if format == :txt
      # puts "xml2rfc --#{format} #{f.path} -o #{outname}"
      system("xml2rfc --#{format} #{f.path} -o #{outname}")
    end
  end
end

#output_formatsObject



14
15
16
17
18
19
20
21
# File 'lib/metanorma/ietf/processor.rb', line 14

def output_formats
  {
    xmlrfc2: "xml",
    xmlrfc3: "v3.xml",
    html: "html",
    txt: "txt"
  }
end

#versionObject



23
24
25
# File 'lib/metanorma/ietf/processor.rb', line 23

def version
  "Metanorma::Ietf #{::Metanorma::Ietf::VERSION}"
end

#which(cmd) ⇒ Object

From mislav: stackoverflow.com/questions/2108727

/which-in-ruby-checking-if-program-exists-in-path-from-ruby


43
44
45
46
47
48
49
50
51
52
# File 'lib/metanorma/ietf/processor.rb', line 43

def which(cmd)
  exts = ENV['PATHEXT'] ? ENV['PATHEXT'].split(';') : ['']
  ENV['PATH'].split(File::PATH_SEPARATOR).each do |path|
    exts.each { |ext|
      exe = File.join(path, "#{cmd}#{ext}")
      return exe if File.executable?(exe) && !File.directory?(exe)
    }
  end
  nil
end