From 679fe7bbefe75b8e360e4d1ba8709c69e9e4da94 Mon Sep 17 00:00:00 2001 From: Dougal Matthews Date: Wed, 5 Apr 2017 09:48:50 +0100 Subject: [PATCH] Add a Mistral lexer for pygments This allows us to use the following code blocks in the Mistral documentation. .. code-block:: mistral When we do that the code will then be highlighted in a way specific to Mistral, rather than the generic YAML highlighting or none. Change-Id: Ie25725b9f803e247d23f58c4b602be938c75cfbd --- doc/source/main_features.rst | 4 +-- doc/source/quickstart.rst | 2 +- mistral/ext/__init__.py | 0 mistral/ext/pygmentplugin.py | 57 ++++++++++++++++++++++++++++++++++++ setup.cfg | 3 ++ 5 files changed, 63 insertions(+), 3 deletions(-) create mode 100644 mistral/ext/__init__.py create mode 100644 mistral/ext/pygmentplugin.py diff --git a/doc/source/main_features.rst b/doc/source/main_features.rst index c3bfb1f40..46f3f8507 100644 --- a/doc/source/main_features.rst +++ b/doc/source/main_features.rst @@ -7,7 +7,7 @@ Task result / Data flow Mistral supports transferring data from one task to another. In other words, if *taskA* produces a value then *taskB* which follows *taskA* can use it. -In order to use this data Mistral relies on a query language called `YAQL `_. +In order to use this data Mistral relies on a query language called `YAQL `_. YAQL is a powerful yet simple tool that allows the user to filter information, transform data and call functions. Find more information about it in the `YAQL official documentation `_ . This mechanism @@ -17,7 +17,7 @@ referred to as Data Flow. Below is a simple example of how Mistral Data Flow looks like from the DSL (workflow language) perspective: -.. code-block:: yaml +.. code-block:: mistral version: '2.0' diff --git a/doc/source/quickstart.rst b/doc/source/quickstart.rst index 88cf77bda..dde38b699 100644 --- a/doc/source/quickstart.rst +++ b/doc/source/quickstart.rst @@ -37,7 +37,7 @@ Write a workflow For example, we have the following workflow. -.. code-block:: yaml +.. code-block:: mistral --- version: "2.0" diff --git a/mistral/ext/__init__.py b/mistral/ext/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/mistral/ext/pygmentplugin.py b/mistral/ext/pygmentplugin.py new file mode 100644 index 000000000..f4da6f06a --- /dev/null +++ b/mistral/ext/pygmentplugin.py @@ -0,0 +1,57 @@ +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +from pygments import lexer +from pygments import token + + +class MistralLexer(lexer.RegexLexer): + + name = 'Mistral' + aliases = ['mistral'] + + tokens = { + "root": [ + (r'^(\s)*(workflows|tasks|input|type)(\s)*:', token.Keyword), + (r'^(\s)*(version|name|description)(\s)*:', token.Keyword), + (r'^(\s)*(publish|timeout|retry|with\-items)(\s)*:', + token.Keyword), + (r'^(\s)*(on\-success|on\-error|on\-complete)(\s)*:', + token.Keyword), + (r'^(\s)*(action|workflow)(\s)*:', token.Keyword, 'call'), + (r'(\-|\:)(\s)*(fail|succeed|pause)(\s)+', token.Operator.Word), + (r'<%', token.Name.Entity, 'expression'), + (r'\{\{', token.Name.Entity, 'expression'), + (r'#.*$', token.Comment), + (r'(^|\s|\-)+\d+', token.Number), + lexer.include("generic"), + ], + "expression": [ + (r'\$', token.Operator), + (r'\s(json_pp|task|tasks|execution|env|uuid)(?!\w)', + token.Name.Builtin), + lexer.include("generic"), + (r'%>', token.Name.Entity, '#pop'), + (r'}\\}', token.Name.Entity, '#pop'), + ], + "call": [ + (r'(\s)*[\w\.]+($|\s)', token.Name.Function), + lexer.default('#pop'), + ], + "generic": [ + (r'(\-|:|=|!|\[|\])', token.Operator), + (r'(null|None|True|False)', token.Name.Builtin), + (r'"(\\\\|\\"|[^"])*"', token.String.Double), + (r"'(\\\\|\\'|[^'])*'", token.String.Single), + (r'\w|\s|\(|\)|,|\.', token.Text), + ] + } diff --git a/setup.cfg b/setup.cfg index da8101944..9ee531c28 100644 --- a/setup.cfg +++ b/setup.cfg @@ -89,3 +89,6 @@ mistral.auth = kombu_driver.executors = blocking = futurist:SynchronousExecutor threading = futurist:ThreadPoolExecutor + +pygments.lexers = + mistral = mistral.ext.pygmentplugin:MistralLexer