From 18a7fdb2ce1695e471071f7d635cb98de6080ee8 Mon Sep 17 00:00:00 2001 From: Christopher Trudeau Date: Sat, 29 May 2021 16:03:44 -0400 Subject: Add JavaScript Node.js Console Lexer --- pygments/lexers/javascript.py | 70 ++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 66 insertions(+), 4 deletions(-) (limited to 'pygments/lexers/javascript.py') diff --git a/pygments/lexers/javascript.py b/pygments/lexers/javascript.py index f04d42d4..1ea1f553 100644 --- a/pygments/lexers/javascript.py +++ b/pygments/lexers/javascript.py @@ -10,16 +10,17 @@ import re -from pygments.lexer import RegexLexer, include, bygroups, default, using, \ - this, words, combined +from pygments.lexer import RegexLexer, Lexer, include, bygroups, default, \ + using, this, words, combined, do_insertions from pygments.token import Text, Comment, Operator, Keyword, Name, String, \ - Number, Punctuation, Other + Number, Punctuation, Other, Generic from pygments.util import get_bool_opt import pygments.unistring as uni __all__ = ['JavascriptLexer', 'KalLexer', 'LiveScriptLexer', 'DartLexer', 'TypeScriptLexer', 'LassoLexer', 'ObjectiveJLexer', - 'CoffeeScriptLexer', 'MaskLexer', 'EarlGreyLexer', 'JuttleLexer'] + 'CoffeeScriptLexer', 'MaskLexer', 'EarlGreyLexer', 'JuttleLexer', + 'NodeConsoleLexer'] JS_IDENT_START = ('(?:[$_' + uni.combine('Lu', 'Ll', 'Lt', 'Lm', 'Lo', 'Nl') + ']|\\\\u[a-fA-F0-9]{4})') @@ -28,6 +29,7 @@ JS_IDENT_PART = ('(?:[$' + uni.combine('Lu', 'Ll', 'Lt', 'Lm', 'Lo', 'Nl', '\u200c\u200d]|\\\\u[a-fA-F0-9]{4})') JS_IDENT = JS_IDENT_START + '(?:' + JS_IDENT_PART + ')*' +line_re = re.compile('.*?\n') class JavascriptLexer(RegexLexer): """ @@ -1534,3 +1536,63 @@ class JuttleLexer(RegexLexer): ] } + + +class NodeConsoleLexer(Lexer): + """ + For parsing JavaScript within an interactive Node.js shell, such as: + + .. sourcecode:: nodejs + + > let a = 3 + undefined + > a + 3 + > let b = '4' + undefined + > b + '4' + > b == a + false + """ + name = 'JavaScript Node.js console session' + aliases = ['nodejs', 'nodecon'] + mimetypes = ['application/javascript', 'application/x-javascript', + 'text/x-javascript', 'text/javascript'] + + def get_tokens_unprocessed(self, text): + jslexer = JavascriptLexer(**self.options) + + curcode = '' + insertions = [] + + for match in line_re.finditer(text): + line = match.group() + if line.startswith('> '): + insertions.append((len(curcode), + [(0, Generic.Prompt, line[:2])])) + + curcode += line[2:] + elif line.startswith('...'): + # node does a nested ... thing depending on depth + code = line.lstrip('.') + lead = len(line) - len(code) + + insertions.append((len(curcode), + [(0, Generic.Prompt, line[:lead])])) + + curcode += code + else: + if curcode: + yield from do_insertions(insertions, + jslexer.get_tokens_unprocessed(curcode)) + + curcode = '' + insertions = [] + + yield from do_insertions([], + jslexer.get_tokens_unprocessed(line)) + + if curcode: + yield from do_insertions(insertions, + jslexer.get_tokens_unprocessed(curcode)) -- cgit v1.2.1 From fbdcfa8066f98d896c2a746aa4fc6d5869f7e8d2 Mon Sep 17 00:00:00 2001 From: Christopher Trudeau Date: Mon, 21 Jun 2021 16:25:58 -0400 Subject: Changes from code review: - renamed nodecon to nodejsrepl - removed bad mimetypes --- pygments/lexers/javascript.py | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) (limited to 'pygments/lexers/javascript.py') diff --git a/pygments/lexers/javascript.py b/pygments/lexers/javascript.py index 1ea1f553..501ccfe9 100644 --- a/pygments/lexers/javascript.py +++ b/pygments/lexers/javascript.py @@ -1540,9 +1540,9 @@ class JuttleLexer(RegexLexer): class NodeConsoleLexer(Lexer): """ - For parsing JavaScript within an interactive Node.js shell, such as: + For parsing within an interactive Node.js REPL, such as: - .. sourcecode:: nodejs + .. sourcecode:: nodejsrepl > let a = 3 undefined @@ -1554,11 +1554,12 @@ class NodeConsoleLexer(Lexer): '4' > b == a false + + .. versionadded: 2.10 """ - name = 'JavaScript Node.js console session' - aliases = ['nodejs', 'nodecon'] - mimetypes = ['application/javascript', 'application/x-javascript', - 'text/x-javascript', 'text/javascript'] + name = 'Node.js REPL console session' + aliases = ['nodejsrepl', ] + mimetypes = ['text/x-nodejsrepl', ] def get_tokens_unprocessed(self, text): jslexer = JavascriptLexer(**self.options) -- cgit v1.2.1