Class: TTL2HTML::Template

Inherits:
Object
  • Object
show all
Includes:
ActionView::Helpers::NumberHelper, ERB::Util, I18n::Base, Util
Defined in:
lib/ttl2html/template.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Util

#uri_mapping_to_path

Constructor Details

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

Returns a new instance of Template.



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

def initialize(template, param = {})
  @template = template
  @param = param.dup
  @template_path = [ File.join(Dir.pwd, "templates") ]
  @template_path << File.join(File.dirname(__FILE__), "..", "..", "templates")
  I18n.load_path << Dir[File.join(File.dirname(__FILE__), "..", "..", "locales") + "/*.yml"]
  I18n.load_path << Dir[File.expand_path("locales") + "/*.yml"]
  I18n.locale = @param[:locale] if @param[:locale]
end

Instance Attribute Details

#paramObject (readonly)

Returns the value of attribute param.



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

def param
  @param
end

Instance Method Details

#expand_shape(data, uri, prefixes = {}) ⇒ Object



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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/ttl2html/template.rb', line 59

def expand_shape(data, uri, prefixes = {})
  return nil if not data[uri]
  return nil if not data[uri]["http://www.w3.org/ns/shacl#property"]
  prefix_used = {}
  result = data[uri]["http://www.w3.org/ns/shacl#property"].sort_by do |e|
    e["http://www.w3.org/ns/shacl#order"]
  end.map do |property|
    path = data[property]["http://www.w3.org/ns/shacl#path"].first
    shorten_path = path.dup
    prefixes.each do |prefix, val|
      if path.index(val) == 0
        shorten_path = path.sub(/\A#{val}/, "#{prefix}:")
        prefix_used[prefix] = val
      end
    end
    repeatable = false
    if data[property]["http://www.w3.org/ns/shacl#maxCount"]
      max_count = data[property]["http://www.w3.org/ns/shacl#maxCount"].first.to_i
      if max_count > 1
        repeatable = true
      end
    else
      repeatable = true
    end
    nodes = nil
    if data[property]["http://www.w3.org/ns/shacl#node"]
      node = data[property]["http://www.w3.org/ns/shacl#node"].first
      if data[node]["http://www.w3.org/ns/shacl#or"]
        node_or = data[data[node]["http://www.w3.org/ns/shacl#or"].first]
        node_mode = :or
        nodes = []
        nodes << expand_shape(data, node_or["http://www.w3.org/1999/02/22-rdf-syntax-ns#first"].first, prefixes)
        rest = node_or["http://www.w3.org/1999/02/22-rdf-syntax-ns#rest"].first
        while data[rest] do
          nodes << expand_shape(data, data[rest]["http://www.w3.org/1999/02/22-rdf-syntax-ns#first"].first, prefixes)
          rest = data[rest]["http://www.w3.org/1999/02/22-rdf-syntax-ns#rest"].first
        end
      else
        nodes = expand_shape(data, node, prefixes)
      end
      #p nodes
    end
    {
      path: path,
      shorten_path: shorten_path,
      name: get_language_literal(data[property]["http://www.w3.org/ns/shacl#name"]),
      example: data[property]["http://www.w3.org/2004/02/skos/core#example"] ? data[property]["http://www.w3.org/2004/02/skos/core#example"].first : nil,
      description: get_language_literal(data[property]["http://www.w3.org/ns/shacl#description"]),
      required: data[property]["http://www.w3.org/ns/shacl#minCount"] ? data[property]["http://www.w3.org/ns/shacl#minCount"].first.to_i > 0 : false,
      repeatable: repeatable,
      nodeKind: data[property]["http://www.w3.org/ns/shacl#nodeKind"] ? data[property]["http://www.w3.org/ns/shacl#nodeKind"].first : nil,
      nodes: nodes,
      node_mode: node_mode,
      prefix: prefix_used,
    }
  end
  template = "shape-table.html.erb"
  tmpl = Template.new(template)
  tmpl.to_html_raw(template, { properties: result, prefix: prefix_used })
end

#find_template_path(fname) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
# File 'lib/ttl2html/template.rb', line 47

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, type = {}) ⇒ Object



214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
# File 'lib/ttl2html/template.rb', line 214

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

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



203
204
205
206
207
208
209
210
211
212
213
# File 'lib/ttl2html/template.rb', line 203

def format_property(property, labels = {}, subject = nil)
  subject = @param[:blank_subject] if not subject and @param[:blank_subject]
  subject_class = @param[:data_global][subject][RDF.type.to_s]&.first if subject
  if subject_class and @param[:labels_with_class][subject_class] and @param[:labels_with_class][subject_class][property]
    @param[:labels_with_class][subject_class][property]
  elsif labels and labels[property]
    labels[property]
  else
    property.split(/[\/\#]/).last.capitalize
  end
end

#format_triples(triples, type = {}) ⇒ Object



232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
# File 'lib/ttl2html/template.rb', line 232

def format_triples(triples, type = {})
  param_local = @param.dup.merge(data: triples)
  param_local[:type] = type
  if @param[:labels_with_class] and triples["http://www.w3.org/1999/02/22-rdf-syntax-ns#type"]
    @param[:labels_with_class].reverse_each do |k, v|
      triples["http://www.w3.org/1999/02/22-rdf-syntax-ns#type"].each do |entity_class|
        if entity_class == k
          v.each do |property, label_value|
            param_local[:labels] ||= {}
            param_local[:labels][property] = label_value
          end
        end
      end
    end
  end
  if @param[:orders_with_class] and triples["http://www.w3.org/1999/02/22-rdf-syntax-ns#type"]
    @param[:orders_with_class].reverse_each do |k, v|
      triples["http://www.w3.org/1999/02/22-rdf-syntax-ns#type"].each do |entity_class|
        if entity_class == k
          v.each do |property, order|
            param_local[:orders] ||= {}
            param_local[:orders][property] = order || Float::INFINITY
          end
        end
      end
    end
  end
  if type[:inverse] == true
    if type[:blank] == true
      #p param_local[:data]
      #p param_local[:data_inverse]
      #p param_local[:data_inverse].values.first.first
      param_local[:blank_subject] = param_local[:data].values.first.first
      param_local[:blank_triples] = {
        param_local[:data].keys.first => param_local[:data_global][param_local[:blank_subject]][param_local[:data].keys.first]
      }
      #p param_local[:blank_subject]
      #p param_local[:blank_triples]
      param_local[:type] = {}
      #pp param_local
      to_html_raw("triples-blank.html.erb", param_local)
    else
      to_html_raw("triples-inverse.html.erb", param_local)
    end
  else
    to_html_raw("triples.html.erb", param_local)
  end
end

#format_version_info(version) ⇒ Object



280
281
282
283
# File 'lib/ttl2html/template.rb', line 280

def format_version_info(version)
  param_local = @param.dup.merge(data: version)
  to_html_raw("version.html.erb", param_local)
end

#get_language_literal(object) ⇒ Object



189
190
191
192
193
194
195
196
197
198
199
200
201
202
# File 'lib/ttl2html/template.rb', line 189

def get_language_literal(object)
  if object.is_a? Array
    object_lang = object.select do |e|
      e.language? and e.language == I18n.locale
    end
    if not object_lang.empty?
      object_lang.first
    else
      object.first
    end
  else
    object
  end
end

#get_title(data, default_title = "no title") ⇒ Object



166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
# File 'lib/ttl2html/template.rb', line 166

def get_title(data, default_title = "no title")
  return default_title if data.nil?
  if @param[:title_property_perclass] and data["http://www.w3.org/1999/02/22-rdf-syntax-ns#type"]
    @param[:title_property_perclass].each do |klass, property|
      if data["http://www.w3.org/1999/02/22-rdf-syntax-ns#type"].include?(klass) and data[property]
        return shorten_title(get_language_literal(data[property]))
      end
    end
  end
  if @param[:title_property] and data[@param[:title_property]]
    return shorten_title(get_language_literal(data[@param[:title_property]]))
  end
  %w(
    http://www.w3.org/2000/01/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 shorten_title(get_language_literal(data[property])) if data[property]
  end
  default_title
end

#html_title(param) ⇒ Object



149
150
151
152
153
154
155
156
157
158
# File 'lib/ttl2html/template.rb', line 149

def html_title(param)
  titles = []
  if @template.start_with? "about.html"
    titles << t("about.title", title: param[:site_title])
  else
    titles << param[:title]
    titles << param[:site_title]
  end
  titles.compact.join(" - ")
end

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



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

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



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/ttl2html/template.rb', line 122

def relative_path(dest)
  path = nil
  dest_uri = RDF::IRI.parse(dest)
  if dest_uri.absolute?
    path = dest
  else
    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))
    if @param[:output_dir] and File.directory?(Pathname.new(@param[:output_dir]) + path)
      path = path.to_s + "/"
    elsif File.directory?(path)
      path = path.to_s + "/"
    end
  end
  #p [ :relative_path, path, dest, src ]
  path
end

#relative_path_uri(dest_uri, base_uri = ) ⇒ Object



140
141
142
143
144
145
146
147
148
# File 'lib/ttl2html/template.rb', line 140

def relative_path_uri(dest_uri, base_uri = @param[:base_uri])
  if dest_uri.start_with? base_uri
    dest = dest_uri.sub(base_uri, "")
    dest = uri_mapping_to_path(dest, @param, "")
    relative_path(dest)
  else
    dest_uri
  end
end

#shorten_title(title, length = 140) ⇒ Object



159
160
161
162
163
164
165
# File 'lib/ttl2html/template.rb', line 159

def shorten_title(title, length = 140)
  if title.to_s.length > length
    title.to_s[0..length] + "..."
  else
    title
  end
end

#to_html(param) ⇒ Object



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

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



38
39
40
41
42
43
44
45
# File 'lib/ttl2html/template.rb', line 38

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