Skip to content

Commit

Permalink
Rust magics have their own regexp
Browse files Browse the repository at this point in the history
  • Loading branch information
mwouts committed Oct 16, 2019
1 parent 790dad8 commit 297cf56
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 9 deletions.
5 changes: 5 additions & 0 deletions jupytext/magics.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@
_SCRIPT_EXTENSIONS[ext]['comment'])) for ext in _SCRIPT_EXTENSIONS}
_LINE_CONTINUATION_RE = re.compile(r'.*\\\s*$')

# Rust magics start with single ':' #351
_MAGIC_RE['rust'] = re.compile(r"^(// |//)*:[a-zA-Z]")
_MAGIC_FORCE_ESC_RE['rust'] = re.compile(r"^(// |//)*:[a-zA-Z](.*)//\s*escape")
_MAGIC_FORCE_ESC_RE['rust'] = re.compile(r"^(// |//)*:[a-zA-Z](.*)//\s*noescape")

# Commands starting with a question or exclamation mark have to be escaped
_PYTHON_HELP_OR_BASH_CMD = re.compile(r"^(# |#)*(\?|!)\s*[A-Za-z]")

Expand Down
6 changes: 3 additions & 3 deletions tests/notebooks/mirror/ipynb_to_Rmd/evcxr_jupyter_tour.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ std::thread::spawn({
We can load external crates. This one takes a while to compile, but once it's compiled, subsequent cells shouldn't need to recompile it, so it should be much quicker.

```{rust}
:dep base64 = "0.10.1"
// :dep base64 = "0.10.1"
base64::encode(&vec![1, 2, 3, 4])
```

Expand Down Expand Up @@ -169,13 +169,13 @@ s.push_str(format!("foo {}", 42));
We can print a table of defined variables and their types with the :vars command.

```{rust}
:vars
// :vars
```

Other built-in commands can be found via :help

```{rust}
:help
// :help
```

```{rust}
Expand Down
6 changes: 3 additions & 3 deletions tests/notebooks/mirror/ipynb_to_percent/evcxr_jupyter_tour.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ std::thread::spawn({
// We can load external crates. This one takes a while to compile, but once it's compiled, subsequent cells shouldn't need to recompile it, so it should be much quicker.

// %%
:dep base64 = "0.10.1"
// :dep base64 = "0.10.1"
base64::encode(&vec![1, 2, 3, 4])

// %% [markdown]
Expand Down Expand Up @@ -162,12 +162,12 @@ s.push_str(format!("foo {}", 42));
// We can print a table of defined variables and their types with the :vars command.

// %%
:vars
// :vars

// %% [markdown]
// Other built-in commands can be found via :help

// %%
:help
// :help

// %%
6 changes: 3 additions & 3 deletions tests/notebooks/mirror/ipynb_to_script/evcxr_jupyter_tour.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ std::thread::spawn({
// ## Loading external crates
// We can load external crates. This one takes a while to compile, but once it's compiled, subsequent cells shouldn't need to recompile it, so it should be much quicker.

:dep base64 = "0.10.1"
// :dep base64 = "0.10.1"
base64::encode(&vec![1, 2, 3, 4])

// ## Customizing how types are displayed
Expand Down Expand Up @@ -134,10 +134,10 @@ s.push_str(format!("foo {}", 42));
// ## Seeing what variables have been defined
// We can print a table of defined variables and their types with the :vars command.

:vars
// :vars

// Other built-in commands can be found via :help

:help
// :help


45 changes: 45 additions & 0 deletions tests/test_read_simple_rust.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
from nbformat.v4.nbbase import new_notebook, new_markdown_cell, new_code_cell
import jupytext
from jupytext.compare import compare, compare_notebooks


def test_read_magics(text="// :vars\n"):
nb = jupytext.reads(text, 'rs')
compare_notebooks(nb, new_notebook(cells=[new_code_cell(':vars')]))
compare(jupytext.writes(nb, 'rs'), text)


def test_read_simple_file(text='''println!("Hello world");
eprintln!("Hello error");
format!("Hello {}", "world")
// A Function
pub fn fib(x: i32) -> i32 {
if x <= 2 {0} else {fib(x - 2) + fib(x - 1)}
}
// This is a
// Markdown cell
// This is a magic instruction
// :vars
// This is a rust identifier
::std::mem::drop
'''):
nb = jupytext.reads(text, 'rs')
compare_notebooks(nb, new_notebook(cells=[
new_code_cell('''println!("Hello world");
eprintln!("Hello error");
format!("Hello {}", "world")'''),
new_code_cell('''// A Function
pub fn fib(x: i32) -> i32 {
if x <= 2 {0} else {fib(x - 2) + fib(x - 1)}
}'''),
new_markdown_cell("This is a\nMarkdown cell"),
new_code_cell('''// This is a magic instruction
:vars'''),
new_code_cell('''// This is a rust identifier
::std::mem::drop''')
]))
compare(jupytext.writes(nb, 'rs'), text)

0 comments on commit 297cf56

Please sign in to comment.