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.

111 lines
4.1 KiB

  1. import datetime
  2. from datetime import datetime
  3. import markdown
  4. import os
  5. from os import path
  6. import pathlib
  7. WEBSITE = 'localhost:5000'
  8. ENTRY_DIR = 'templates/entry'
  9. def gen_arch_string():
  10. path_ex = ENTRY_DIR
  11. if path.exists(path_ex):
  12. name_list = os.listdir(path_ex)
  13. full_list = [os.path.join(path_ex, i) for i in name_list]
  14. contents = sorted(full_list, key=os.path.getctime)
  15. content_string = ''
  16. last_month = ''
  17. for file in reversed(contents):
  18. curr_date = datetime.fromtimestamp(os.path.getctime(file)).strftime('%Y-%m-%d')
  19. curr_month = datetime.fromtimestamp(os.path.getctime(file)).strftime('%b %Y')
  20. if curr_month != last_month:
  21. content_string += '<h2>' + curr_month + '</h2>'
  22. last_month = curr_month
  23. filename = pathlib.PurePath(file)
  24. title = open(filename).readline().rstrip('\n')
  25. filename = filename.name
  26. if filename[0] != '.':
  27. filename = filename.split('.', 1)[0]
  28. content_string += curr_date + ' - '
  29. content_string += '<a href="' + '/index.html#' + \
  30. filename + '">' + title + '</a><br>\n'
  31. return content_string
  32. def gen_index_string():
  33. path_ex = ENTRY_DIR
  34. content_string = ''
  35. if path.exists(path_ex):
  36. name_list = os.listdir(path_ex)
  37. full_list = [os.path.join(path_ex, i) for i in name_list]
  38. contents = sorted(full_list, key=os.path.getctime)
  39. for file in reversed(contents):
  40. filename = pathlib.PurePath(file)
  41. purefile = filename
  42. title = open(filename).readline().rstrip('\n')
  43. text = open(filename).readlines()[1:]
  44. filename = filename.name
  45. if filename[0] != '.':
  46. filename = filename.split('.', 1)[0]
  47. content_string += '<div class=\'entry\'>\n'
  48. content_string += '<h2 id=\'' + filename + '\'>' + title + '</h2>\n'
  49. if file.endswith('.html'):
  50. for line in text:
  51. content_string += line
  52. content_string += '<br>'
  53. if file.endswith('.md'):
  54. content_string += gen_md_content(file)
  55. content_string += '<small>' + \
  56. datetime.fromtimestamp(os.path.getctime(
  57. file)).strftime('%Y-%m-%d') + '</small>'
  58. content_string += '</div>'
  59. return content_string
  60. def gen_md_content(path_ex):
  61. content_string = ''
  62. if path.exists(path_ex):
  63. filename = path_ex.split('.', 1)
  64. fileend = filename[len(filename) - 1]
  65. markdown_file = open(path_ex, "r")
  66. depth = 2
  67. header = '#'
  68. for i in range(depth):
  69. header += '#'
  70. header += ' '
  71. markdown_text = markdown_file.read().replace('# ', header)
  72. content_string = markdown.markdown(
  73. markdown_text, extensions=["fenced_code", "tables"]
  74. )
  75. return content_string
  76. def get_rss_string():
  77. path_ex = ENTRY_DIR
  78. if path.exists(path_ex):
  79. name_list = os.listdir(path_ex)
  80. full_list = [os.path.join(path_ex, i) for i in name_list]
  81. contents = sorted(full_list, key=os.path.getctime)
  82. content_string = ''
  83. for file in reversed(contents):
  84. filename = pathlib.PurePath(file)
  85. title = open(filename).readline().rstrip('\n')
  86. text = open(filename).readlines()[1:]
  87. filename = filename.name
  88. if filename[0] != '.':
  89. filename = filename.split('.', 1)[0]
  90. content_string += '<item>\n'
  91. content_string += '<title>' + title + '</title>\n'
  92. content_string += '<guid>' + '/index.html#' + filename + '</guid>\n'
  93. content_string += '<pubDate>' + \
  94. datetime.fromtimestamp(os.path.getctime(file)).strftime(
  95. '%Y-%m-%d') + '</pubDate>\n'
  96. content_string += '<description>'
  97. for line in text:
  98. content_string += line
  99. content_string += '</description>\n'
  100. content_string += '</item>\n'
  101. return content_string