Skip to content

Commit

Permalink
Merge pull request #3 from muellerzr/keep-as-yaml
Browse files Browse the repository at this point in the history
Remove subprocess
  • Loading branch information
muellerzr authored Jun 7, 2023
2 parents 6753a4f + 5ee7ff6 commit 6f62e6c
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 7 deletions.
2 changes: 1 addition & 1 deletion docs/getting_started.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ To install the latest stable version of this package with pypi, run:
pip install nbquarto
```

Also please ensure you have Quarto installed on your machine. You can find installation
Also please ensure you have Quarto installed on your machine for the best experience (though it is not required). You can find installation
instructions [here](https://quarto.org/docs/get-started/).

To install the latest development version of this package, run:
Expand Down
1 change: 0 additions & 1 deletion docs/package_reference/notebook.qmd
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -151,4 +151,3 @@ Makes a blank notebook cell
</div>
</div>


44 changes: 39 additions & 5 deletions src/nbquarto/cli.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import argparse
import logging
import subprocess
from pathlib import Path, PurePosixPath

import yaml

from .notebook import write_notebook
from .processor import NotebookProcessor


Expand Down Expand Up @@ -83,10 +81,46 @@ def process_notebook(notebook_location: str, config_file: str, output_folder: st

output_location = output_folder / notebook_location.relative_to(documentation_source)
Path(output_location.parent).mkdir(parents=True, exist_ok=True)
write_notebook(notebook_processor.notebook, output_location)

# Convert notebook to `qmd`
notebook = notebook_processor.notebook

# Initialize the markdown string
md = []

# For each cell in the notebook
for i, cell in enumerate(notebook["cells"]):
if i == 0 and cell["cell_type"] == "markdown":
# If the first cell is markdown, it's probably the title
# so add it to the markdown string
content = cell["source"].split("\n")
title = content[0]
# Generate quarto metadata
md.append(f'---\ntitle: {title.replace("#", "").lstrip().rstrip()}\njupyter: python3\n---\n')
md.append("\n".join(content[1:]))
# Add a newline to separate cells
md.append("\n")

# Depending on the cell's type, handle it differently
elif cell["cell_type"] in ("markdown", "raw"):
md.extend(cell["source"].split("\n"))

elif cell["cell_type"] == "code":
# Add code wrapped in triple backticks and curly braces
md.append("```{python}")
md.extend(cell["source"].split("\n"))
md.append("```")

else:
raise ValueError(f"Unexpected cell type {cell['cell_type']}")

# Join the markdown lines into a single string
md_source = "\n".join(md)
output_location = output_location.with_suffix(".qmd")

with open(output_location, "w") as f:
f.write(md_source)
logger.info(f"Successfully processed notebook at {notebook_location} and saved to {output_location}")
subprocess.run(["quarto", "convert", output_location])
Path(output_location).unlink()


def main():
Expand Down

0 comments on commit 6f62e6c

Please sign in to comment.