Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cooklang syntax highlighting - initial add #3337

Merged
merged 22 commits into from Feb 19, 2022
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion components.js

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions components.json
Expand Up @@ -299,6 +299,10 @@
"title": "Content-Security-Policy",
"owner": "ScottHelme"
},
"cooklang": {
"title": "Cooklang",
"owner": "ahue"
},
"coq": {
"title": "Coq",
"owner": "RunDevelopment"
Expand Down
83 changes: 83 additions & 0 deletions components/prism-cooklang.js
@@ -0,0 +1,83 @@
(function (Prism) {

var ingr_cw_prefix = /(?:[@#][^{@#]+)\{/;
var ingr_cw_suffix = /\}/;
var quant1 = /(?:[^}|*%]*\*?)/;
var quant_unit = /(?:[^}|*%]+\*?%[^}]+)/;
var quant_servings_unit = /(?:(?:[^*}%|]+\|)+[^*}%|]+(?:%[^*|%}]+)?)/;

Prism.languages.cooklang = {
'comment': [
/\[-[\s\S]*?-\]/, // -- comment
/-{2}[^\r\n]*/, // [- comment -]
],
ahue marked this conversation as resolved.
Show resolved Hide resolved
'meta': { // >> key: value
'pattern': />{2}.*:.*/,
'inside': {
ahue marked this conversation as resolved.
Show resolved Hide resolved
'property': { // key:
'pattern': /(>{2}\s*)[^:]+/,
'lookbehind': true,
ahue marked this conversation as resolved.
Show resolved Hide resolved
}
}
},
'quant': { // @some ingredient{...} #some cookware{...}
'pattern': new RegExp(
ingr_cw_prefix.source
+ '(?:'
+ quant1.source + '|'
+ quant_unit.source + '|'
+ quant_servings_unit.source
+ ')'
+ ingr_cw_suffix.source
),
'inside': {
'variable': { // some ingredient, some cookware
pattern: /([@#])[^{@#]+/,
lookbehind: true
},
'amount-group': {
'pattern': /\{[^{}]*\}/, // {...}
'inside': {
'punctuation': /[{}]/,
'symbol': { // unit
'pattern': /(%)[^}]+/,
'lookbehind': true
},
'operator': /\*%|\*|%|\|/, // already includes not yet officially implemented servings operators
'number': /[^{}*%]+/, // amount
}
},
'keyword': /[@#]/,
}
},
'no-quant': { // ingredient, cookware
ahue marked this conversation as resolved.
Show resolved Hide resolved
'pattern': /[@#][^{@#\s]+/,
ahue marked this conversation as resolved.
Show resolved Hide resolved
'inside': {
'keyword': /[@#]/,
'variable': /[^{@#]+/
}
},
'timer-group': { // ~timer{...}
'pattern': /~[^{}]*\{\d+%(?:hours|minutes)\}/,
'inside': {
'variable': { // timer name
pattern: /(~)[^{]+/,
ahue marked this conversation as resolved.
Show resolved Hide resolved
lookbehind: true
},
'duration-group': { // {...}
'pattern': /\{[^{}]+\}/,
'inside': {
'punctuation': /[{}]/,
'symbol': { // unit
ahue marked this conversation as resolved.
Show resolved Hide resolved
'pattern': /(%)(?:minutes|min|m|hours|hrs|h)/,
ahue marked this conversation as resolved.
Show resolved Hide resolved
'lookbehind': true
},
'operator': /%/,
'number': /\d+/, // amount
}
},
'keyword': /~/,
}
}
};
}(Prism));
1 change: 1 addition & 0 deletions components/prism-cooklang.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

41 changes: 41 additions & 0 deletions examples/prism-cooklang.html
@@ -0,0 +1,41 @@
<h2>Comments</h2>
<pre><code>
-- This is a single line comment
[- this
is
a multi line comment -]
</code></pre>

<h2>Meta</h2>
<pre><code>
>> servings: 3
>> source: https://cooklang.org/docs/spec
>> any key: any value
</code></pre>

<h2>Ingredients</h2>
<pre><code>
@salt without amount
@egg{1}
@milk{1%l}
@milk{1|2%l}
@egg{1|2}
@egg{1*}
@milk{2*%l}
</code></pre>

<h2>Cookware</h2>
<pre><code>
#spoon without amount
#spoon{10%pair}
#spoon{1|2}
#spoon{1*%pair}
#spoon{1|2%pair}
#spoon{1*}
</code></pre>

<h2>Timer</h2>
<pre><code>
~{25%minutes} without name
~named timer{1%hours}
</code></pre>
20 changes: 20 additions & 0 deletions tests/languages/cooklang/comment_feature.test
@@ -0,0 +1,20 @@
-- a single line comment
[- a multi line comment on a single line -]
[- a multi
line comment 1 -]
[- a multi
line comment 2
-]
[-
a multi
line comment 3 -]

----------------------------------------------------

[
["comment", "-- a single line comment"],
["comment", "[- a multi line comment on a single line -]"],
["comment", "[- a multi\r\nline comment 1 -]"],
["comment", "[- a multi\r\nline comment 2 \r\n-]"],
["comment", "[- \r\na multi\r\nline comment 3 -]"]
]
129 changes: 129 additions & 0 deletions tests/languages/cooklang/cookware_feature.test
@@ -0,0 +1,129 @@
#spoon
#spoon and more @spoon
#more spoon{}
#even more spoon{1}
#spoon{1%set}
#spoon{2*%kg}
#spoon{3|1%set}
#spoon{3*set} must not match
#spoon{3|set}
#spoon{3%*set}
#spoon{3%*%set}

----------------------------------------------------

[
["no-quant", [
["keyword", "#"],
["variable", "spoon"]
]],

["no-quant", [
["keyword", "#"],
["variable", "spoon"]
]],
" and more ",
["no-quant", [
["keyword", "@"],
["variable", "spoon"]
]],

["quant", [
["keyword", "#"],
["variable", "more spoon"],
["amount-group", [
["punctuation", "{"],
["punctuation", "}"]
]]
]],

["quant", [
["keyword", "#"],
["variable", "even more spoon"],
["amount-group", [
["punctuation", "{"],
["number", "1"],
["punctuation", "}"]
]]
]],

["quant", [
["keyword", "#"],
["variable", "spoon"],
["amount-group", [
["punctuation", "{"],
["number", "1"],
["operator", "%"],
["symbol", "set"],
["punctuation", "}"]
]]
]],

["quant", [
["keyword", "#"],
["variable", "spoon"],
["amount-group", [
["punctuation", "{"],
["number", "2"],
["operator", "*%"],
["symbol", "kg"],
["punctuation", "}"]
]]
]],

["quant", [
["keyword", "#"],
["variable", "spoon"],
["amount-group", [
["punctuation", "{"],
["number", "3"],
["operator", "|"],
["number", "1"],
["operator", "%"],
["symbol", "set"],
["punctuation", "}"]
]]
]],

["no-quant", [
["keyword", "#"],
["variable", "spoon"]
]],
"{3*set} must not match\r\n",

["quant", [
["keyword", "#"],
["variable", "spoon"],
["amount-group", [
["punctuation", "{"],
["number", "3"],
["operator", "|"],
["number", "set"],
["punctuation", "}"]
]]
]],

["quant", [
["keyword", "#"],
["variable", "spoon"],
["amount-group", [
["punctuation", "{"],
["number", "3"],
["operator", "%"],
["symbol", "*set"],
["punctuation", "}"]
]]
]],

["quant", [
["keyword", "#"],
["variable", "spoon"],
["amount-group", [
["punctuation", "{"],
["number", "3"],
["operator", "%"],
["symbol", "*%set"],
["punctuation", "}"]
]]
]]
]