render.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. #!/usr/bin/env python3
  2. # A Python script to generate a single PDF document with all the tldr pages. It works by generating
  3. # intermediate HTML files from existing md files using Python-markdown, applying desired formatting
  4. # through CSS, and finally rendering them as PDF. There is no LaTeX dependency for generating the PDF.
  5. import os
  6. import sys
  7. import glob
  8. import re
  9. import markdown
  10. import argparse
  11. from weasyprint import HTML
  12. def main(loc, colorscheme):
  13. oslist = []
  14. allmd = []
  15. group = []
  16. ap = []
  17. # Checking correctness of path
  18. if not os.path.isdir(loc):
  19. print("Invalid directory. Please try again!", file = sys.stderr)
  20. sys.exit(1)
  21. # Writing names of all directories inside 'pages' to a list
  22. for os_dir in os.listdir(loc):
  23. oslist.append(os_dir)
  24. oslist.sort()
  25. # Required strings to create intermediate HTML files
  26. header = '<!doctype html><html><head><meta charset="utf-8"><link rel="stylesheet" href="basic.css">'
  27. if colorscheme != "basic":
  28. header += '<link rel="stylesheet" href="' + colorscheme + '.css"></head><body>\n'
  29. header += "</head><body>\n"
  30. footer = "</body></html>"
  31. title_content = "<h1 class=title-main>tldr pages</h1><h4 class=title-sub>Simplified and community-driven man pages</h4></body></html>"
  32. # Creating title page
  33. with open("title.html", "w") as f:
  34. f.write(header + title_content)
  35. group.append(HTML("title.html").render())
  36. for operating_sys in oslist:
  37. i = 1
  38. # Required string to create directory title pages
  39. dir_title = "<h2 class=title-dir>" + operating_sys.capitalize() + "</h2></body></html>"
  40. # Creating directory title page for current directory
  41. with open("dir_title.html", "w") as os_html:
  42. os_html.write(header + dir_title)
  43. group.append(HTML("dir_title.html").render())
  44. # Creating a list of all md files in the current directory
  45. for temp in glob.glob(os.path.join(loc, operating_sys, "*.md")):
  46. allmd.append(temp)
  47. # Sorting all filenames in the directory, to maintain the order of the PDF
  48. allmd.sort()
  49. # Conversion of Markdown to HTML
  50. for md in allmd:
  51. with open(md, "r") as inp:
  52. text = inp.readlines()
  53. with open("htmlout.html", "w") as out:
  54. out.write(header)
  55. for line in text:
  56. if re.match(r'^>', line):
  57. line = line[:0] + '####' + line[1:]
  58. html = markdown.markdown(line)
  59. out.write(html)
  60. out.write(footer)
  61. group.append(HTML("htmlout.html").render())
  62. print("Rendered page {} of the directory {}".format(str(i), operating_sys))
  63. i += 1
  64. allmd.clear()
  65. # Merging all the documents into a single PDF
  66. for doc in group:
  67. for p in doc.pages:
  68. ap.append(p)
  69. # Writing the PDF to disk, preserving metadata of first tldr page
  70. group[2].copy(ap).write_pdf('tldr.pdf')
  71. if os.path.exists("tldr.pdf"):
  72. print("\nCreated tldr.pdf in the current directory!\n")
  73. # Removing unnecessary intermediate files
  74. try:
  75. os.remove("htmlout.html")
  76. os.remove("title.html")
  77. os.remove("dir_title.html")
  78. except OSError:
  79. print("Error removing temporary file(s)")
  80. if __name__ == "__main__":
  81. # Unless specified otherwise by the user, this is the default colorscheme
  82. colorscheme = "basic"
  83. # Parsing the arguments
  84. parser = argparse.ArgumentParser()
  85. parser.add_argument("dir_path", help = "Path to tldr 'pages' directory")
  86. parser.add_argument("-c", choices=["solarized-light", "solarized-dark"], help="Color scheme of the PDF")
  87. args = parser.parse_args()
  88. loc = args.dir_path
  89. if args.c == "solarized-light" or args.c == "solarized-dark":
  90. colorscheme = args.c
  91. main(loc, colorscheme)