Skip to content

Commit

Permalink
fix: render loose lists (#8)
Browse files Browse the repository at this point in the history
* fix: loose lists render

* fix test

* update README.md and bump version

* update: README.md

* update: README.md and preprocessor

* fix: loose list in blockquote

* remove unnecessary code
  • Loading branch information
TOsmanov authored Aug 28, 2024
1 parent a93f2da commit c23d1ad
Show file tree
Hide file tree
Showing 6 changed files with 175 additions and 13 deletions.
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,17 @@

# EscapeCode and UnescapeCode

> **Warning**
>
> Starting from version `1.0.5`, the preprocessor uses the [marko parser](https://github.com/frostming/marko/tree/6ee598746bc9a76e62a158c5aa5226a3d65c0864).
> This is necessary to more accurately identify code blocks nested in other markdown elements. But using the parser imposes the following restrictions:
> - the indent of the list items will be converted to 2 spaces after processing by the preprocessor.
>
> If your documentation does not use deep nesting of markdown elements, you may want to use version `1.0.4`, as it is more stable. For install version `1.0.4`, run:
> ```
> pip install foliantcontrib.escapecode==1.0.4
> ```
EscapeCode and UnescapeCode preprocessors work in pair.
EscapeCode finds in the source Markdown content the parts that should not be modified by any next preprocessors. Examples of content that should be left raw: fence code blocks, pre code blocks, inline code.
Expand Down
4 changes: 4 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# 1.0.8

- fix: render loose lists

# 1.0.7

- fix: the bug of incorrect selection of the frontmatter has been fixed
Expand Down
17 changes: 5 additions & 12 deletions foliant/preprocessors/escapecode.py
Original file line number Diff line number Diff line change
Expand Up @@ -434,6 +434,7 @@ def render_thematic_break(self, element: block.ThematicBreak) -> str:

def render_list(self, element: block.List) -> str:
result = []
sep = ""
if element.ordered:
for num, child in enumerate(element.children, element.start):
with self.container(f"{num}. ", " " * (len(str(num)) + 2)):
Expand All @@ -444,19 +445,11 @@ def render_list(self, element: block.List) -> str:
result.append(self.render(child))
self._prefix = self._second_prefix
for num, item in enumerate(result):
no_new_line = False
lines = item.split("\n")
for i, line in enumerate(lines):
if len(lines) <= 2:
no_new_line = True
if line.strip() == "":
lines[i] = ""
if no_new_line:
result[num] = "\n".join(lines)
else:
result[num] = "\n".join(lines) +"\n"

return "".join(result)
result[num] = "\n".join(lines)
if not element.tight:
sep = f"{self._prefix}\n"
return sep.join(result)

def render_html_block(self, element: block.HTMLBlock) -> str:
children = element.children
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
description=SHORT_DESCRIPTION,
long_description=LONG_DESCRIPTION,
long_description_content_type='text/markdown',
version='1.0.7',
version='1.0.8',
author='Artemy Lomov',
author_email='[email protected]',
url='https://github.com/foliant-docs/foliantcontrib.escapecode',
Expand Down
77 changes: 77 additions & 0 deletions tests/data/expected/inline_code.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,80 @@ quis nostrud exercitation `<escaped hash="49b4b9995319e31372cf09aff7885915"></es
consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse
cillum dolore eu fugiat nulla `<escaped hash="9aa1e24ead885028f329faa242d69012"></escaped>` pariatur. Excepteur sint occaecat cupidatat non
proident, sunt in culpa qui officia `<escaped hash="48e04d12f0f44333e6e80e00aec360ee"></escaped>` deserunt mollit anim id est laborum.

## List 1 (thing)

- `<escaped hash="0f618b27b0067206888dc852850ff192"></escaped>` – tempor incididunt ut labore et dolore magna aliqua `<escaped hash="49b4b9995319e31372cf09aff7885915"></escaped>` :
- `<escaped hash="ebb67a4271abe715344471b0f16321f6"></escaped>` – lorem ipsum `<escaped hash="17c3d4fb075d05370d828e0915b7335a"></escaped>` dolor sit amet, consectetur adipisicing;
- `<escaped hash="730bfa378b905ab806a3e0a03a170835"></escaped>` – aute irure dolor in reprehenderit in voluptate velit esse:
- `<escaped hash="e0ebc3c409070d07f1df0f2f4132509e"></escaped>` – enim ad minim veniam, quis nostrud exercitation;
- `<escaped hash="8529ac387c11f1f51d71758ccd492692"></escaped>` – aute irure dolor in reprehenderit in voluptate velit esse;
- `<escaped hash="8b5742f168cb41d71453f903bd423ea6"></escaped>` – velit esse cillum dolore eu fugiat nulla `<escaped hash="bdb40bfe98082570a65394f4f2049943"></escaped>`;
- `<escaped hash="28bd2a64abfe3383d3ce89d3348a48fc"></escaped>` – aute irure dolor in reprehenderit in voluptate velit esse.

## List 2 (loose)

- `<escaped hash="0f618b27b0067206888dc852850ff192"></escaped>` – tempor incididunt ut labore et dolore magna aliqua `<escaped hash="49b4b9995319e31372cf09aff7885915"></escaped>` :

- `<escaped hash="ebb67a4271abe715344471b0f16321f6"></escaped>` – lorem ipsum `<escaped hash="17c3d4fb075d05370d828e0915b7335a"></escaped>` dolor sit amet, consectetur adipisicing;

- `<escaped hash="730bfa378b905ab806a3e0a03a170835"></escaped>` – aute irure dolor in reprehenderit in voluptate velit esse:

- `<escaped hash="e0ebc3c409070d07f1df0f2f4132509e"></escaped>` – enim ad minim veniam, quis nostrud exercitation;

- `<escaped hash="8529ac387c11f1f51d71758ccd492692"></escaped>` – aute irure dolor in reprehenderit in voluptate velit esse;

- `<escaped hash="8b5742f168cb41d71453f903bd423ea6"></escaped>` – velit esse cillum dolore eu fugiat nulla `<escaped hash="bdb40bfe98082570a65394f4f2049943"></escaped>`;

- `<escaped hash="28bd2a64abfe3383d3ce89d3348a48fc"></escaped>` – aute irure dolor in reprehenderit in voluptate velit esse.

## List 3 in blockquote (thing)

> Test:
>
> - `<escaped hash="0f618b27b0067206888dc852850ff192"></escaped>` – tempor incididunt ut labore et dolore magna aliqua `<escaped hash="49b4b9995319e31372cf09aff7885915"></escaped>` :
> - `<escaped hash="ebb67a4271abe715344471b0f16321f6"></escaped>` – lorem ipsum `<escaped hash="17c3d4fb075d05370d828e0915b7335a"></escaped>` dolor sit amet, consectetur adipisicing;
> - `<escaped hash="730bfa378b905ab806a3e0a03a170835"></escaped>` – aute irure dolor in reprehenderit in voluptate velit esse:
> - `<escaped hash="e0ebc3c409070d07f1df0f2f4132509e"></escaped>` – enim ad minim veniam, quis nostrud exercitation;
> - `<escaped hash="8529ac387c11f1f51d71758ccd492692"></escaped>` – aute irure dolor in reprehenderit in voluptate velit esse;
> - `<escaped hash="8b5742f168cb41d71453f903bd423ea6"></escaped>` – velit esse cillum dolore eu fugiat nulla `<escaped hash="bdb40bfe98082570a65394f4f2049943"></escaped>`;
> - `<escaped hash="28bd2a64abfe3383d3ce89d3348a48fc"></escaped>` – aute irure dolor in reprehenderit in voluptate velit esse.
## List 4 in blockquote (half-loose)

> Test:
>
> - `<escaped hash="0f618b27b0067206888dc852850ff192"></escaped>` – tempor incididunt ut labore et dolore magna aliqua `<escaped hash="49b4b9995319e31372cf09aff7885915"></escaped>` :
>
> - `<escaped hash="ebb67a4271abe715344471b0f16321f6"></escaped>` – lorem ipsum `<escaped hash="17c3d4fb075d05370d828e0915b7335a"></escaped>` dolor sit amet, consectetur adipisicing;
>
> - `<escaped hash="730bfa378b905ab806a3e0a03a170835"></escaped>` – aute irure dolor in reprehenderit in voluptate velit esse:
>
> - `<escaped hash="e0ebc3c409070d07f1df0f2f4132509e"></escaped>` – enim ad minim veniam, quis nostrud exercitation;
>
> - `<escaped hash="8529ac387c11f1f51d71758ccd492692"></escaped>` – aute irure dolor in reprehenderit in voluptate velit esse;
>
> - `<escaped hash="8b5742f168cb41d71453f903bd423ea6"></escaped>` – velit esse cillum dolore eu fugiat nulla `<escaped hash="bdb40bfe98082570a65394f4f2049943"></escaped>`;
>
> - `<escaped hash="28bd2a64abfe3383d3ce89d3348a48fc"></escaped>` – aute irure dolor in reprehenderit in voluptate velit esse.
## List in the blockquote in the list

- test 1
> Test:
>
> - `<escaped hash="0f618b27b0067206888dc852850ff192"></escaped>` – tempor incididunt ut labore et dolore magna aliqua `<escaped hash="49b4b9995319e31372cf09aff7885915"></escaped>` :
>
> - `<escaped hash="ebb67a4271abe715344471b0f16321f6"></escaped>` – lorem ipsum `<escaped hash="17c3d4fb075d05370d828e0915b7335a"></escaped>` dolor sit amet, consectetur adipisicing;
>
> - `<escaped hash="730bfa378b905ab806a3e0a03a170835"></escaped>` – aute irure dolor in reprehenderit in voluptate velit esse:
>
> - `<escaped hash="e0ebc3c409070d07f1df0f2f4132509e"></escaped>` – enim ad minim veniam, quis nostrud exercitation;
>
> - `<escaped hash="8529ac387c11f1f51d71758ccd492692"></escaped>` – aute irure dolor in reprehenderit in voluptate velit esse;
>
> - `<escaped hash="8b5742f168cb41d71453f903bd423ea6"></escaped>` – velit esse cillum dolore eu fugiat nulla `<escaped hash="bdb40bfe98082570a65394f4f2049943"></escaped>`;
>
> - `<escaped hash="28bd2a64abfe3383d3ce89d3348a48fc"></escaped>` – aute irure dolor in reprehenderit in voluptate velit esse.
- test 2
77 changes: 77 additions & 0 deletions tests/data/input/inline_code.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,80 @@ quis nostrud exercitation `$var = 0` ullamco laboris nisi ut aliquip ex ea commo
consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse
cillum dolore eu fugiat nulla `func(start, end)` pariatur. Excepteur sint occaecat cupidatat non
proident, sunt in culpa qui officia `<tag>` deserunt mollit anim id est laborum.

## List 1 (thing)

- `cache_dir` – tempor incididunt ut labore et dolore magna aliqua `$var = 0` :
- `actions` – lorem ipsum `inline code` dolor sit amet, consectetur adipisicing;
- `normalize` – aute irure dolor in reprehenderit in voluptate velit esse:
- `escape` – enim ad minim veniam, quis nostrud exercitation;
- `fence_blocks` – aute irure dolor in reprehenderit in voluptate velit esse;
- `pre_blocks` – velit esse cillum dolore eu fugiat nulla `action = normalize`;
- `inline_code` – aute irure dolor in reprehenderit in voluptate velit esse.

## List 2 (loose)

- `cache_dir` – tempor incididunt ut labore et dolore magna aliqua `$var = 0` :

- `actions` – lorem ipsum `inline code` dolor sit amet, consectetur adipisicing;

- `normalize` – aute irure dolor in reprehenderit in voluptate velit esse:

- `escape` – enim ad minim veniam, quis nostrud exercitation;

- `fence_blocks` – aute irure dolor in reprehenderit in voluptate velit esse;

- `pre_blocks` – velit esse cillum dolore eu fugiat nulla `action = normalize`;

- `inline_code` – aute irure dolor in reprehenderit in voluptate velit esse.

## List 3 in blockquote (thing)

> Test:
>
> - `cache_dir` – tempor incididunt ut labore et dolore magna aliqua `$var = 0` :
> - `actions` – lorem ipsum `inline code` dolor sit amet, consectetur adipisicing;
> - `normalize` – aute irure dolor in reprehenderit in voluptate velit esse:
> - `escape` – enim ad minim veniam, quis nostrud exercitation;
> - `fence_blocks` – aute irure dolor in reprehenderit in voluptate velit esse;
> - `pre_blocks` – velit esse cillum dolore eu fugiat nulla `action = normalize`;
> - `inline_code` – aute irure dolor in reprehenderit in voluptate velit esse.
## List 4 in blockquote (half-loose)

> Test:
>
> - `cache_dir` – tempor incididunt ut labore et dolore magna aliqua `$var = 0` :
>
> - `actions` – lorem ipsum `inline code` dolor sit amet, consectetur adipisicing;
>
> - `normalize` – aute irure dolor in reprehenderit in voluptate velit esse:
>
> - `escape` – enim ad minim veniam, quis nostrud exercitation;
>
> - `fence_blocks` – aute irure dolor in reprehenderit in voluptate velit esse;
>
> - `pre_blocks` – velit esse cillum dolore eu fugiat nulla `action = normalize`;
>
> - `inline_code` – aute irure dolor in reprehenderit in voluptate velit esse.
## List in the blockquote in the list

- test 1
> Test:
>
> - `cache_dir` – tempor incididunt ut labore et dolore magna aliqua `$var = 0` :
>
> - `actions` – lorem ipsum `inline code` dolor sit amet, consectetur adipisicing;
>
> - `normalize` – aute irure dolor in reprehenderit in voluptate velit esse:
>
> - `escape` – enim ad minim veniam, quis nostrud exercitation;
>
> - `fence_blocks` – aute irure dolor in reprehenderit in voluptate velit esse;
>
> - `pre_blocks` – velit esse cillum dolore eu fugiat nulla `action = normalize`;
>
> - `inline_code` – aute irure dolor in reprehenderit in voluptate velit esse.
- test 2

0 comments on commit c23d1ad

Please sign in to comment.