From c3953bf6ab2ba6b3ae725b87aea753af57eaec33 Mon Sep 17 00:00:00 2001 From: jack1142 <6032823+jack1142@users.noreply.github.com> Date: Thu, 18 Mar 2021 22:35:53 +0100 Subject: [PATCH 1/5] Allow to configure the prompt using options --- sphinx-prompt/__init__.py | 43 ++++++++++++++++++++++++++------------- 1 file changed, 29 insertions(+), 14 deletions(-) diff --git a/sphinx-prompt/__init__.py b/sphinx-prompt/__init__.py index ecce3a6..d2e7e05 100644 --- a/sphinx-prompt/__init__.py +++ b/sphinx-prompt/__init__.py @@ -4,6 +4,7 @@ from docutils import nodes from docutils.parsers import rst +from docutils.parsers.rst import directives from pygments import highlight from pygments.formatters import HtmlFormatter from pygments.lexers import BashLexer, BatchLexer, PowerShellLexer, PythonLexer, ScalaLexer, TextLexer @@ -37,11 +38,21 @@ def get_prompt_class(self, prompt): cache = PromptCache() +PROMPTS = { + "bash": "$", + "batch": r"C:\\>", + "powershell": r"PS C:\\>", +} class PromptDirective(rst.Directive): optional_arguments = 3 + option_spec = { + "language": directives.unchanged_required, + "prompts": directives.unchanged_required, + "modifiers": directives.unchanged_required, + } has_content = True def run(self): @@ -51,20 +62,24 @@ def run(self): prompt = None modifiers = [] - if self.arguments: - language = self.arguments[0] - if len(self.arguments) > 1: - prompt = self.arguments[1] - elif language == "bash": - prompt = "$" - elif language == "batch": - prompt = r"C:\\>" - elif language == "powershell": - prompt = r"PS C:\\>" - if len(self.arguments) > 2: - modifiers = self.arguments[2].split(",") - if "auto" in modifiers: - prompts = prompt.split(",") + arg_count = len(self.arguments) + + for idx, option_name in enumerate(("language", "prompts", "modifiers")): + if arg_count > idx: + if self.options.get(option_name): + self.warning( + "{0} is already passed as an option, ignoring the value passed" + " as positional argument and all arguments that come after it.".format(option_name) + ) + break + else: + self.options[option_name] = self.arguments[idx] + + language = self.options.get("language") or "text" + prompt = self.options.get("prompts") or PROMPTS.get(language, "") + modifiers = self.options.get("modifiers", "").split(",") + if "auto" in modifiers: + prompts = prompt.split(",") html = '
'
styles = ""
From 9e76fca40072fc70092cac589944ca803b327be3 Mon Sep 17 00:00:00 2001
From: jack1142 <6032823+jack1142@users.noreply.github.com>
Date: Thu, 18 Mar 2021 21:43:52 +0100
Subject: [PATCH 2/5] Put lexer classes into a mapping
---
sphinx-prompt/__init__.py | 19 ++++++++-----------
1 file changed, 8 insertions(+), 11 deletions(-)
diff --git a/sphinx-prompt/__init__.py b/sphinx-prompt/__init__.py
index d2e7e05..4c8516c 100644
--- a/sphinx-prompt/__init__.py
+++ b/sphinx-prompt/__init__.py
@@ -43,6 +43,13 @@ def get_prompt_class(self, prompt):
"batch": r"C:\\>",
"powershell": r"PS C:\\>",
}
+LEXERS = {
+ "bash": BashLexer,
+ "batch": BatchLexer,
+ "powershell": PowerShellLexer,
+ "python": PythonLexer,
+ "scala": ScalaLexer,
+}
class PromptDirective(rst.Directive):
@@ -93,17 +100,7 @@ def run(self):
html += '"
latex = "\\begin{Verbatim}[commandchars=\\\\\\{\\}]"
- Lexer = TextLexer
- if language == "bash":
- Lexer = BashLexer
- elif language == "batch":
- Lexer = BatchLexer
- elif language == "powershell":
- Lexer = PowerShellLexer
- elif language == "python":
- Lexer = PythonLexer
- elif language == "scala":
- Lexer = ScalaLexer
+ Lexer = LEXERS.get(language, TextLexer)
statement = []
if "auto" in modifiers:
From fd7156ac04aa9799b4aec116ec6f52ecea72326b Mon Sep 17 00:00:00 2001
From: jack1142 <6032823+jack1142@users.noreply.github.com>
Date: Thu, 18 Mar 2021 21:44:14 +0100
Subject: [PATCH 3/5] Fix incorrect line continuation handling for Batch and
PowerShell
---
sphinx-prompt/__init__.py | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/sphinx-prompt/__init__.py b/sphinx-prompt/__init__.py
index 4c8516c..d68ea68 100644
--- a/sphinx-prompt/__init__.py
+++ b/sphinx-prompt/__init__.py
@@ -129,7 +129,7 @@ def run(self):
prompt_class,
highlight("\n".join(statement), Lexer(), HtmlFormatter(nowrap=True)).strip("\r\n"),
)
- elif language in ["bash", "batch", "powershell", "python"]:
+ elif language in ["bash", "python"]:
for line in self.content:
statement.append(line)
if len(line) == 0 or not line[-1] == "\\":
From 9c2b703c3fe6fac5fe2ac9f091863fb56a82ec41 Mon Sep 17 00:00:00 2001
From: jack1142 <6032823+jack1142@users.noreply.github.com>
Date: Thu, 18 Mar 2021 22:11:32 +0100
Subject: [PATCH 4/5] Update README with new options
---
README.rst | 39 ++++++++++++++++++++++++++++++++++++++-
1 file changed, 38 insertions(+), 1 deletion(-)
diff --git a/README.rst b/README.rst
index 7cdf35a..a233454 100644
--- a/README.rst
+++ b/README.rst
@@ -11,12 +11,48 @@ In ``conf.py`` add ``extensions += ['sphinx-prompt']``.
Syntax
------
+A default prompt can be created using a ``prompt`` directive:
+
.. code::
- .. prompt:: [ [ []]]
+ .. prompt::
+The prompt can be further customized in one of two ways:
+
+- Using positional arguments:
+
+ .. code::
+
+ .. prompt:: [ [ []]]
+
+
+
+- Using options:
+
+ .. code::
+
+ .. prompt::
+ :language:
+ :prompts:
+ :modifier:
+
+
+
+While these constructs generate the same output, the positional arguments cannot be used
+if you want to use a prompt that contains spaces. This is a limitation of reStructuredText.
+
+Positional arguments can be mixed with options **if** they don't overlap
+(so if you pass prompts using options, you can only pass the language using positional arguments):
+
+.. code::
+
+ .. prompt:: bash
+ :prompts: (cool_project) $
+
+ python -m pip install -U sphinx-prompt
+
Language
~~~~~~~~
@@ -38,6 +74,7 @@ Else the prompt to add on each statements, for Python and Bash language the end
``\`` is supported.
Defaults to empty, except for the shell languages listed below:
+
- ``bash`` - ``$``
- ``batch`` - ``C:\>``
- ``powershell`` - ``PS C:\>``
From 5bd57d2c26f5e4a8c53a617bf1744e45613461c1 Mon Sep 17 00:00:00 2001
From: jack1142 <6032823+jack1142@users.noreply.github.com>
Date: Thu, 18 Mar 2021 23:16:27 +0100
Subject: [PATCH 5/5] Rename `modifier` to `modifiers` to be consistent with
the implementation
---
README.rst | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/README.rst b/README.rst
index a233454..d632d53 100644
--- a/README.rst
+++ b/README.rst
@@ -25,7 +25,7 @@ The prompt can be further customized in one of two ways:
.. code::
- .. prompt:: [ [ []]]
+ .. prompt:: [ [ []]]
@@ -36,7 +36,7 @@ The prompt can be further customized in one of two ways:
.. prompt::
:language:
:prompts:
- :modifier:
+ :modifiers: