Class: TTL2HTML::Template
- Inherits:
-
Object
- Object
- TTL2HTML::Template
- Includes:
- ActionView::Helpers::NumberHelper, ERB::Util, I18n::Base, Util
- Defined in:
- lib/ttl2html/template.rb
Instance Method Summary collapse
- #expand_shape(data, uri, prefixes = {}, depth = 0) ⇒ Object
- #find_template_path(fname) ⇒ Object
- #format_object(object, param) ⇒ Object
- #format_property(property, param, subject = nil) ⇒ Object
- #format_triples(triples, param, 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(src, dest) ⇒ Object
- #relative_path_uri(src, 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
#_uri_mapping_to_path, #make_mapping_uris_cache, #safe_output_path, #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.("locales") + "/*.yml"] I18n.locale = @param[:locale].to_sym if @param[:locale] end |
Instance Method Details
#expand_shape(data, uri, prefixes = {}, depth = 0) ⇒ 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 121 122 123 124 125 126 127 128 129 130 131 132 |
# File 'lib/ttl2html/template.rb', line 60 def (data, uri, prefixes = {}, depth = 0) return nil if not data[uri] return nil if not data[uri]["http://www.w3.org/ns/shacl#property"] return nil if depth > 10 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, depth + 1) 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, depth + 1) rest = data[rest]["http://www.w3.org/1999/02/22-rdf-syntax-ns#rest"].first end else nodes = (data, node, prefixes, depth + 1) end #p nodes end example = data[property]["http://www.w3.org/2004/02/skos/core#example"].first if data[property]["http://www.w3.org/2004/02/skos/core#example"] if example.respond_to?(:datatype) and example.datatype? datatype = example.datatype.pname or example.datatype.to_s example = example.to_s + "<span class=\"datatype\">^^" + example.datatype.pname + "</span>" end descriptions = [ get_language_literal(data[property]["http://www.w3.org/ns/shacl#description"]) ] if data[property]["http://www.w3.org/ns/shacl#datatype"] datatypes = data[property]["http://www.w3.org/ns/shacl#datatype"].map do |datatype| datatype.to_s.sub(%r{\Ahttp://www\.w3\.org/2001/XMLSchema#}, 'xsd:') end.join(", ") descriptions << t('shape-table.datatype') + datatypes end { path: path, shorten_path: shorten_path, name: get_language_literal(data[property]["http://www.w3.org/ns/shacl#name"]), example: example, description: descriptions.compact.join("<br>"), 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" 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.file? file end nil end |
#format_object(object, param) ⇒ Object
228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 |
# File 'lib/ttl2html/template.rb', line 228 def format_object(object, param) type = param[:type] || {} data = param[:data] || {} if /\Ahttps?:\/\// =~ object.to_s #p [:format_object_uri, param[:output_file], object] rel_path = relative_path_uri(param[:output_file], object) 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], param, inverse: true, blank: true) else format_triples(param[:data_global][object], param, blank: true) end else ERB::Util.html_escape object end end |
#format_property(property, param, subject = nil) ⇒ Object
216 217 218 219 220 221 222 223 224 225 226 227 |
# File 'lib/ttl2html/template.rb', line 216 def format_property(property, param, subject = nil) param = @param.merge(param) 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 param[:labels] and param[:labels][property] param[:labels][property] else property.split(/[\/\#]/).last.capitalize end end |
#format_triples(triples, param, type = {}) ⇒ Object
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 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 |
# File 'lib/ttl2html/template.rb', line 255 def format_triples(triples, param, type = {}) param_local = @param.dup.merge(param) param_local = param_local.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
304 305 306 307 |
# File 'lib/ttl2html/template.rb', line 304 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
202 203 204 205 206 207 208 209 210 211 212 213 214 215 |
# File 'lib/ttl2html/template.rb', line 202 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
194 195 196 197 198 199 200 201 |
# File 'lib/ttl2html/template.rb', line 194 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
186 187 188 189 190 191 192 193 |
# File 'lib/ttl2html/template.rb', line 186 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
168 169 170 171 172 173 174 175 176 177 |
# File 'lib/ttl2html/template.rb', line 168 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 |
# File 'lib/ttl2html/template.rb', line 24 def output_to(file, param = {}) param = @param.merge(param) param[:output_file] = file FileUtils.mkdir_p(File.dirname(file)) File.open(file, "w") do |io| io.print to_html(param) end end |
#relative_path(src, dest) ⇒ Object
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 |
# File 'lib/ttl2html/template.rb', line 136 def relative_path(src, dest) path = nil dest_uri = RDF::IRI.parse(dest) if dest_uri.absolute? path = dest else dest.sub!(/^\/+/, "") #p [:relative_path, src, dest] if @param[:output_dir] src = Pathname.new(src).relative_path_from(Pathname.new(@param[:output_dir])) else src = Pathname.new(File.(src)).relative_path_from(Pathname.new(File.("."))) end path = Pathname(dest).relative_path_from(Pathname(File.dirname src)) if @param[:output_dir] and File.directory?(File.join(@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(src, dest_uri, base_uri = ) ⇒ Object
159 160 161 162 163 164 165 166 167 |
# File 'lib/ttl2html/template.rb', line 159 def relative_path_uri(src, 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(src, dest) else dest_uri end end |
#shorten_title(title, length = 140) ⇒ Object
178 179 180 181 182 183 184 185 |
# File 'lib/ttl2html/template.rb', line 178 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
32 33 34 35 36 |
# File 'lib/ttl2html/template.rb', line 32 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
37 38 39 40 41 42 43 44 45 46 |
# File 'lib/ttl2html/template.rb', line 37 def to_html_raw(template, param) param = @param.merge(param) template = find_template_path(template) tmpl = File.open(template) { |io| io.read } erb = ERB.new(tmpl, trim_mode: "-") erb.filename = template I18n.with_locale(@param[:locale] || I18n.default_locale) do erb.result(binding) end end |