blog.py 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. # blog.py
  2. # A general purpose blog rss feed generator.
  3. # Supports input .md, .org and basic .html files.
  4. import os.path
  5. import time
  6. import sys
  7. import re
  8. from config import *
  9. from pathlib import Path
  10. def mini_help():
  11. print('''An incorrect or no argument was passed.
  12. +---------------+---------------+------------------+
  13. |Short options: | Long options: | Meaning: |
  14. +---------------+---------------+------------------+
  15. | | | New File in Blog |
  16. | -g | --generate | Get NEW RSS file |
  17. | -h | --help | Full Help |
  18. | -e | --export | Export org files |
  19. +---------------+---------------+------------------+''')
  20. if len(sys.argv) == 1:
  21. files = os.listdir(blog_dir)
  22. paths = [os.path.join(blog_dir, basename) for basename in files]
  23. genfile = max(paths, key=os.path.getctime)
  24. print(f"Pressing enter the file will be {genfile}")
  25. file = input("File: ")
  26. print("---")
  27. if file == "":
  28. file = genfile
  29. try:
  30. with open(file) as f:
  31. text = f.read()
  32. except:
  33. print("ERROR: Can not find/read file")
  34. quit()
  35. # Generate RSS description
  36. if file.endswith(".md") == True:
  37. import markdown
  38. description = markdown.markdown(text).replace("\n","")
  39. elif file.endswith(".org") == True:
  40. from orgpython import to_html
  41. description = to_html(text, toc=False, offset=0, highlight=True).replace("\n","")
  42. print(description)
  43. elif file.endswith(".html") == True:
  44. # This is only for very basic html documents. More complicated
  45. # ones, probably won't work.
  46. description = text.replace("\n","")
  47. description = re.sub("<head>.*?</head>","",description)
  48. description = description.replace("<body>","").replace("</body>","").replace("<!DOCTYPE html>","").replace("</html>","")
  49. else:
  50. print("ERROR: This file extension is not supported :(")
  51. quit()
  52. # Get other necassary information
  53. gentitle = re.findall("<h1.+?>.+?</h1>", description)
  54. gentitle = gentitle[0].replace("&#39;","")
  55. clean = re.compile('<.*?>')
  56. gentitle = re.sub(clean, '', gentitle)
  57. print(f"Press enter and the title will be \"{gentitle}\"")
  58. title = input("Title: ")
  59. if title == "":
  60. title = gentitle
  61. print(f"---\nPress enter and the article link will be {website_dir}{file.replace(blog_dir,'')}")
  62. link = input("Link: ")
  63. if link == "":
  64. link = f"{website_dir}{file.replace(blog_dir,'')}"
  65. date = str(time.ctime(os.path.getmtime(file)))
  66. #from time import gmtime, strftime
  67. #date = str(strftime("%a, %d %b %Y %X"))
  68. try:
  69. with open(rssfile) as r:
  70. text = r.read()
  71. text = text.splitlines()
  72. text = text[:-2]
  73. text = '\n'.join(text)
  74. except:
  75. print("ERROR: could not find/read RSS file")
  76. with open(rssfile, "w") as w:
  77. w.write(text)
  78. with open(rssfile, 'a') as f:
  79. f.write(f"""
  80. <item>
  81. <title>{title}</title>
  82. <link>{link}</link>
  83. <guid>{link}</guid>
  84. <pubDate>{date}</pubDate>
  85. <description>
  86. <![CDATA[ {description} ]]>
  87. </description>
  88. </item>
  89. </channel>
  90. </rss>""")
  91. f = open('rss.xml', 'r')
  92. content = f.read()
  93. print(content)
  94. f.close()
  95. with open(blog_file) as r:
  96. text = r.read()
  97. # THE IKSVO BASED REPLACE "the only form of regex I actually use
  98. # is .replace" --iksvo
  99. replacement = f"""<!-- placeholder -->
  100. <li><a href=\"{link}\">{title}<a></li>"""
  101. text = text.replace("<!-- placeholder -->",replacement)
  102. with open(blog_file, "w") as w:
  103. w.write(text)
  104. elif sys.argv[1] == "-g" or sys.argv[1] == "--generate":
  105. try:
  106. file = open(rssfile)
  107. print(f"THIS WILL OVERWIRTE THE CURRENT {rssfile} FILE")
  108. except:
  109. print(f"{rss.xml} doesn't exist, going to create a new one.")
  110. rsstitle = input("Title for your rss feed: ")
  111. rssdescription = input("---\nDescription for your rss feed: ")
  112. rsslanguage = "en-us"
  113. rsssite = input("---\nWebsite: ")
  114. print("---\nIf you have a image/favicon link. Press enter for no image/favicon")
  115. rssimage = input("image/favicon link: ")
  116. if rssimage == "":
  117. rssimage = ""
  118. with open(rssfile, "w") as f:
  119. f.write(f"""<?xml version=\"1.0\" encoding=\"utf-8\"?>
  120. <rss version=\"2.0\">
  121. <channel>
  122. <title>{rsstitle}</title>
  123. <description>{rssdescription}</description>
  124. <language>{rsslanguage}</language>
  125. <link>{rsssite}</link>""")
  126. if rssimage == "":
  127. pass
  128. else:
  129. f.write(f"""
  130. <image>
  131. <title>{rsstitle}</title>
  132. <url>{rssimage}</url>
  133. </image>""")
  134. f.write("""
  135. </channel>
  136. </rss>""")
  137. print("""
  138. Now, you should have a working rss file to add blog entries to.
  139. python blog.py FILE to add new content to the rss file.""")
  140. elif sys.argv[1] == "-h" or sys.argv[1] == "--help":
  141. print("""Stetup:
  142. - Make sure you have both this script \"blog.py\" and also another file called \"config.py\"
  143. - Change stuff in config.py to work for your setup
  144. - If you don't have a RSS/rss.xml file you can run \"python blog.py -g\" or \"python blog.py --generate\". Answer the questions and now you have a RSS file. WOW!
  145. - In your index.html file write \"<!-- placeholder -->\" for where you want the link to the article to be.
  146. - Run \"python blog.py\" to add a new entry. Answer the questions. Press enter and you can use geussed file, link and title.
  147. - Your done, you now have rss!
  148. Article files supported:
  149. - .md with the markdown extention
  150. - .org with the orgpython extention
  151. - .html very simple html documents that follow a certain format
  152. Other options in this program:
  153. -g as mentioned a bit in the setup will initialize a rss.xml file for you.
  154. -e will export documents in the blog_dir into html. Requires pandoc.
  155. This program is kind of for my setup and how I manage my website. However you can try to contact me on matrix @trueauracoral:tchncs.de for help in this script.""")
  156. quit()
  157. elif sys.argv[1] == "-e" or sys.argv[1] == "--export":
  158. files = sorted(Path(blog_dir+markup).iterdir(), key=os.path.getmtime)
  159. for file in files:
  160. export = str(file).replace(f".{markup}","").replace(f"{markup}\\","").replace(f"{markup}/","")
  161. print(export)
  162. command = f'pandoc -s -c {blog_dir}styles.css {file} -o {export}.html --highlight-style=tango'
  163. os.system(command)
  164. else:
  165. mini_help()
  166. quit()