Class: TTL2HTML::Template
- Inherits:
-
Object
- Object
- TTL2HTML::Template
- Includes:
- ActionView::Helpers::NumberHelper, ERB::Util, I18n::Base, 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, type = {}) ⇒ Object
- #format_property(property, labels = {}, subject = nil) ⇒ Object
- #format_triples(triples, type = {}) ⇒ Object
- #format_version_info(version) ⇒ Object
- #get_language_literal(object) ⇒ Object
- #get_subtitle(data, default_title = nil) ⇒ Object
- #get_title(data, default_title = "no title") ⇒ Object
- #html_title(param) ⇒ Object
-
#initialize(template, param = {}) ⇒ Template
constructor
A new instance of Template.
- #output_to(file, param = {}) ⇒ Object
- #relative_path(dest) ⇒ Object
- #relative_path_uri(dest_uri, base_uri = ) ⇒ Object
- #shorten_title(title, length = 140) ⇒ Object
- #to_html(param) ⇒ Object
- #to_html_raw(template, param) ⇒ Object
Methods included from Util
Constructor Details
#initialize(template, param = {}) ⇒ Template
Returns a new instance of Template.
16 17 18 19 20 21 22 23 24 |
# File 'lib/ttl2html/template.rb', line 16 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.("locales") + "/*.yml"] I18n.locale = @param[:locale] if @param[:locale] end |
Instance Attribute Details
#param ⇒ Object (readonly)
Returns the value of attribute param.
12 13 14 |
# File 'lib/ttl2html/template.rb', line 12 def param @param end |
Instance Method Details
#expand_shape(data, uri, prefixes = {}) ⇒ Object
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 119 120 |
# File 'lib/ttl2html/template.rb', line 60 def (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 << (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: 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, class: data[property]["http://www.w3.org/ns/shacl#class"] ? data[property]["http://www.w3.org/ns/shacl#class"].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
48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/ttl2html/template.rb', line 48 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
210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 |
# File 'lib/ttl2html/template.rb', line 210 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] result = "<a href=\"#{rel_path}\">#{get_title(param[:data_global][object]) or ERB::Util.html_escape(object)}</a>" subtitle = get_subtitle(param[:data_global][object]) if subtitle result += " <small>#{subtitle}</small>" else result end else "<a href=\"#{rel_path}\">#{ERB::Util.html_escape 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 ERB::Util.html_escape object end end |
#format_property(property, labels = {}, subject = nil) ⇒ Object
199 200 201 202 203 204 205 206 207 208 209 |
# File 'lib/ttl2html/template.rb', line 199 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
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 280 281 |
# File 'lib/ttl2html/template.rb', line 234 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
282 283 284 285 |
# File 'lib/ttl2html/template.rb', line 282 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
185 186 187 188 189 190 191 192 193 194 195 196 197 198 |
# File 'lib/ttl2html/template.rb', line 185 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_subtitle(data, default_title = nil) ⇒ Object
177 178 179 180 181 182 183 184 |
# File 'lib/ttl2html/template.rb', line 177 def get_subtitle(data, default_title = nil) resolve_title(data, default: default_title, use_default: false, property_key: :subtitle_property, perclass_key: :subtitle_property_perclass ) end |
#get_title(data, default_title = "no title") ⇒ Object
169 170 171 172 173 174 175 176 |
# File 'lib/ttl2html/template.rb', line 169 def get_title(data, default_title = "no title") resolve_title(data, default: default_title, use_default: true, property_key: :title_property, perclass_key: :title_property_perclass ) end |
#html_title(param) ⇒ Object
151 152 153 154 155 156 157 158 159 160 |
# File 'lib/ttl2html/template.rb', line 151 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
25 26 27 28 29 30 31 32 33 |
# File 'lib/ttl2html/template.rb', line 25 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
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 |
# File 'lib/ttl2html/template.rb', line 124 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
142 143 144 145 146 147 148 149 150 |
# File 'lib/ttl2html/template.rb', line 142 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
161 162 163 164 165 166 167 168 |
# File 'lib/ttl2html/template.rb', line 161 def shorten_title(title, length = 140) if title.to_s.length > length short_title = Nokogiri::HTML::DocumentFragment.parse(title.to_s[0..length]) short_title.to_html + "..." else ERB::Util.html_escape title end end |
#to_html(param) ⇒ Object
34 35 36 37 38 |
# File 'lib/ttl2html/template.rb', line 34 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
39 40 41 42 43 44 45 46 |
# File 'lib/ttl2html/template.rb', line 39 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 |