pdf script: format & add datetime (#4993)

This commit is contained in:
Starbeamrainbowlabs
2020-12-11 20:25:11 +00:00
committed by GitHub
parent bd07d874ee
commit 5b5b590ee2
2 changed files with 102 additions and 92 deletions

View File

@@ -1,34 +1,34 @@
@font-face { @font-face {
font-family: "PT Serif"; font-family: "PT Serif";
src: url("pt-serif-web-regular.ttf") format("truetype"); src: url("pt-serif-web-regular.ttf") format("truetype");
} }
p { p {
margin-left: 2.5em; margin-left: 2.5em;
} }
code { code {
color: darkslategrey; color: darkslategrey;
} }
h1, h2, h4, ul { h1, h2, h4, ul {
font-family: "PT Serif"; font-family: "PT Serif";
} }
.title-main { .title-main {
text-align: center; text-align: center;
margin-top: 6em; margin-top: 6em;
font-size: 350%; font-size: 350%;
} }
.title-sub { .title-sub {
text-align: center; text-align: center;
font-size: 120%; font-size: 120%;
color: darkslategrey; color: darkslategrey;
} }
.title-dir { .title-dir {
text-align: center; text-align: center;
margin-top: 8.2em; margin-top: 8.2em;
font-size: 300%; font-size: 300%;
} }

View File

@@ -10,116 +10,126 @@ import glob
import re import re
import markdown import markdown
import argparse import argparse
from datetime import datetime
from weasyprint import HTML from weasyprint import HTML
def main(loc, colorscheme): def main(loc, colorscheme):
oslist = [] oslist = []
allmd = [] allmd = []
group = [] group = []
ap = [] ap = []
# Checking correctness of path # Checking correctness of path
if not os.path.isdir(loc): if not os.path.isdir(loc):
print("Invalid directory. Please try again!", file = sys.stderr) print("Invalid directory. Please try again!", file=sys.stderr)
sys.exit(1) sys.exit(1)
# Writing names of all directories inside 'pages' to a list # Writing names of all directories inside 'pages' to a list
for os_dir in os.listdir(loc): for os_dir in os.listdir(loc):
oslist.append(os_dir) oslist.append(os_dir)
oslist.sort() oslist.sort()
# Required strings to create intermediate HTML files # Required strings to create intermediate HTML files
header = '<!doctype html><html><head><meta charset="utf-8"><link rel="stylesheet" href="basic.css">' header = '<!doctype html><html><head><meta charset="utf-8"><link rel="stylesheet" href="basic.css">'
if colorscheme != "basic": if colorscheme != "basic":
header += '<link rel="stylesheet" href="' + colorscheme + '.css"></head><body>\n' header += '<link rel="stylesheet" href="' + colorscheme + '.css"></head><body>\n'
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></body></html>"
# Creating title page header += "</head><body>\n"
with open("title.html", "w") as f: footer = "</body></html>"
f.write(header + title_content) 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>"
group.append(HTML("title.html").render()) # Creating title page
with open("title.html", "w") as f:
f.write(header + title_content)
for operating_sys in oslist: group.append(HTML("title.html").render())
i = 1 for operating_sys in oslist:
# Required string to create directory title pages i = 1
dir_title = "<h2 class=title-dir>" + operating_sys.capitalize() + "</h2></body></html>"
# Creating directory title page for current directory # Required string to create directory title pages
with open("dir_title.html", "w") as os_html: dir_title = "<h2 class=title-dir>" + \
os_html.write(header + dir_title) operating_sys.capitalize() + "</h2></body></html>"
group.append(HTML("dir_title.html").render()) # Creating directory title page for current directory
with open("dir_title.html", "w") as os_html:
os_html.write(header + dir_title)
# Creating a list of all md files in the current directory group.append(HTML("dir_title.html").render())
for temp in glob.glob(os.path.join(loc, operating_sys, "*.md")):
allmd.append(temp)
# Sorting all filenames in the directory, to maintain the order of the PDF # Creating a list of all md files in the current directory
allmd.sort() for temp in glob.glob(os.path.join(loc, operating_sys, "*.md")):
allmd.append(temp)
# Conversion of Markdown to HTML # Sorting all filenames in the directory, to maintain the order of the PDF
for md in allmd: allmd.sort()
with open(md, "r") as inp: # Conversion of Markdown to HTML
text = inp.readlines() for md in allmd:
with open("htmlout.html", "w") as out: with open(md, "r") as inp:
out.write(header) text = inp.readlines()
for line in text: with open("htmlout.html", "w") as out:
if re.match(r'^>', line): out.write(header)
line = line[:0] + '####' + line[1:]
html = markdown.markdown(line)
out.write(html)
out.write(footer)
group.append(HTML("htmlout.html").render()) for line in text:
print("Rendered page {} of the directory {}".format(str(i), operating_sys)) if re.match(r'^>', line):
i += 1 line = line[:0] + '####' + line[1:]
html = markdown.markdown(line)
out.write(html)
out.write(footer)
allmd.clear() group.append(HTML("htmlout.html").render())
print("Rendered page {} of the directory {}".format(
str(i), operating_sys))
i += 1
# Merging all the documents into a single PDF allmd.clear()
for doc in group:
for p in doc.pages:
ap.append(p)
# Writing the PDF to disk, preserving metadata of first `tldr` page # Merging all the documents into a single PDF
group[2].copy(ap).write_pdf('tldr-pages.pdf') for doc in group:
for p in doc.pages:
ap.append(p)
if os.path.exists("tldr-pages.pdf"): # Writing the PDF to disk, preserving metadata of first `tldr` page
print("\nCreated tldr-pages.pdf in the current directory!\n") group[2].copy(ap).write_pdf('tldr-pages.pdf')
# Removing unnecessary intermediate files if os.path.exists("tldr-pages.pdf"):
try: print("\nCreated tldr-pages.pdf in the current directory!\n")
os.remove("htmlout.html")
os.remove("title.html") # Removing unnecessary intermediate files
os.remove("dir_title.html") try:
except OSError: os.remove("htmlout.html")
print("Error removing temporary file(s)") os.remove("title.html")
os.remove("dir_title.html")
except OSError:
print("Error removing temporary file(s)")
if __name__ == "__main__": if __name__ == "__main__":
# Unless specified otherwise by the user, this is the default colorscheme # Unless specified otherwise by the user, this is the default colorscheme
colorscheme = "basic" colorscheme = "basic"
# Parsing the arguments # Parsing the arguments
parser = argparse.ArgumentParser() parser = argparse.ArgumentParser()
parser.add_argument("dir_path", help = "Path to the 'pages' directory") parser.add_argument("dir_path", help="Path to the 'pages' directory")
parser.add_argument("-c", choices=["solarized-light", "solarized-dark"], help="Color scheme of the PDF") parser.add_argument("-c",
args = parser.parse_args() choices=["solarized-light", "solarized-dark"],
help="Color scheme of the PDF")
args = parser.parse_args()
loc = args.dir_path loc = args.dir_path
if args.c == "solarized-light" or args.c == "solarized-dark": if args.c == "solarized-light" or args.c == "solarized-dark":
colorscheme = args.c colorscheme = args.c
main(loc, colorscheme) main(loc, colorscheme)