Class: Skos2Html
- Inherits:
-
Object
- Object
- Skos2Html
- Defined in:
- lib/skos2html.rb
Overview
Converts a SKOS RDF file to a bare-bones HTML file readabale for humans. Rendering a HTML file consists of three basic steps:
-
Create a new instance of this class and specify input and output file.
-
Call load_and_parse.
-
Call generate_html_document.
-
Call the write method.
Instance Attribute Summary collapse
-
#buffer ⇒ Object
The output buffer to which we write all HTML.
Instance Method Summary collapse
-
#concept_scheme_title ⇒ Object
Get the title of this vocabulary for use in the HTML title element.
-
#generate_concept(concept_uri) ⇒ Object
Generate the HTML for an individual concept.
- #generate_concepts ⇒ Object
- #generate_conceptscheme_info ⇒ Object
- #generate_html_body ⇒ Object
- #generate_html_document ⇒ Object
- #generate_html_head ⇒ Object
-
#initialize(infile = nil, outfile = "vocab.html") ⇒ Skos2Html
constructor
Initialize the class.
- #label_for(uri, lang) ⇒ Object
- #labels_for(uri) ⇒ Object
-
#load_and_parse ⇒ Object
Load the SKOS RDF file into a graph object.
-
#set_up_vocabs ⇒ Object
Load some basic vocabularies in order to retrieve labels etc.
- #string_for(obj, lang) ⇒ Object
-
#write ⇒ Object
Write the output to disk as an UTF-8 encoded file.
Constructor Details
#initialize(infile = nil, outfile = "vocab.html") ⇒ Skos2Html
Initialize the class.
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/skos2html.rb', line 23 def initialize(infile=nil, outfile="vocab.html") @log = defined?(Rails) ? Rails.logger : Logger.new(STDOUT) @log.info("init") @infile = infile @outfile = outfile @default_lang = :en @graph = nil @vocabs = nil set_up_vocabs @buffer = "" @builder = Builder::XmlMarkup.new(:target=>@buffer, :indent=>2) @title = "" end |
Instance Attribute Details
#buffer ⇒ Object
The output buffer to which we write all HTML.
17 18 19 |
# File 'lib/skos2html.rb', line 17 def buffer @buffer end |
Instance Method Details
#concept_scheme_title ⇒ Object
Get the title of this vocabulary for use in the HTML title element
301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 |
# File 'lib/skos2html.rb', line 301 def concept_scheme_title title_properties = ["http://purl.org/dc/terms/title", "http://www.w3.org/2000/01/rdf-schema#label", "http://www.w3.org/2004/02/skos/core#prefLabel"] title = "" conceptschemes = RDF::Query.execute(@graph) do pattern [:scheme, RDF.type, RDF::SKOS.ConceptScheme] end if conceptschemes.size == 1 scheme = conceptschemes[0] @log.info("Looking for title for " + scheme.scheme) scheme_info = RDF::Query.execute(@graph) do pattern [scheme.scheme, :predicate, :object] end scheme_info.each do |solution| case solution.predicate.to_s when *title_properties title = solution.object.value end end return title end end |
#generate_concept(concept_uri) ⇒ Object
Generate the HTML for an individual concept.
339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 |
# File 'lib/skos2html.rb', line 339 def generate_concept(concept_uri) concept_info = RDF::Query.execute(@graph) do pattern [concept_uri, :predicate, :object] end preflabels = [] altlabels = [] definition = "" concept_info.each do |solution| case solution.predicate when "http://www.w3.org/2004/02/skos/core#prefLabel" if solution.object.language? preflabels << {"lang" => solution.object.language.to_sym, "val" => solution.object.to_s} else preflabels << {"lang" => nil, "val" => solution.object.to_s} end when "http://www.w3.org/2004/02/skos/core#altLabel" if solution.object.language? altlabels << {"lang" => solution.object.language.to_sym, "val" => solution.object.to_s} else altlabels << {"lang" => nil, "val" => solution.object.to_s} end when "http://www.w3.org/2004/02/skos/core#definition" definition = string_for(solution.object, @default_lang) end end @builder.div(:id => concept_uri.fragment, :class => "concept") { |html| # preflabel in defaultlang html.h2(preflabels.detect {|label| label["lang"] == @default_lang}["val"]) html.dl { html.dt("Definition", :class => "definition") html.dd(definition, :class => "definition") if altlabels.size > 0 html.dt("Alternative labels", {:class => "altlabels"}) altlabels.each do |label| if label["lang"] if label["lang"] != @default_lang html.dd(label["val"] + "(#{label['lang']})", :lang => label["lang"], :class => "altlabel") else html.dd(label["val"], :class => "altlabel") end else html.dd(label["val"], :class => "altlabel") end end end html.dt("Identifier", :class => "identifier") html.dd(concept_uri, :class => "identifier") } } end |
#generate_concepts ⇒ Object
283 284 285 286 287 288 289 290 291 292 293 294 |
# File 'lib/skos2html.rb', line 283 def generate_concepts concepts = RDF::Query.execute(@graph) do pattern [:concept_uri, RDF.type, RDF::SKOS.Concept] end @log.info("Concept count: #{concepts.size}") concepts.each do |concept| generate_concept(concept.concept_uri) end end |
#generate_conceptscheme_info ⇒ Object
199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 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 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/skos2html.rb', line 199 def generate_conceptscheme_info conceptschemes = RDF::Query.execute(@graph) do pattern [:scheme, RDF.type, RDF::SKOS.ConceptScheme] end @log.info("Concept scheme count #{conceptschemes.size}") if conceptschemes.size == 1 scheme = conceptschemes[0] @log.info("Looping concept scheme " + scheme.scheme) scheme_info = RDF::Query.execute(@graph) do pattern [scheme.scheme, :predicate, :object] end # Title title = "" # Description description = "" # Creators creators = [] # Contributors contributors = [] # get values scheme_info.each do |solution| case solution.predicate when "http://purl.org/dc/terms/title" when "http://www.w3.org/2000/01/rdf-schema#label" title = string_for(solution.object, @default_lang) when "http://purl.org/dc/terms/description" description = string_for(solution.object, @default_lang) when "http://purl.org/dc/terms/contributor" contributors << string_for(solution.object, nil) when "http://purl.org/dc/terms/creator" creators << string_for(solution.object, nil) end end @builder.div(:class => "conceptscheme") { |html| html.h1(title) html.dl { |html| html.dt("Description") html.dd(description, "class" => "description") if contributors.size > 0 html.dt("Contributors") contributors.each do |item| html.dd(item, "class" => "contributor") end end if creators.size > 0 html.dt("Creators") creators.each do |item| html.dd(item, "class" => "creator") end end } } else @log.info("Concept scheme count wrong. Expected 1 was " + conceptschemes.size) end end |
#generate_html_body ⇒ Object
73 74 75 76 77 78 |
# File 'lib/skos2html.rb', line 73 def generate_html_body @builder.body { |body| generate_conceptscheme_info generate_concepts } end |
#generate_html_document ⇒ Object
63 64 65 66 67 68 69 70 |
# File 'lib/skos2html.rb', line 63 def generate_html_document @builder.declare! :DOCTYPE, :html @builder.html(:lang => @default_lang) { |html| generate_html_head generate_html_body } end |
#generate_html_head ⇒ Object
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 |
# File 'lib/skos2html.rb', line 81 def generate_html_head title = concept_scheme_title @builder.head { |head| head.(:charset => "UTF-8") head.title(title) head.(:name => "generator", :content => "skos2html") head.style(""" html { margin: auto; max-width: 800px; font-family: Helvetica, Arial, sans-serif; } dt { font-weight: bold; margin: 0.8em 0 0.2em 0; } dd { margin: 0; } dd.contributor { display:inline; } dd.contributor:after { content: ', '; } dd.contributor:last-child:after { content: ''; } dd.identifier { font-family: Consolas, 'Lucida Console', 'Courier New', sans-serif; } div.conceptscheme { border-bottom: 1px solid #aaa; margin: 1em 0 2em 0; } div.concept { margin: 0 0 2.5em 0; } """) } end |
#label_for(uri, lang) ⇒ Object
178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 |
# File 'lib/skos2html.rb', line 178 def label_for(uri, lang) @log.info("label_for #{uri} #{lang}") labels = labels_for(uri) @log.info("labels: #{labels.inspect}") if labels labels.each do |label| @log.info("label: #{label}") if label.language == lang return label.value end end end end |
#labels_for(uri) ⇒ Object
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 |
# File 'lib/skos2html.rb', line 131 def labels_for(uri) solutions = RDF::Query.execute(@vocabs) do pattern [RDF::URI.new(uri), RDF::RDFS.label, :label] end result = [] if solutions solutions.each do |solution| # adding RDF literals result << solution.label end end return result end |
#load_and_parse ⇒ Object
Load the SKOS RDF file into a graph object.
54 55 56 57 58 59 60 |
# File 'lib/skos2html.rb', line 54 def load_and_parse if @infile @graph = RDF::Graph.load(@infile) end end |
#set_up_vocabs ⇒ Object
Load some basic vocabularies in order to retrieve labels etc.
46 47 48 49 50 |
# File 'lib/skos2html.rb', line 46 def set_up_vocabs @vocabs = RDF::Graph.load(File.("skos.rdf", File.dirname(__FILE__))) @vocabs << RDF::Graph.load(File.("dcterms.rdf", File.dirname(__FILE__))) @vocabs << RDF::Graph.load(File.("rdf-schema.rdf", File.dirname(__FILE__))) end |
#string_for(obj, lang) ⇒ Object
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 |
# File 'lib/skos2html.rb', line 151 def string_for(obj, lang) # return a human readable representation of obj - for literals the literal # value in lang. For URI:s lookup something. case obj when String return obj when RDF::Literal if lang if obj.language == lang return obj.to_s else return "[Not available in #{lang}]" end else return obj.to_s end when RDF::URI # TODO: fetch it... return "[fetched label for #{obj.to_s}]" else @log.info("unknown class") end end |
#write ⇒ Object
Write the output to disk as an UTF-8 encoded file.
407 408 409 410 411 412 413 |
# File 'lib/skos2html.rb', line 407 def write # output to disk File.open(@outfile, 'w:UTF-8') { |file| file.write(@buffer) } end |