Skip to content

Commit

Permalink
Any instruction starting with one or two percent signs is a Jupyter m…
Browse files Browse the repository at this point in the history
…agic #94
  • Loading branch information
mwouts committed Oct 8, 2018
1 parent b134347 commit c55b1a0
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 42 deletions.
41 changes: 5 additions & 36 deletions jupytext/magics.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,40 +4,11 @@
from .stringparser import StringParser
from .languages import _SCRIPT_EXTENSIONS

# Line magics retrieved manually (Aug 18) with %lsmagic
_LINE_MAGICS = '%alias %alias_magic %autocall %automagic %autosave ' \
'%bookmark %cd %clear %cls %colors %config ' \
'%connect_info %copy %ddir %debug %dhist %dirs ' \
'%doctest_mode %echo %ed %edit %env %gui %hist ' \
'%history %killbgscripts %ldir %less %load %load_ext ' \
'%loadpy %logoff %logon %logstart %logstate %logstop ' \
'%ls %lsmagic %macro %magic %matplotlib %mkdir %more ' \
'%notebook %page %pastebin %pdb %pdef %pdoc %pfile ' \
'%pinfo %pinfo2 %popd %pprint %precision %profile ' \
'%prun %psearch %psource %pushd %pwd %pycat %pylab ' \
'%qtconsole %quickref %recall %rehashx %reload_ext ' \
'%ren %rep %rerun %reset %reset_selective %rmdir %run ' \
'%save %sc %set_env %store %sx %system %tb %time ' \
'%timeit %unalias %unload_ext %who %who_ls %whos ' \
'%xdel %xmode'.split(' ')

# Add classical line magics
_LINE_MAGICS += ('%autoreload %aimport ' # autoreload
'%R %Rpush %Rpull %Rget ' # rmagic
'%store ' # storemagic
).split(' ')

# Remove any blank line
_LINE_MAGICS = [magic for magic in _LINE_MAGICS if magic.startswith('%')]

# A magic expression is a line or cell magic escaped zero, or multiple times
_FORCE_ESC_RE = {_SCRIPT_EXTENSIONS[ext]['language']: re.compile(
r"^({0} |{0})*%(.*)({0}| )escape".format(_SCRIPT_EXTENSIONS[ext]['comment'])) for ext in _SCRIPT_EXTENSIONS}
_FORCE_NOT_ESC_RE = {_SCRIPT_EXTENSIONS[ext]['language']: re.compile(
r"^({0} |{0})*%(.*)({0}| )noescape".format(_SCRIPT_EXTENSIONS[ext]['comment'])) for ext in _SCRIPT_EXTENSIONS}
_MAGIC_RE = {_SCRIPT_EXTENSIONS[ext]['language']: re.compile(
r"^({0} |{0})*(%%[a-zA-Z]|{1})".format(
_SCRIPT_EXTENSIONS[ext]['comment'], '|'.join(_LINE_MAGICS))) for ext in _SCRIPT_EXTENSIONS}
r"^({0} |{0})*(%|%%)[a-zA-Z]".format(_SCRIPT_EXTENSIONS[ext]['comment'])) for ext in _SCRIPT_EXTENSIONS}
_MAGIC_NOT_ESC_RE = {_SCRIPT_EXTENSIONS[ext]['language']: re.compile(
r"^({0} |{0})*(%|%%)[a-zA-Z](.*){0}\s*noescape".format(_SCRIPT_EXTENSIONS[ext]['comment'])) for ext in _SCRIPT_EXTENSIONS}
_COMMENT = {_SCRIPT_EXTENSIONS[ext]['language']: _SCRIPT_EXTENSIONS[ext]['comment'] for ext in _SCRIPT_EXTENSIONS}

# Commands starting with a question marks have to be escaped
Expand All @@ -46,10 +17,8 @@

def is_magic(line, language):
"""Is the current line a (possibly escaped) Jupyter magic?"""
if _FORCE_ESC_RE.get(language, _FORCE_ESC_RE['python']).match(line):
return True
if not _FORCE_NOT_ESC_RE.get(language, _FORCE_ESC_RE['python']).match(line) and \
_MAGIC_RE.get(language, _FORCE_ESC_RE['python']).match(line):
if not _MAGIC_NOT_ESC_RE.get(language, _MAGIC_NOT_ESC_RE['python']).match(line) and \
_MAGIC_RE.get(language, _MAGIC_RE['python']).match(line):
return True
if language == 'python':
return _HELP_RE.match(line)
Expand Down
7 changes: 1 addition & 6 deletions tests/test_escape_magics.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def test_escape(line):
assert uncomment_magic(comment_magic([line])) == [line]


@pytest.mark.parametrize('line', ['%pytest.fixture'])
@pytest.mark.parametrize('line', ['@pytest.fixture'])
def test_escape_magic_only(line):
assert comment_magic([line]) == [line]

Expand All @@ -29,11 +29,6 @@ def test_force_noescape(line):
assert comment_magic([line]) == [line]


@pytest.mark.parametrize('line', ['%pytest.fixture #escape'])
def test_force_escape(line):
assert comment_magic([line]) == ['# ' + line]


@pytest.mark.parametrize('ext_and_format_name,commented',
zip(['md', 'Rmd', 'py:light', 'py:percent', 'py:sphinx', 'R', 'ss:light', 'ss:percent'],
[False, True, True, False, True, True, True, False]))
Expand Down

0 comments on commit c55b1a0

Please sign in to comment.