Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Export markdown cells as block strings instead of comments #818

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions nbconvert/exporters/templateexporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
'markdown2latex': filters.markdown2latex,
'markdown2rst': filters.markdown2rst,
'comment_lines': filters.comment_lines,
'block_string_lines': filters.block_string_lines,
'strip_ansi': filters.strip_ansi,
'strip_dollars': filters.strip_dollars,
'strip_files_prefix': filters.strip_files_prefix,
Expand Down
51 changes: 34 additions & 17 deletions nbconvert/filters/strings.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
'strip_dollars',
'strip_files_prefix',
'comment_lines',
'block_string_lines',
'get_lines',
'ipython2python',
'posix_path',
Expand All @@ -43,10 +44,10 @@


def wrap_text(text, width=100):
"""
"""
Intelligently wrap text.
Wrap text without breaking words if possible.

Parameters
----------
text : str
Expand All @@ -63,7 +64,7 @@ def wrap_text(text, width=100):

def html2text(element):
"""extract inner text from html

Analog of jQuery's $(element).text()
"""
if isinstance(element, py3compat.string_types):
Expand All @@ -72,7 +73,7 @@ def html2text(element):
except Exception:
# failed to parse, just return it unmodified
return element

text = element.text or ""
for child in element:
text += html2text(child)
Expand All @@ -82,7 +83,7 @@ def html2text(element):

def _convert_header_id(header_contents):
"""Convert header contents to valid id value. Takes string as input, returns string.

Note: this may be subject to change in the case of changes to how we wish to generate ids.

For use on markdown headings.
Expand All @@ -91,7 +92,7 @@ def _convert_header_id(header_contents):

def add_anchor(html, anchor_link_text=u'¶'):
"""Add an id and an anchor-link to an html header

For use on markdown headings
"""
try:
Expand Down Expand Up @@ -120,11 +121,11 @@ def add_prompts(code, first='>>> ', cont='... '):
new_code.append(cont + line)
return '\n'.join(new_code)


def strip_dollars(text):
"""
Remove all dollar symbols from text

Parameters
----------
text : str
Expand All @@ -141,7 +142,7 @@ def strip_files_prefix(text):
"""
Fix all fake URLs that start with `files/`, stripping out the `files/` prefix.
Applies to both urls (for html) and relative paths (for markdown paths).

Parameters
----------
text : str
Expand All @@ -155,26 +156,42 @@ def strip_files_prefix(text):
def comment_lines(text, prefix='# '):
"""
Build a Python comment line from input text.

Parameters
----------
text : str
Text to comment out.
prefix : str
Character to append to the start of each line.
"""

#Replace line breaks with line breaks and comment symbols.
#Also add a comment symbol at the beginning to comment out
#the first line.
return prefix + ('\n'+prefix).join(text.split('\n'))
return prefix + ('\n'+prefix).join(text.split('\n'))


def block_string_lines(text, prefix='# '):
"""
Wrap text in a block text quotes.

Parameters
----------
text : str
Text to comment out.
"""
quotes = '"""'

if quotes in text:
quotes = "'''"

return '\n'.join((quotes+text, quotes))

def get_lines(text, start=None,end=None):
"""
Split the input text into separate lines and then return the
Split the input text into separate lines and then return the
lines that the caller is interested in.

Parameters
----------
text : str
Expand All @@ -184,10 +201,10 @@ def get_lines(text, start=None,end=None):
end : int, optional
Last line to grab from.
"""

# Split the input into lines.
lines = text.split("\n")

# Return the right lines.
return "\n".join(lines[start:end]) #re-join

Expand All @@ -214,7 +231,7 @@ def ipython2python(code):

def posix_path(path):
"""Turn a path into posix-style path/to/etc

Mainly for use in latex on Windows,
where native Windows paths are not allowed.
"""
Expand Down
2 changes: 1 addition & 1 deletion nbconvert/templates/python.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,5 @@
{% endblock input %}

{% block markdowncell scoped %}
{{ cell.source | comment_lines }}
{{ cell.source | block_string_lines }}
{% endblock markdowncell %}