A blog system based on plain-files.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

108 lines
3.8 KiB

7 months ago
7 months ago
7 months ago
  1. from flask import Flask, flash, make_response, render_template, request, redirect
  2. import datetime
  3. from datetime import datetime
  4. import os
  5. from os import path
  6. import pathlib
  7. app = Flask(__name__)
  8. website = 'localhost:5000'
  9. @app.errorhandler(404)
  10. def page_not_found(e):
  11. return render_template('error.html', title='Error 404', errorcode='404'), 404
  12. @app.route('/')
  13. @app.route('/index.html')
  14. def index():
  15. content = gen_index_string()
  16. print('content is: ', content)
  17. return render_template('index.html', title='Blog', content_string=content)
  18. @app.route('/archive')
  19. @app.route('/archive.html')
  20. def blog_archive():
  21. content = gen_arch_string()
  22. return render_template('archive.html', title='Blog Archive', content_string=content)
  23. @app.route('/feed.xml')
  24. @app.route('/rss.xml')
  25. def feed():
  26. content = get_rss_string()
  27. rss_xml = render_template('rss.xml', content_string=content)
  28. response = make_response(rss_xml)
  29. response.headers['Content-Type'] = 'application/rss+xml'
  30. return response
  31. def gen_arch_string():
  32. path_ex = 'templates/entry'
  33. if path.exists(path_ex):
  34. name_list = os.listdir(path_ex)
  35. full_list = [os.path.join(path_ex,i) for i in name_list]
  36. contents = sorted(full_list, key=os.path.getctime)
  37. content_string = ''
  38. for file in contents:
  39. filename = pathlib.PurePath(file)
  40. title = open(filename).readline().rstrip('\n')
  41. filename = filename.name
  42. if filename[0] != '.':
  43. filename = filename.split('.',1)[0]
  44. content_string += '<a href="' + '/index.html#' + filename + '">' + title + '</a><br>\n'
  45. return content_string
  46. def gen_index_string():
  47. path_ex = 'templates/entry'
  48. content_string = ''
  49. if path.exists(path_ex):
  50. name_list = os.listdir(path_ex)
  51. full_list = [os.path.join(path_ex,i) for i in name_list]
  52. contents = sorted(full_list, key=os.path.getctime)
  53. for file in contents:
  54. filename = pathlib.PurePath(file)
  55. title = open(filename).readline().rstrip('\n')
  56. text = open(filename).readlines()[1:]
  57. filename = filename.name
  58. if filename[0] != '.':
  59. filename = filename.split('.',1)[0]
  60. content_string += '<div class=\'entry\'>\n'
  61. content_string += '<h2 id=\'' + filename + '\'>' + title + '</h2>\n'
  62. for line in text:
  63. content_string += line
  64. content_string += '<br><small>' + datetime.fromtimestamp(os.path.getctime(file)).strftime('%Y-%m-%d') + '</small>'
  65. content_string += '</div>'
  66. return content_string
  67. def get_rss_string():
  68. path_ex = 'templates/entry'
  69. if path.exists(path_ex):
  70. name_list = os.listdir(path_ex)
  71. full_list = [os.path.join(path_ex,i) for i in name_list]
  72. contents = sorted(full_list, key=os.path.getctime)
  73. content_string = ''
  74. for file in contents:
  75. filename = pathlib.PurePath(file)
  76. title = open(filename).readline().rstrip('\n')
  77. text = open(filename).readlines()[1:]
  78. filename = filename.name
  79. if filename[0] != '.':
  80. filename = filename.split('.',1)[0]
  81. content_string += '<item>\n'
  82. content_string += '<title>' + title + '</title>\n'
  83. content_string += '<guid>' + website + '/index.html#' + filename + '</guid>\n'
  84. content_string += '<pubDate>' + datetime.fromtimestamp(os.path.getctime(file)).strftime('%Y-%m-%d') + '</pubDate>\n'
  85. content_string += '<description>'
  86. for line in text:
  87. content_string += line
  88. content_string += '</description>\n'
  89. content_string += '</item>\n'
  90. return content_string
  91. if __name__ == '__main__':
  92. app.run(host='0.0.0.0')