Skip to content

Commit

Permalink
Added support for Cooklang (#3337)
Browse files Browse the repository at this point in the history
Co-authored-by: Michael Schmidt <msrd0000@gmail.com>
  • Loading branch information
ahue and RunDevelopment authored Feb 19, 2022
1 parent 703881e commit 4eb928c
Show file tree
Hide file tree
Showing 10 changed files with 580 additions and 1 deletion.
2 changes: 1 addition & 1 deletion components.js

Large diffs are not rendered by default.

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

// see https://github.com/cooklang/spec/blob/main/EBNF.md

var single_token_suffix = /(?:(?!\s)[\d$+<=a-zA-Z\x80-\uFFFF])+/.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: /\{[^{}]*\}/,
inside: {
'amount': {
pattern: /([\{|])[^{}|*%]+/,
lookbehind: true,
alias: 'number',
},
'unit': {
pattern: /(%)[^}]+/,
lookbehind: true,
alias: 'symbol',
},
'servings-scaler': {
pattern: /\*/,
alias: 'operator',
},
'servings-alternative-separator': {
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:
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',
},
'quantity-group': {
pattern: new RegExp(/\{[^{}@#]*\}/),
inside: {
'quantity': {
pattern: new RegExp(/(^\{)/.source + multi_token_infix),
lookbehind: true,
alias: 'number',
},
'punctuation': /[{}]/,
}
}
},
},
'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: /~(?!\s)[^@#~{}]*\{[^{}]*\}/,
inside: {
'timer': {
pattern: /(^~)[^{]+/,
lookbehind: true,
alias: 'variable',
},
'duration-group': { // {...}
pattern: /\{[^{}]*\}/,
inside: {
'punctuation': /[{}]/,
'unit': {
pattern: new RegExp(/(%\s*)/.source + timer_units + /\b/.source),
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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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 -]"]
]
131 changes: 131 additions & 0 deletions tests/languages/cooklang/cookware_feature.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
#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}
#spoon{3|set}
#spoon{3%*set}
#spoon{3%*%set}

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

[
["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"],
["quantity-group", [
["punctuation", "{"],
["punctuation", "}"]
]]
]],

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

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

["cookware-group", [
["cookware-keyword", "#"],
["cookware", "spoon"],
["quantity-group", [
["punctuation", "{"],
["quantity", "2*%kg"],
["punctuation", "}"]
]]
]],

["cookware-group", [
["cookware-keyword", "#"],
["cookware", "spoon"],
["quantity-group", [
["punctuation", "{"],
["quantity", "3|1%set"],
["punctuation", "}"]
]]
]],

["cookware-group", [
["cookware-keyword", "#"],
["cookware", "spoon"],
["quantity-group", [
["punctuation", "{"],
["quantity", "3*set"],
["punctuation", "}"]
]]
]],

["cookware-group", [
["cookware-keyword", "#"],
["cookware", "spoon"],
["quantity-group", [
["punctuation", "{"],
["quantity", "3|set"],
["punctuation", "}"]
]]
]],

["cookware-group", [
["cookware-keyword", "#"],
["cookware", "spoon"],
["quantity-group", [
["punctuation", "{"],
["quantity", "3%*set"],
["punctuation", "}"]
]]
]],

["cookware-group", [
["cookware-keyword", "#"],
["cookware", "spoon"],
["quantity-group", [
["punctuation", "{"],
["quantity", "3%*%set"],
["punctuation", "}"]
]]
]]
]
Loading

0 comments on commit 4eb928c

Please sign in to comment.