Skip to content

Commit c71d460

Browse files
committed
Sync extract-examples
1 parent c5efd4a commit c71d460

File tree

1 file changed

+25
-7
lines changed

1 file changed

+25
-7
lines changed

common/extract-examples.rb

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,20 +36,22 @@
3636
schema: "http://schema.org/",
3737
xsd: "http://www.w3.org/2001/XMLSchema#"
3838
}
39-
example_dir = yaml_dir = verbose = number = nil
39+
example_dir = yaml_dir = verbose = number = line = nil
4040

4141
opts = GetoptLong.new(
4242
["--example-dir", GetoptLong::REQUIRED_ARGUMENT],
4343
["--yaml-dir", GetoptLong::REQUIRED_ARGUMENT],
4444
["--verbose", '-v', GetoptLong::NO_ARGUMENT],
4545
["--number", '-n', GetoptLong::REQUIRED_ARGUMENT],
46+
["--line", '-l', GetoptLong::REQUIRED_ARGUMENT],
4647
)
4748
opts.each do |opt, arg|
4849
case opt
4950
when '--example-dir' then example_dir = arg && FileUtils::mkdir_p(arg)
5051
when '--yaml-dir' then yaml_dir = arg && FileUtils::mkdir_p(arg)
5152
when '--verbose' then verbose = true
5253
when '--number' then number = arg.to_i
54+
when '--line' then line = arg.to_i
5355
end
5456
end
5557

@@ -106,17 +108,15 @@ def table_to_dataset(table)
106108
when /IRI/, '-', /^\s*$/, " "
107109
else
108110
# We might think something was an IRI, but determine that it's not
109-
object = RDF::Literal(object.to_s) unless object.literal?
110-
object.datatype = RDF::Vocabulary.expand_pname(cell.sub("dcterms:", "dc:"))
111+
dt = RDF::Vocabulary.expand_pname(cell.sub("dcterms:", "dc:"))
112+
object = RDF::Literal(object.to_s, datatype: dt)
111113
end
112114
when 'Language'
113115
case cell
114116
when '-', /^\s*$/
115117
else
116118
# We might think something was an IRI, but determine that it's not
117-
object = RDF::Literal(object.to_s) unless object.literal?
118-
object.datatype = RDF.langString
119-
object.language = cell.to_sym
119+
object = RDF::Literal(object.to_s, language: cell.to_sym)
120120
end
121121
end
122122
end
@@ -187,6 +187,9 @@ def dataset_to_table(repo)
187187
end.join("\n ") + "\n </tbody>\n</table>"
188188
end
189189

190+
# Allow linting examples
191+
RDF::Reasoner.apply(:rdfs, :schema)
192+
190193
ARGV.each do |input|
191194
$stderr.puts "\ninput: #{input}"
192195
example_number = 1 # Account for imported Example 1 in typographical conventions
@@ -244,6 +247,7 @@ def save_example(examples:, element:, title:, example_number:, error:, warn:)
244247
fromRdf: element.attr('data-from-rdf'),
245248
toRdf: element.attr('data-to-rdf'),
246249
frame_for: element.attr('data-frame-for'),
250+
no_lint: element.attr('data-no-lint'),
247251
frame: element.attr('data-frame'),
248252
result_for: element.attr('data-result-for'),
249253
options: element.attr('data-options'),
@@ -282,6 +286,7 @@ def save_example(examples:, element:, title:, example_number:, error:, warn:)
282286
# Process API functions for
283287
examples.values.sort_by {|ex| ex[:number]}.each do |ex|
284288
next if number && number != ex[:number]
289+
next if line && line != ex[:line]
285290

286291
xpath = '//script[@type="application/ld+json"]'
287292
xpath += %([@id="#{ex[:target][1..-1]}"]) if ex[:target]
@@ -588,14 +593,27 @@ def save_example(examples:, element:, title:, example_number:, error:, warn:)
588593
if expected.is_a?(RDF::Dataset)
589594
expected_norm = RDF::Normalize.new(expected).map(&:to_nquads)
590595
result_norm = RDF::Normalize.new(result).map(&:to_nquads)
591-
unless expected_norm.sort == result_norm.sort
596+
if expected_norm.sort != result_norm.sort
592597
if verbose
593598
$stderr.puts "expected_norm:\n" + expected_norm.sort.join("")
594599
$stderr.puts "result_norm:\n" + result_norm.sort.join("")
595600
end
596601
errors << "Example #{ex[:number]} at line #{ex[:line]} not isomorphic with #{examples[ex[:result_for]][:number]}"
597602
$stdout.write "F".colorize(:red)
598603
next
604+
elsif !ex[:no_lint] && !(messages = expected.lint).empty?
605+
# Lint problems in resulting graph.
606+
if verbose
607+
messages.each do |kind, term_messages|
608+
term_messages.each do |term, messages|
609+
$stderr.puts "lint #{kind} #{term}"
610+
messages.each {|m| $stderr.puts " #{m}"}
611+
end
612+
end
613+
end
614+
errors << "Example #{ex[:number]} at line #{ex[:line]} has lint errors"
615+
$stdout.write "F".colorize(:red)
616+
next
599617
end
600618
else
601619
unless result == expected

0 commit comments

Comments
 (0)