ttl2html 1.1.1 → 1.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 6bd052e74baedc74a4f4a5d3f78770d00f281dea52371c9b795fd48fc98c08c4
4
- data.tar.gz: 320a243b01882fed22cda54f1403f420b795f2bc8a3a0bd68f8648003a827382
3
+ metadata.gz: b9d57dc99fea0827d317a9dc3597cae9c69d1ac38dbcd66000327f1b913dea03
4
+ data.tar.gz: d30441245cd56e6322fe3ab0b27351a83900b6c6826b3c5a6d611bffa8245d3e
5
5
  SHA512:
6
- metadata.gz: a6470f1e2fe766e98623869b1a0c08adba40e6bc8444d545d95fe42f9593ab2b805269e093a2efc69992acf6bf78f95468580ad42a1a21d7238a9909de22a67c
7
- data.tar.gz: ed809702cdcf07a74b8dbe0db7552b663fb2a688dda840e094f45ca843dc8b9bfb3bf5778b416a4e2ad72a700609c305edfb68f1c993afbe9ecd3d719eef13dc
6
+ metadata.gz: 6c5068b43cc54072f3958f46b5192c5fe118d5942c0b53d4f2fc35f8e35560a6768a2905d1cd946efc8462faa51b2d2ea4d844f8f73f982e18fb73ea5a812ee5
7
+ data.tar.gz: 6dc8f656583fc5d8a96baab454b1ea47e446e5694c55f457b3e236e91945595d8b2838d7a7b628d918d7a9c85bb138e081e8919453edce96228572a93dd617eb
@@ -12,7 +12,7 @@ module TTL2HTML
12
12
  include I18n::Base
13
13
  def initialize(template, param = {})
14
14
  @template = template
15
- @param = param
15
+ @param = param.dup
16
16
  @template_path = [ Dir.pwd, File.join(Dir.pwd, "templates") ]
17
17
  @template_path << File.join(File.dirname(__FILE__), "..", "..", "templates")
18
18
  I18n.load_path << Dir[File.join(File.dirname(__FILE__), "..", "..", "locales") + "/*.yml"]
@@ -113,6 +113,33 @@ module TTL2HTML
113
113
  end
114
114
 
115
115
  # helper method:
116
+ def uri_mapping_to_path(uri, suffix = ".html")
117
+ path = nil
118
+ if @param[:uri_mappings]
119
+ @param[:uri_mappings].each do |mapping|
120
+ local_file = uri.sub(@param[:base_uri], "")
121
+ if mapping["regexp"] =~ local_file
122
+ path = local_file.sub(mapping["regexp"], mapping["path"])
123
+ end
124
+ end
125
+ end
126
+ if path.nil?
127
+ if suffix == ".html"
128
+ if @param[:data_global] and @param[:data_global].keys.find{|e| e.start_with?(uri + "/") }
129
+ path = uri + "/index"
130
+ elsif uri.end_with?("/")
131
+ path = uri + "index"
132
+ else
133
+ path = uri
134
+ end
135
+ else
136
+ path = uri
137
+ end
138
+ end
139
+ path = path.sub(@param[:base_uri], "") if @param[:base_uri]
140
+ path << suffix
141
+ path
142
+ end
116
143
  def relative_path(dest)
117
144
  src = @param[:output_file]
118
145
  src = Pathname.new(src).relative_path_from(Pathname.new(@param[:output_dir])) if @param[:output_dir]
@@ -123,6 +150,7 @@ module TTL2HTML
123
150
  def relative_path_uri(dest_uri, base_uri)
124
151
  if dest_uri.start_with? base_uri
125
152
  dest = dest_uri.sub(base_uri, "")
153
+ dest = uri_mapping_to_path(dest, "")
126
154
  relative_path(dest)
127
155
  else
128
156
  dest_uri
@@ -173,7 +201,7 @@ module TTL2HTML
173
201
  def format_object(object, data)
174
202
  if object =~ /\Ahttps?:\/\//
175
203
  rel_path = relative_path_uri(object, param[:base_uri])
176
- if data[object]
204
+ if param[:data_global][object]
177
205
  "<a href=\"#{rel_path}\">#{get_title(param[:data_global][object]) or object}</a>"
178
206
  else
179
207
  "<a href=\"#{rel_path}\">#{object}</a>"
@@ -1 +1 @@
1
- TTL2HTML::VERSION = "1.1.1"
1
+ TTL2HTML::VERSION = "1.2.0"
data/lib/ttl2html.rb CHANGED
@@ -11,7 +11,6 @@ require "ttl2html/template"
11
11
  module TTL2HTML
12
12
  class App
13
13
  def initialize(config = "config.yml")
14
- @template = {}
15
14
  @config = load_config(config)
16
15
  if not @config[:base_uri]
17
16
  raise "load_config: base_uri not found"
@@ -67,50 +66,41 @@ module TTL2HTML
67
66
  def format_turtle(subject, depth = 1)
68
67
  turtle = RDF::Turtle::Writer.new
69
68
  result = ""
70
- if subject.iri?
71
- result << "<#{subject}>\n#{" "*depth}"
72
- else
69
+ if subject =~ /^_:/
73
70
  result << "[\n#{" "*depth}"
71
+ else
72
+ result << "<#{subject}>\n#{" "*depth}"
74
73
  end
75
- result << @graph.query([subject, nil, nil]).predicates.sort.map do |predicate|
74
+ result << @data[subject.to_s].keys.sort.map do |predicate|
76
75
  str = "<#{predicate}> "
77
- str << @graph.query([subject, predicate, nil]).objects.sort_by do |object|
78
- if object.resource? and not object.iri? # blank node:
79
- @graph.query([object, nil, nil]).statements.sort_by{|e|
80
- [ e.predicate, e.object ]
81
- }.map{|e|
82
- [ e.predicate, e.object ]
83
- }
84
- else
85
- object
86
- end
87
- end.map do |object|
88
- if object.resource? and not object.iri? # blank node:
76
+ str << @data[subject.to_s][predicate].sort.map do |object|
77
+ if object =~ /^_:/ # blank node:
89
78
  format_turtle(object, depth + 1)
79
+ elsif object =~ RDF::URI::IRI
80
+ turtle.format_uri(RDF::URI.new object)
81
+ elsif object.respond_to?(:first) and object.first.kind_of?(Symbol)
82
+ turtle.format_literal(RDF::Literal.new(object[1], language: object[0]))
90
83
  else
91
- case object
92
- when RDF::URI
93
- turtle.format_uri(object)
94
- else
95
- turtle.format_literal(object)
96
- end
84
+ turtle.format_literal(object)
97
85
  end
98
86
  end.join(", ")
99
87
  str
100
88
  end.join(";\n#{" "*depth}")
101
- result << " ." if subject.iri?
89
+ result << " ." if not subject =~ /^_:/
102
90
  result << "\n"
103
- result << "#{" "*(depth-1)}]" if not subject.iri?
91
+ result << "#{" "*(depth-1)}]" if subject =~ /^_:/
104
92
  result
105
93
  end
106
94
  def format_turtle_inverse(object)
107
- turtle = RDF::Turtle::Writer.new
108
95
  result = ""
109
- @graph.query([nil, nil, object]).statements.sort_by do |e|
110
- [ e.subject, e.predicate, object ]
111
- end.map do |e|
112
- next if e.subject.node?
113
- result << "<#{e.subject}> <#{e.predicate}> <#{object}>.\n"
96
+ return result if not object.start_with? @config[:base_uri]
97
+ return result if not @data_inverse.has_key? object
98
+ turtle = RDF::Turtle::Writer.new
99
+ @data_inverse[object].keys.sort.each do |predicate|
100
+ @data_inverse[object.to_s][predicate].sort.each do |subject|
101
+ next if subject =~ /^_:/
102
+ result << "<#{subject}> <#{predicate}> <#{object}>.\n"
103
+ end
114
104
  end
115
105
  result
116
106
  end
@@ -122,7 +112,7 @@ module TTL2HTML
122
112
  end
123
113
  end
124
114
  def output_html_files
125
- template = Template.new("")
115
+ template = Template.new("", @config)
126
116
  shapes = @graph.query([nil,
127
117
  RDF::URI("http://www.w3.org/1999/02/22-rdf-syntax-ns#type"),
128
118
  RDF::URI("http://www.w3.org/ns/shacl#NodeShape")])
@@ -230,8 +220,8 @@ module TTL2HTML
230
220
  Dir.mkdir @config[:output_dir] if not File.exist? @config[:output_dir]
231
221
  file = File.join(@config[:output_dir], file)
232
222
  end
233
- str = format_turtle(RDF::URI.new uri)
234
- str << format_turtle_inverse(RDF::URI.new uri)
223
+ str = format_turtle(uri)
224
+ str << format_turtle_inverse(uri)
235
225
  open(file, "w") do |io|
236
226
  io.puts str.strip
237
227
  end
@@ -22,7 +22,7 @@
22
22
  <hr>
23
23
  <footer>
24
24
  <%- if param[:uri] -%>
25
- <p class="pull-right"><a href="<%=h param[:uri] %>.ttl"><img src="https://www.w3.org/RDF/icons/rdf_flyer.24" alt="<%=h t("layout.rdf_data") %>"></a></p>
25
+ <p class="pull-right"><a href="<%=h param[:uri] %>.ttl"><img src="https://www.w3.org/RDF/icons/rdf_flyer.24" alt="<%=h t("layout.rdf-data") %>"></a></p>
26
26
  <%- end -%>
27
27
  </footer>
28
28
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ttl2html
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.1
4
+ version: 1.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Masao Takaku
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-11-21 00:00:00.000000000 Z
11
+ date: 2021-11-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: nokogiri