Class: TTL2HTML::App

Inherits:
Object
  • Object
show all
Defined in:
lib/ttl2html.rb

Instance Method Summary collapse

Constructor Details

#initialize(config = "config.yml") ⇒ App

Returns a new instance of App.



13
14
15
16
17
18
19
20
21
# File 'lib/ttl2html.rb', line 13

def initialize(config = "config.yml")
  @template = {}
  @config = load_config(config)
  if not @config[:base_uri]
    raise "load_config: base_uri not found"
  end
  @data = {}
  @graph = RDF::Graph.new
end

Instance Method Details

#cleanupObject



136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/ttl2html.rb', line 136

def cleanup
  each_data do |uri, v|
    if @data.keys.find{|e| e.start_with?(uri + "/") }
      file = uri + "/index.html"
    else
      file = uri + ".html"
    end
    html_file = file.sub(@config[:base_uri], "")
    html_file = File.join(@config[:output_dir], html_file) if @config[:output_dir]
    File.unlink html_file
    ttl_file = uri.sub(@config[:base_uri], "") + ".ttl"
    ttl_file = File.join(@config[:output_dir], ttl_file) if @config[:output_dir]
    File.unlink ttl_file
  end
end

#each_dataObject



94
95
96
97
98
99
# File 'lib/ttl2html.rb', line 94

def each_data
  @data.each do |uri, v|
    next if not uri.start_with? @config[:base_uri]
    yield uri, v
  end
end

#format_turtle(subject, depth = 1) ⇒ Object



54
55
56
57
58
59
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
# File 'lib/ttl2html.rb', line 54

def format_turtle(subject, depth = 1)
  turtle = RDF::Turtle::Writer.new
  result = ""
  if subject.iri?
    result << "<#{subject}>\n#{"  "*depth}"
  else
    result << "[\n#{"  "*depth}"
  end
  result << @graph.query([subject, nil, nil]).predicates.sort.map do |predicate|
    str = "<#{predicate}> "
    str << @graph.query([subject, predicate, nil]).objects.sort_by do |object|
      if object.resource? and not object.iri? # blank node:
        i@graph.query([object, nil, nil]).statements.sort_by{|e|
          [ e.predicate, e.object ]
        }.map{|e|
          [ e.predicate, e.object ]
        }
      else
        object
      end
    end.map do |object|
      if object.resource? and not object.iri? # blank node:
        format_turtle(object, depth + 1)
      else
        case object
        when RDF::URI
          turtle.format_uri(object)
        else
          turtle.format_literal(object)
        end
      end
    end.join(", ")
    str
  end.join(";\n#{"  "*depth}")
  result << " ." if subject.iri?
  result << "\n"
  result << "#{"  "*(depth-1)}]" if not subject.iri?
  result
end

#load_config(file) ⇒ Object



23
24
25
26
27
28
29
# File 'lib/ttl2html.rb', line 23

def load_config(file)
  config = {}
  YAML.load_file(file).each do |k, v|
    config[k.intern] = v
  end
  config
end

#load_turtle(file) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/ttl2html.rb', line 31

def load_turtle(file)
  STDERR.puts "loading #{file}..."
  count = 0
  RDF::Turtle::Reader.open(file) do |reader|
    reader.statements.each do |statement|
      @graph.insert(statement)
      s = statement.subject
      v = statement.predicate
      o = statement.object
      count += 1
      @data[s.to_s] ||= {}
      if o.respond_to?(:has_language?) and o.has_language?
        @data[s.to_s][v.to_s] ||= {}
        @data[s.to_s][v.to_s][o.language] = o.to_s
      else
        @data[s.to_s][v.to_s] ||= []
        @data[s.to_s][v.to_s] << o.to_s
      end
    end
  end
  STDERR.puts "#{count} triples. #{@data.size} subjects."
  @data
end

#output_html_filesObject



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/ttl2html.rb', line 100

def output_html_files
  each_data do |uri, v|
    template = Template.new("default.html.erb", @config)
    param = @config.dup
    param[:uri] = uri
    param[:data] = v
    param[:data_global] = @data
    param[:title] = template.get_title(v)
    if @data.keys.find{|e| e.start_with?(uri + "/") }
      file = uri + "/index.html"
    else
      file = uri + ".html"
    end
    #p uri, param
    file = file.sub(@config[:base_uri], "")
    if @config[:output_dir]
      Dir.mkdir @config[:output_dir] if not File.exist? @config[:output_dir]
      file = File.join(@config[:output_dir], file)
    end
    template.output_to(file, param)
  end
end

#output_turtle_filesObject



122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/ttl2html.rb', line 122

def output_turtle_files
  each_data do |uri, v|
    file = uri.sub(@config[:base_uri], "")
    file << ".ttl"
    if @config[:output_dir]
      Dir.mkdir @config[:output_dir] if not File.exist? @config[:output_dir]
      file = File.join(@config[:output_dir], file)
    end
    str = format_turtle(RDF::URI.new uri)
    open(file, "w") do |io|
      io.puts str.strip
    end
  end
end