Skip to content

Commit b19dc78

Browse files
author
Jeanine Adkisson
authored
Merge pull request #514 from jakubklimek/master
Add Turtle/TriG lexer
2 parents 90c3ed8 + 09aac92 commit b19dc78

File tree

4 files changed

+182
-0
lines changed

4 files changed

+182
-0
lines changed

lib/rouge/demos/turtle

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
@prefix xsd: <http://www.w3.org/2001/XMLSchema#>
2+
@prefix dcat: <http://www.w3.org/ns/dcat#> .
3+
@prefix dcterms: <http://purl.org/dc/terms/> .
4+
@prefix foaf: <http://xmlns.com/foaf/0.1/> .
5+
@base <http://base.of.relative.iris> .
6+
7+
PREFIX test: <http://example.org>
8+
PrEfIx insensitive: <http://insensitive.example.org>
9+
10+
GRAPH <https://trig.testing.graph> {
11+
<https://example.org/resource/dataset> a dcat:Dataset ;
12+
13+
#-----Mandatory-----#
14+
15+
dcterms:title 'Test title'@cs, "Test title"@en ;
16+
dcterms:description """Multiline
17+
string"""@cs, '''Another
18+
multiline string '''@en ;
19+
20+
#-----Recommended-----#
21+
dcat:contactPoint [ a foaf:Person ] ;
22+
test:list ( <http://ex.org> 1 1.1 +1 -1 1.2E+4 "Test" "\"Quote\"" ) ;
23+
test:datatype "2016-07-20"^^xsd:date ;
24+
test:text """next multiline""";
25+
.
26+
}

lib/rouge/lexers/turtle.rb

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
# -*- coding: utf-8 -*- #
2+
3+
module Rouge
4+
module Lexers
5+
class Turtle < RegexLexer
6+
title "Turtle/TriG"
7+
desc "Terse RDF Triple Language, TriG"
8+
tag 'turtle'
9+
filenames *%w(*.ttl *.trig)
10+
mimetypes *%w(
11+
text/turtle
12+
application/trig
13+
)
14+
15+
def self.analyze_text(text)
16+
start = text[0..1000]
17+
return 0.5 if start =~ %r(@prefix\b)
18+
return 0.5 if start =~ %r(@base\b)
19+
return 0.4 if start =~ %r(PREFIX\b)i
20+
return 0.4 if start =~ %r(BASE\b)i
21+
end
22+
23+
state :root do
24+
rule /@base\b/, Keyword::Declaration
25+
rule /@prefix\b/, Keyword::Declaration
26+
rule /true\b/, Keyword::Constant
27+
rule /false\b/, Keyword::Constant
28+
29+
rule /""".*?"""/m, Literal::String
30+
rule /"([^"\\]|\\.)*"/, Literal::String
31+
rule /'''.*?'''/m, Literal::String
32+
rule /'([^'\\]|\\.)*'/, Literal::String
33+
34+
rule /#.*$/, Comment::Single
35+
36+
rule /@[^\s,.; ]+/, Name::Attribute
37+
38+
rule /[+-]?[0-9]+\.[0-9]*E[+-]?[0-9]+/, Literal::Number::Float
39+
rule /[+-]?\.[0-9]+E[+-]?[0-9]+/, Literal::Number::Float
40+
rule /[+-]?[0-9]+E[+-]?[0-9]+/, Literal::Number::Float
41+
42+
rule /[+-]?[0-9]*\.[0-9]+?/, Literal::Number::Float
43+
44+
rule /[+-]?[0-9]+/, Literal::Number::Integer
45+
46+
rule /\./, Punctuation
47+
rule /,/, Punctuation
48+
rule /;/, Punctuation
49+
rule /\(/, Punctuation
50+
rule /\)/, Punctuation
51+
rule /\{/, Punctuation
52+
rule /\}/, Punctuation
53+
rule /\[/, Punctuation
54+
rule /\]/, Punctuation
55+
rule /\^\^/, Punctuation
56+
57+
rule /<[^>]*>/, Name::Label
58+
59+
rule /base\b/i, Keyword::Declaration
60+
rule /prefix\b/i, Keyword::Declaration
61+
rule /GRAPH\b/, Keyword
62+
rule /a\b/, Keyword
63+
64+
rule /\s+/, Text::Whitespace
65+
66+
rule /[^:;<>#@"\(\).\[\]\{\} ]+:/, Name::Namespace
67+
rule /[^:;<>#@"\(\).\[\]\{\} ]+/, Name
68+
69+
end
70+
end
71+
end
72+
end

spec/lexers/turtle_spec.rb

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# -*- coding: utf-8 -*- #
2+
3+
describe Rouge::Lexers::Turtle do
4+
let(:subject) { Rouge::Lexers::Turtle.new }
5+
6+
describe 'guessing' do
7+
include Support::Guessing
8+
9+
it 'guesses by filename' do
10+
assert_guess :filename => 'foo.ttl'
11+
assert_guess :filename => 'foo.trig'
12+
end
13+
14+
it 'guesses by mimetype' do
15+
assert_guess :mimetype => 'text/turtle'
16+
assert_guess :mimetype => 'application/trig'
17+
end
18+
19+
it 'guesses by source' do
20+
assert_guess :source => '@base'
21+
assert_guess :source => '@prefix'
22+
end
23+
end
24+
end

spec/visual/samples/turtle

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
2+
@prefix dcat: <http://www.w3.org/ns/dcat#> .
3+
@prefix dcterms: <http://purl.org/dc/terms/> .
4+
@prefix foaf: <http://xmlns.com/foaf/0.1/> .
5+
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
6+
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
7+
@prefix schema: <http://schema.org/> .
8+
@prefix vcard: <http://www.w3.org/2006/vcard/ns#> .
9+
@prefix void: <http://rdfs.org/ns/void#> .
10+
@prefix xml: <http://www.w3.org/XML/1998/namespace> .
11+
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
12+
@prefix adms: <http://www.w3.org/ns/adms#> .
13+
@prefix owl: <http://www.w3.org/2002/07/owl#> .
14+
@prefix spdx: <http://spdx.org/rdf/terms#> .
15+
@prefix skos: <http://www.w3.org/2004/02/skos/core#> .
16+
@prefix org: <http://www.w3.org/ns/org#> .
17+
18+
<https://data.cssz.cz/resource/dataset/zanikle-duchody> a dcat:Dataset ;
19+
20+
#-----Mandatory-----#
21+
22+
dcterms:title "Počet zaniklých důchodů v České republice"@cs, "Number of deprecated pensions in the Czech Republic"@en ;
23+
dcterms:description "Počet zaniklých důchodů v České republice podle roku, druhu penze, statistického důvodu zániku a pohlaví"@cs, "Number of deprecated pensions in the Czech Republic according to years, pension kinds and sex."@en ;
24+
25+
#-----Recommended-----#
26+
dcat:contactPoint <https://data.cssz.cz/resource/dataset/zanikle-duchody/contactPoint> ;
27+
dcat:distribution <https://data.cssz.cz/resource/dataset/zanikle-duchody/distribution/csv>, <https://data.cssz.cz/resource/dataset/zanikle-duchody/distribution/rdf> ;
28+
dcat:keyword "důchodová ročenka"@cs, "důchodové pojištění"@cs, "statistika"@cs, "statistics"@en ;
29+
dcterms:publisher <http://www.cssz.cz> ;
30+
dcat:theme <http://publications.europa.eu/resource/authority/data-theme/SOCI>, <http://eurovoc.europa.eu/3751>, <http://eurovoc.europa.eu/5329> ;
31+
32+
#-----Optional-----#
33+
34+
dcterms:accessRights <https://data.cssz.cz/dataset/zanikle-duchody/rightsStatement> ;
35+
dcat:conformsTo <http://purl.org/linked-data/cube#> ;
36+
foaf:page <https://data.cssz.cz/documentation/zanikle-duchody.html> ;
37+
dcterms:accrualPeriodicity <http://publications.europa.eu/resource/authority/frequency/ANNUAL> ;
38+
#dcat:hasVersion <> ;
39+
#dcat:isVersionOf <> ;
40+
dcterms:identifier "zanikle-duchody" ;
41+
dcat:landingPage <https://data.cssz.cz/dataset/pocet-zaniklych-duchodu-v-ceske-republice> ;
42+
dcterms:language <http://publications.europa.eu/resource/authority/language/CES>, <http://publications.europa.eu/resource/authority/language/ENG> ;
43+
adms:identifier <https://data.cssz.cz/resource/dataset/zanikle-duchody/id1>, <https://data.cssz.cz/resource/dataset/zanikle-duchody/id2> ;
44+
#dct:provenance <>;
45+
dcterms:relation <https://data.cssz.cz/resource/dataset/invalidita>, <https://data.cssz.cz/resource/dataset/pocet-vyplacenych-invalidnich-duchodu-v-ceske-republice> ;
46+
dcterms:issued "2015-09-30T09:00:01"^^xsd:dateTime ;
47+
adms:sample <https://data.cssz.cz/resource/dataset/zanikle-duchody/distribution/rdf>, <https://data.cssz.cz/resource/dataset/zanikle-duchody/distribution/csv> ;
48+
dcterms:source <https://data.cssz.cz/resource/dataset/invalidita>, <https://data.cssz.cz/resource/dataset/pocet-vyplacenych-invalidnich-duchodu-v-ceske-republice> ;
49+
dcterms:spatial <http://publications.europa.eu/resource/authority/country/CZE>, <http://ruian.linked.opendata.cz/resource/staty/1> ;
50+
dcterms:temporal <https://data.cssz.cz/resource/dataset/zanikle-duchody/temporal> ;
51+
dcterms:type <http://publications.europa.eu/resource/authority/datasetType/Open> ; # UNOFFICIAL, TBD
52+
dcterms:modified "2015-09-06T23:59:12"^^xsd:dateTime ;
53+
owl:versionInfo "5.0" ;
54+
adms:versionNotes "Lepší než předtim"@cs, "This version is better"@en .
55+
56+
<https://data.cssz.cz/resource/dataset/zanikle-duchody/id1> a adms:Identifier ;
57+
skos:notation "https://data.cssz.cz/dataset/pocet-zaniklych-duchodu-v-ceske-republice"^^<http://purl.org/spar/datacite/url> .
58+
59+
<https://data.cssz.cz/resource/dataset/zanikle-duchody/id2> a adms:Identifier ;
60+
skos:notation "https://data.cssz.cz/dataset/zanikle-duchody"^^<http://purl.org/spar/datacite/url> .

0 commit comments

Comments
 (0)