From dba92d4c201bfabbcac87b4760a7bd3cf5afac7b Mon Sep 17 00:00:00 2001 From: Kevin Traini Date: Mon, 16 Sep 2024 14:00:17 +0200 Subject: [PATCH] feat: Add pygments lexer for TQL + Tuleap style It allow to have syntax highlight for TQL code blocks Block code are now highlighted with Tuleap style --- .gitignore | 3 +- .vscode/settings.json | 3 ++ languages/en/_pygments/lexer.py | 38 ++++++++++++++++++++++++++ languages/en/_pygments/tuleap_style.py | 22 +++++++++++++++ languages/en/conf.py | 7 ++++- languages/en/user-guide/tql.rst | 8 +++--- 6 files changed, 75 insertions(+), 6 deletions(-) create mode 100644 languages/en/_pygments/lexer.py create mode 100644 languages/en/_pygments/tuleap_style.py diff --git a/.gitignore b/.gitignore index 41e1cd2e..b69dbf18 100644 --- a/.gitignore +++ b/.gitignore @@ -3,4 +3,5 @@ nbproject .idea/ venv/ node_modules/ -languages/en/_themes/tuleap_org/static/assets/ \ No newline at end of file +languages/en/_themes/tuleap_org/static/assets/ +__pycache__ \ No newline at end of file diff --git a/.vscode/settings.json b/.vscode/settings.json index 29892003..6f3ba53f 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -3,5 +3,8 @@ "restructuredtext.preview.name": "docutils", "cSpell.words": [ "Tuleap" + ], + "python.analysis.extraPaths": [ + "./languages/en/_pygments" ] } \ No newline at end of file diff --git a/languages/en/_pygments/lexer.py b/languages/en/_pygments/lexer.py new file mode 100644 index 00000000..080fcc55 --- /dev/null +++ b/languages/en/_pygments/lexer.py @@ -0,0 +1,38 @@ +def setup(app): + return + +from pygments.lexer import RegexLexer +from pygments.token import * +import re + +class TqlLexer(RegexLexer): + """ + A lexer for TQL (Tuleap Query Language) + """ + name = 'TQL' + flags = re.MULTILINE + re.IGNORECASE + + tokens = { + 'root': [ + (r'\s+', Whitespace), + (r'\/{2}.*', Comment), + + (r"'(?:[^\\]|\\.)*?(?:'|$)", String), + (r'"(?:[^\\]|\\.)*?(?:"|$)', String), + + (r'/\d+[dwmy]/i', Number), + (r'\d+(?:\.\d+)?', Number), + + (r'linked\s*from\b', Name), + (r'(?:and|from|or|select|where)\b', Keyword), + (r'(?:artifact|between|by|child|children|covered|covering|from|in|is|linked|myself|not|now|open|parent|to|tracker|type|with|without)\b', Name), + + (r'[=<>!+-]+', Operator), + (r'[()]', Operator), + + (r'@[.\w-]+', Name.Constant), + (r'[.\w-]+', Literal), + + (r',', Punctuation) + ], + } \ No newline at end of file diff --git a/languages/en/_pygments/tuleap_style.py b/languages/en/_pygments/tuleap_style.py new file mode 100644 index 00000000..a83a26d7 --- /dev/null +++ b/languages/en/_pygments/tuleap_style.py @@ -0,0 +1,22 @@ +def setup(app): + return + +from pygments.style import Style +from pygments.token import * + +class TuleapStyle(Style): + styles = { + Token: '#282621', # typo-default-text-color + + Keyword: '#005f90', # class:'k' deep-blue-text + Name: '#6a14a7', # class:'n' plum-crazy-text + Name.Constant: '#945600', # class:'no' clockwork-orange-text + Name.Other: '#774a0a', # class:'nx' teddy-brown-text + String: '#137900', # class:'s' neon-green-text + Number: '#bf4747', # class:'m' coral-pink-text + Literal: '#525252', # class:'l' inca-silver-text + + Comment: '#717171', # class:'c' dimmed-color + Comment.Preproc: '#1b805e', # class:'cp' surf-green-text + Punctuation: '#717171', # class:'p' dimmed-color + } \ No newline at end of file diff --git a/languages/en/conf.py b/languages/en/conf.py index 3fa34d09..070e2773 100644 --- a/languages/en/conf.py +++ b/languages/en/conf.py @@ -17,6 +17,7 @@ # add these directories to sys.path here. If the directory is relative to the # documentation root, use os.path.abspath to make it absolute, like shown here. #sys.path.insert(0, os.path.abspath('.')) +sys.path.append(os.path.abspath("./_pygments")) # -- General configuration ----------------------------------------------------- @@ -30,6 +31,8 @@ 'sphinx_rtd_theme', 'sphinxext.rediraffe', 'notfound.extension', + 'lexer', + 'tuleap_style', ] # Add any paths that contain templates here, relative to this directory. @@ -86,7 +89,7 @@ #show_authors = False # The name of the Pygments (syntax highlighting) style to use. -pygments_style = 'tango' +pygments_style = 'tuleap_style.TuleapStyle' # A list of ignored prefixes for module index sorting. #modindex_common_prefix = [] @@ -282,6 +285,8 @@ lexers['php-annotations'] = PhpLexer(startinline=True, linenos=1) from pygments.lexers.markup import MarkdownLexer lexers['markdown'] = MarkdownLexer(startinline=True, linenos=1) +from lexer import TqlLexer +lexers['tql'] = TqlLexer() # Redirections diff --git a/languages/en/user-guide/tql.rst b/languages/en/user-guide/tql.rst index 6c46116c..f02ebc0d 100644 --- a/languages/en/user-guide/tql.rst +++ b/languages/en/user-guide/tql.rst @@ -94,7 +94,7 @@ To construct a query you can combine all these elements. Query example: -.. code-block:: sql +.. code-block:: tql (summary = "soap" OR summary = "rest") AND description = "documentation" AND story_points BETWEEN(3, 8) @@ -261,7 +261,7 @@ Comparison values Example ------- -:: +.. code-block:: tql @title = 'documentation' AND @status = OPEN() AND @last_update_date > NOW() - 1w //Returns all open artifacts with 'documentation' in the title that have been @@ -284,7 +284,7 @@ Cross-tracker search widget is also available in expert mode allowing you to use In this extended syntax of TQL you can choose which fields you want to display on the widget through ``SELECT`` syntax, and also on which tracker to perform the query with ``FROM``: -:: +.. code-block:: tql SELECT @pretty_title, @status, open_date FROM @project = 'self' AND @tracker.name IN('release', 'sprint') WHERE @assigned_to = MYSELF() // Returns all artifacts from current project release and sprint trackers assigned to me and display their title, status and opening date. @@ -332,7 +332,7 @@ To provide both condition, you can use ``AND`` between them. There is no restric Some example you can take inspiration from: -:: +.. code-block:: tql SELECT @pretty_title, @status, @submitted_by, @last_update_date FROM @project.name = 'support' AND @tracker.name = 'ticket'