mirror of
https://github.com/TomHodson/tomhodson.github.com.git
synced 2025-06-26 10:01:18 +02:00
add script to convert notebooks to markdown
This commit is contained in:
parent
6b21844d4a
commit
e065537680
55
pandoc/convert_ipynb_to_blog.py
Normal file
55
pandoc/convert_ipynb_to_blog.py
Normal file
@ -0,0 +1,55 @@
|
||||
import sys
|
||||
from pathlib import Path
|
||||
import subprocess
|
||||
from datetime import date
|
||||
|
||||
sitepath = Path("~/git/tomhodson.github.com/").expanduser()
|
||||
defaults_file = sitepath / "pandoc/pandoc_notebook_to_markdown.yaml"
|
||||
postspath = sitepath / "_posts"
|
||||
|
||||
filepath = Path(sys.argv[1]).resolve()
|
||||
title = sys.argv[2]
|
||||
# title = input(f"Give this blogpost a title (default to filename):")
|
||||
if not title: title = filepath.stem.replace("_", " ")
|
||||
print(f'Title: "{title}"')
|
||||
|
||||
filename = title.lower().replace(" ", "_")
|
||||
print(f'Filename: "{title}"')
|
||||
|
||||
# subprocess.run(["jupyter", "nbconvert", "--to=markdown", filepath, "--output", f"{filename}.md"])
|
||||
|
||||
print(f'Running pandoc to generate {title}.md')
|
||||
subprocess.run(["pandoc", "-d", defaults_file, f"--extract-media=./{filename}", filepath, "--output", f"{filename}.md"])
|
||||
|
||||
with open(f"{filename}.md", "r") as f:
|
||||
content = f.read()
|
||||
|
||||
print(f'Replacing image links')
|
||||
content = content.replace(f"./{filename}/", f"/assets/blog/{filename}/")
|
||||
|
||||
# remove lines that begin with with ":::"
|
||||
print(f'Removing lines that begin with :::')
|
||||
lines = content.split("\n")
|
||||
lines = [line for line in lines if not line.startswith(":::")]
|
||||
content = "\n".join(lines)
|
||||
|
||||
metadata = f"""---
|
||||
title: {title}
|
||||
layout: post
|
||||
image:
|
||||
---
|
||||
"""
|
||||
|
||||
print(f'Adding metadata')
|
||||
print(f'Writing out to {filename}.md')
|
||||
with open(f"{filename}.md", "w") as f:
|
||||
f.write(metadata + content)
|
||||
|
||||
print(f'Copying over')
|
||||
todays_date = date.today().isoformat()
|
||||
subprocess.run(["mv", f"{filename}.md", postspath / f"{todays_date}-{filename}.md"])
|
||||
subprocess.run(["mv", f"./{filename}", sitepath / f"assets/blog/{filename}"])
|
||||
|
||||
|
||||
|
||||
|
3
pandoc/pandoc_notebook_to_markdown.yaml
Normal file
3
pandoc/pandoc_notebook_to_markdown.yaml
Normal file
@ -0,0 +1,3 @@
|
||||
from: ipynb
|
||||
to: markdown
|
||||
wrap: none
|
Loading…
x
Reference in New Issue
Block a user