Skip to content

Commit

Permalink
fix: don't strip whitespace from code blocks
Browse files Browse the repository at this point in the history
Fixes #206.
  • Loading branch information
redimp committed Feb 17, 2025
1 parent 5f04b05 commit 680f74a
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 4 deletions.
8 changes: 4 additions & 4 deletions otterwiki/renderer.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,15 +60,15 @@ def wrap(self, source):

def pygments_render(code, lang, linenumbers=False):
try:
lexer = get_lexer_by_name(lang, stripall=True)
lexer = get_lexer_by_name(lang.strip(), stripall=False)
except ClassNotFound:
return (
'\n'
+ _pre_copy_to_clipboard_tag()
+ '%s\n%s</pre></div>\n'
% (
mistune.escape(lang.strip()),
mistune.escape(code.strip()),
mistune.escape(lang),
mistune.escape(code),
)
)
linenos = "table" if linenumbers else None
Expand Down Expand Up @@ -223,7 +223,7 @@ def block_code(self, code, info=None):
html = (
'\n'
+ _pre_copy_to_clipboard_tag()
+ '{}</pre></div>\n'.format(mistune.escape(code.strip()))
+ '{}</pre></div>\n'.format(mistune.escape(code))
)
return html
if cursormagicword in info:
Expand Down
50 changes: 50 additions & 0 deletions tests/test_renderer.py
Original file line number Diff line number Diff line change
Expand Up @@ -561,3 +561,53 @@ def test_render_python_doublebracket():
assert code
code = code.text
assert """df[['a', "b"]]""" in code


def test_indent_preformatted_issue206():
md = """# Preformatted
Hello :) This is at 5 spaces.
5 spaces here as well."""
html, _ = render.markdown(md)
pre = BeautifulSoup(html, "html.parser").find('pre')
assert pre
assert (
""" Hello :) This is at 5 spaces.
5 spaces here as well.\n"""
== pre.text
)
# backtick block
md = """# Code block
```
One leading space here
and two here.
```
"""
html, _ = render.markdown(md)
pre = BeautifulSoup(html, "html.parser").find('pre')
assert pre
assert (
""" One leading space here
and two here.\n"""
== pre.text
)

# backtick block with language
md = """# Code block
```c
int main(int argc, char **argv) {
}
```
"""
html, _ = render.markdown(md)
code = BeautifulSoup(html, "html.parser").find(
'div', {'class': 'highlight'}
)
assert code
assert (
""" int main(int argc, char **argv) {
}\n"""
== code.text
)

0 comments on commit 680f74a

Please sign in to comment.