Skip to content
This repository has been archived by the owner on May 3, 2024. It is now read-only.

Commit

Permalink
Merge pull request #36 from sernst/fix-markdown-lines
Browse files Browse the repository at this point in the history
Markdown Enhancements

A markdown extension was added to attempt to make line breaking handling more explicit but was too aggressive and make every `\n` a line line-break. This removes that extension and restores more natural text flow in markdown rendering.

Add parameters to markdown rendering to allow for preserving line breaks and for adjusting font size.
  • Loading branch information
sernst committed Jun 24, 2018
2 parents 4ffc45c + b6a6e63 commit acb1af2
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 13 deletions.
39 changes: 28 additions & 11 deletions cauldron/render/texts.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,13 @@ def preformatted_text(source: str) -> str:
)


def markdown(source: str = None, source_path: str = None, **kwargs) -> dict:
def markdown(
source: str = None,
source_path: str = None,
preserve_lines: bool = False,
font_size: float = None,
**kwargs
) -> dict:
"""
Renders a markdown file with support for Jinja2 templating. Any keyword
arguments will be passed to Jinja2 for templating prior to rendering the
Expand All @@ -165,7 +171,15 @@ def markdown(source: str = None, source_path: str = None, **kwargs) -> dict:
:param source_path:
The path to a markdown file that should be rendered to HTML for
notebook display.
:param preserve_lines:
If True, all line breaks will be treated as hard breaks. Use this
for pre-formatted markdown text where newlines should be retained
during rendering.
:param font_size:
Specifies a relative font size adjustment. The default value is 1.0,
which preserves the inherited font size values. Set it to a value
below 1.0 for smaller font-size rendering and greater than 1.0 for
larger font size rendering.
:return:
The HTML results of rendering the specified markdown string or file.
"""
Expand Down Expand Up @@ -223,16 +237,19 @@ def markdown(source: str = None, source_path: str = None, **kwargs) -> dict:

offset = end_index

body = templating.render(
"""
<div class="textbox markdown">{{ text }}</div>
""",
extensions = [
'markdown.extensions.extra',
'markdown.extensions.admonition',
'markdown.extensions.sane_lists',
'markdown.extensions.nl2br' if preserve_lines else None
]

body = templating.render_template(
'markdown-block.html',
text=md.markdown(rendered, extensions=[
'markdown.extensions.nl2br',
'markdown.extensions.extra',
'markdown.extensions.admonition',
'markdown.extensions.sane_lists'
])
e for e in extensions if e is not None
]),
font_size=font_size
)

pattern = re.compile('src="(?P<url>[^"]+)"')
Expand Down
6 changes: 6 additions & 0 deletions cauldron/resources/templates/markdown-block.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<div
class="textbox markdown cd-Markdown"
style="{{ 'font-size:{}em'.format(font_size) if font_size else '' }}"
>
{{ text }}
</div>
19 changes: 18 additions & 1 deletion cauldron/session/display/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,13 @@ def text(value: str, preformatted: bool = False):
)


def markdown(source: str = None, source_path: str = None, **kwargs):
def markdown(
source: str = None,
source_path: str = None,
preserve_lines: bool = False,
font_size: float = None,
**kwargs
):
"""
Renders the specified source string or source file using markdown and
adds the resulting HTML to the notebook display.
Expand All @@ -82,6 +88,15 @@ def markdown(source: str = None, source_path: str = None, **kwargs):
A markdown formatted string.
:param source_path:
A file containing markdown text.
:param preserve_lines:
If True, all line breaks will be treated as hard breaks. Use this
for pre-formatted markdown text where newlines should be retained
during rendering.
:param font_size:
Specifies a relative font size adjustment. The default value is 1.0,
which preserves the inherited font size values. Set it to a value
below 1.0 for smaller font-size rendering and greater than 1.0 for
larger font size rendering.
:param kwargs:
Any variable replacements to make within the string using Jinja2
templating syntax.
Expand All @@ -91,6 +106,8 @@ def markdown(source: str = None, source_path: str = None, **kwargs):
result = render_texts.markdown(
source=source,
source_path=source_path,
preserve_lines=preserve_lines,
font_size=font_size,
**kwargs
)
r.library_includes += result['library_includes']
Expand Down
2 changes: 1 addition & 1 deletion cauldron/settings.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"version": "0.3.8",
"version": "0.3.9",
"notebookVersion": "v1"
}

0 comments on commit acb1af2

Please sign in to comment.