From ab56c2f841aa3b9cc96d87c2d61ba90eb3ef4308 Mon Sep 17 00:00:00 2001 From: Vyacheslav Egorov Date: Tue, 4 Feb 2020 09:16:52 +0000 Subject: [PATCH] [runtime/docs] Add custom lexer for shell sessions in runtime wiki. This was missing from the previous wiki related commit. TBR=aam@google.com Change-Id: Ib68ead3bab2841bb8e02cb93fd71bce7515ff335 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/134285 Reviewed-by: Vyacheslav Egorov --- .../custom_shell_session/__init__.py | 5 ++ .../custom_shell_session/lexer.py | 49 +++++++++++++++++++ .../CustomShellSessionPygmentsLexer/setup.py | 15 ++++++ runtime/tools/wiki/README.md | 9 +++- 4 files changed, 76 insertions(+), 2 deletions(-) create mode 100644 runtime/tools/wiki/CustomShellSessionPygmentsLexer/custom_shell_session/__init__.py create mode 100644 runtime/tools/wiki/CustomShellSessionPygmentsLexer/custom_shell_session/lexer.py create mode 100644 runtime/tools/wiki/CustomShellSessionPygmentsLexer/setup.py diff --git a/runtime/tools/wiki/CustomShellSessionPygmentsLexer/custom_shell_session/__init__.py b/runtime/tools/wiki/CustomShellSessionPygmentsLexer/custom_shell_session/__init__.py new file mode 100644 index 00000000000..58baf985feb --- /dev/null +++ b/runtime/tools/wiki/CustomShellSessionPygmentsLexer/custom_shell_session/__init__.py @@ -0,0 +1,5 @@ +# +# Copyright (c) 2020, the Dart project authors. Please see the AUTHORS file +# for details. All rights reserved. Use of this source code is governed by a +# BSD-style license that can be found in the LICENSE file. +# diff --git a/runtime/tools/wiki/CustomShellSessionPygmentsLexer/custom_shell_session/lexer.py b/runtime/tools/wiki/CustomShellSessionPygmentsLexer/custom_shell_session/lexer.py new file mode 100644 index 00000000000..3e1e6356dc8 --- /dev/null +++ b/runtime/tools/wiki/CustomShellSessionPygmentsLexer/custom_shell_session/lexer.py @@ -0,0 +1,49 @@ +# +# Copyright (c) 2020, the Dart project authors. Please see the AUTHORS file +# for details. All rights reserved. Use of this source code is governed by a +# BSD-style license that can be found in the LICENSE file. +# +"""Simple lexer for shell sessions. + +Highlights command lines (lines starting with $ prompt) and comments starting +with #. For example: + + # This is a comment + $ this-is-a-command + This is output of the command + + $ this-is-a-multiline-command with-some-arguments \ + and more arguments \ + and more arguments + And some output. + +""" + +from pygments.lexer import RegexLexer, words +from pygments.token import Comment, Generic, Keyword + +_comment_style = Comment +# Note: there is a slight inversion of styles to make it easier to read. +# We highlight output with Prompt style and command as a normal text. +_output_style = Generic.Prompt +_command_style = Generic.Text +_prompt_style = Keyword + + +class CustomShellSessionLexer(RegexLexer): + name = 'CustomShellSession' + aliases = ['custom-shell-session'] + filenames = ['*.log'] + tokens = { + 'root': [ + (r'#.*\n', _comment_style), + (r'^\$', _prompt_style, 'command'), + (r'.', _output_style), + ], + 'command': [ + (r'\\\n', _command_style), # Continue in 'command' state. + (r'$', _command_style, '#pop'), # End of line without escape. + (r'.', + _command_style), # Anything else continue in 'command' state. + ] + } diff --git a/runtime/tools/wiki/CustomShellSessionPygmentsLexer/setup.py b/runtime/tools/wiki/CustomShellSessionPygmentsLexer/setup.py new file mode 100644 index 00000000000..b00ade937fb --- /dev/null +++ b/runtime/tools/wiki/CustomShellSessionPygmentsLexer/setup.py @@ -0,0 +1,15 @@ +# +# Copyright (c) 2020, the Dart project authors. Please see the AUTHORS file +# for details. All rights reserved. Use of this source code is governed by a +# BSD-style license that can be found in the LICENSE file. +# +from setuptools import setup, find_packages + +setup( + name='custom-shell-session', + packages=find_packages(), + entry_points=""" + [pygments.lexers] + custom-shell-session = custom_shell_session.lexer:CustomShellSessionLexer + """, +) diff --git a/runtime/tools/wiki/README.md b/runtime/tools/wiki/README.md index a81d26ff854..2606ac8a4f2 100644 --- a/runtime/tools/wiki/README.md +++ b/runtime/tools/wiki/README.md @@ -32,6 +32,11 @@ the given C++ symbol. ```console $ pip3 install coloredlogs jinja2 markdown aiohttp watchdog pymdown-extensions pygments ``` -2. Install SASS compiler (make sure that SASS binary is in your path). -3. Generate `xref.json` file following instructions in +2. Install the custom pygments lexer we use for shell session examples: + ``` + $ cd runtime/tools/wiki/CustomShellSessionPygmentsLexer + $ python3 setup.py develop + ``` +3. Install SASS compiler (make sure that SASS binary is in your path). +4. Generate `xref.json` file following instructions in `xref_extractor/README.md`. \ No newline at end of file