diff --git a/scripts/pdf/basic.css b/scripts/pdf/basic.css index 916b4f297..76bc255b5 100644 --- a/scripts/pdf/basic.css +++ b/scripts/pdf/basic.css @@ -1,34 +1,34 @@ @font-face { - font-family: "PT Serif"; - src: url("pt-serif-web-regular.ttf") format("truetype"); + font-family: "PT Serif"; + src: url("pt-serif-web-regular.ttf") format("truetype"); } p { - margin-left: 2.5em; + margin-left: 2.5em; } code { - color: darkslategrey; + color: darkslategrey; } h1, h2, h4, ul { - font-family: "PT Serif"; + font-family: "PT Serif"; } .title-main { - text-align: center; - margin-top: 6em; - font-size: 350%; + text-align: center; + margin-top: 6em; + font-size: 350%; } .title-sub { - text-align: center; - font-size: 120%; - color: darkslategrey; + text-align: center; + font-size: 120%; + color: darkslategrey; } .title-dir { - text-align: center; - margin-top: 8.2em; - font-size: 300%; + text-align: center; + margin-top: 8.2em; + font-size: 300%; } diff --git a/scripts/pdf/render.py b/scripts/pdf/render.py index 220b679e9..3af17e96c 100644 --- a/scripts/pdf/render.py +++ b/scripts/pdf/render.py @@ -10,116 +10,126 @@ import glob import re import markdown import argparse +from datetime import datetime from weasyprint import HTML + def main(loc, colorscheme): - oslist = [] - allmd = [] - group = [] - ap = [] + 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) + # Checking correctness of path + if not os.path.isdir(loc): + print("Invalid directory. Please try again!", file=sys.stderr) + sys.exit(1) - # Writing names of all directories inside 'pages' to a list - for os_dir in os.listdir(loc): - oslist.append(os_dir) + # Writing names of all directories inside 'pages' to a list + for os_dir in os.listdir(loc): + oslist.append(os_dir) - oslist.sort() + oslist.sort() - # Required strings to create intermediate HTML files - header = '' - if colorscheme != "basic": - header += '\n' - header += "\n" - footer = "" - title_content = "

tldr pages

Simplified and community-driven man pages

" + # Required strings to create intermediate HTML files + header = '' + if colorscheme != "basic": + header += '\n' - # Creating title page - with open("title.html", "w") as f: - f.write(header + title_content) + header += "\n" + footer = "" + title_content = "

tldr pages

" \ + + "

Simplified and community-driven man pages

" \ + + "
Generated on " + datetime.now().strftime("%c") + "
" \ + + "" - 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 - dir_title = "

" + operating_sys.capitalize() + "

" + i = 1 - # Creating directory title page for current directory - with open("dir_title.html", "w") as os_html: - os_html.write(header + dir_title) + # Required string to create directory title pages + dir_title = "

" + \ + operating_sys.capitalize() + "

" - 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 - for temp in glob.glob(os.path.join(loc, operating_sys, "*.md")): - allmd.append(temp) + group.append(HTML("dir_title.html").render()) - # Sorting all filenames in the directory, to maintain the order of the PDF - allmd.sort() + # 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) - # Conversion of Markdown to HTML - for md in allmd: + # Sorting all filenames in the directory, to maintain the order of the PDF + allmd.sort() - with open(md, "r") as inp: - text = inp.readlines() + # Conversion of Markdown to HTML + for md in allmd: - with open("htmlout.html", "w") as out: - out.write(header) + with open(md, "r") as inp: + text = inp.readlines() - for line in text: - if re.match(r'^>', line): - line = line[:0] + '####' + line[1:] - html = markdown.markdown(line) - out.write(html) - out.write(footer) + with open("htmlout.html", "w") as out: + out.write(header) - group.append(HTML("htmlout.html").render()) - print("Rendered page {} of the directory {}".format(str(i), operating_sys)) - i += 1 + for line in text: + if re.match(r'^>', line): + 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 - for doc in group: - for p in doc.pages: - ap.append(p) + allmd.clear() - # Writing the PDF to disk, preserving metadata of first `tldr` page - group[2].copy(ap).write_pdf('tldr-pages.pdf') + # Merging all the documents into a single PDF + for doc in group: + for p in doc.pages: + ap.append(p) - if os.path.exists("tldr-pages.pdf"): - print("\nCreated tldr-pages.pdf in the current directory!\n") + # Writing the PDF to disk, preserving metadata of first `tldr` page + group[2].copy(ap).write_pdf('tldr-pages.pdf') - # 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)") + if os.path.exists("tldr-pages.pdf"): + print("\nCreated tldr-pages.pdf in the current directory!\n") + + # 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)") if __name__ == "__main__": - # Unless specified otherwise by the user, this is the default colorscheme - colorscheme = "basic" + # Unless specified otherwise by the user, this is the default colorscheme + colorscheme = "basic" - # Parsing the arguments - parser = argparse.ArgumentParser() - 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") - args = parser.parse_args() + # Parsing the arguments + parser = argparse.ArgumentParser() + 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") + args = parser.parse_args() - loc = args.dir_path - if args.c == "solarized-light" or args.c == "solarized-dark": - colorscheme = args.c + loc = args.dir_path + if args.c == "solarized-light" or args.c == "solarized-dark": + colorscheme = args.c - main(loc, colorscheme) + main(loc, colorscheme)