Skip to content

Commit

Permalink
Comment Python and Julia help calls in scripts #73
Browse files Browse the repository at this point in the history
  • Loading branch information
mwouts committed Sep 13, 2018
1 parent 557be0a commit c669fd0
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 9 deletions.
8 changes: 5 additions & 3 deletions jupytext/cell_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,12 @@ def uncomment(lines):
for line in lines]


def paragraph_is_fully_commented(lines):
def paragraph_is_fully_commented(lines, main_language):
"""Is the paragraph fully commented?"""
for i, line in enumerate(lines):
if line.startswith('#'):
if line.startswith('# %') and is_magic(line):
if (line.startswith('# %') or line.startswith('# ?'))\
and is_magic(line, main_language):
return False
continue
return i > 0 and _BLANK_LINE.match(line)
Expand Down Expand Up @@ -191,7 +192,8 @@ def find_cell_end_r(self, lines):
def find_cell_end_py(self, lines):
"""Return position of end of cell marker, and position
of first line after cell"""
if self.metadata is None and paragraph_is_fully_commented(lines):
if self.metadata is None and \
paragraph_is_fully_commented(lines, 'python'):
self.cell_type = 'markdown'
for i, line in enumerate(lines):
if _BLANK_LINE.match(line):
Expand Down
18 changes: 13 additions & 5 deletions jupytext/magics.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,18 +34,26 @@
_FORCE_NOT_ESC_RE = re.compile(r"^(# |#)*%(.*)noescape")
_MAGIC_RE = re.compile(r"^(# |#)*(%%|{})".format('|'.join(_LINE_MAGICS)))

# Commands starting with a question marks have to be escaped
_HELP_RE = re.compile(r"^(# |#)*\?")

def is_magic(line):

def is_magic(line, language):
"""Is the current line a (possibly escaped) Jupyter magic?"""
return (_FORCE_ESC_RE.match(line) or (not _FORCE_NOT_ESC_RE.match(line)
and _MAGIC_RE.match(line)))
if _FORCE_ESC_RE.match(line):
return True
if not _FORCE_NOT_ESC_RE.match(line) and _MAGIC_RE.match(line):
return True
if language == 'R':
return False
return _HELP_RE.match(line)


def escape_magic(source, language='python'):
"""Escape Jupyter magics with '# '"""
parser = StringParser(language)
for pos, line in enumerate(source):
if not parser.is_quoted() and is_magic(line):
if not parser.is_quoted() and is_magic(line, language):
source[pos] = '# ' + line
parser.read_line(line)
return source
Expand All @@ -64,7 +72,7 @@ def unescape_magic(source, language='python'):
"""Unescape Jupyter magics"""
parser = StringParser(language)
for pos, line in enumerate(source):
if not parser.is_quoted() and is_magic(line):
if not parser.is_quoted() and is_magic(line, language):
source[pos] = unesc(line)
parser.read_line(line)
return source
Expand Down
2 changes: 1 addition & 1 deletion tests/mirror/jupyter_again.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,4 @@
import yaml
print(yaml.dump(yaml.load(c)))

?next
# ?next

0 comments on commit c669fd0

Please sign in to comment.