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 9 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
142 changes: 142 additions & 0 deletions components/prism-cooklang.js
@@ -0,0 +1,142 @@
(function (Prism) {

var single_token_suffix = /[^{}@#\s]+/.source;
var multi_token_infix = /[^{}@#]+/.source;
var multi_token_suffix = /\{[^}#@]*\}/.source;

var multi_token = multi_token_infix + multi_token_suffix;

var timer_units = /(?:h|hours|hrs|m|min|minutes)/.source;

var amount_group_impl = {
pattern: new RegExp(/\{/.source
+ '(?:(?:'
+ /(?:[^{}|*%]+\*?)/.source // optional serving scale
+ '|'
+ /(?:(?:[^{}|*%]+\|)+[^{}|*%]+)/.source // serving alternatives
+ ')' + /(?:%[^{}|*%]+)?/.source //optional unit
+ ')?'
+ /\}/.source
),
ahue marked this conversation as resolved.
Show resolved Hide resolved
inside: {
'amount': {
pattern: /([\{|])[^{}|*%]+/,
lookbehind: true,
alias: 'number',
},
'unit': {
pattern: /(%)[^}]+/,
lookbehind: true,
alias: 'symbol',
},
'servings-scaler': {
pattern: /\*/,
alias: 'operator',
},
'servings-alternative-seperator': {
ahue marked this conversation as resolved.
Show resolved Hide resolved
pattern: /\|/,
alias: 'operator',
},
'unit-separator': {
pattern: /(?:%|(\*)%)/,
lookbehind: true,
alias: 'operator',
},
'punctuation': /[{}]/,
}
};

Prism.languages.cooklang = {
'comment': {
// [- comment -]
// -- comment
pattern: /\[-[\s\S]*?-\]|--.*/,
greedy: true,
},
'meta': { // >> key: value
pattern: />>.*:.*/,
inside: {
property: { // key:
ahue marked this conversation as resolved.
Show resolved Hide resolved
pattern: /(>>\s*)[^\s:](?:[^:]*[^\s:])?/,
lookbehind: true,
}
}
},
'cookware-group': { // #...{...}, #...
pattern: new RegExp('#(?:'
+ multi_token
+ '|'
+ single_token_suffix
+ ')'
),
inside: {
'cookware': {
pattern: new RegExp('(#)(?:'
+ multi_token_infix
+ ')'
),
lookbehind: true,
alias: 'variable',
},
'cookware-keyword': {
pattern: /^#/,
alias: 'keyword',
},
'amount-group': amount_group_impl,
},
},
'ingredient-group': { // @...{...}, @...
pattern: new RegExp('@(?:'
+ multi_token
+ '|'
+ single_token_suffix
+ ')'),
inside: {
'ingredient': {
pattern: new RegExp('(@)(?:'
+ multi_token_infix
+ ')'),
lookbehind: true,
alias: 'variable',
},
'ingredient-keyword': {
pattern: /^@/,
alias: 'keyword',
},
'amount-group': amount_group_impl
}
},
'timer-group': { // ~timer{...}
// eslint-disable-next-line regexp/sort-alternatives
pattern: new RegExp(/~[^{}]*\{\d+%/.source + timer_units + /\}/.source),
ahue marked this conversation as resolved.
Show resolved Hide resolved
inside: {
'timer': {
pattern: /(~)[^{]+/,
ahue marked this conversation as resolved.
Show resolved Hide resolved
lookbehind: true,
alias: 'variable',
},
'duration-group': { // {...}
pattern: /\{[^{}]+\}/,
ahue marked this conversation as resolved.
Show resolved Hide resolved
inside: {
'punctuation': /[{}]/,
'unit': {
// eslint-disable-next-line regexp/no-dupe-disjunctions
pattern: new RegExp(/(%)/.source + timer_units),
ahue marked this conversation as resolved.
Show resolved Hide resolved
lookbehind: true,
alias: 'symbol',
},
'operator': /%/,
'duration': {
pattern: /\d+/,
alias: 'number',
},
}
},
'timer-keyword': {
pattern: /^~/,
alias: '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 -]"]
]
133 changes: 133 additions & 0 deletions tests/languages/cooklang/cookware_feature.test
@@ -0,0 +1,133 @@
#spoon
#spoon and more #spoon
#spoon is good but more #spoon are better
#more spoon{}
#even more spoon{1}
#spoon{1%set}
#spoon{2*%kg}
#spoon{3|1%set}
#spoon{3*set} must not match amount-group
#spoon{3|set}
#spoon{3%*set} must noch match amount-group
#spoon{3%*%set} must noch match amount-group

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

[
["cookware-group", [
["cookware-keyword", "#"],
["cookware", "spoon"]
]],

["cookware-group", [
["cookware-keyword", "#"],
["cookware", "spoon"]
]],
" and more ",
["cookware-group", [
["cookware-keyword", "#"],
["cookware", "spoon"]
]],

["cookware-group", [
["cookware-keyword", "#"],
["cookware", "spoon"]
]],
" is good but more ",
["cookware-group", [
["cookware-keyword", "#"],
["cookware", "spoon"]
]],
" are better\r\n",

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

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

["cookware-group", [
["cookware-keyword", "#"],
["cookware", "spoon"],
["amount-group", [
["punctuation", "{"],
["amount", "1"],
["unit-separator", "%"],
["unit", "set"],
["punctuation", "}"]
]]
]],

["cookware-group", [
["cookware-keyword", "#"],
["cookware", "spoon"],
["amount-group", [
["punctuation", "{"],
["amount", "2"],
["servings-scaler", "*"],
["unit-separator", "%"],
["unit", "kg"],
["punctuation", "}"]
]]
]],

["cookware-group", [
["cookware-keyword", "#"],
["cookware", "spoon"],
["amount-group", [
["punctuation", "{"],
["amount", "3"],
["servings-alternative-seperator", "|"],
["amount", "1"],
["unit-separator", "%"],
["unit", "set"],
["punctuation", "}"]
]]
]],

["cookware-group", [
["cookware-keyword", "#"],
["cookware", "spoon"],
"{3*set}"
]],
" must not match amount-group\r\n",

["cookware-group", [
["cookware-keyword", "#"],
["cookware", "spoon"],
["amount-group", [
["punctuation", "{"],
["amount", "3"],
["servings-alternative-seperator", "|"],
["amount", "set"],
["punctuation", "}"]
]]
]],

["cookware-group", [
["cookware-keyword", "#"],
["cookware", "spoon"],
"{3%*set}"
]],
" must noch match amount-group\r\n",

["cookware-group", [
["cookware-keyword", "#"],
["cookware", "spoon"],
"{3%*%set}"
]],
" must noch match amount-group"
]