From e2ae50661316e9739032f6c43ddbfe16d9756e73 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Zlatan=20Vasovi=C4=87?= Date: Tue, 17 Dec 2019 21:25:52 +0100 Subject: [PATCH] Rework PDF script (#3654) --- .gitignore | 4 + scripts/pdf/README.md | 21 +-- scripts/pdf/basic.css | 38 ++-- ...b-Regular.ttf => pt-serif-web-regular.ttf} | Bin scripts/pdf/render.py | 169 +++++++++--------- scripts/pdf/requirements.txt | 2 + scripts/pdf/solarized-dark.css | 35 +--- scripts/pdf/solarized-light.css | 34 +--- 8 files changed, 126 insertions(+), 177 deletions(-) rename scripts/pdf/{PT_Serif-Web-Regular.ttf => pt-serif-web-regular.ttf} (100%) create mode 100644 scripts/pdf/requirements.txt diff --git a/.gitignore b/.gitignore index 345d37e83..1836f2433 100644 --- a/.gitignore +++ b/.gitignore @@ -14,3 +14,7 @@ npm-debug.log # hosted at https://tldr-pages.github.io/assets/index.json pages/index.json index.json + +# Generated PDF pages +scripts/pdf/*.html +scripts/pdf/tldr.pdf diff --git a/scripts/pdf/README.md b/scripts/pdf/README.md index 89472a3fe..8bf048da5 100644 --- a/scripts/pdf/README.md +++ b/scripts/pdf/README.md @@ -11,22 +11,14 @@ This directory contains the script and related resources to generate a PDF copy ## Highlights - No LaTeX dependencies for generating the PDF. - -- 3 available color-schemes- *Basic*, *Solarized Light* and *Solarized Dark*. More can be added easily through CSS. - +- 3 available color-schemes: *Basic*, *Solarized Light* and *Solarized Dark*. More can be added easily through CSS. ## Requirements -The PDF is generated by first converting the markdown files to HTML, and then rendering those HTML files as PDF. It depends on the following libraries: +The PDF is generated by first converting the markdown files to HTML, and then rendering those HTML files as PDF. It depends on `markdown` and `weasyprint` libraries. To install the dependencies, run: -#### Python-Markdown + pip3 install -r requirements.txt - pip3 install markdown - -#### WeasyPrint - - pip3 install WeasyPrint - Make sure OS specific dependencies for WeasyPrint are installed by following the instructions [here](http://weasyprint.readthedocs.io/en/latest/install.html). ## Usage @@ -34,8 +26,8 @@ Make sure OS specific dependencies for WeasyPrint are installed by following the Generating the PDF is as simple as running python3 render.py -c - -Complete information about the arguments can be viewed by running + +Complete information about the arguments can be viewed by running python3 render.py --help @@ -43,6 +35,3 @@ The color-schemes that can be specified are * `solarized-light` * `solarized-dark` - - - diff --git a/scripts/pdf/basic.css b/scripts/pdf/basic.css index 1ee793198..916b4f297 100644 --- a/scripts/pdf/basic.css +++ b/scripts/pdf/basic.css @@ -1,34 +1,34 @@ @font-face { - font-family: 'PT_Serif-Web-Regular'; - 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, h4, h2, ul { - font-family: "PT_Serif-Web-Regular"; +h1, h2, h4, ul { + font-family: "PT Serif"; } -h1.titlemain { - text-align: center; - margin-top: 6em; - font-size: 350%; +.title-main { + text-align: center; + margin-top: 6em; + font-size: 350%; } -h4.titlesub { - text-align: center; - font-size: 120%; - color: darkslategrey; +.title-sub { + text-align: center; + font-size: 120%; + color: darkslategrey; } -h2.titledir { - text-align: center; - margin-top: 8.2em; - font-size: 300%; -} \ No newline at end of file +.title-dir { + text-align: center; + margin-top: 8.2em; + font-size: 300%; +} diff --git a/scripts/pdf/PT_Serif-Web-Regular.ttf b/scripts/pdf/pt-serif-web-regular.ttf similarity index 100% rename from scripts/pdf/PT_Serif-Web-Regular.ttf rename to scripts/pdf/pt-serif-web-regular.ttf diff --git a/scripts/pdf/render.py b/scripts/pdf/render.py index 8c9aef925..4369c17a4 100644 --- a/scripts/pdf/render.py +++ b/scripts/pdf/render.py @@ -1,8 +1,8 @@ #!/usr/bin/env python3 -#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. +# 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. import os import sys @@ -15,108 +15,111 @@ 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 = "\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' + header += "\n" + footer = "" + title_content = "

tldr pages

Simplified and community-driven man pages

" - #Creating title page - with open("title.html", 'w') as f: - f.write(header + title_content) + # Creating title page + with open("title.html", "w") as f: + f.write(header + title_content) - group.append(HTML('title.html').render()) + group.append(HTML("title.html").render()) - for operating_sys in oslist: + for operating_sys in oslist: - i = 1 + i = 1 - #Required string to create directory title pages - dir_title = "

" + operating_sys.capitalize() + "

" + # Required string to create directory title pages + dir_title = "

" + operating_sys.capitalize() + "

" - #Creating directory title page for current directory - with open("dir_title.html", 'w') as os_html: - os_html.write(header + dir_title) - - 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 md 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 - - allmd.clear() + for line in text: + if re.match(r'^>', line): + line = line[:0] + '####' + line[1:] + html = markdown.markdown(line) + out.write(html) + out.write(footer) - #Merging all the documents into a single PDF - for doc in group: - for p in doc.pages: - ap.append(p) + group.append(HTML("htmlout.html").render()) + print("Rendered page {} of the directory {}".format(str(i), operating_sys)) + i += 1 - #Writing the PDF to disk, preserving metadata of first tldr page - group[2].copy(ap).write_pdf('tldr.pdf') + allmd.clear() - if os.path.exists("tldr.pdf"): - print("\nCreated tldr.pdf in the current directory!\n") + # Merging all the documents into a single PDF + for doc in group: + for p in doc.pages: + ap.append(p) - #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)") + # Writing the PDF to disk, preserving metadata of first tldr page + group[2].copy(ap).write_pdf('tldr.pdf') + + if os.path.exists("tldr.pdf"): + print("\nCreated tldr.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__': +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 tldr '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 - - main(loc, colorscheme) + # Parsing the arguments + parser = argparse.ArgumentParser() + parser.add_argument("dir_path", help = "Path to tldr '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 + + main(loc, colorscheme) diff --git a/scripts/pdf/requirements.txt b/scripts/pdf/requirements.txt new file mode 100644 index 000000000..815b416b1 --- /dev/null +++ b/scripts/pdf/requirements.txt @@ -0,0 +1,2 @@ +markdown +weasyprint diff --git a/scripts/pdf/solarized-dark.css b/scripts/pdf/solarized-dark.css index 7e7e90d1c..543b51097 100644 --- a/scripts/pdf/solarized-dark.css +++ b/scripts/pdf/solarized-dark.css @@ -1,40 +1,15 @@ -@font-face { - font-family: 'PT_Serif-Web-Regular'; - src: url('PT_Serif-Web-Regular.ttf') format('truetype'); -} - -p { - margin-left: 2.5em; -} - code { - color: #b58900; + color: #b58900; } h1, h2, h4, ul { - font-family: "PT_Serif-Web-Regular"; - color: #93a1a1; + color: #93a1a1; } body { - background-color: #002b36; + background-color: #002b36; } - -h1.titlemain { - text-align: center; - margin-top: 6em; - font-size: 350%; +.title-sub { + color: #b58900; } - -h4.titlesub { - text-align: center; - font-size: 120%; - color: #b58900; -} - -h2.titledir { - text-align: center; - margin-top: 8.2em; - font-size: 300%; -} \ No newline at end of file diff --git a/scripts/pdf/solarized-light.css b/scripts/pdf/solarized-light.css index 7318e346b..eb2ff4fcb 100644 --- a/scripts/pdf/solarized-light.css +++ b/scripts/pdf/solarized-light.css @@ -1,39 +1,15 @@ -@font-face { - font-family: 'PT_Serif-Web-Regular'; - src: url('PT_Serif-Web-Regular.ttf') format('truetype'); -} - -p { - margin-left: 2.5em; -} - code { - color: #dc322f + color: #dc322f } h1, h2, h4, ul { - font-family: "PT_Serif-Web-Regular"; - color: #586e75; + color: #586e75; } body { - background-color: #fdf6e3; + background-color: #fdf6e3; } -h1.titlemain { - text-align: center; - margin-top: 6em; - font-size: 350%; +.title-sub { + color: #dc322f } - -h4.titlesub { - text-align: center; - font-size: 120%; - color: #dc322f -} - -h2.titledir { - text-align: center; - margin-top: 8.2em; - font-size: 300%; -} \ No newline at end of file