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.

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