From 24c2cf8ccf039312d43974d7461209928cf51349 Mon Sep 17 00:00:00 2001 From: Kyle Anthony Williams Date: Fri, 4 Dec 2020 11:04:25 -0500 Subject: [PATCH 1/9] Added to gitignore to hide a virtual environment. --- .gitignore | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/.gitignore b/.gitignore index 52e19f6e0e..1e7a9994eb 100644 --- a/.gitignore +++ b/.gitignore @@ -18,3 +18,7 @@ index.json # Generated PDF pages scripts/pdf/*.html scripts/pdf/tldr-pages.pdf + +# Python venv for testing the PDF script +# Envoke it with: python3 -m scripts/pdf/venv/ +scripts/pdf/venv/ \ No newline at end of file From e6a86c563c0c37f8ea0fb0855225df1f82fb5b7b Mon Sep 17 00:00:00 2001 From: Kyle Anthony Williams Date: Fri, 4 Dec 2020 11:22:15 -0500 Subject: [PATCH 2/9] Ignore VS Code files. --- .gitignore | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.gitignore b/.gitignore index 1e7a9994eb..2b4b2f78bd 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,6 @@ +# VS Code +.vscode/ + # macOS filesystem custom folder attributes .DS_Store From 954ceb7b987abc0d87eb431a7e4612e679170d2c Mon Sep 17 00:00:00 2001 From: Kyle Anthony Williams Date: Fri, 4 Dec 2020 11:58:27 -0500 Subject: [PATCH 3/9] Made render.py more Pythonic. * Converted the top comment to a multi-line string. * Replaced `i` with `enumerate`. * Gave the `ArgumentParser` a name and description. * Added `basic` as a color choice and made it the default. * Added `color` as a long flag. * Removed no longer used variables and `if` statement. --- scripts/pdf/render.py | 28 ++++++++++------------------ 1 file changed, 10 insertions(+), 18 deletions(-) diff --git a/scripts/pdf/render.py b/scripts/pdf/render.py index 220b679e9e..00e22761dd 100644 --- a/scripts/pdf/render.py +++ b/scripts/pdf/render.py @@ -1,8 +1,10 @@ #!/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 @@ -47,8 +49,6 @@ def main(loc, colorscheme): for operating_sys in oslist: - i = 1 - # Required string to create directory title pages dir_title = "

" + operating_sys.capitalize() + "

" @@ -66,7 +66,7 @@ def main(loc, colorscheme): allmd.sort() # Conversion of Markdown to HTML - for md in allmd: + for page_number, md in enumerate(allmd, start=1): with open(md, "r") as inp: text = inp.readlines() @@ -82,8 +82,7 @@ def main(loc, colorscheme): out.write(footer) group.append(HTML("htmlout.html").render()) - print("Rendered page {} of the directory {}".format(str(i), operating_sys)) - i += 1 + print("Rendered page {} of the directory {}".format(str(page_number), operating_sys)) allmd.clear() @@ -109,17 +108,10 @@ def main(loc, colorscheme): if __name__ == "__main__": - # Unless specified otherwise by the user, this is the default colorscheme - colorscheme = "basic" - # Parsing the arguments - parser = argparse.ArgumentParser() + parser = argparse.ArgumentParser(prog="tdlr-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", choices=["solarized-light", "solarized-dark"], help="Color scheme of the PDF") + parser.add_argument("-c", "--color", choices=["solarized-light", "solarized-dark", "basic"], default="basic", 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) + main(args.dir_path, args.color) From 896555f79e51f7abe6a5610a2943c0f421ec0cf5 Mon Sep 17 00:00:00 2001 From: Kyle Anthony Williams Date: Fri, 4 Dec 2020 12:09:48 -0500 Subject: [PATCH 4/9] Updated the render.py README. * Updated command line usage to: * Reflect changes made to the CLI. * Demonstrate recommended use of pip. * Made some grammar changes. * Added more useful alt text to the images. --- scripts/pdf/README.md | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/scripts/pdf/README.md b/scripts/pdf/README.md index 6ff29a61c0..b93e1b1a10 100644 --- a/scripts/pdf/README.md +++ b/scripts/pdf/README.md @@ -4,9 +4,9 @@ This directory contains the script and related resources to generate a PDF docum ## Preview -![aa](https://user-images.githubusercontent.com/29029116/35637791-4e42af80-06db-11e8-8b8e-42ce6c905ff4.jpg) -![bb](https://user-images.githubusercontent.com/29029116/35637798-51e3784a-06db-11e8-9576-6e57ef5c5c20.jpg) -![cc](https://user-images.githubusercontent.com/29029116/35637801-54449fce-06db-11e8-93f7-d90cdc34044b.jpg) +![cryptsetup in the Basic color-scheme.](https://user-images.githubusercontent.com/29029116/35637791-4e42af80-06db-11e8-8b8e-42ce6c905ff4.jpg) +![cryptsetup in the Solarized Light color-scheme.](https://user-images.githubusercontent.com/29029116/35637798-51e3784a-06db-11e8-9576-6e57ef5c5c20.jpg) +![cryptsetup in the Solarized Dark color-scheme.](https://user-images.githubusercontent.com/29029116/35637801-54449fce-06db-11e8-93f7-d90cdc34044b.jpg) ## Highlights @@ -15,23 +15,24 @@ This directory contains the script and related resources to generate a PDF docum ## Requirements -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: +The PDF is generated by first converting the Markdown files to HTML, and then rendering those HTML files as a PDF. It depends on the `markdown` and `weasyprint` libraries. To install the dependencies, run: - pip3 install -r requirements.txt + python3 -m pip install -r requirements.txt Make sure OS specific dependencies for WeasyPrint are installed by following the instructions [here](http://weasyprint.readthedocs.io/en/latest/install.html). ## Usage -Generating the PDF is as simple as running +Generating the PDF is as simple as running: - python3 render.py -c + python3 render.py --color -Complete information about the arguments can be viewed by running +Complete information about the arguments can be viewed by running: python3 render.py --help -The color-schemes that can be specified are +The color-schemes that can be specified are: +* `basic` * `solarized-light` * `solarized-dark` From 2e9b8a6fb447597b50b14892e7a7ddb8b9cbb250 Mon Sep 17 00:00:00 2001 From: Kyle Anthony Williams Date: Fri, 4 Dec 2020 12:11:50 -0500 Subject: [PATCH 5/9] Added newline to .gitignore. --- .gitignore | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 2b4b2f78bd..2d065bba97 100644 --- a/.gitignore +++ b/.gitignore @@ -24,4 +24,4 @@ scripts/pdf/tldr-pages.pdf # Python venv for testing the PDF script # Envoke it with: python3 -m scripts/pdf/venv/ -scripts/pdf/venv/ \ No newline at end of file +scripts/pdf/venv/ From 42c943d762e4760b452095c159139ad1b44a873e Mon Sep 17 00:00:00 2001 From: Kyle Anthony Williams Date: Fri, 4 Dec 2020 12:17:52 -0500 Subject: [PATCH 6/9] Made an oopsie in the venv instructions. --- .gitignore | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 2d065bba97..62378dfd1b 100644 --- a/.gitignore +++ b/.gitignore @@ -23,5 +23,5 @@ scripts/pdf/*.html scripts/pdf/tldr-pages.pdf # Python venv for testing the PDF script -# Envoke it with: python3 -m scripts/pdf/venv/ +# Create it with: python3 -m venv scripts/pdf/venv/ scripts/pdf/venv/ From 080fe1a01fcc21d84109c4f1ec0ace0873a42275 Mon Sep 17 00:00:00 2001 From: Kyle Anthony Williams Date: Fri, 18 Dec 2020 22:40:29 -0500 Subject: [PATCH 7/9] Changed indent size for Python files. --- .editorconfig | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.editorconfig b/.editorconfig index c6c8b36219..be8416b252 100644 --- a/.editorconfig +++ b/.editorconfig @@ -7,3 +7,6 @@ end_of_line = lf charset = utf-8 trim_trailing_whitespace = true insert_final_newline = true + +[*.py] +indent_size = 4 From 6251e4eb3b4c6885b4ee86765df84d6ebdba0021 Mon Sep 17 00:00:00 2001 From: Kyle Anthony Williams Date: Fri, 18 Dec 2020 22:52:07 -0500 Subject: [PATCH 8/9] Finished resolving of commits. --- scripts/pdf/render.py | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) diff --git a/scripts/pdf/render.py b/scripts/pdf/render.py index 59f4de06ba..80f18be7dc 100644 --- a/scripts/pdf/render.py +++ b/scripts/pdf/render.py @@ -55,8 +55,6 @@ def main(loc, colorscheme): for operating_sys in oslist: - i = 1 - # Required string to create directory title pages dir_title = "

" + \ operating_sys.capitalize() + "

" @@ -92,10 +90,9 @@ def main(loc, colorscheme): group.append(HTML("htmlout.html").render()) print("Rendered page {} of the directory {}".format( - str(i), operating_sys)) - i += 1 + str(page_number), operating_sys)) - allmd.clear() + allmd.clear() # Merging all the documents into a single PDF for doc in group: @@ -119,10 +116,10 @@ def main(loc, colorscheme): if __name__ == "__main__": - # Parsing the arguments - parser = argparse.ArgumentParser(prog="tdlr-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") - args = parser.parse_args() + # Parsing the arguments + parser = argparse.ArgumentParser(prog="tdlr-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") + args = parser.parse_args() - main(args.dir_path, args.color) + main(args.dir_path, args.color) From f320863c9d84f98773d6ec5878820dd69392dc69 Mon Sep 17 00:00:00 2001 From: Kyle Anthony Williams Date: Sat, 19 Dec 2020 14:58:03 -0500 Subject: [PATCH 9/9] Repaired indentation error. --- scripts/pdf/render.py | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/scripts/pdf/render.py b/scripts/pdf/render.py index 80f18be7dc..ff8c32d493 100644 --- a/scripts/pdf/render.py +++ b/scripts/pdf/render.py @@ -72,27 +72,27 @@ def main(loc, colorscheme): # Sorting all filenames in the directory, to maintain the order of the PDF allmd.sort() - # Conversion of Markdown to HTML - for page_number, md in enumerate(allmd, start=1): + # Conversion of Markdown to HTML + for page_number, md in enumerate(allmd, start=1): - with open(md, "r") as inp: - text = inp.readlines() + with open(md, "r") as inp: + text = inp.readlines() - with open("htmlout.html", "w") as out: - out.write(header) + with open("htmlout.html", "w") as out: + out.write(header) - for line in text: - if re.match(r'^>', line): - line = line[:0] + '####' + line[1:] - html = markdown.markdown(line) - out.write(html) - out.write(footer) + for line in text: + if re.match(r'^>', line): + line = line[:0] + '####' + line[1:] + html = markdown.markdown(line) + out.write(html) + out.write(footer) - group.append(HTML("htmlout.html").render()) - print("Rendered page {} of the directory {}".format( - str(page_number), operating_sys)) + group.append(HTML("htmlout.html").render()) + print("Rendered page {} of the directory {}".format( + str(page_number), operating_sys)) - allmd.clear() + allmd.clear() # Merging all the documents into a single PDF for doc in group: