diff options
| author | Armand Philippot <git@armandphilippot.com> | 2021-12-30 19:47:21 +0100 |
|---|---|---|
| committer | Armand Philippot <git@armandphilippot.com> | 2021-12-30 19:47:21 +0100 |
| commit | a98b5ea6fe8e8cc98a55e0fd793e6e8660ea31c1 (patch) | |
| tree | 542810ab5aef99150db228bb54fd58303dcb31c7 /public/prism/prism-icu-message-format.js | |
| parent | ab355897a12b7bda1089a44de326d41455a0f7a3 (diff) | |
chore: add prismjs for syntax highlighting
Diffstat (limited to 'public/prism/prism-icu-message-format.js')
| -rw-r--r-- | public/prism/prism-icu-message-format.js | 160 |
1 files changed, 160 insertions, 0 deletions
diff --git a/public/prism/prism-icu-message-format.js b/public/prism/prism-icu-message-format.js new file mode 100644 index 0000000..b12ee2c --- /dev/null +++ b/public/prism/prism-icu-message-format.js @@ -0,0 +1,160 @@ +// https://unicode-org.github.io/icu/userguide/format_parse/messages/ +// https://unicode-org.github.io/icu-docs/apidoc/released/icu4j/com/ibm/icu/text/MessageFormat.html + +(function (Prism) { + /** + * @param {string} source + * @param {number} level + * @returns {string} + */ + function nested(source, level) { + if (level <= 0) { + return /[]/.source; + } else { + return source.replace(/<SELF>/g, function () { + return nested(source, level - 1); + }); + } + } + + var stringPattern = /'[{}:=,](?:[^']|'')*'(?!')/; + + var escape = { + pattern: /''/, + greedy: true, + alias: 'operator', + }; + var string = { + pattern: stringPattern, + greedy: true, + inside: { + escape: escape, + }, + }; + + var argumentSource = nested( + /\{(?:[^{}']|'(?![{},'])|''|<STR>|<SELF>)*\}/.source.replace( + /<STR>/g, + function () { + return stringPattern.source; + } + ), + 8 + ); + + var nestedMessage = { + pattern: RegExp(argumentSource), + inside: { + message: { + pattern: /^(\{)[\s\S]+(?=\}$)/, + lookbehind: true, + inside: null, // see below + }, + 'message-delimiter': { + pattern: /./, + alias: 'punctuation', + }, + }, + }; + + Prism.languages['icu-message-format'] = { + argument: { + pattern: RegExp(argumentSource), + greedy: true, + inside: { + content: { + pattern: /^(\{)[\s\S]+(?=\}$)/, + lookbehind: true, + inside: { + 'argument-name': { + pattern: /^(\s*)[^{}:=,\s]+/, + lookbehind: true, + }, + 'choice-style': { + // https://unicode-org.github.io/icu-docs/apidoc/released/icu4c/classicu_1_1ChoiceFormat.html#details + pattern: /^(\s*,\s*choice\s*,\s*)\S(?:[\s\S]*\S)?/, + lookbehind: true, + inside: { + punctuation: /\|/, + range: { + pattern: /^(\s*)[+-]?(?:\d+(?:\.\d*)?|\u221e)\s*[<#\u2264]/, + lookbehind: true, + inside: { + operator: /[<#\u2264]/, + number: /\S+/, + }, + }, + rest: null, // see below + }, + }, + 'plural-style': { + // https://unicode-org.github.io/icu-docs/apidoc/released/icu4j/com/ibm/icu/text/PluralFormat.html#:~:text=Patterns%20and%20Their%20Interpretation + pattern: + /^(\s*,\s*(?:plural|selectordinal)\s*,\s*)\S(?:[\s\S]*\S)?/, + lookbehind: true, + inside: { + offset: /^offset:\s*\d+/, + 'nested-message': nestedMessage, + selector: { + pattern: /=\d+|[^{}:=,\s]+/, + inside: { + keyword: /^(?:few|many|one|other|two|zero)$/, + }, + }, + }, + }, + 'select-style': { + // https://unicode-org.github.io/icu-docs/apidoc/released/icu4j/com/ibm/icu/text/SelectFormat.html#:~:text=Patterns%20and%20Their%20Interpretation + pattern: /^(\s*,\s*select\s*,\s*)\S(?:[\s\S]*\S)?/, + lookbehind: true, + inside: { + 'nested-message': nestedMessage, + selector: { + pattern: /[^{}:=,\s]+/, + inside: { + keyword: /^other$/, + }, + }, + }, + }, + keyword: /\b(?:choice|plural|select|selectordinal)\b/, + 'arg-type': { + pattern: /\b(?:date|duration|number|ordinal|spellout|time)\b/, + alias: 'keyword', + }, + 'arg-skeleton': { + pattern: /(,\s*)::[^{}:=,\s]+/, + lookbehind: true, + }, + 'arg-style': { + pattern: + /(,\s*)(?:currency|full|integer|long|medium|percent|short)(?=\s*$)/, + lookbehind: true, + }, + 'arg-style-text': { + pattern: RegExp( + /(^\s*,\s*(?=\S))/.source + + nested(/(?:[^{}']|'[^']*'|\{(?:<SELF>)?\})+/.source, 8) + + '$' + ), + lookbehind: true, + alias: 'string', + }, + punctuation: /,/, + }, + }, + 'argument-delimiter': { + pattern: /./, + alias: 'operator', + }, + }, + }, + escape: escape, + string: string, + }; + + nestedMessage.inside.message.inside = Prism.languages['icu-message-format']; + Prism.languages['icu-message-format'].argument.inside.content.inside[ + 'choice-style' + ].inside.rest = Prism.languages['icu-message-format']; +})(Prism); |
