render.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. #!/usr/bin/env python3
  2. # SPDX-License-Identifier: MIT
  3. """
  4. A Python script to generate a single PDF document with all the `tldr` pages. It works by generating
  5. intermediate HTML files from existing md files using Python-markdown, applying desired formatting
  6. through CSS, and finally rendering them as PDF. There is no LaTeX dependency for generating the PDF.
  7. """
  8. import os
  9. import sys
  10. import glob
  11. import markdown
  12. import argparse
  13. from datetime import datetime
  14. from weasyprint import HTML
  15. def main(loc, colorscheme, output_filename, platform):
  16. # Checking correctness of path
  17. if not os.path.isdir(loc):
  18. print("Invalid directory. Please try again!", file=sys.stderr)
  19. sys.exit(1)
  20. # Set up css style sheets
  21. csslist = ["basic.css"]
  22. if colorscheme != "basic":
  23. csslist.append(f"{colorscheme}.css")
  24. # A string that stores all pages in HTML format
  25. html = (
  26. '<!doctype html><html><head><meta charset="utf-8"></head>'
  27. + "<body><h1 class=title-main>tldr pages book</h1>"
  28. + "<div class=title-sub>Simplified and community-driven man pages</div>"
  29. + "<div class=title-sub><em><small>Generated on "
  30. + datetime.now().strftime("%c")
  31. + "</small></em></div><br><br>"
  32. + "<div class=title-sub>Website: <a href=https://tldr.sh>https://tldr.sh</a></div><br>"
  33. + "<div class=title-sub>GitHub: <a href=https://github.com/tldr-pages/tldr>https://github.com/tldr-pages/tldr</a></div><br>"
  34. + '<p style="page-break-before: always" ></p>'
  35. )
  36. # Writing names of all directories inside 'pages' to a list
  37. for operating_sys in sorted(os.listdir(loc)):
  38. if platform and operating_sys not in platform:
  39. continue
  40. # Required string to create directory title pages
  41. html += (
  42. "<h1 class=title-dir>"
  43. + operating_sys.capitalize()
  44. + "</h1>"
  45. + '<p style="page-break-before: always" ></p>'
  46. )
  47. # Conversion of Markdown to HTML string
  48. for page_number, md in enumerate(
  49. sorted(glob.glob(os.path.join(loc, operating_sys, "*.md"))), start=1
  50. ):
  51. with open(md, "r") as inp:
  52. text = inp.readlines()
  53. # modify our page to have an H2 header, so that it is grouped under
  54. # the H1 header for the directory
  55. text[0] = "<h2 class='title-page'>" + text[0][2:] + "</h2>"
  56. for line in text:
  57. if line.startswith(">"):
  58. line = "####" + line[1:]
  59. html += markdown.markdown(line)
  60. html += '<p style="page-break-before: always" ></p>'
  61. print(f"Rendered page {page_number} of the directory {operating_sys}")
  62. html += "</body></html>"
  63. # Writing the PDF to disk
  64. if platform:
  65. output_filename = f"{output_filename[:-4]}-{'+'.join(platform)}.pdf"
  66. print("\nConverting all pages to PDF...")
  67. HTML(string=html).write_pdf(output_filename, stylesheets=csslist)
  68. if os.path.exists(output_filename):
  69. print(f"\nCreated {output_filename} in the current directory!\n")
  70. if __name__ == "__main__":
  71. # Parsing the arguments
  72. parser = argparse.ArgumentParser(
  73. prog="tldr-pages-to-pdf",
  74. description="A Python script to generate a single PDF document with all the `tldr` pages.",
  75. )
  76. parser.add_argument("dir_path", help="Path to the 'pages' directory")
  77. parser.add_argument(
  78. "-c",
  79. "--color",
  80. choices=["solarized-light", "solarized-dark", "basic"],
  81. default="basic",
  82. help="Color scheme of the PDF",
  83. )
  84. parser.add_argument(
  85. "-o",
  86. "--output",
  87. default="tldr-book.pdf",
  88. help="Custom filename for the output PDF (default is 'tldr-book.pdf')",
  89. )
  90. parser.add_argument(
  91. "-p",
  92. "--platform",
  93. nargs="+",
  94. help="Specify one or more platforms to generate PDFs for",
  95. )
  96. args = parser.parse_args()
  97. main(args.dir_path, args.color, args.output, args.platform)