Sha256: ed91107f8774dd9511a67d6d00c97129b00153ac1f719e188f3962006aa89cad

Contents?: true

Size: 1.98 KB

Versions: 6

Compression:

Stored size: 1.98 KB

Contents

require "metanorma/processor"
require "tempfile"

module Metanorma
  module Ietf
    class Processor < Metanorma::Processor

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

      def output_formats
        {
          rxl: "rxl",
          xml: "xml",
          rfc: "rfc.xml",
          html: "html",
          txt: "txt"
        }
      end

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

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

      def extract_options(isodocxml)
        {}
      end

      # From mislav: https://stackoverflow.com/questions/2108727
      #        /which-in-ruby-checking-if-program-exists-in-path-from-ruby
      def which(cmd)
        exts = ENV['PATHEXT'] ? ENV['PATHEXT'].split(';') : ['']
        ENV['PATH'].split(File::PATH_SEPARATOR).each do |path|
          exts.each do |ext|
            exe = File.join(path, "#{cmd}#{ext}")
            return exe if File.executable?(exe) && !File.directory?(exe)
          end
        end
        nil
      end

      def output(isodoc_node, outname, format, options={})
        case format
        when :rfc
          IsoDoc::Ietf::RfcConvert.new(options).convert(outname.sub(/\.xml/, ""), isodoc_node)
          @done_rfc = true

        when :txt, :html
          rfcname = outname.sub(/\.(html|txt)$/, ".rfc.xml")
          output(isodoc_node, outname, :rfc, options) unless @done_rfc
          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
          system("xml2rfc --#{format} #{rfcname} -o #{outname}")
        else
          super
        end
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
metanorma-ietf-2.0.6 lib/metanorma/ietf/processor.rb
metanorma-ietf-2.0.5 lib/metanorma/ietf/processor.rb
metanorma-ietf-2.0.4 lib/metanorma/ietf/processor.rb
metanorma-ietf-2.0.3 lib/metanorma/ietf/processor.rb
metanorma-ietf-2.0.2 lib/metanorma/ietf/processor.rb
metanorma-ietf-2.0.1 lib/metanorma/ietf/processor.rb