Skip to content

Commit

Permalink
Scripts with at least one unescaped magic command are 'hydrogen' scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
mwouts committed Oct 13, 2019
1 parent 6907a6f commit 7405b84
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
5 changes: 3 additions & 2 deletions jupytext/formats.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
from .stringparser import StringParser
from .languages import _SCRIPT_EXTENSIONS, _COMMENT_CHARS
from .pandoc import pandoc_version, is_pandoc_available
from .magics import is_magic


class JupytextFormatError(ValueError):
Expand Down Expand Up @@ -219,8 +220,8 @@ def guess_format(text, ext):
# Or a Sphinx-gallery script?
if ext in _SCRIPT_EXTENSIONS:
comment = _SCRIPT_EXTENSIONS[ext]['comment']
language = _SCRIPT_EXTENSIONS[ext]['language']
twenty_hash = ''.join(['#'] * 20)
magic_re = re.compile(r'^(%|%%|%%%)[a-zA-Z]')
double_percent_re = re.compile(r'^{}( %%|%%)$'.format(comment))
double_percent_and_space_re = re.compile(r'^{}( %%|%%)\s'.format(comment))
nbconvert_script_re = re.compile(r'^{}( <codecell>| In\[[0-9 ]*\]:?)'.format(comment))
Expand All @@ -245,7 +246,7 @@ def guess_format(text, ext):
nbconvert_script_re.match(line):
double_percent_count += 1

if magic_re.match(line):
if not line.startswith(comment) and is_magic(line, language):
magic_command_count += 1

if line.startswith(twenty_hash) and ext == '.py':
Expand Down
7 changes: 7 additions & 0 deletions tests/test_formats.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,13 @@ def test_guess_format_sphinx(nb_file):
assert guess_format(stream.read(), ext='.py')[0] == 'sphinx'


def test_guess_format_hydrogen():
text = """# %%
cat hello.txt
"""
assert guess_format(text, ext='.py')[0] == 'hydrogen'


def test_divine_format():
assert divine_format('{"cells":[]}') == 'ipynb'
assert divine_format('''def f(x):
Expand Down

0 comments on commit 7405b84

Please sign in to comment.