Skip to content

Commit 943d576

Browse files
committed
allow rendering of text before/after a specific part
1 parent 5d00323 commit 943d576

File tree

14 files changed

+255
-90
lines changed

14 files changed

+255
-90
lines changed

README.md

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -73,12 +73,6 @@ Configure the tutorial that is being displayed - this will not show any output:
7373

7474
## TODO
7575

76-
* Running commands:
77-
78-
* Clear env
79-
* Add env (value is template)
80-
81-
* Pre/post text for each part
8276
* Test file existence or something like that
8377
* Platform independent "echo" step (useful for debugging/testing)
8478
* Run in vagrant

structured_tutorials/models/base.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,13 @@ class ConfigurationMixin:
6767
update_context: dict[str, Any] = Field(default_factory=dict)
6868

6969

70+
class DocumentationConfigurationMixin:
71+
"""Mixin for documentation configuration models."""
72+
73+
text_before: str = Field(default="", description="Text before documenting this part.")
74+
text_after: str = Field(default="", description="Text after documenting this part.")
75+
76+
7077
class FileMixin:
7178
"""Mixin for specifying a file (used in file part and for stdin of commands)."""
7279

structured_tutorials/models/parts.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
CommandBaseModel,
1515
CommandType,
1616
ConfigurationMixin,
17+
DocumentationConfigurationMixin,
1718
FileMixin,
1819
template_field_title_generator,
1920
)
@@ -100,7 +101,7 @@ class CommandsRuntimeConfigurationModel(ConfigurationMixin, BaseModel):
100101
model_config = ConfigDict(extra="forbid")
101102

102103

103-
class CommandsDocumentationConfigurationModel(ConfigurationMixin, BaseModel):
104+
class CommandsDocumentationConfigurationModel(ConfigurationMixin, DocumentationConfigurationMixin, BaseModel):
104105
"""Documentation configuration for an entire commands part."""
105106

106107
model_config = ConfigDict(extra="forbid")
@@ -124,7 +125,7 @@ class FileRuntimeConfigurationModel(ConfigurationMixin, BaseModel):
124125
model_config = ConfigDict(extra="forbid", title="File part runtime configuration")
125126

126127

127-
class FileDocumentationConfigurationModel(ConfigurationMixin, BaseModel):
128+
class FileDocumentationConfigurationModel(ConfigurationMixin, DocumentationConfigurationMixin, BaseModel):
128129
"""Configure a file part when rendering it as documentation.
129130
130131
For the `language`, `caption`, `linenos`, `lineno_start`, `emphasize_lines` and `name` options, please
@@ -203,7 +204,9 @@ class AlternativeRuntimeConfigurationModel(ConfigurationMixin, BaseModel):
203204
model_config = ConfigDict(extra="forbid", title="File part runtime configuration")
204205

205206

206-
class AlternativeDocumentationConfigurationModel(ConfigurationMixin, BaseModel):
207+
class AlternativeDocumentationConfigurationModel(
208+
ConfigurationMixin, DocumentationConfigurationMixin, BaseModel
209+
):
207210
"""Configure an alternative part when documenting the tutorial."""
208211

209212
model_config = ConfigDict(extra="forbid", title="File part runtime configuration")

structured_tutorials/sphinx/utils.py

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -114,11 +114,15 @@ def render_code_block(self, part: CommandsPartModel) -> str:
114114
# Update the context from update_context
115115
self.context.update(command_config.doc.update_context)
116116

117-
template = """.. code-block:: console
118-
119-
{% for cmd in commands %}{{ cmd|indent(4, first=True) }}
120-
{% endfor %}"""
121-
return self.env.from_string(template).render({"commands": commands})
117+
template_str = TEMPLATE_DIR.joinpath("commands_part.rst.template").read_text("utf-8")
118+
template = self.env.from_string(template_str)
119+
return template.render(
120+
{
121+
"commands": commands,
122+
"text_after": self.render(part.doc.text_after),
123+
"text_before": self.render(part.doc.text_before),
124+
}
125+
)
122126

123127
def render_file(self, part: FilePartModel) -> str:
124128
content = part.contents
@@ -155,7 +159,15 @@ def render_file(self, part: FilePartModel) -> str:
155159

156160
# Render template
157161
template = self.env.from_string(template_str)
158-
value = template.render({"part": part, "content": content, "caption": caption})
162+
value = template.render(
163+
{
164+
"part": part,
165+
"content": content,
166+
"caption": caption,
167+
"text_after": self.render(part.doc.text_after),
168+
"text_before": self.render(part.doc.text_before),
169+
}
170+
)
159171
return value
160172

161173
def render_alternatives(self, part: AlternativeModel) -> str:
@@ -175,7 +187,14 @@ def render_alternatives(self, part: AlternativeModel) -> str:
175187

176188
# Render template
177189
template = self.env.from_string(template_str)
178-
value = template.render({"part": part, "tabs": tabs})
190+
value = template.render(
191+
{
192+
"part": part,
193+
"tabs": tabs,
194+
"text_after": self.render(part.doc.text_after),
195+
"text_before": self.render(part.doc.text_before),
196+
}
197+
)
179198
return value.strip()
180199

181200
def render_part(self, part_id: str | None = None) -> str:
Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
1+
{% if text_before %}{{ text_before }}
2+
3+
{% endif -%}
14
{% for title, contents in tabs %}
25
.. tab:: {{ title }}
36

47
{{ contents|indent() }}
5-
{% endfor %}
8+
{% endfor %}
9+
{% if text_after %}{{ text_after }}{% endif %}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{% if text_before %}{{ text_before }}
2+
3+
{% endif -%}
4+
.. code-block:: console
5+
6+
{% for cmd in commands %}{{ cmd|indent(4, first=True) }}
7+
{% endfor %}{% if text_after %}
8+
9+
{{ text_after }}{% endif %}
Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
1+
{% if text_before %}{{ text_before }}
2+
3+
{% endif -%}
14
.. code-block::{% if part.doc.language %} {{ part.doc.language }}{% endif %}{% if caption %}
25
:caption: {{ caption }}{% endif %}{% if part.doc.linenos or part.doc.lineno_start %}
36
:linenos:{% endif %}{% if part.doc.lineno_start %}
47
:lineno-start: {{ part.doc.lineno_start }}{% endif %}{% if part.doc.emphasize_lines %}
58
:emphasize-lines: {{ part.doc.emphasize_lines }}{% endif %}{% if part.doc.name %}
69
:name: {{ part.doc.name }}{% endif %}
710

8-
{{ content|indent() }}
11+
{{ content|indent() }}{% if text_after %}
12+
13+
{{ text_after }}{% endif %}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# alternatives with text before/after
2+
configuration:
3+
context:
4+
key: value
5+
parts:
6+
- alternatives:
7+
foo:
8+
commands:
9+
- command: ls foo
10+
bar:
11+
commands:
12+
- command: ls bar
13+
doc:
14+
text_after: "after: {{ key }}"
15+
text_before: "before: {{ key }}"
16+
- alternatives:
17+
foo:
18+
commands:
19+
- command: ls foo
20+
doc:
21+
text_after: "foo-after: {{ key }}"
22+
text_before: "foo-before: {{ key }}"
23+
24+
bar:
25+
commands:
26+
- command: ls bar
27+
doc:
28+
text_after: "foo-after: {{ key }}"
29+
text_before: "foo-before: {{ key }}"
30+
doc:
31+
text_after: "after2: {{ key }}"
32+
text_before: "before2: {{ key }}"
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# commands with text before/after
2+
configuration:
3+
context:
4+
key: value
5+
parts:
6+
- commands:
7+
- command: ls
8+
doc:
9+
text_after: "after: {{ key }}"
10+
text_before: "before: {{ key }}"
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# file with text before/after
2+
configuration:
3+
context:
4+
key: value
5+
parts:
6+
- contents: foo
7+
destination: "test.txt"
8+
doc:
9+
text_after: "after: {{ key }}"
10+
text_before: "before: {{ key }}"

0 commit comments

Comments
 (0)