render.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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 formating
  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 = "<html><head><link rel=stylesheet type=text/css href=" + colorscheme + ".css></head><body>\n"
  27. footer = "</body></html>"
  28. title_content = "<h1 class=titlemain>tldr pages</h1><h4 class=titlesub>Simplified and community driven man pages</h4></body></html>"
  29. #Creating title page
  30. with open("title.html", 'w') as f:
  31. f.write(header + title_content)
  32. group.append(HTML('title.html').render())
  33. for operating_sys in oslist:
  34. i = 1
  35. #Required string to create directory title pages
  36. dir_title = "<h2 class=titledir>" + operating_sys.capitalize() + "</h2></body></html>"
  37. #Creating directory title page for current directory
  38. with open("dir_title.html", 'w') as os_html:
  39. os_html.write(header + dir_title)
  40. group.append(HTML('dir_title.html').render())
  41. #Creating a list of all md files in the current directory
  42. for temp in glob.glob(os.path.join(loc, operating_sys, '*.md')):
  43. allmd.append(temp)
  44. #Sorting all filenames in the directory, to maintain the order of the PDF
  45. allmd.sort()
  46. #Conversion of md to HTML
  47. for md in allmd:
  48. with open(md, "r") as inp:
  49. text = inp.readlines()
  50. with open("htmlout.html", "w") as out:
  51. out.write(header)
  52. for line in text:
  53. if re.match(r'^>', line):
  54. line = line[:0] + '####' + line[1:]
  55. html = markdown.markdown(line)
  56. out.write(html)
  57. out.write(footer)
  58. group.append(HTML('htmlout.html').render())
  59. print("Rendered page {} of the directory {}".format(str(i), operating_sys))
  60. i += 1
  61. allmd.clear()
  62. #Merging all the documents into a single PDF
  63. for doc in group:
  64. for p in doc.pages:
  65. ap.append(p)
  66. #Writing the PDF to disk, preserving metadata of first tldr page
  67. group[2].copy(ap).write_pdf('tldr.pdf')
  68. if os.path.exists("tldr.pdf"):
  69. print("\nCreated tldr.pdf in the current directory!\n")
  70. #Removing unnecessary intermediate files
  71. try:
  72. os.remove("htmlout.html")
  73. os.remove("title.html")
  74. os.remove("dir_title.html")
  75. except OSError:
  76. print("Error removing temporary file(s)")
  77. if __name__ == '__main__':
  78. #Unless specified otherwise by the user, this is the default colorscheme
  79. colorscheme = "basic"
  80. #Parsing the arguments
  81. parser = argparse.ArgumentParser()
  82. parser.add_argument("dir_path", help = "Path to tldr 'pages' directory")
  83. parser.add_argument("-c", choices=["solarized-light", "solarized-dark"], help="Color scheme of the PDF")
  84. args = parser.parse_args()
  85. loc = args.dir_path
  86. if args.c == "solarized-light" or args.c == "solarized-dark":
  87. colorscheme = args.c
  88. main(loc, colorscheme)