2018-03-11 18:53:48 +00:00
|
|
|
#!/usr/bin/env python3
|
2021-07-16 14:15:31 +01:00
|
|
|
# SPDX-License-Identifier: MIT
|
2018-03-11 18:53:48 +00:00
|
|
|
|
2020-12-04 16:58:27 +00:00
|
|
|
"""
|
|
|
|
A Python script to generate a single PDF document with all the `tldr` pages. It works by generating
|
|
|
|
intermediate HTML files from existing md files using Python-markdown, applying desired formatting
|
|
|
|
through CSS, and finally rendering them as PDF. There is no LaTeX dependency for generating the PDF.
|
|
|
|
"""
|
2018-03-11 18:53:48 +00:00
|
|
|
|
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
import glob
|
|
|
|
import markdown
|
|
|
|
import argparse
|
2020-12-11 20:25:11 +00:00
|
|
|
from datetime import datetime
|
2018-03-11 18:53:48 +00:00
|
|
|
|
2021-09-03 16:17:51 +01:00
|
|
|
from weasyprint import HTML
|
|
|
|
|
2020-12-11 20:25:11 +00:00
|
|
|
|
2023-10-23 20:29:43 +01:00
|
|
|
def main(loc, colorscheme, output_filename, platform):
|
2020-12-11 20:25:11 +00:00
|
|
|
# Checking correctness of path
|
|
|
|
if not os.path.isdir(loc):
|
|
|
|
print("Invalid directory. Please try again!", file=sys.stderr)
|
|
|
|
sys.exit(1)
|
2018-03-11 18:53:48 +00:00
|
|
|
|
2021-08-26 06:17:50 +01:00
|
|
|
# Set up css style sheets
|
|
|
|
csslist = ["basic.css"]
|
2020-12-11 20:25:11 +00:00
|
|
|
if colorscheme != "basic":
|
2023-10-04 09:32:15 +01:00
|
|
|
csslist.append(f"{colorscheme}.css")
|
2018-03-11 18:53:48 +00:00
|
|
|
|
2021-08-26 06:17:50 +01:00
|
|
|
# A string that stores all pages in HTML format
|
2021-09-03 16:17:51 +01:00
|
|
|
html = (
|
|
|
|
'<!doctype html><html><head><meta charset="utf-8"></head>'
|
2023-10-21 22:04:22 +01:00
|
|
|
+ "<body><h1 class=title-main>tldr pages book</h1>"
|
2022-10-08 00:10:44 +01:00
|
|
|
+ "<div class=title-sub>Simplified and community-driven man pages</div>"
|
|
|
|
+ "<div class=title-sub><em><small>Generated on "
|
2021-09-03 16:17:51 +01:00
|
|
|
+ datetime.now().strftime("%c")
|
2023-10-21 22:04:22 +01:00
|
|
|
+ "</small></em></div><br><br>"
|
|
|
|
+ "<div class=title-sub>Website: <a href=https://tldr.sh>https://tldr.sh</a></div><br>"
|
|
|
|
+ "<div class=title-sub>GitHub: <a href=https://github.com/tldr-pages/tldr>https://github.com/tldr-pages/tldr</a></div><br>"
|
2021-08-26 06:17:50 +01:00
|
|
|
+ '<p style="page-break-before: always" ></p>'
|
2021-09-03 16:17:51 +01:00
|
|
|
)
|
2018-03-11 18:53:48 +00:00
|
|
|
|
2021-08-26 06:17:50 +01:00
|
|
|
# Writing names of all directories inside 'pages' to a list
|
|
|
|
for operating_sys in sorted(os.listdir(loc)):
|
2023-10-23 20:29:43 +01:00
|
|
|
if platform and operating_sys not in platform:
|
|
|
|
continue
|
|
|
|
|
2020-12-11 20:25:11 +00:00
|
|
|
# Required string to create directory title pages
|
2021-09-03 16:17:51 +01:00
|
|
|
html += (
|
2022-10-08 00:10:44 +01:00
|
|
|
"<h1 class=title-dir>"
|
2021-09-03 16:17:51 +01:00
|
|
|
+ operating_sys.capitalize()
|
2022-10-08 00:10:44 +01:00
|
|
|
+ "</h1>"
|
2021-08-26 06:17:50 +01:00
|
|
|
+ '<p style="page-break-before: always" ></p>'
|
2021-09-03 16:17:51 +01:00
|
|
|
)
|
2021-08-26 06:17:50 +01:00
|
|
|
|
|
|
|
# Conversion of Markdown to HTML string
|
2021-09-03 16:17:51 +01:00
|
|
|
for page_number, md in enumerate(
|
|
|
|
sorted(glob.glob(os.path.join(loc, operating_sys, "*.md"))), start=1
|
|
|
|
):
|
2021-08-26 06:17:50 +01:00
|
|
|
with open(md, "r") as inp:
|
|
|
|
text = inp.readlines()
|
2022-10-08 00:10:44 +01:00
|
|
|
# modify our page to have an H2 header, so that it is grouped under
|
|
|
|
# the H1 header for the directory
|
|
|
|
text[0] = "<h2 class='title-page'>" + text[0][2:] + "</h2>"
|
2021-08-26 06:17:50 +01:00
|
|
|
for line in text:
|
2022-10-08 00:10:44 +01:00
|
|
|
if line.startswith(">"):
|
|
|
|
line = "####" + line[1:]
|
2021-08-26 06:17:50 +01:00
|
|
|
html += markdown.markdown(line)
|
|
|
|
html += '<p style="page-break-before: always" ></p>'
|
|
|
|
print(f"Rendered page {page_number} of the directory {operating_sys}")
|
2021-09-03 16:17:51 +01:00
|
|
|
|
2021-08-26 06:17:50 +01:00
|
|
|
html += "</body></html>"
|
2021-09-03 16:17:51 +01:00
|
|
|
|
2021-08-26 06:17:50 +01:00
|
|
|
# Writing the PDF to disk
|
2023-10-23 20:29:43 +01:00
|
|
|
if platform:
|
|
|
|
output_filename = f"{output_filename[:-4]}-{'+'.join(platform)}.pdf"
|
|
|
|
|
2021-08-26 06:17:50 +01:00
|
|
|
print("\nConverting all pages to PDF...")
|
2023-10-21 22:04:22 +01:00
|
|
|
HTML(string=html).write_pdf(output_filename, stylesheets=csslist)
|
2018-03-11 18:53:48 +00:00
|
|
|
|
2023-10-21 22:04:22 +01:00
|
|
|
if os.path.exists(output_filename):
|
|
|
|
print(f"\nCreated {output_filename} in the current directory!\n")
|
2018-03-11 18:53:48 +00:00
|
|
|
|
2021-09-03 16:17:51 +01:00
|
|
|
|
2019-12-17 20:25:52 +00:00
|
|
|
if __name__ == "__main__":
|
2020-12-19 03:52:07 +00:00
|
|
|
# Parsing the arguments
|
2021-09-03 16:17:51 +01:00
|
|
|
parser = argparse.ArgumentParser(
|
|
|
|
prog="tldr-pages-to-pdf",
|
|
|
|
description="A Python script to generate a single PDF document with all the `tldr` pages.",
|
|
|
|
)
|
|
|
|
parser.add_argument("dir_path", help="Path to the 'pages' directory")
|
|
|
|
parser.add_argument(
|
|
|
|
"-c",
|
|
|
|
"--color",
|
|
|
|
choices=["solarized-light", "solarized-dark", "basic"],
|
|
|
|
default="basic",
|
|
|
|
help="Color scheme of the PDF",
|
|
|
|
)
|
2023-10-21 22:04:22 +01:00
|
|
|
parser.add_argument(
|
|
|
|
"-o",
|
|
|
|
"--output",
|
|
|
|
default="tldr-book.pdf",
|
|
|
|
help="Custom filename for the output PDF (default is 'tldr-book.pdf')",
|
|
|
|
)
|
2023-10-23 20:29:43 +01:00
|
|
|
parser.add_argument(
|
|
|
|
"-p",
|
|
|
|
"--platform",
|
|
|
|
nargs="+",
|
|
|
|
help="Specify one or more platforms to generate PDFs for",
|
|
|
|
)
|
2020-12-19 03:52:07 +00:00
|
|
|
args = parser.parse_args()
|
2019-12-17 20:25:52 +00:00
|
|
|
|
2023-10-23 20:29:43 +01:00
|
|
|
main(args.dir_path, args.color, args.output, args.platform)
|