Method: TTL2HTML::App#format_turtle

Defined in:
lib/ttl2html.rb

#format_turtle(subject, depth = 1) ⇒ Object



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
# File 'lib/ttl2html.rb', line 71

def format_turtle(subject, depth = 1)
  turtle = RDF::Turtle::Writer.new
  result = ""
  if subject =~ /^_:/
    result << "[\n#{"  "*depth}"
  else
    result << "<#{subject}>\n#{"  "*depth}"
  end
  result << @data[subject.to_s].keys.sort.map do |predicate|
    str = "<#{predicate}> "
    str << @data[subject.to_s][predicate].sort.map do |object|
      if object.respond_to?(:resource?) and object.resource? and /^_:/ =~ object # blank node:
        format_turtle(object, depth + 1)
      elsif RDF::URI::IRI =~ object.to_s
        turtle.format_uri(RDF::URI.new object)
      elsif object.respond_to?(:first) and object.first.kind_of?(Symbol)
        turtle.format_literal(RDF::Literal.new(object[1], language: object[0]))
      else
        turtle.format_literal(object)
      end
    end.join(", ")
    str
  end.join(";\n#{"  "*depth}")
  result << " ." if not subject =~ /^_:/
  result << "\n"
  result << "#{"  "*(depth-1)}]" if subject =~ /^_:/
  result
end