Class: Relaton::Cli::XmlToHtmlRenderer

Inherits:
Object
  • Object
show all
Defined in:
lib/relaton/cli/xml_to_html_renderer.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ XmlToHtmlRenderer

Returns a new instance of XmlToHtmlRenderer.



8
9
10
11
12
13
14
15
16
# File 'lib/relaton/cli/xml_to_html_renderer.rb', line 8

def initialize(options)
  @liquid_dir = options[:liquid_dir]
  @stylesheet = File.read(options[:stylesheet], encoding: "utf-8")

  puts "HTML html_template_dir #{@liquid_dir}"
  @file_system = Liquid::LocalFileSystem.new(@liquid_dir)
  @template = File.read(@file_system.full_path("index"), encoding: "utf-8")
  Liquid::Template.file_system = @file_system
end

Class Method Details

.render(file, options) ⇒ Object



53
54
55
# File 'lib/relaton/cli/xml_to_html_renderer.rb', line 53

def self.render(file, options)
  new(options).render(file)
end

Instance Method Details

#empty2nil(v) ⇒ Object



25
26
27
28
# File 'lib/relaton/cli/xml_to_html_renderer.rb', line 25

def empty2nil(v)
  return nil if !v.nil? && v.is_a?(String) && v.empty?
  v
end

#hash_to_liquid(hash) ⇒ Object

TODO: This should be recursive, but it’s not



31
32
33
# File 'lib/relaton/cli/xml_to_html_renderer.rb', line 31

def hash_to_liquid(hash)
  hash.map { |k, v| [k.to_s, empty2nil(v)] }.to_h
end

#ns(xpath) ⇒ Object



18
19
20
21
22
23
# File 'lib/relaton/cli/xml_to_html_renderer.rb', line 18

def ns(xpath)
  xpath.gsub(%r{/([a-zA-z])}, "/xmlns:\\1").
    gsub(%r{::([a-zA-z])}, "::xmlns:\\1").
    gsub(%r{\[([a-zA-z][a-z0-9A-Z@/]* ?=)}, "[xmlns:\\1").
    gsub(%r{\[([a-zA-z][a-z0-9A-Z@/]*\])}, "[xmlns:\\1")
end

#render(index_xml) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/relaton/cli/xml_to_html_renderer.rb', line 35

def render(index_xml)
  source = Nokogiri::XML(index_xml)
  bibcollection = ::Relaton::Bibcollection.from_xml(source)
  locals = {
    css: @stylesheet,
    title: bibcollection.title,
    author: bibcollection.author,
    documents: bibcollection.to_h["root"]["items"].map { |i| hash_to_liquid(i) },
    depth: 2
  }
  Liquid::Template.parse(@template).render(hash_to_liquid(locals))
end

#uri_for_extension(uri, extension) ⇒ Object



48
49
50
51
# File 'lib/relaton/cli/xml_to_html_renderer.rb', line 48

def uri_for_extension(uri, extension)
  return nil if uri.nil?
  uri.sub(/\.[^.]+$/, ".#{extension.to_s}")
end