Skip to content

Commit

Permalink
WIP replace %% with gonb:%% in percent go notebooks
Browse files Browse the repository at this point in the history
  • Loading branch information
mwouts committed Jul 1, 2024
1 parent 444b834 commit 37f6d3f
Show file tree
Hide file tree
Showing 10 changed files with 50 additions and 28 deletions.
5 changes: 5 additions & 0 deletions src/jupytext/cell_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -555,6 +555,11 @@ def uncomment_code_and_magics(self, lines):
else:
lines = uncomment(lines)

if self.default_language == "go" and self.language is None:
lines = [
re.sub(r"^([/\s]*)(//\s+gonb:%%)", r"\1%%", line) for line in lines
]

if self.cell_type == "code":
return unescape_code_start(
lines, self.ext, self.language or self.default_language
Expand Down
6 changes: 6 additions & 0 deletions src/jupytext/cell_to_text.py
Original file line number Diff line number Diff line change
Expand Up @@ -487,6 +487,12 @@ def __init__(self, *args, **kwargs):

def cell_to_text(self):
"""Return the text representation for the cell"""
if self.default_language == "go" and self.language == "go":
self.source = [
re.sub(r"^([/\s]*)(%%$|%%\s+.*$)", r"\1// gonb:\2", line)
for line in self.source
]

active = is_active(
self.ext, self.metadata, same_language(self.language, self.default_language)
)
Expand Down
6 changes: 4 additions & 2 deletions src/jupytext/languages.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Determine notebook or cell language"""
import re

# Jupyter magic commands that are also languages
_JUPYTER_LANGUAGES = [
Expand Down Expand Up @@ -111,6 +112,7 @@
_JUPYTER_LANGUAGES_LOWER_AND_UPPER = _JUPYTER_LANGUAGES.union(
{str.upper(lang) for lang in _JUPYTER_LANGUAGES}
)
_GO_DOUBLE_PERCENT_COMMAND = re.compile(r"^(%%|%%\s+.*)$")


def default_language_from_metadata_and_ext(metadata, ext, pop_main_language=False):
Expand Down Expand Up @@ -207,6 +209,8 @@ def cell_language(source, default_language, custom_cell_magics):
"""Return cell language and language options, if any"""
if source:
line = source[0]
if default_language == "go" and _GO_DOUBLE_PERCENT_COMMAND.match(line):
return None, None
if default_language == "csharp":
if line.startswith("#!"):
lang = line[2:].strip()
Expand All @@ -215,8 +219,6 @@ def cell_language(source, default_language, custom_cell_magics):
return lang, ""
elif line.startswith("%%"):
magic = line[2:]
if default_language == "go" and magic.strip() == "":
return None, None
if " " in magic:
lang, magic_args = magic.split(" ", 1)
else:
Expand Down
18 changes: 9 additions & 9 deletions src/jupytext/magics.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,33 +13,33 @@ def get_comment(ext):
# A magic expression is a line or cell or metakernel magic (#94, #61) escaped zero, or multiple times
_MAGIC_RE = {
_SCRIPT_EXTENSIONS[ext]["language"]: re.compile(
r"^\s*({0} |{0})*(%|%%|%%%)[a-zA-Z]".format(get_comment(ext))
r"^\s*({0} |{0})*(%|%%|%%%)[a-zA-Z]+".format(get_comment(ext))
)
for ext in _SCRIPT_EXTENSIONS
}
_MAGIC_FORCE_ESC_RE = {
_SCRIPT_EXTENSIONS[ext]["language"]: re.compile(
r"^\s*({0} |{0})*(%|%%|%%%)[a-zA-Z](.*){0}\s*escape".format(get_comment(ext))
r"^\s*({0} |{0})*(%|%%|%%%)[a-zA-Z]+(.*){0}\s*escape".format(get_comment(ext))
)
for ext in _SCRIPT_EXTENSIONS
}
_MAGIC_NOT_ESC_RE = {
_SCRIPT_EXTENSIONS[ext]["language"]: re.compile(
r"^\s*({0} |{0})*(%|%%|%%%)[a-zA-Z](.*){0}\s*noescape".format(get_comment(ext))
r"^\s*({0} |{0})*(%|%%|%%%)[a-zA-Z]+(.*){0}\s*noescape".format(get_comment(ext))
)
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")
_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")

# C# magics start with '#!'
_MAGIC_RE["csharp"] = re.compile(r"^(// |//)*#![a-zA-Z]")
_MAGIC_FORCE_ESC_RE["csharp"] = re.compile(r"^(// |//)*#![a-zA-Z](.*)//\s*escape")
_MAGIC_FORCE_ESC_RE["csharp"] = re.compile(r"^(// |//)*#![a-zA-Z](.*)//\s*noescape")
_MAGIC_RE["csharp"] = re.compile(r"^(// |//)*#![a-zA-Z]+")
_MAGIC_FORCE_ESC_RE["csharp"] = re.compile(r"^(// |//)*#![a-zA-Z]+(.*)//\s*escape")
_MAGIC_FORCE_ESC_RE["csharp"] = 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*(# |#)*\s*(\?|!)\s*[A-Za-z\.\~\$\\\/\{\}]")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ func main() {
```

```{go}
%%
fmt.Printf("Hello World!")
```

<!-- #region magic_args="--who=world can pass flags to main func" -->
<!-- #endregion -->
%% --who=world can pass flags to main func

```{go}
import (
Expand Down Expand Up @@ -55,6 +55,7 @@ gonbui.DisplayHtml(`<span style="background:pink; color:#111; border-radius: 3px
```

```{go}
%%
gonbui.DisplayMarkdown("#### Objective\n\n1. Have fun coding **Go**;\n1. Profit...\n"+
`$$f(x) = \int_{-\infty}^{\infty} e^{-x^2} dx$$`)
```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ func main() {
}

// %%
%%
// gonb:%%
fmt.Printf("Hello World!")

// %% [markdown]
// %% --who=world can pass flags to main func
// // gonb:%% --who=world can pass flags to main func

// %%
import (
Expand All @@ -33,7 +33,7 @@ import (

var flagWho = flag.String("who", "", "Your name!")

%% --who=world
// gonb:%% --who=world
fmt.Printf("Hello %s!\n", *flagWho)

// %% [markdown]
Expand All @@ -50,17 +50,17 @@ func main() {
// %%
import "github.com/janpfeifer/gonb/gonbui"

%%
// gonb:%%
gonbui.DisplayHtml(`<span style="background:pink; color:#111; border-radius: 3px; border: 3px solid orange; font-size: 18px;">I 🧡 GoNB!</span>`)

// %%
%%
// gonb:%%
gonbui.DisplayMarkdown("#### Objective\n\n1. Have fun coding **Go**;\n1. Profit...\n"+
`$$f(x) = \int_{-\infty}^{\infty} e^{-x^2} dx$$`)

// %%
func init_a() {
fmt.Println("init_a")
}
%%
// gonb:%%
fmt.Println("main")
5 changes: 3 additions & 2 deletions tests/data/notebooks/outputs/ipynb_to_md/hello_world_gonb.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ func main() {
```

```go
%%
fmt.Printf("Hello World!")
```

<!-- #region magic_args="--who=world can pass flags to main func" -->
<!-- #endregion -->
%% --who=world can pass flags to main func

```go
import (
Expand Down Expand Up @@ -55,6 +55,7 @@ gonbui.DisplayHtml(`<span style="background:pink; color:#111; border-radius: 3px
```

```go
%%
gonbui.DisplayMarkdown("#### Objective\n\n1. Have fun coding **Go**;\n1. Profit...\n"+
`$$f(x) = \int_{-\infty}^{\infty} e^{-x^2} dx$$`)
```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,11 @@ func main() {
}

// %%
// gonb:%%
fmt.Printf("Hello World!")

// %% [markdown] magic_args="--who=world can pass flags to main func"
// %% [markdown]
// // gonb:%% --who=world can pass flags to main func

// %%
import (
Expand All @@ -31,7 +33,7 @@ import (

var flagWho = flag.String("who", "", "Your name!")

%% --who=world
// gonb:%% --who=world
fmt.Printf("Hello %s!\n", *flagWho)

// %% [markdown]
Expand All @@ -48,16 +50,17 @@ func main() {
// %%
import "github.com/janpfeifer/gonb/gonbui"

%%
// gonb:%%
gonbui.DisplayHtml(`<span style="background:pink; color:#111; border-radius: 3px; border: 3px solid orange; font-size: 18px;">I 🧡 GoNB!</span>`)

// %%
// gonb:%%
gonbui.DisplayMarkdown("#### Objective\n\n1. Have fun coding **Go**;\n1. Profit...\n"+
`$$f(x) = \int_{-\infty}^{\infty} e^{-x^2} dx$$`)

// %%
func init_a() {
fmt.Println("init_a")
}
%%
// gonb:%%
fmt.Println("main")
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@ func main() {
fmt.Printf("Hello World!")
}

%%
fmt.Printf("Hello World!")

// + [markdown] magic_args="--who=world can pass flags to main func"
// %% --who=world can pass flags to main func

// +
import (
Expand Down Expand Up @@ -48,6 +49,7 @@ import "github.com/janpfeifer/gonb/gonbui"
gonbui.DisplayHtml(`<span style="background:pink; color:#111; border-radius: 3px; border: 3px solid orange; font-size: 18px;">I 🧡 GoNB!</span>`)
// -

%%
gonbui.DisplayMarkdown("#### Objective\n\n1. Have fun coding **Go**;\n1. Profit...\n"+
`$$f(x) = \int_{-\infty}^{\infty} e^{-x^2} dx$$`)

Expand Down
6 changes: 4 additions & 2 deletions tests/functional/simple_notebooks/test_read_simple_percent.py
Original file line number Diff line number Diff line change
Expand Up @@ -565,8 +565,9 @@ def test_read_simple_gonb_cell_with_double_percent(
// language: go
// name: gonb
// ---
// %%
%%
// gonb:%%
fmt.Printf("Hello World!")
""",
):
Expand All @@ -591,8 +592,9 @@ def test_write_simple_gonb_cell_with_double_percent(
// language: go
// name: gonb
// ---
// %%
%%
// gonb:%%
fmt.Printf("Hello World!")
""",
):
Expand Down

0 comments on commit 37f6d3f

Please sign in to comment.