Skip to content

Commit d803387

Browse files
committed
update language definitions
1 parent 69402f7 commit d803387

File tree

8 files changed

+228
-203
lines changed

8 files changed

+228
-203
lines changed

README.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ If you're not using a build system and just want to embed this in your webpage:
1212

1313
```html
1414
<script type="text/javascript" src="/path/to/highlight.pack.js"></script>
15-
<script type="text/javascript" src="/path/to/highlightjs-turtle/turtle.js"></script>
15+
<script type="text/javascript" src="/path/to/highlightjs-turtle/src/languages/turtle.js"></script>
1616
<script type="text/javascript">
1717
hljs.registerLanguage('turtle', window.hljsDefineTurtle);
1818
hljs.initHighlightingOnLoad();
@@ -35,4 +35,8 @@ This is a pretty simple package, the only thing you might want to do differently
3535

3636
### About the author
3737

38-
The code for Turtle was taken from the non-merged pull request by Mark Ellis, at https://github.com/ellismarkf/highlight.js/blob/c65ea836b3785a5b2ee2078461edaf38c005247d/src/languages/sparql.js
38+
Redmer KRONEMEIJER is an information modeller. Read more about him at https://rdmr.eu.
39+
40+
The code for Turtle was taken from two unmaintained repositories:
41+
- Mark Ellis, at https://github.com/ellismarkf/highlight.js
42+
- Vladimir Alexiev, at https://github.com/VladimirAlexiev/highlight.js

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,32 @@
11
{
2-
"name": "highlightjs-solidity",
3-
"version": "1.0.19",
4-
"description": "highlight.js syntax definition for Ethereum's Solidity language",
5-
"main": "solidity.js",
2+
"name": "highlightjs-turtle",
3+
"version": "1.0.1",
4+
"description": "highlight.js syntax definition for the semantic web language Turtle",
5+
"main": "src/languages/turtle.js",
66
"files": [],
77
"scripts": {
8-
"test": "mocha"
8+
"test": "mocha test/markup/turtle/default.js"
99
},
1010
"repository": {
1111
"type": "git",
12-
"url": "git+ssh://[email protected]/pospi/highlightjs-solidity.git"
12+
"url": "git+ssh://[email protected]/redmer/highlightjs-turtle.git"
1313
},
1414
"keywords": [
15-
"Solidity",
16-
"Ethereum",
15+
"turtle",
16+
"semantic web",
1717
"highlight.js",
1818
"highlightjs",
1919
"syntax"
2020
],
21-
"author": "pospi",
21+
"author": "redmer",
2222
"license": "MIT",
2323
"bugs": {
24-
"url": "https://github.com/highlightjs/highlightjs-solidity/issues"
24+
"url": "https://github.com/redmer/highlightjs-turtle/issues"
2525
},
26-
"homepage": "https://github.com/highlightjs/highlightjs-solidity#readme",
26+
"homepage": "https://github.com/redmer/highlightjs-turtle#readme",
2727
"devDependencies": {
2828
"highlightjs": "^9.12.0",
2929
"mocha": "^6.2.1",
3030
"parse5": "^5.1.0"
3131
}
32-
}
32+
}

src/languages/turtle.js

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
/*
2+
Language: Turtle
3+
Author: Redmer KRONEMEIJER <[email protected]>
4+
Contributors: Mark ELLIS <[email protected]>, Vladimir ALEXIEV <[email protected]>
5+
*/
6+
7+
var module = module ? module : {}; // shim for browser use
8+
9+
function hljsDefineTurtle(hljs) {
10+
var KEYWORDS = {
11+
keyword: 'BASE|10 PREFIX|10 @base|10 @prefix|10',
12+
literal: 'true|0 false|0',
13+
built_in: 'a|0'
14+
};
15+
16+
var IRI_LITERAL = {// https://www.w3.org/TR/turtle/#grammar-production-IRIREF
17+
className: 'literal',
18+
relevance: 1, // XML tags look also like relative IRIs
19+
begin: /</,
20+
end: />/,
21+
illegal: /[^\x00-\x20<>"{}|^`]/, // TODO: https://www.w3.org/TR/turtle/#grammar-production-UCHAR
22+
};
23+
24+
// https://www.w3.org/TR/turtle/#terminals
25+
var PN_CHARS_BASE = 'A-Za-z\u00C0-\u00D6\u00D8-\u00F6\u00F8-\u02FF\u0370-\u037D\u037F-\u1FFF\u200C-\u200D\u2070-\u218F\u2C00-\u2FEF\u3001-\uD7FF\uF900-\uFDCF\uFDF0-\uFFFD\u10000-\uEFFFF';
26+
var PN_CHARS_U = PN_CHARS_BASE + '_';
27+
var PN_CHARS = '-' + PN_CHARS_U + '0-9\u00B7\u0300-\u036F\u203F-\u2040';
28+
var BLANK_NODE_LABEL = '_:[' + PN_CHARS_U + '0-9]([' + PN_CHARS + '.]*[' + PN_CHARS + '])?';
29+
var PN_PREFIX = '[' + PN_CHARS_BASE + ']([' + PN_CHARS + '.]*[' + PN_CHARS + '])?';
30+
var PERCENT = '%[0-9A-Fa-f][0-9A-Fa-f]';
31+
var PN_LOCAL_ESC = '\\\\[_~.!$&\'()*+,;=/?#@%-]';
32+
var PLX = PERCENT + '|' + PN_LOCAL_ESC;
33+
var PNAME_NS = '(' + PN_PREFIX + ')?:';
34+
var PN_LOCAL = '([' + PN_CHARS_U + ':0-9]|' + PLX + ')([' + PN_CHARS + '.:]|' + PLX + ')*([' + PN_CHARS + ':]|' + PLX + ')?';
35+
var PNAME_LN = PNAME_NS + PN_LOCAL;
36+
var PNAME_NS_or_LN = PNAME_NS + '(' + PN_LOCAL + ')?';
37+
38+
var PNAME = {
39+
begin: PNAME_NS_or_LN,
40+
relevance: 0,
41+
className: 'symbol',
42+
};
43+
44+
var BLANK_NODE = {
45+
begin: BLANK_NODE_LABEL,
46+
relevance: 10,
47+
className: 'template-variable',
48+
};
49+
50+
var LANGTAG = {
51+
begin: /@[a-zA-Z]+([a-zA-Z0-9-]+)*/,
52+
className: 'type',
53+
relevance: 5, // also catches objectivec keywords like: @protocol, @optional
54+
};
55+
56+
var DATATYPE = {
57+
begin: '\\^\\^' + PNAME_LN,
58+
className: 'type',
59+
relevance: 10,
60+
};
61+
62+
var TRIPLE_APOS_STRING = {
63+
begin: /'''/,
64+
end: /'''/,
65+
className: 'string',
66+
relevance: 0,
67+
};
68+
69+
var TRIPLE_QUOTE_STRING = {
70+
begin: /"""/,
71+
end: /"""/,
72+
className: 'string',
73+
relevance: 0,
74+
};
75+
76+
var APOS_STRING_LITERAL = JSON.parse(JSON.stringify(hljs.APOS_STRING_MODE));
77+
APOS_STRING_LITERAL.relevance = 0;
78+
79+
var QUOTE_STRING_LITERAL = JSON.parse(JSON.stringify(hljs.QUOTE_STRING_MODE));
80+
QUOTE_STRING_LITERAL.relevance = 0;
81+
82+
var NUMBER = JSON.parse(JSON.stringify(hljs.C_NUMBER_MODE));
83+
NUMBER.relevance = 0;
84+
85+
return {
86+
name: "Turtle",
87+
case_insensitive: false, // was: true, but PREFIX and @prefix are oblig. cased thus
88+
keywords: KEYWORDS,
89+
aliases: ['turtle', 'ttl', 'n3'],
90+
contains: [
91+
LANGTAG,
92+
DATATYPE,
93+
IRI_LITERAL,
94+
BLANK_NODE,
95+
PNAME,
96+
TRIPLE_APOS_STRING, TRIPLE_QUOTE_STRING, // order matters
97+
APOS_STRING_LITERAL, QUOTE_STRING_LITERAL,
98+
NUMBER,
99+
hljs.HASH_COMMENT_MODE,
100+
],
101+
exports: {
102+
LANGTAG: LANGTAG,
103+
DATATYPE: DATATYPE,
104+
IRI_LITERAL: IRI_LITERAL,
105+
BLANK_NODE: BLANK_NODE,
106+
PNAME: PNAME,
107+
TRIPLE_APOS_STRING: TRIPLE_APOS_STRING,
108+
TRIPLE_QUOTE_STRING: TRIPLE_QUOTE_STRING,
109+
APOS_STRING_LITERAL: APOS_STRING_LITERAL,
110+
QUOTE_STRING_LITERAL: QUOTE_STRING_LITERAL,
111+
NUMBER: NUMBER,
112+
KEYWORDS: KEYWORDS,
113+
}
114+
};
115+
}
116+
117+
module.exports = function (hljs) {
118+
hljs.registerLanguage('turtle', hljsDefineTurtle);
119+
};
120+
121+
module.exports.definer = hljsDefineTurtle;

test.js

Lines changed: 0 additions & 86 deletions
This file was deleted.

test/detect/turtle/default.txt

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
@base <http://example.org/> .
2+
@prefix : <http://example.org/>.
3+
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
4+
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
5+
@prefix foaf: <http://xmlns.com/foaf/0.1/> .
6+
@prefix rel: <http://www.perceive.net/schemas/relationship/> .
7+
@prefix ff-map: <http://factforge.net/ff2016-mapping/>.
8+
9+
<#green-goblin>
10+
rel:enemyOf <#spiderman> ;
11+
a foaf:Person ; # in the context of the Marvel universe
12+
foaf:description """Long TRIPLE_QUOTE_STRING with ''' embedded
13+
apostrophes '''""",
14+
'''The librarian said, "Perhaps you would enjoy 'War and Peace'."'''.
15+
16+
<#spiderman> rel:enemyOf <#green-goblin> ;
17+
foaf:name "Spiderman"@en, "Человек-паук"@ru ;
18+
foaf:age "23"^^xsd:integer, 23;
19+
foaf:height 1.68, 0.168e1, "1.68"^^xsd:decimal, true, false;
20+
foaf:mbox <mailto:[email protected]>;
21+
rdfs:label 'chat'@fr,
22+
23+
_:blank1 fb:object.type fb:object.type.car. # trailing dot not part of symbol
24+
: rel:weird :bar, rel: ; rel:weird rel:.
25+
26+
</doc#char=10,20> ff-map:mentionsEntity </entity>.

test/markup/turtle/default.js

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
const assert = require('assert');
2+
const parse5 = require('parse5');
3+
4+
const hljs = require('highlightjs');
5+
const defineTurtle = require('../../../src/languages/turtle');
6+
7+
defineTurtle(hljs);
8+
9+
// Receives a Turtle snippet and returns an array of [type, text] tuples.
10+
// Type is the detected token type, and text the corresponding source text.
11+
function getTokens(source) {
12+
const { value } = hljs.highlight('turtle', source);
13+
const frag = parse5.parseFragment(value);
14+
15+
return frag.childNodes.map(function (node) {
16+
if (node.nodeName === '#text') {
17+
return ['none', node.value];
18+
} else {
19+
const type = node.attrs.find(a => a.name === 'class').value.replace(/^hljs-/, '');
20+
assert(
21+
node.childNodes.length === 1 && node.childNodes[0].nodeName === '#text',
22+
'Unexpected nested tags',
23+
);
24+
return [type, node.childNodes[0].value];
25+
}
26+
});
27+
}
28+
29+
// Taken from the Solidity repo.
30+
it('numbers', function () {
31+
const ok = [
32+
'-5',
33+
'-5.0',
34+
'4.2E9',
35+
];
36+
37+
const fail = [
38+
'1-5',
39+
'5,0',
40+
];
41+
42+
for (const n of ok) {
43+
assert.deepStrictEqual(getTokens(n), [['number', n]]);
44+
}
45+
46+
for (const n of fail) {
47+
assert.notDeepStrictEqual(getTokens(n), [['number', n]]);
48+
}
49+
});
50+
51+
// it('namespaces', function () {
52+
// assert.deepStrictEqual(getTokens(':local'), [['symbol', ':local']]);
53+
// assert.deepStrictEqual(getTokens('_:local'), [['template-variable', '_:local']]);
54+
// assert.deepStrictEqual(getTokens('ns1:hello'), [['symbol', 'ns1:hello']]);
55+
// assert.notDeepStrictEqual(getTokens('ns1:ns2:wrong'), [['symbol', 'ns1:ns2:wrong']]);
56+
// });
57+
58+
it('strings', function () {
59+
assert.deepStrictEqual(getTokens('"hello"@en'), [['string', '"hello"'], ['type', '@en']]);
60+
assert.deepStrictEqual(getTokens('"daar"@nl-NL'), [['string', '"daar"'], ['type', '@nl-NL']]);
61+
});

0 commit comments

Comments
 (0)