Skip to content

Commit

Permalink
improved quarto ref regex replace
Browse files Browse the repository at this point in the history
  • Loading branch information
MartinSchobben committed Aug 15, 2024
1 parent f384f9a commit 0cf7e59
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 5 deletions.
18 changes: 16 additions & 2 deletions src/eo_datascience/clean_nb.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import os
import nbformat
from pathlib import Path
import re

def clean_up_frontmatter(dir = './notebooks', save=False):
# Define the path to the notebooks
Expand Down Expand Up @@ -41,14 +42,27 @@ def convert_refs(dir="./notebooks", save=True):
for i in range(len(nb.cells)):
if i != 0:
if nb.cells[i]["cell_type"] == "markdown":
nb.cells[i].source = nb.cells[i].source.replace(r"[@", r"{cite}`").replace(r"]", r"`")

nb.cells[i].source = quarto_ref_person_replace(nb.cells[i].source)
nb.cells[i].source = quarto_ref_time_replace(nb.cells[i].source)

# Save the notebook
if save:
nbformat.write(nb, nb_path)
else:
return nb

def quarto_ref_person_replace(quarto):
bibs = re.findall(r"(?<=\[\@)[^\]]+", quarto)
for i in bibs:
quarto = re.sub(r"\[\@" + i + "\]", r"{cite:p}`" + i + "`", quarto)
return quarto

def quarto_ref_time_replace(quarto):
bibs = re.findall(r"(?<=\@)[^\s]+", quarto)
for i in bibs:
quarto = re.sub(r"\@" + i, r"{cite:t}`" + i + "`", quarto)
return quarto

def find_ipynb(dir):
root = Path(dir).resolve()
nb_paths = [root / file for file in os.listdir(root) if file.endswith('.ipynb')]
Expand Down
2 changes: 1 addition & 1 deletion tests/mock.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"[@ref1] "
"lorem ipsum [@anon2024] and [@anon2025] and lorem ipsum @anon2024 and @anon2025"
]
}
],
Expand Down
10 changes: 8 additions & 2 deletions tests/test_quarto_nb_conversions.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
import nbformat
from pathlib import Path
import pytest
from eo_datascience.clean_nb import clean_up_frontmatter, convert_refs
from eo_datascience.clean_nb import clean_up_frontmatter, convert_refs, quarto_ref_person_replace, quarto_ref_time_replace

def test_remove_front_matter():
assert clean_up_frontmatter("./tests", False)["cells"][0]["source"] == "# This a mock Jupyter file\nWe use it for testing\n"

def test_ref_regex():
quarto = [r"lorem ipsum [@anon2024] and [@anon2025]", r"lorem ipsum @anon2024 and @anon2025"]
quarto[0] = quarto_ref_person_replace(quarto[0])
quarto[1] = quarto_ref_time_replace(quarto[1])
assert quarto == [r"lorem ipsum {cite:p}`anon2024` and {cite:p}`anon2025`", r"lorem ipsum {cite:t}`anon2024` and {cite:t}`anon2025`"]

def test_conversion_of_refs():
assert convert_refs("./tests", False)["cells"][1]["source"] == r'{cite}`ref1` '
assert convert_refs("./tests", False)["cells"][1]["source"] == r"lorem ipsum {cite:p}`anon2024` and {cite:p}`anon2025` and lorem ipsum {cite:t}`anon2024` and {cite:t}`anon2025`"

0 comments on commit 0cf7e59

Please sign in to comment.