Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix shebang handling for languages with non-# comments #434

Merged
merged 1 commit into from
Feb 13, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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))
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Regarding the encoding, do you know it it should use the language line comment, or #?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I only know the encoding line from Python 2 programs, so I am not sure if other languages use this feature as well.


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)