class Relaton::Cli::XmlToHtmlRenderer

Attributes

liquid_dir[R]
stylesheet[R]
template[R]

Public Class Methods

new(liquid_dir: nil, stylesheet: nil) click to toggle source
# File lib/relaton/cli/xml_to_html_renderer.rb, line 7
def initialize(liquid_dir: nil, stylesheet: nil)
  @liquid_dir = liquid_dir
  @stylesheet = read_file(stylesheet)
  init_liquid_template_and_filesystem
end
render(file, options) click to toggle source

Render HTML

This interface allow us to convert a Relaton XML to HTML using the specified liquid template and stylesheets. It also do some minor clean during this conversion.

@param file [String] Relaton XML @param options [Hash] @return [String] HTML

# File lib/relaton/cli/xml_to_html_renderer.rb, line 34
def self.render(file, options)
  new(**options).render(file)
end

Public Instance Methods

render(index_xml) click to toggle source

@param index_xml [String] Relaton XML @return [String] HTML

# File lib/relaton/cli/xml_to_html_renderer.rb, line 15
def render(index_xml)
  Liquid::Template
    .parse(template)
    .render(build_liquid_document(index_xml))
end
uri_for_extension(uri, extension) click to toggle source
# File lib/relaton/cli/xml_to_html_renderer.rb, line 21
def uri_for_extension(uri, extension)
  uri&.sub(/\.[^.]+$/, ".#{extension}")
end

Private Instance Methods

build_bibcollection(source) click to toggle source

@param source [String] Relaton XML @return [Relaton::Bibcollection]

# File lib/relaton/cli/xml_to_html_renderer.rb, line 113
def build_bibcollection(source)
  Relaton::Bibcollection.from_xml(Nokogiri::XML(source))
end
build_liquid_document(source) click to toggle source

rubocop:disable Metrics/MethodLength @param source [String] Relaton XML

# File lib/relaton/cli/xml_to_html_renderer.rb, line 48
def build_liquid_document(source)
  bibcollection = build_bibcollection(source)
  begin
    mnv = `metanorma -v`
  rescue Errno::ENOENT
    mnv = ""
  end
  hash_to_liquid(
    depth: 2,
    css: stylesheet,
    title: bibcollection.title,
    date: Date.today.to_s,
    metanorma_v: mnv.lines.first&.strip,
    author: bibcollection.author,
    documents: document_items(bibcollection)
  )
end
document_items(bibcollection) click to toggle source

@param bibcollection [Relaton::Bibcollection] @return [Array<Hash>]

# File lib/relaton/cli/xml_to_html_renderer.rb, line 119
def document_items(bibcollection)
  bibcollection.to_h["root"]["items"].map { |item| hash_to_liquid(item) }
end
empty2nil(value) click to toggle source

rubocop:enable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength, Metrics/PerceivedComplexity

# File lib/relaton/cli/xml_to_html_renderer.rb, line 107
def empty2nil(value)
  value unless value.is_a?(String) && value.empty? && !value.nil?
end
hash_to_liquid(hash) click to toggle source

TODO: This should be recursive, but it's not @param hash [Hash] @option hash [Integer] :dept @option hash [String] :css path to stylesheet file @option hash [String] :title @option hash [String] :author @option hash [Array<Hash>] :documents

@return [Hash]

# File lib/relaton/cli/xml_to_html_renderer.rb, line 84
def hash_to_liquid(hash)
  hash.map do |key, value|
    case key
    when "title"
      if value.is_a?(Array)
        title = value.detect { |t| t["type"] == "main" } || value.first
        v = title ? title["content"] : nil
      elsif value.is_a?(Hash) then v = value["content"]
      else v = value
      end
    when "docid"
      v = if value.is_a?(Array)
            value.detect { |did| did["id"] !~ /^(http|https):\/\// } ||
              value.first
          else value
          end
    else v = value
    end
    [key.to_s, empty2nil(v)]
  end.to_h
end
init_liquid_template_and_filesystem() click to toggle source
# File lib/relaton/cli/xml_to_html_renderer.rb, line 66
def init_liquid_template_and_filesystem
  file_system = Liquid::LocalFileSystem.new(liquid_dir)
  @template = read_file(file_system.full_path("index"))

  Liquid::Template.file_system = file_system
end
read_file(file) click to toggle source
# File lib/relaton/cli/xml_to_html_renderer.rb, line 42
def read_file(file)
  File.read(file, encoding: "utf-8")
end