Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def markdown_to_text(self, source):
"""Escape the given source, for a markdown cell"""
cell_markers = self.unfiltered_metadata.get('cell_marker', self.fmt.get('cell_markers'))
if cell_markers:
if ',' in cell_markers:
left, right = cell_markers.split(',', 1)
else:
left = cell_markers + '\n'
right = '\n' + cell_markers
if left[:3] == right[-3:] and left[:3] in ['"""', "'''"]:
source = copy(source)
source[0] = left + source[0]
source[-1] = source[-1] + right
return source
if self.comment and self.comment != "#'" and is_active(self.ext, self.metadata) and \
self.fmt.get('format_name') not in ['percent', 'hydrogen']:
source = copy(source)
comment_magic(source, self.language, self.comment_magics, explicitly_code=self.cell_type == 'code')
return comment_lines(source, self.comment)
def code_to_text(self):
"""Return the text representation of a code cell"""
source = copy(self.source)
comment_magic(source, self.language, self.comment_magics)
if self.metadata.get('active') == '':
self.metadata.pop('active')
self.language = self.metadata.pop('language', self.language)
if self.cell_type == 'raw' and not is_active(self.ext, self.metadata, False):
return self.html_comment(self.metadata, 'raw')
options = metadata_to_text(self.language, self.metadata)
return ['```' + options] + source + ['```']
source = source[:-2]
lines_to_end_of_cell_marker = 2
else:
lines_to_end_of_cell_marker = 0
pep8_lines = pep8_lines_between_cells(source, lines[cell_end_marker:], self.ext)
if lines_to_end_of_cell_marker != (0 if pep8_lines == 1 else 2):
self.metadata['lines_to_end_of_cell_marker'] = lines_to_end_of_cell_marker
# Uncomment content
self.explicit_soc = cell_start > 0
self.content = self.extract_content(source)
# Is this an inactive cell?
if self.cell_type == 'code':
if not is_active('.ipynb', self.metadata):
if self.metadata.get('active') == '':
del self.metadata['active']
self.cell_type = 'raw'
elif self.ext in ['.md', '.markdown'] and not self.language:
# Markdown files in version >= 1.3 represent code chunks with no language as Markdown cells
if self.format_version not in ['1.0', '1.1']:
self.cell_type = 'markdown'
self.explicit_eoc = False
cell_end_marker += 1
self.content = lines[:cell_end_marker]
# Previous versions mapped those to raw cells
else:
self.cell_type = 'raw'
# Explicit end of cell marker?
if (next_cell_start + 1 < len(lines) and
def cell_to_text(self):
"""Return the text representation for the cell"""
if self.cell_type != 'code':
self.metadata['cell_type'] = self.cell_type
active = is_active(self.ext, self.metadata, self.language == self.default_language)
if self.cell_type == 'raw' and 'active' in self.metadata and self.metadata['active'] == '':
del self.metadata['active']
options = metadata_to_double_percent_options(self.metadata, self.cell_metadata_json)
if options.startswith('%') or not options:
lines = [self.comment + ' %%' + options]
else:
lines = [self.comment + ' %% ' + options]
if self.cell_type == 'code' and active:
source = copy(self.source)
comment_magic(source, self.language, self.comment_magics)
if source == ['']:
return lines
return lines + source
def is_code(self):
# Treat markdown cells with metadata as code cells (#66)
if (self.cell_type == 'markdown' and self.metadata) or self.use_triple_quotes():
if is_active(self.ext, self.metadata):
self.metadata['cell_type'] = self.cell_type
self.source = self.markdown_to_text(self.source)
self.cell_type = 'code'
self.unfiltered_metadata.pop('cell_marker', '')
return True
return super(LightScriptCellExporter, self).is_code()
def extract_content(self, lines):
# Code cells with just a multiline string become Markdown cells
if self.ext == '.py' and not is_active(self.ext, self.metadata, self.cell_type == 'code'):
content = '\n'.join(lines).strip()
for triple_quote in ['"""', "'''"]:
if content.startswith(triple_quote) and content.endswith(triple_quote) and len(content) >= 6:
left = right = triple_quote
content = content[3:-3]
# Trim first/last line return
if content.startswith('\n'):
content = content[1:]
left = triple_quote + '\n'
if content.endswith('\n'):
content = content[:-1]
right = '\n' + triple_quote
if len(left) == len(right) == 4:
self.metadata['cell_marker'] = left[:3]
else:
self.metadata['cell_marker'] = left + ',' + right
def uncomment_code_and_magics(self, lines):
if self.cell_type == 'code' and self.comment_magics and is_active(self.ext, self.metadata):
uncomment_magic(lines, self.language or self.default_language)
return lines
def code_to_text(self):
"""Return the text representation of a code cell"""
active = is_active(self.ext, self.metadata)
source = copy(self.source)
if active:
comment_magic(source, self.language, self.comment_magics)
lines = []
if not is_active(self.ext, self.metadata):
self.metadata['eval'] = False
options = metadata_to_rmd_options(self.language, self.metadata, self.use_runtools)
lines.append('```{{{}}}'.format(options))
lines.extend(source)
lines.append('```')
return lines