Class: TTL2HTML::Template

Inherits:
Object
  • Object
show all
Includes:
ERB::Util
Defined in:
lib/ttl2html/template.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(template, param = {}) ⇒ Template

Returns a new instance of Template.



11
12
13
14
15
# File 'lib/ttl2html/template.rb', line 11

def initialize(template, param = {})
  @template = template
  @param = param
  @template_path = [ Dir.pwd, File.join(Dir.pwd, "templates") ]
end

Instance Attribute Details

#paramObject (readonly)

Returns the value of attribute param.



9
10
11
# File 'lib/ttl2html/template.rb', line 9

def param
  @param
end

Instance Method Details

#find_template_path(fname) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
# File 'lib/ttl2html/template.rb', line 39

def find_template_path(fname)
  if @param[:template_dir] and Dir.exist?(@param[:template_dir])
    @template_path.unshift(@param[:template_dir])
    @template_path.uniq!
  end
  @template_path.each do |dir|
    file = File.join(dir, fname)
    return file if File.exist? file
  end
  return nil
end

#format_object(object, data) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/ttl2html/template.rb', line 96

def format_object(object, data)
  if object =~ /\Ahttps?:\/\//
    rel_path = relative_path_uri(object, param[:base_uri])
    if data[object]
      "<a href=\"#{rel_path}\">#{get_title(param[:data_global][object]) or object}</a>"
    else
      "<a href=\"#{rel_path}\">#{object}</a>"
    end
  elsif object =~ /\A_:/ and param[:data_global][object]
    format_triples(param[:data_global][object])
  else
    object
  end
end

#format_property(property, labels = {}) ⇒ Object



89
90
91
92
93
94
95
# File 'lib/ttl2html/template.rb', line 89

def format_property(property, labels = {})
  if labels and labels[property]
    labels[property]
  else
    property.split(/[\/\#]/).last.capitalize
  end
end

#format_triples(triples) ⇒ Object



110
111
112
113
# File 'lib/ttl2html/template.rb', line 110

def format_triples(triples)
  param_local = @param.merge(data: triples)
  to_html_raw("templates/triples.html.erb", param_local)
end

#get_language_literal(object) ⇒ Object



82
83
84
85
86
87
88
# File 'lib/ttl2html/template.rb', line 82

def get_language_literal(object)
  if object.respond_to? :has_key?
    object.values.first
  else
    object
  end
end

#get_title(data) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/ttl2html/template.rb', line 67

def get_title(data)
  if @param[:title_property] and data[@param[:title_property]]
    return get_language_literal(data[@param[:title_property]])
  end
  %w(
    https://www.w3.org/TR/rdf-schema/#label
    http://purl.org/dc/terms/title
    http://purl.org/dc/elements/1.1/title
    http://schema.org/name
    http://www.w3.org/2004/02/skos/core#prefLabel
  ).each do |property|
    return get_language_literal(data[property]) if data[property]
  end
  "no title"
end

#output_to(file, param = {}) ⇒ Object



16
17
18
19
20
21
22
23
24
# File 'lib/ttl2html/template.rb', line 16

def output_to(file, param = {})
  @param.update(param)
  @param[:output_file] = file
  dir = File.dirname(file)
  FileUtils.mkdir_p(dir) if not File.exist?(dir)
  open(file, "w") do |io|
    io.print to_html(@param)
  end
end

#relative_path(dest) ⇒ Object

helper method:



52
53
54
55
56
57
58
# File 'lib/ttl2html/template.rb', line 52

def relative_path(dest)
  src = @param[:output_file]
  src = Pathname.new(src).relative_path_from(Pathname.new(@param[:output_dir])) if @param[:output_dir]
  path = Pathname(dest).relative_path_from(Pathname(File.dirname src))
  path = path.to_s + "/" if File.directory? path
  path
end

#relative_path_uri(dest_uri, base_uri) ⇒ Object



59
60
61
62
63
64
65
66
# File 'lib/ttl2html/template.rb', line 59

def relative_path_uri(dest_uri, base_uri)
  if dest_uri.start_with? base_uri
    dest = dest_uri.sub(base_uri, "")
    relative_path(dest)
  else
    dest_uri
  end
end

#to_html(param) ⇒ Object



25
26
27
28
29
# File 'lib/ttl2html/template.rb', line 25

def to_html(param)
  param[:content] = to_html_raw(@template, param)
  layout_fname = "layout.html.erb"
  to_html_raw(layout_fname, param)
end

#to_html_raw(template, param) ⇒ Object



30
31
32
33
34
35
36
37
# File 'lib/ttl2html/template.rb', line 30

def to_html_raw(template, param)
  @param.update(param)
  template = find_template_path(template)
  tmpl = open(template){|io| io.read }
  erb = ERB.new(tmpl, $SAFE, "-")
  erb.filename = template
  erb.result(binding)
end