Class: TTL2HTML::Template
- Inherits:
-
Object
- Object
- TTL2HTML::Template
- Includes:
- ERB::Util
- Defined in:
- lib/ttl2html/template.rb
Instance Attribute Summary collapse
-
#param ⇒ Object
readonly
Returns the value of attribute param.
Instance Method Summary collapse
- #expand_shape(data, uri, prefixes = {}) ⇒ Object
- #find_template_path(fname) ⇒ Object
- #format_object(object, data) ⇒ Object
- #format_property(property, labels = {}) ⇒ Object
- #format_triples(triples) ⇒ Object
- #get_language_literal(object) ⇒ Object
- #get_title(data, default_title = "no title") ⇒ Object
-
#initialize(template, param = {}) ⇒ Template
constructor
A new instance of Template.
- #output_to(file, param = {}) ⇒ Object
-
#relative_path(dest) ⇒ Object
helper method:.
- #relative_path_uri(dest_uri, base_uri) ⇒ Object
- #to_html(param) ⇒ Object
- #to_html_raw(template, param) ⇒ Object
Constructor Details
#initialize(template, param = {}) ⇒ Template
Returns a new instance of Template.
11 12 13 14 15 16 |
# File 'lib/ttl2html/template.rb', line 11 def initialize(template, param = {}) @template = template @param = param @template_path = [ Dir.pwd, File.join(Dir.pwd, "templates") ] @template_path << File.join(File.dirname(__FILE__), "..", "..", "templates") end |
Instance Attribute Details
#param ⇒ Object (readonly)
Returns the value of attribute param.
9 10 11 |
# File 'lib/ttl2html/template.rb', line 9 def param @param end |
Instance Method Details
#expand_shape(data, uri, prefixes = {}) ⇒ Object
52 53 54 55 56 57 58 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 |
# File 'lib/ttl2html/template.rb', line 52 def (data, uri, prefixes = {}) return nil if not data[uri] return nil if not data[uri]["http://www.w3.org/ns/shacl#property"] 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}:") 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 << (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 << (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 = (data, node, prefixes) end #p nodes end { path: path, shorten_path: shorten_path, name: data[property]["http://www.w3.org/ns/shacl#name"].first, 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: data[property]["http://www.w3.org/ns/shacl#description"] ? data[property]["http://www.w3.org/ns/shacl#description"].first : nil, 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, } end template = "shape-table.html.erb" tmpl = Template.new(template) tmpl.to_html_raw(template, {properties: result}) end |
#find_template_path(fname) ⇒ Object
40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/ttl2html/template.rb', line 40 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
157 158 159 160 161 162 163 164 165 166 167 168 169 170 |
# File 'lib/ttl2html/template.rb', line 157 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
150 151 152 153 154 155 156 |
# File 'lib/ttl2html/template.rb', line 150 def format_property(property, labels = {}) if labels and labels[property] labels[property] else property.split(/[\/\#]/).last.capitalize end end |
#format_triples(triples) ⇒ Object
171 172 173 174 |
# File 'lib/ttl2html/template.rb', line 171 def format_triples(triples) param_local = @param.merge(data: triples) to_html_raw("triples.html.erb", param_local) end |
#get_language_literal(object) ⇒ Object
141 142 143 144 145 146 147 148 149 |
# File 'lib/ttl2html/template.rb', line 141 def get_language_literal(object) if object.respond_to? :has_key? object.values.first elsif object.is_a? Array object.first else object end end |
#get_title(data, default_title = "no title") ⇒ Object
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 |
# File 'lib/ttl2html/template.rb', line 126 def get_title(data, default_title = "no title") if @param[:title_property] and data[@param[:title_property]] return 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 get_language_literal(data[property]) if data[property] end default_title end |
#output_to(file, param = {}) ⇒ Object
17 18 19 20 21 22 23 24 25 |
# File 'lib/ttl2html/template.rb', line 17 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:
111 112 113 114 115 116 117 |
# File 'lib/ttl2html/template.rb', line 111 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
118 119 120 121 122 123 124 125 |
# File 'lib/ttl2html/template.rb', line 118 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
26 27 28 29 30 |
# File 'lib/ttl2html/template.rb', line 26 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
31 32 33 34 35 36 37 38 |
# File 'lib/ttl2html/template.rb', line 31 def to_html_raw(template, param) @param.update(param) template = find_template_path(template) tmpl = open(template){|io| io.read } erb = ERB.new(tmpl, nil, "-") erb.filename = template erb.result(binding) end |