Skip to content

Commit

Permalink
Fix shebang handling for languages with non-# comments
Browse files Browse the repository at this point in the history
The shebang always starts with the sequence "#!" independend of the
comment symbol of the programming language.
Add a test with Rust, which uses "//" as the comment symbol.
  • Loading branch information
jonasbb authored and mwouts committed Feb 13, 2020
1 parent bcf1a7b commit d6abab5
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
4 changes: 2 additions & 2 deletions jupytext/header.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def encoding_and_executable(notebook, metadata, ext):
jupytext_metadata = metadata.get('jupytext', {})

if comment is not None and 'executable' in jupytext_metadata:
lines.append(comment + '!' + jupytext_metadata.pop('executable'))
lines.append('#!' + jupytext_metadata.pop('executable'))

if 'encoding' in jupytext_metadata:
lines.append(jupytext_metadata.pop('encoding'))
Expand Down Expand Up @@ -144,7 +144,7 @@ def header_to_metadata_and_cell(lines, header_prefix, ext=None):
encoding_re = re.compile(r'^[ \t\f]*{}.*?coding[:=][ \t]*([-_.a-zA-Z0-9]+)'.format(comment))

for i, line in enumerate(lines):
if i == 0 and line.startswith(comment + '!'):
if i == 0 and line.startswith('#!'):
metadata.setdefault('jupytext', {})['executable'] = line[2:]
start = i + 1
continue
Expand Down
23 changes: 23 additions & 0 deletions tests/test_read_simple_rust.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,26 @@ def test_read_simple_file(text='''println!("Hello world");
::std::mem::drop''')
]))
compare(jupytext.writes(nb, 'rs'), text)


def test_read_write_script_with_metadata_241(no_jupytext_version_number, rsnb="""#!/usr/bin/env scriptisto
// ---
// jupyter:
// jupytext:
// text_representation:
// extension: .rs
// format_name: light
// kernelspec:
// display_name: Rust
// language: rust
// name: rust
// ---
let mut a: i32 = 2;
a += 1;
"""):
nb = jupytext.reads(rsnb, 'rs')
assert 'executable' in nb.metadata['jupytext']
rsnb2 = jupytext.writes(nb, 'rs')

compare(rsnb, rsnb2)

0 comments on commit d6abab5

Please sign in to comment.