From b59a5b8cdb9bf8cdc28701e16c3d581fb96f4ef8 Mon Sep 17 00:00:00 2001 From: toasted-nutbread Date: Fri, 8 Nov 2019 22:57:20 -0500 Subject: Add support for some regex functions in handlebars templates --- ext/bg/js/handlebars.js | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) (limited to 'ext/bg/js/handlebars.js') diff --git a/ext/bg/js/handlebars.js b/ext/bg/js/handlebars.js index fba437da..d6307e1d 100644 --- a/ext/bg/js/handlebars.js +++ b/ext/bg/js/handlebars.js @@ -79,6 +79,47 @@ function handlebarsSanitizeCssClass(options) { return options.fn(this).replace(/[^_a-z0-9\u00a0-\uffff]/ig, '_'); } +function handlebarsRegexReplace(...args) { + // Usage: + // {{#regexReplace regex string [flags]}}content{{/regexReplace}} + // regex: regular expression string + // string: string to replace + // flags: optional flags for regular expression + // e.g. "i" for case-insensitive, "g" for replace all + let value = args[args.length - 1].fn(this); + if (args.length >= 3) { + try { + let flags = args.length > 3 ? args[2] : 'g'; + const regex = new RegExp(args[0], flags); + value = value.replace(regex, args[1]); + } catch (e) { + return `${e}`; + } + } + return value; +} + +function handlebarsRegexMatch(...args) { + // Usage: + // {{#regexMatch regex [flags]}}content{{/regexMatch}} + // regex: regular expression string + // flags: optional flags for regular expression + // e.g. "i" for case-insensitive, "g" for match all + let value = args[args.length - 1].fn(this); + if (args.length >= 2) { + try { + const flags = args.length > 2 ? args[1] : ''; + const regex = new RegExp(args[0], flags); + const parts = []; + value.replace(regex, (g0) => parts.push(g0)); + value = parts.join(''); + } catch (e) { + return `${e}`; + } + } + return value; +} + function handlebarsRegisterHelpers() { if (Handlebars.partials !== Handlebars.templates) { Handlebars.partials = Handlebars.templates; @@ -88,6 +129,8 @@ function handlebarsRegisterHelpers() { Handlebars.registerHelper('kanjiLinks', handlebarsKanjiLinks); Handlebars.registerHelper('multiLine', handlebarsMultiLine); Handlebars.registerHelper('sanitizeCssClass', handlebarsSanitizeCssClass); + Handlebars.registerHelper('regexReplace', handlebarsRegexReplace); + Handlebars.registerHelper('regexMatch', handlebarsRegexMatch); } } -- cgit v1.2.3 From ef833d2bea07cd2e979dd558eaba141f880063cb Mon Sep 17 00:00:00 2001 From: toasted-nutbread Date: Mon, 25 Nov 2019 14:28:52 -0500 Subject: Use const rather than let where possible --- ext/bg/js/handlebars.js | 2 +- ext/bg/js/search-query-parser.js | 2 +- ext/bg/js/search.js | 2 +- ext/fg/js/frontend.js | 4 ++-- ext/fg/js/source.js | 2 +- 5 files changed, 6 insertions(+), 6 deletions(-) (limited to 'ext/bg/js/handlebars.js') diff --git a/ext/bg/js/handlebars.js b/ext/bg/js/handlebars.js index d6307e1d..8f43cf9a 100644 --- a/ext/bg/js/handlebars.js +++ b/ext/bg/js/handlebars.js @@ -89,7 +89,7 @@ function handlebarsRegexReplace(...args) { let value = args[args.length - 1].fn(this); if (args.length >= 3) { try { - let flags = args.length > 3 ? args[2] : 'g'; + const flags = args.length > 3 ? args[2] : 'g'; const regex = new RegExp(args[0], flags); value = value.replace(regex, args[1]); } catch (e) { diff --git a/ext/bg/js/search-query-parser.js b/ext/bg/js/search-query-parser.js index 1a43347c..2bf24ce3 100644 --- a/ext/bg/js/search-query-parser.js +++ b/ext/bg/js/search-query-parser.js @@ -134,7 +134,7 @@ class QueryParser { }); } if (this.search.options.parsing.enableMecabParser) { - let mecabResults = await apiTextParseMecab(text, this.search.getOptionsContext()); + const mecabResults = await apiTextParseMecab(text, this.search.getOptionsContext()); for (const mecabDictName in mecabResults) { results.push({ name: `MeCab: ${mecabDictName}`, diff --git a/ext/bg/js/search.js b/ext/bg/js/search.js index 552b7a59..e5cdf272 100644 --- a/ext/bg/js/search.js +++ b/ext/bg/js/search.js @@ -356,7 +356,7 @@ class DisplaySearch extends Display { } static getSearchQueryFromLocation(url) { - let match = /^[^?#]*\?(?:[^&#]*&)?query=([^&#]*)/.exec(url); + const match = /^[^?#]*\?(?:[^&#]*&)?query=([^&#]*)/.exec(url); return match !== null ? decodeURIComponent(match[1]) : null; } } diff --git a/ext/fg/js/frontend.js b/ext/fg/js/frontend.js index 16302e82..8297f54d 100644 --- a/ext/fg/js/frontend.js +++ b/ext/fg/js/frontend.js @@ -447,8 +447,8 @@ class Frontend { } getIndexOfTouch(touchList, identifier) { - for (let i in touchList) { - let t = touchList[i]; + for (const i in touchList) { + const t = touchList[i]; if (t.identifier === identifier) { return i; } diff --git a/ext/fg/js/source.js b/ext/fg/js/source.js index 886093d7..e6b991c4 100644 --- a/ext/fg/js/source.js +++ b/ext/fg/js/source.js @@ -361,7 +361,7 @@ class TextSourceElement { let consumed = 0; let content = ''; - for (let currentChar of this.content || '') { + for (const currentChar of this.content || '') { if (consumed >= length) { break; } else if (!currentChar.match(IGNORE_TEXT_PATTERN)) { -- cgit v1.2.3