ttl2html 0.0.6 → 0.1.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: 02d0284dc6455fa2b571b8c6ff8ef730889327ca819dd5f05ee39514867fcd7c
4
- data.tar.gz: '038ab96245d4ff3a5688716d41416bb9ca1f7c65b544283990faa0d8009799fe'
3
+ metadata.gz: 3285a319273819d00281486dadbdea6c82c162856157075db553e18b659b16cc
4
+ data.tar.gz: 2f66f22a4ad30bd4dc6e0e9a44f24be37cf2e5c11396f4a2c85414becb1dbe6a
5
5
  SHA512:
6
- metadata.gz: d8591544285b417add4522e69deb85618c8e883058fad0d4080b4306cd7948d55d57b3c626dc47531952c448a874db3773184f6c7fd1ab0122d69f7813b76bd2
7
- data.tar.gz: c1f4a606959b5214584b3fdaef8b7c429a7b2a6429f3a5954d111203bf5e59cdcdbeebc1a11739ea95cd40d2896d5561d901f4545e4ccc0932c475bae6f9888f
6
+ metadata.gz: 4301d623df7599b6344806d66c97c71efb0633f45f663be45a460d5ceb23c1fe452d4ed2b01b2df4f02109d4168b8a31acb2dccb21a7f4e4234926cc59e4f4f5
7
+ data.tar.gz: 930b188d2f0d89601ba6af9199fd0ce87d40b3a00bd8a6711c14a82cb7dc52dae90d871772cd4e39dd4680ef8894e4c9245651e2a158c33f690f887a267f597a
data/bin/xlsx2shape ADDED
@@ -0,0 +1,11 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "xlsx2shape"
4
+
5
+ if ARGV.size == 0
6
+ puts "USAGE: xlsx2shape file.xlsx"
7
+ exit
8
+ end
9
+
10
+ turtle = XLSX2Shape.xlsx2shape(ARGV[0])
11
+ puts turtle
@@ -1 +1 @@
1
- TTL2HTML::VERSION = "0.0.6
1
+ TTL2HTML::VERSION = "0.1.0"
data/lib/xlsx2shape.rb ADDED
@@ -0,0 +1,122 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "roo"
4
+
5
+ module XLSX2Shape
6
+ def xlsx2shape(filename)
7
+ shapes = {}
8
+ prefix = {}
9
+ xlsx = Roo::Excelx.new(filename)
10
+ xlsx.each_with_pagename do |name, sheet|
11
+ if name =~ /\Aprefix\z/i
12
+ sheet.each do |row|
13
+ prefix[row[0]] = row[1] if not row[1].empty?
14
+ end
15
+ else
16
+ headers = sheet.row(1)
17
+ uri = headers.first
18
+ shapes[uri] = ["<#{uri}> a sh:NodeShape"]
19
+ order = 1
20
+ sheet.each_with_index do |row, idx|
21
+ row_h = map_xlsx_row_headers(row, headers)
22
+ case row.first
23
+ when "sh:targetClass"
24
+ shapes[uri] << "#{format_property("sh:targetClass", row[1])}" if row[1]
25
+ when "sh:property"
26
+ prop_values = []
27
+ headers[1..-1].each do |prop|
28
+ next if row_h[prop].empty?
29
+ case prop
30
+ when /\@(\w+)\z/
31
+ lang = $1
32
+ property_name = prop.sub(/\@(\w+)\z/, "")
33
+ prop_values << format_property(property_name, row_h[prop], lang)
34
+ when "sh:minCount", "sh:maxCount"
35
+ prop_values << format_property(prop, row_h[prop].to_i)
36
+ when "sh:languageIn"
37
+ prop_values << " sh:languageIn (#{row_h[prop].split.map{|e| format_pvalue(e) }.join(" ")})"
38
+ when "sh:uniqueLang"
39
+ case row_h[prop]
40
+ when "true"
41
+ prop_values << " sh:uniqueLang true"
42
+ when "false"
43
+ prop_values << " sh:uniqueLang false"
44
+ else
45
+ logger.warn "sh:uniqueLang value unknown: #{row_h[prop]} at #{uri}"
46
+ end
47
+ else
48
+ prop_values << format_property(prop, row_h[prop])
49
+ end
50
+ end
51
+ prop_values << format_property("sh:order", order)
52
+ order += 1
53
+ str = prop_values.join(";\n ")
54
+ shapes[uri] << " sh:property [\n #{str}\n ]"
55
+ when "sh:or"
56
+ shapes[uri] << " sh:or (#{row[1..-1].select{|e| not e.empty? }.map{|e| format_pvalue(e) }.join(" ")})"
57
+ end
58
+ end
59
+ end
60
+ end
61
+ result = ""
62
+ prefix.sort.each do |prefix, val|
63
+ result << "@prefix #{prefix} <#{val}>.\n"
64
+ end
65
+ shapes.sort_by{|uri, val| uri }.each do |uri, val|
66
+ result << "\n"
67
+ result << shapes[uri].join(";\n")
68
+ result << ".\n"
69
+ end
70
+ result
71
+ end
72
+
73
+ def map_xlsx_row_headers(data_row, headers)
74
+ hash = {}
75
+ headers.each_with_index do |h, idx|
76
+ hash[h] = data_row[idx].to_s
77
+ end
78
+ hash
79
+ end
80
+ def format_pvalue(value, lang = nil)
81
+ str = ""
82
+ if value.is_a? Hash
83
+ result = ["["]
84
+ array = []
85
+ value.keys.sort.each do |k|
86
+ array << format_property(k, value[k])
87
+ end
88
+ result << array.join(";\n")
89
+ result << " ]"
90
+ str = result.join("\n")
91
+ elsif value.is_a? Integer
92
+ str = value
93
+ elsif value =~ /\Ahttps?:\/\//
94
+ str = %Q|<#{value}>|
95
+ elsif value =~ /\A\w+:[\w\-\.]+\Z/
96
+ str = value
97
+ elsif value =~ /\A(.+?)\^\^(\w+:\w+)\z/
98
+ str = %Q|"#{escape_turtle($1)}"^^#{$2}|
99
+ elsif lang
100
+ str = %Q|"#{escape_turtle(value)}"@#{lang}|
101
+ else
102
+ str = %Q|"#{escape_turtle(value)}"|
103
+ end
104
+ str
105
+ end
106
+ def format_property(property, value, lang = nil)
107
+ if value.is_a? Array
108
+ value = value.sort_by{|e|
109
+ format_pvalue(e)
110
+ }.map do |e|
111
+ format_pvalue(e)
112
+ end
113
+ %Q| #{property} #{ value.join(", ") }|
114
+ else
115
+ value = format_pvalue(value, lang)
116
+ %Q| #{property} #{value}|
117
+ end
118
+ end
119
+ def escape_turtle(str)
120
+ str.gsub(/\\/){ '\\\\' }.gsub(/"/){ '\"' }
121
+ end
122
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ttl2html
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.6
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Masao Takaku
@@ -38,6 +38,20 @@ dependencies:
38
38
  - - ">="
39
39
  - !ruby/object:Gem::Version
40
40
  version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: roo
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
41
55
  - !ruby/object:Gem::Dependency
42
56
  name: ruby-progressbar
43
57
  requirement: !ruby/object:Gem::Requirement
@@ -98,13 +112,16 @@ description: Static site generator for RDF/Turtle
98
112
  email: tmasao@acm.org
99
113
  executables:
100
114
  - ttl2html
115
+ - xlsx2shape
101
116
  extensions: []
102
117
  extra_rdoc_files: []
103
118
  files:
104
119
  - bin/ttl2html
120
+ - bin/xlsx2shape
105
121
  - lib/ttl2html.rb
106
122
  - lib/ttl2html/template.rb
107
123
  - lib/ttl2html/version.rb
124
+ - lib/xlsx2shape.rb
108
125
  - templates/default.html.erb
109
126
  - templates/index.html.erb
110
127
  - templates/layout.html.erb