From 9c3fb60bc30faa290b8a7398b213627a80c5dc02 Mon Sep 17 00:00:00 2001 From: Kris Maglione Date: Sat, 1 Nov 2008 22:05:27 +0000 Subject: [PATCH] Some string highlighting stuff. --- content/style.js | 5 ++--- content/template.js | 41 ++++++++++++++++++++++++++++++++++------- 2 files changed, 36 insertions(+), 10 deletions(-) diff --git a/content/style.js b/content/style.js index 70dbbea5..aed2d770 100644 --- a/content/style.js +++ b/content/style.js @@ -485,10 +485,9 @@ liberator.registerObserver("load_commands", function () ["padding: 0 1em 0 0; vertical-align: top", "text-align: center"], ([h.class, XXX, - template.maybeXML(h.value.replace(/\s+/g, "\u00a0") - .replace(/\b([-\w]+):/g, "$1:"))] + template.highlightRegexp(h.value, /\b[-\w]+(?=:)/g)] for (h in highlight) - if (!key || h.class.indexOf(key) > -1))); + if (!key || h.class.indexOf(key) > -1))); commandline.echo(str, commandline.HL_NORMAL, commandline.FORCE_MULTILINE); return; } diff --git a/content/template.js b/content/template.js index abea429c..e6b42d99 100644 --- a/content/template.js +++ b/content/template.js @@ -76,24 +76,51 @@ const template = { }, highlightFilter: function (str, filter) + { + if (typeof str == "xml") + return str; + + return this.highlightSubstrings(str, (function () + { + let lcstr = String.toLowerCase(str); + let lcfilter = filter.toLowerCase(); + let start = 0; + while ((start = lcstr.indexOf(lcfilter, start)) > -1) + { + yield [start, filter.length]; + start += filter.length; + } + })()); + }, + + highlightRegexp: function (str, re) + { + if (typeof str == "xml") + return str; + + return this.highlightSubstrings(str, (function () + { + while (res = re.exec(str)) + yield [res.index, res[0].length]; + })()); + }, + + highlightSubstrings: function (str, iter) { if (typeof str == "xml") return str; if (str == "") return <>{str}; - let lcstr = String(str).toLowerCase(); - let lcfilter = filter.toLowerCase(); + XML.ignoreWhitespace = false; str = String(str).replace(" ", "\u00a0"); let s = <>; let start = 0; - let i; - while ((i = lcstr.indexOf(lcfilter, start)) > -1) + for (let [i, length] in iter) { - XML.ignoreWhitespace = false; s += <>{str.substring(start, i)}; - s += {str.substr(i, filter.length)}; - start = i + filter.length; + s += {str.substr(i, length)}; + start = i + length; } return s + <>{str.substr(start)}; },