2018-03-11 18:53:48 +00:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
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 re
|
|
|
|
import markdown
|
|
|
|
import argparse
|
2020-12-11 20:25:11 +00:00
|
|
|
from datetime import datetime
|
2018-03-11 18:53:48 +00:00
|
|
|
|
|
|
|
from weasyprint import HTML
|
|
|
|
|
2020-12-11 20:25:11 +00:00
|
|
|
|
2018-03-11 18:53:48 +00:00
|
|
|
def main(loc, colorscheme):
|
|
|
|
|
2020-12-11 20:25:11 +00:00
|
|
|
oslist = []
|
|
|
|
allmd = []
|
|
|
|
group = []
|
|
|
|
ap = []
|
|
|
|
|
|
|
|
# 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
|
|
|
|
2020-12-11 20:25:11 +00:00
|
|
|
# Writing names of all directories inside 'pages' to a list
|
|
|
|
for os_dir in os.listdir(loc):
|
|
|
|
oslist.append(os_dir)
|
2018-03-11 18:53:48 +00:00
|
|
|
|
2020-12-11 20:25:11 +00:00
|
|
|
oslist.sort()
|
2018-03-11 18:53:48 +00:00
|
|
|
|
2020-12-11 20:25:11 +00:00
|
|
|
# Required strings to create intermediate HTML files
|
|
|
|
header = '<!doctype html><html><head><meta charset="utf-8"><link rel="stylesheet" href="basic.css">'
|
|
|
|
if colorscheme != "basic":
|
|
|
|
header += '<link rel="stylesheet" href="' + colorscheme + '.css"></head><body>\n'
|
2018-03-11 18:53:48 +00:00
|
|
|
|
2020-12-11 20:25:11 +00:00
|
|
|
header += "</head><body>\n"
|
|
|
|
footer = "</body></html>"
|
|
|
|
title_content = "<h1 class=title-main>tldr pages</h1>" \
|
|
|
|
+ "<h4 class=title-sub>Simplified and community-driven man pages</h4>" \
|
|
|
|
+ "<h6 class=title-sub><em><small>Generated on " + datetime.now().strftime("%c") + "</small></em></h6>" \
|
|
|
|
+ "</body></html>"
|
2018-03-11 18:53:48 +00:00
|
|
|
|
2020-12-11 20:25:11 +00:00
|
|
|
# Creating title page
|
|
|
|
with open("title.html", "w") as f:
|
|
|
|
f.write(header + title_content)
|
2018-03-11 18:53:48 +00:00
|
|
|
|
2020-12-11 20:25:11 +00:00
|
|
|
group.append(HTML("title.html").render())
|
2018-03-11 18:53:48 +00:00
|
|
|
|
2020-12-11 20:25:11 +00:00
|
|
|
for operating_sys in oslist:
|
2018-03-11 18:53:48 +00:00
|
|
|
|
2020-12-11 20:25:11 +00:00
|
|
|
# Required string to create directory title pages
|
|
|
|
dir_title = "<h2 class=title-dir>" + \
|
|
|
|
operating_sys.capitalize() + "</h2></body></html>"
|
2018-03-11 18:53:48 +00:00
|
|
|
|
2020-12-11 20:25:11 +00:00
|
|
|
# Creating directory title page for current directory
|
|
|
|
with open("dir_title.html", "w") as os_html:
|
|
|
|
os_html.write(header + dir_title)
|
2018-03-11 18:53:48 +00:00
|
|
|
|
2020-12-11 20:25:11 +00:00
|
|
|
group.append(HTML("dir_title.html").render())
|
2018-03-11 18:53:48 +00:00
|
|
|
|
2020-12-11 20:25:11 +00:00
|
|
|
# Creating a list of all md files in the current directory
|
|
|
|
for temp in glob.glob(os.path.join(loc, operating_sys, "*.md")):
|
|
|
|
allmd.append(temp)
|
2018-03-11 18:53:48 +00:00
|
|
|
|
2020-12-11 20:25:11 +00:00
|
|
|
# Sorting all filenames in the directory, to maintain the order of the PDF
|
|
|
|
allmd.sort()
|
2018-03-11 18:53:48 +00:00
|
|
|
|
2020-12-19 19:58:03 +00:00
|
|
|
# Conversion of Markdown to HTML
|
|
|
|
for page_number, md in enumerate(allmd, start=1):
|
2018-03-11 18:53:48 +00:00
|
|
|
|
2020-12-19 19:58:03 +00:00
|
|
|
with open(md, "r") as inp:
|
|
|
|
text = inp.readlines()
|
2018-03-11 18:53:48 +00:00
|
|
|
|
2020-12-19 19:58:03 +00:00
|
|
|
with open("htmlout.html", "w") as out:
|
|
|
|
out.write(header)
|
2018-03-11 18:53:48 +00:00
|
|
|
|
2020-12-19 19:58:03 +00:00
|
|
|
for line in text:
|
|
|
|
if re.match(r'^>', line):
|
|
|
|
line = line[:0] + '####' + line[1:]
|
|
|
|
html = markdown.markdown(line)
|
|
|
|
out.write(html)
|
|
|
|
out.write(footer)
|
2018-03-11 18:53:48 +00:00
|
|
|
|
2020-12-19 19:58:03 +00:00
|
|
|
group.append(HTML("htmlout.html").render())
|
|
|
|
print("Rendered page {} of the directory {}".format(
|
|
|
|
str(page_number), operating_sys))
|
2018-03-11 18:53:48 +00:00
|
|
|
|
2020-12-19 19:58:03 +00:00
|
|
|
allmd.clear()
|
2018-03-11 18:53:48 +00:00
|
|
|
|
2020-12-11 20:25:11 +00:00
|
|
|
# Merging all the documents into a single PDF
|
|
|
|
for doc in group:
|
|
|
|
for p in doc.pages:
|
|
|
|
ap.append(p)
|
2018-03-11 18:53:48 +00:00
|
|
|
|
2020-12-11 20:25:11 +00:00
|
|
|
# Writing the PDF to disk, preserving metadata of first `tldr` page
|
|
|
|
group[2].copy(ap).write_pdf('tldr-pages.pdf')
|
2018-03-11 18:53:48 +00:00
|
|
|
|
2020-12-11 20:25:11 +00:00
|
|
|
if os.path.exists("tldr-pages.pdf"):
|
|
|
|
print("\nCreated tldr-pages.pdf in the current directory!\n")
|
2018-03-11 18:53:48 +00:00
|
|
|
|
2020-12-11 20:25:11 +00:00
|
|
|
# Removing unnecessary intermediate files
|
|
|
|
try:
|
|
|
|
os.remove("htmlout.html")
|
|
|
|
os.remove("title.html")
|
|
|
|
os.remove("dir_title.html")
|
|
|
|
except OSError:
|
|
|
|
print("Error removing temporary file(s)")
|
2018-03-11 18:53:48 +00:00
|
|
|
|
|
|
|
|
2019-12-17 20:25:52 +00:00
|
|
|
if __name__ == "__main__":
|
|
|
|
|
2020-12-19 03:52:07 +00:00
|
|
|
# Parsing the arguments
|
2021-01-31 16:27:36 +00:00
|
|
|
parser = argparse.ArgumentParser(prog="tldr-pages-to-pdf", description="A Python script to generate a single PDF document with all the `tldr` pages.")
|
2020-12-19 03:52:07 +00:00
|
|
|
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")
|
|
|
|
args = parser.parse_args()
|
2019-12-17 20:25:52 +00:00
|
|
|
|
2020-12-19 03:52:07 +00:00
|
|
|
main(args.dir_path, args.color)
|