typst update 0.7 + back cover

This commit is contained in:
anarsec 2023-08-15 20:37:37 +00:00
parent ab86029512
commit 55d64da01d
No known key found for this signature in database
2 changed files with 44 additions and 3 deletions

View file

@ -6,6 +6,7 @@
description: none, description: none,
subtitle: none, subtitle: none,
category: none, category: none,
backcoverinsidecontent: none,
content content
) = { ) = {
@ -126,6 +127,26 @@
set page(numbering: none) set page(numbering: none)
// back cover inside
page()[
// set headings
#show heading.where(level: 1): it => {
block(width: 100%)[
#set align(center)
#set text(size: 22pt, font: "Jost")
#text(it.body)
#v(10pt)
]
}
// format links
#show link: it => {
it.body
}
#backcoverinsidecontent
]
// back cover // back cover
page()[ page()[
#text()[ #text()[
@ -148,3 +169,4 @@
] ]
] ]

View file

@ -52,6 +52,11 @@ class Converter:
if not recommendations_file.exists() or not recommendations_file.is_file(): if not recommendations_file.exists() or not recommendations_file.is_file():
raise RuntimeError(f"Recommendations file '{recommendations_file}' doesn't exist or isn't a file.") raise RuntimeError(f"Recommendations file '{recommendations_file}' doesn't exist or isn't a file.")
# Set series file
series_file = self.anarsec_root / "content" / "series" / "_index.md"
if not series_file.exists() or not series_file.is_file():
raise RuntimeError(f"Series file '{series_file}' doesn't exist or isn't a file.")
# Set input path # Set input path
input_path = self.post_directory / "index.md" input_path = self.post_directory / "index.md"
if not input_path.exists() or not input_path.is_file(): if not input_path.exists() or not input_path.is_file():
@ -62,6 +67,9 @@ class Converter:
for match in re.findall(r'### (.*?)\n+(.*?)\n*(?=###|\Z)', glossary_file.open().read(), re.DOTALL | re.MULTILINE): for match in re.findall(r'### (.*?)\n+(.*?)\n*(?=###|\Z)', glossary_file.open().read(), re.DOTALL | re.MULTILINE):
glossary[slugify.slugify(match[0])] = (match[0], match[1]) glossary[slugify.slugify(match[0])] = (match[0], match[1])
# Load the series markdown
series_markdown = re.search(r'\+{3}.*?\+{3}(.*)', series_file.open().read(), re.MULTILINE | re.DOTALL).group(1)
# For each paper size # For each paper size
for paper_size in ["a4", "letter"]: for paper_size in ["a4", "letter"]:
# Set the output path # Set the output path
@ -153,6 +161,14 @@ class Converter:
typst_path = pathlib.Path(workingDirectory) / f"{self.post_id}.typ" typst_path = pathlib.Path(workingDirectory) / f"{self.post_id}.typ"
subprocess.check_call([str(self.pandoc_binary), "-f", "markdown", "-t", "typst", "--columns", "999999", "-o", typst_path, input_markdown_path]) subprocess.check_call([str(self.pandoc_binary), "-f", "markdown", "-t", "typst", "--columns", "999999", "-o", typst_path, input_markdown_path])
# Write the series markdown to a file
series_markdown_path = pathlib.Path(workingDirectory) / "series-markdown.md"
series_markdown_path.open("w").write(series_markdown)
# Convert the series markdown to typst
series_typst_path = pathlib.Path(workingDirectory) / f"series.typ"
subprocess.check_call([str(self.pandoc_binary), "-f", "markdown", "-t", "typst", "--columns", "999999", "-o", series_typst_path, series_markdown_path])
# Build the full typst file # Build the full typst file
full_typst_path = pathlib.Path(workingDirectory) / f"{self.post_id}-full.typ" full_typst_path = pathlib.Path(workingDirectory) / f"{self.post_id}-full.typ"
full_typst = f""" full_typst = f"""
@ -168,6 +184,7 @@ class Converter:
description: "{description}", description: "{description}",
subtitle: "{toml_front_matter.get("description")}", subtitle: "{toml_front_matter.get("description")}",
category: "{toml_front_matter["taxonomies"]["categories"][0]}", category: "{toml_front_matter["taxonomies"]["categories"][0]}",
backcoverinsidecontent: [{series_typst_path.open().read()}],
content content
) )
{typst_path.open().read()} {typst_path.open().read()}
@ -180,18 +197,19 @@ class Converter:
os.environ["TYPST_FONT_PATHS"] = str(workingDirectory) os.environ["TYPST_FONT_PATHS"] = str(workingDirectory)
subprocess.check_call( subprocess.check_call(
[str(self.typst_binary), "--root", workingDirectory, "compile", full_typst_path, pdf_path], [str(self.typst_binary), "compile", full_typst_path, pdf_path, "--root", workingDirectory],
stderr = subprocess.STDOUT stderr = subprocess.STDOUT
) )
# Insert blank pages before the back cover if needed # Insert blank pages before the back cover and back cover inside if needed
pdf_reader = PyPDF2.PdfFileReader(pdf_path.open("rb")) pdf_reader = PyPDF2.PdfFileReader(pdf_path.open("rb"))
if len(pdf_reader.pages) % 4 != 0: if len(pdf_reader.pages) % 4 != 0:
pdf_writer = PyPDF2.PdfFileWriter() pdf_writer = PyPDF2.PdfFileWriter()
for page in pdf_reader.pages[:-1]: for page in pdf_reader.pages[:-2]:
pdf_writer.addPage(page) pdf_writer.addPage(page)
for i in range(4 - len(pdf_reader.pages) % 4): for i in range(4 - len(pdf_reader.pages) % 4):
pdf_writer.addBlankPage() pdf_writer.addBlankPage()
pdf_writer.addPage(pdf_reader.pages[-2])
pdf_writer.addPage(pdf_reader.pages[-1]) pdf_writer.addPage(pdf_reader.pages[-1])
pdf_with_blank_pages_path = pathlib.Path(workingDirectory) / f"{self.post_id}-with-blank-pages.pdf" pdf_with_blank_pages_path = pathlib.Path(workingDirectory) / f"{self.post_id}-with-blank-pages.pdf"
pdf_writer.write(pdf_with_blank_pages_path.open("wb")) pdf_writer.write(pdf_with_blank_pages_path.open("wb"))
@ -234,3 +252,4 @@ if __name__ == "__main__":
# Convert # Convert
converter.convert() converter.convert()