Add mdsplit
This commit is contained in:
parent
cb5d71fd22
commit
bb3aed41dc
2 changed files with 97 additions and 0 deletions
|
@ -8,6 +8,10 @@
|
|||
- [tqr.py](./prog/tqr.py): Program to generate a QRCode from
|
||||
clipboard.
|
||||
|
||||
- [mdsplit.py](./prog/mdsplit.py): Program to convert an org-mode file
|
||||
to multiple markdown files used by
|
||||
[mdbook](https://rust-lang.github.io/mdBook/) program.
|
||||
|
||||
|
||||
## Utilities
|
||||
|
||||
|
|
93
prog/mdsplit.py
Normal file
93
prog/mdsplit.py
Normal file
|
@ -0,0 +1,93 @@
|
|||
#!/usr/bin/env python3
|
||||
|
||||
# /// script
|
||||
# dependencies = [
|
||||
# "python-slugify"
|
||||
# ]
|
||||
# ///
|
||||
|
||||
import argparse
|
||||
import re
|
||||
from pathlib import Path
|
||||
|
||||
from slugify import slugify
|
||||
|
||||
parser = argparse.ArgumentParser(description="Convert an org file to mdbook")
|
||||
|
||||
parser.add_argument("orgfile", help="The org file", type=Path)
|
||||
parser.add_argument("mdbook", help="mdbook root diretory", type=Path)
|
||||
args = parser.parse_args()
|
||||
|
||||
if not args.orgfile.is_file():
|
||||
raise ValueError("`orgfile` must be an existing org-mode file")
|
||||
|
||||
if not args.mdbook.is_dir():
|
||||
raise ValueError(
|
||||
"`mdbook` must be a root mdbook directory initialiezd with `mdbook init`"
|
||||
)
|
||||
|
||||
with open("cours.md") as f:
|
||||
data = f.read()
|
||||
|
||||
data = data.split("```")
|
||||
for i, d in enumerate(data[:]):
|
||||
if i % 2 == 0:
|
||||
continue
|
||||
data[i] = "```" + re.sub(r"^(#+) ", r"\1", data[i], flags=re.MULTILINE) + "```"
|
||||
|
||||
data = "".join(data).splitlines(keepends=True)
|
||||
|
||||
output = Path("book") / "src"
|
||||
|
||||
splitn = [idx for (idx, d) in enumerate(data) if d.startswith("# ")]
|
||||
splitn = list(zip(splitn[:], splitn[1:] + [None]))
|
||||
|
||||
summaries = []
|
||||
for idx, (start, end) in enumerate(splitn[:], start=1):
|
||||
d = data[start:end]
|
||||
title = d[0][2:].rstrip()
|
||||
num = f"{idx:02d}"
|
||||
basename = f"{num}.{slugify(title)}.md"
|
||||
summary = {"title": title, "basename": basename, "subs": []}
|
||||
|
||||
subcontent = d[1:]
|
||||
|
||||
name = d[0]
|
||||
|
||||
splitn = [idx for (idx, d) in enumerate(subcontent) if d.startswith("## ")]
|
||||
|
||||
with open(output / basename, "w") as f:
|
||||
print(f"# {title}", file=f)
|
||||
|
||||
if splitn:
|
||||
d = "".join(subcontent[: splitn[0]])
|
||||
print(d, file=f)
|
||||
|
||||
splitn = list(zip(splitn[:], splitn[1:] + [None]))
|
||||
|
||||
for jdx, (start, end) in enumerate(splitn[:], start=1):
|
||||
d = subcontent[start:end]
|
||||
title = d[0][2:].rstrip()
|
||||
basename = f"{idx:02d}.{jdx:02d}.{slugify(title)}.md"
|
||||
summary["subs"].append(
|
||||
{
|
||||
"title": title,
|
||||
"basename": basename,
|
||||
}
|
||||
)
|
||||
|
||||
with open(output / basename, "w") as f:
|
||||
print("".join(d), file=f)
|
||||
|
||||
summaries.append(summary)
|
||||
|
||||
with open(output / "SUMMARY.md", "w") as f:
|
||||
print("# SUMMARY", file=f)
|
||||
for item in summaries:
|
||||
title = item["title"]
|
||||
basename = item["basename"]
|
||||
print(f"- [{title}](./{basename})", file=f)
|
||||
for sub in item["subs"]:
|
||||
title = sub["title"]
|
||||
basename = sub["basename"]
|
||||
print(f" - [{title}](./{basename})", file=f)
|
Loading…
Reference in a new issue