Skip to content

Commit 79a301e

Browse files
authored
✨ Allow for use of the line-block directive (#900)
The `line-block` directive constructs a block where all line breaks (and also initial indentations) are respected. This was not previously supported, due to a missing `MockState` method
1 parent dfaf6d7 commit 79a301e

File tree

3 files changed

+55
-35
lines changed

3 files changed

+55
-35
lines changed

docs/syntax/typography.md

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,11 +87,24 @@ A paragraph with a span of [text with attributes]{.bg-warning}
8787
To put a line break, without a paragraph, use a `\` followed by a new line. This corresponds to a `<br>` in HTML and `\\` in LaTeX.
8888

8989
:::{myst-example}
90-
Fleas \
90+
**Fleas** \
9191
Adam \
9292
Had 'em.
9393
:::
9494

95+
```{versionadded} 3.0
96+
```
97+
98+
Alternatively, you can use the `line-block` directive, which constructs a block where all line breaks (and also initial indentations) are respected:
99+
100+
::::{myst-example}
101+
:::{line-block}
102+
**Fleas**
103+
Adam
104+
Had 'em.
105+
:::
106+
::::
107+
95108
## Bullet points and numbered lists
96109

97110
You can use bullet points and numbered lists as you would in standard Markdown.

myst_parser/mocking.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,35 @@ def build_table(self, tabledata, tableline, stub_columns: int = 0, widths=None):
263263
def build_table_row(self, rowdata, tableline):
264264
return Body.build_table_row(self, rowdata, tableline)
265265

266+
def nest_line_block_lines(self, block: nodes.line_block):
267+
"""Modify the line block element in-place, to nest line block segments.
268+
269+
Line nodes are placed into child line block containers, based on their indentation.
270+
"""
271+
for index in range(1, len(block)):
272+
if getattr(block[index], "indent", None) is None:
273+
block[index].indent = block[index - 1].indent
274+
self._nest_line_block_segment(block)
275+
276+
def _nest_line_block_segment(self, block: nodes.line_block):
277+
indents = [item.indent for item in block]
278+
least = min(indents)
279+
new_items = []
280+
new_block = nodes.line_block()
281+
for item in block:
282+
if item.indent > least:
283+
new_block.append(item)
284+
else:
285+
if len(new_block):
286+
self._nest_line_block_segment(new_block)
287+
new_items.append(new_block)
288+
new_block = nodes.line_block()
289+
new_items.append(item)
290+
if len(new_block):
291+
self._nest_line_block_segment(new_block)
292+
new_items.append(new_block)
293+
block[:] = new_items
294+
266295
def __getattr__(self, name: str):
267296
"""This method is only be called if the attribute requested has not
268297
been defined. Defined attributes will not be overridden.

tests/test_renderers/fixtures/docutil_directives.md

Lines changed: 12 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
--------------------------------
21
[attention] (`docutils.parsers.rst.directives.admonitions.Attention`):
32
.
43
```{attention}
@@ -12,7 +11,6 @@ a
1211
a
1312
.
1413

15-
--------------------------------
1614
[caution] (`docutils.parsers.rst.directives.admonitions.Caution`):
1715
.
1816
```{caution}
@@ -26,7 +24,6 @@ a
2624
a
2725
.
2826

29-
--------------------------------
3027
[danger] (`docutils.parsers.rst.directives.admonitions.Danger`):
3128
.
3229
```{danger}
@@ -40,7 +37,6 @@ a
4037
a
4138
.
4239

43-
--------------------------------
4440
[error] (`docutils.parsers.rst.directives.admonitions.Error`):
4541
.
4642
```{error}
@@ -54,7 +50,6 @@ a
5450
a
5551
.
5652

57-
--------------------------------
5853
[important] (`docutils.parsers.rst.directives.admonitions.Important`):
5954
.
6055
```{important}
@@ -68,7 +63,6 @@ a
6863
a
6964
.
7065

71-
--------------------------------
7266
[note] (`docutils.parsers.rst.directives.admonitions.Note`):
7367
.
7468
```{note}
@@ -82,7 +76,6 @@ a
8276
a
8377
.
8478

85-
--------------------------------
8679
[tip] (`docutils.parsers.rst.directives.admonitions.Tip`):
8780
.
8881
```{tip}
@@ -96,7 +89,6 @@ a
9689
a
9790
.
9891

99-
--------------------------------
10092
[hint] (`docutils.parsers.rst.directives.admonitions.Hint`):
10193
.
10294
```{hint}
@@ -110,7 +102,6 @@ a
110102
a
111103
.
112104

113-
--------------------------------
114105
[warning] (`docutils.parsers.rst.directives.admonitions.Warning`):
115106
.
116107
```{warning}
@@ -124,7 +115,6 @@ a
124115
a
125116
.
126117

127-
--------------------------------
128118
[admonition] (`docutils.parsers.rst.directives.admonitions.Admonition`):
129119
.
130120
```{admonition} myclass
@@ -140,7 +130,6 @@ a
140130
a
141131
.
142132

143-
--------------------------------
144133
[sidebar] (`docutils.parsers.rst.directives.body.Sidebar`):
145134
.
146135
```{sidebar} sidebar title
@@ -156,7 +145,6 @@ a
156145
a
157146
.
158147

159-
--------------------------------
160148
[topic] (`docutils.parsers.rst.directives.body.Topic`):
161149
.
162150
```{topic} Topic Title
@@ -172,18 +160,25 @@ a
172160
a
173161
.
174162

175-
--------------------------------
176-
[line-block] (`docutils.parsers.rst.directives.body.LineBlock`) SKIP: MockingError: MockState has not yet implemented attribute 'nest_line_block_lines'
163+
[line-block] (`docutils.parsers.rst.directives.body.LineBlock`)
177164
.
178165
```{line-block}
179-
180-
166+
line 1
167+
line 2
168+
line 3
181169
```
182170
.
183171
<document source="notset">
172+
<line_block>
173+
<line>
174+
line 1
175+
<line>
176+
line 2
177+
<line_block>
178+
<line>
179+
line 3
184180
.
185181

186-
--------------------------------
187182
[parsed-literal] (`docutils.parsers.rst.directives.body.ParsedLiteral`):
188183
.
189184
```{parsed-literal}
@@ -196,7 +191,6 @@ a
196191
a
197192
.
198193

199-
--------------------------------
200194
[rubric] (`docutils.parsers.rst.directives.body.Rubric`):
201195
.
202196
```{rubric} Rubric Title
@@ -207,7 +201,6 @@ a
207201
Rubric Title
208202
.
209203

210-
--------------------------------
211204
[epigraph] (`docutils.parsers.rst.directives.body.Epigraph`):
212205
.
213206
```{epigraph}
@@ -225,7 +218,6 @@ a
225218
attribution
226219
.
227220

228-
--------------------------------
229221
[highlights] (`docutils.parsers.rst.directives.body.Highlights`):
230222
.
231223
```{highlights}
@@ -243,7 +235,6 @@ a
243235
attribution
244236
.
245237

246-
--------------------------------
247238
[pull-quote] (`docutils.parsers.rst.directives.body.PullQuote`):
248239
.
249240
```{pull-quote}
@@ -261,7 +252,6 @@ a
261252
attribution
262253
.
263254

264-
--------------------------------
265255
[compound] (`docutils.parsers.rst.directives.body.Compound`):
266256
.
267257
```{compound}
@@ -275,7 +265,6 @@ a
275265
a
276266
.
277267

278-
--------------------------------
279268
[container] (`docutils.parsers.rst.directives.body.Container`):
280269
.
281270
```{container}
@@ -289,7 +278,6 @@ a
289278
a
290279
.
291280

292-
--------------------------------
293281
[image] (`docutils.parsers.rst.directives.images.Image`):
294282
.
295283
```{image} path/to/image
@@ -301,7 +289,6 @@ a
301289
<image alt="abc" ids="name" names="name" uri="path/to/image">
302290
.
303291

304-
--------------------------------
305292
[raw] (`docutils.parsers.rst.directives.misc.Raw`):
306293
.
307294
```{raw} raw
@@ -314,7 +301,6 @@ a
314301
a
315302
.
316303

317-
--------------------------------
318304
[class] (`docutils.parsers.rst.directives.misc.Class`):
319305
.
320306
```{class} myclass
@@ -327,7 +313,6 @@ a
327313
a
328314
.
329315

330-
--------------------------------
331316
[role] (`docutils.parsers.rst.directives.misc.Role`) + raw (`docutils.parsers.rst.roles.raw_role`):
332317
.
333318
```{role} raw-latex(raw)
@@ -342,7 +327,6 @@ a
342327
\tag{content}
343328
.
344329

345-
--------------------------------
346330
[title] (`docutils.parsers.rst.directives.misc.Title`):
347331
.
348332
```{title} title
@@ -351,7 +335,6 @@ a
351335
<document source="notset" title="title">
352336
.
353337

354-
--------------------------------
355338
[restructuredtext-test-directive] (`docutils.parsers.rst.directives.misc.TestDirective`):
356339
.
357340
```{restructuredtext-test-directive}
@@ -363,7 +346,6 @@ a
363346
Directive processed. Type="restructuredtext-test-directive", arguments=[], options={}, content: None
364347
.
365348

366-
--------------------------------
367349
[contents] (`docutils.parsers.rst.directives.parts.Contents`):
368350
.
369351
```{contents} Contents
@@ -379,7 +361,6 @@ a
379361
.details:
380362
.
381363

382-
--------------------------------
383364
[sectnum] (`docutils.parsers.rst.directives.parts.Sectnum`):
384365
.
385366
```{sectnum}
@@ -392,7 +373,6 @@ a
392373
.details:
393374
.
394375

395-
--------------------------------
396376
[header] (`docutils.parsers.rst.directives.parts.Header`):
397377
.
398378
```{header}
@@ -407,7 +387,6 @@ a
407387
a
408388
.
409389

410-
--------------------------------
411390
[footer] (`docutils.parsers.rst.directives.parts.Footer`):
412391
.
413392
```{footer}
@@ -422,7 +401,6 @@ a
422401
a
423402
.
424403

425-
--------------------------------
426404
[target-notes] (`docutils.parsers.rst.directives.references.TargetNotes`):
427405
.
428406
```{target-notes}

0 commit comments

Comments
 (0)