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.

149 lines
5.6 KiB

7 months ago
7 months ago
7 months ago
7 months ago
7 months ago
  1. import datetime
  2. from datetime import datetime
  3. import markdown
  4. import os
  5. from os import path
  6. import pathlib
  7. import config
  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 += title + ' ['
  36. content_string += '<a href="' + '/index.html#' + \
  37. filename + '">' + 'link' + '</a> - '
  38. content_string += '<a href="' + '/entry/' + \
  39. pathlib.PurePath(file).name + '">' + 'standalone' + '</a>'
  40. content_string += '] <br>'
  41. content_string += '</li>\n'
  42. content_string += '</ul>\n'
  43. return content_string
  44. def gen_index_string():
  45. path_ex = ENTRY_DIR
  46. content_string = ''
  47. if path.exists(path_ex):
  48. name_list = os.listdir(path_ex)
  49. full_list = [os.path.join(path_ex, i) for i in name_list]
  50. contents = sorted(full_list, key=os.path.getctime)
  51. for file in reversed(contents):
  52. filename = pathlib.PurePath(file)
  53. purefile = filename
  54. title = open(filename).readline().rstrip('\n')
  55. text = open(filename).readlines()[1:]
  56. filename = filename.name
  57. if filename[0] != '.':
  58. filename = filename.split('.', 1)[0]
  59. content_string += '<div class=\'entry\'>\n'
  60. content_string += '<h2 id=\'' + filename + '\'>' + title + '</h2>\n'
  61. content_string += '[<a href="' + '/entry/' + \
  62. pathlib.PurePath(file).name + '">' + \
  63. 'standalone' + '</a>]<br>\n'
  64. if file.endswith('.html'):
  65. for line in text:
  66. content_string += line
  67. content_string += '<br>'
  68. if file.endswith('.md'):
  69. content_string += gen_md_content(file, 2)
  70. content_string += '<small>' + \
  71. datetime.fromtimestamp(os.path.getctime(
  72. file)).strftime('%Y-%m-%d') + '</small>'
  73. content_string += '</div>'
  74. return content_string
  75. def gen_stand_string(path_ex):
  76. filename = os.path.join(ENTRY_DIR, path_ex)
  77. content_string = ''
  78. if path.exists(filename):
  79. title = open(filename).readline().rstrip('\n')
  80. text = open(filename).readlines()[1:]
  81. filename_no_end = filename.split('.', 1)[0]
  82. content_string += '<h1>' + title + '</h1>\n'
  83. content_string += '['
  84. content_string += '<a href="' + '/index.html#' + \
  85. filename_no_end + '">' + 'link' + '</a>'
  86. content_string += ']<br>\n'
  87. if filename.endswith('.html'):
  88. for line in text:
  89. content_string += line
  90. content_string += '<br>'
  91. if filename.endswith('.md'):
  92. content_string += gen_md_content(filename, 1)
  93. return content_string
  94. def gen_md_content(path_ex, depth):
  95. content_string = ''
  96. if path.exists(path_ex):
  97. filename = path_ex.split('.', 1)
  98. fileend = filename[len(filename) - 1]
  99. header = '#'
  100. for i in range(depth):
  101. header += '#'
  102. header += ' '
  103. markdown_lines = open(path_ex, "r").readlines()[1:]
  104. markdown_text = ''
  105. for line in markdown_lines:
  106. markdown_text += line.replace('# ', header)
  107. content_string = markdown.markdown(
  108. markdown_text, extensions=["fenced_code", "tables"]
  109. )
  110. return content_string
  111. def get_rss_string():
  112. path_ex = ENTRY_DIR
  113. if path.exists(path_ex):
  114. name_list = os.listdir(path_ex)
  115. full_list = [os.path.join(path_ex, i) for i in name_list]
  116. contents = sorted(full_list, key=os.path.getctime)
  117. content_string = ''
  118. for file in reversed(contents):
  119. filename = pathlib.PurePath(file)
  120. title = open(filename).readline().rstrip('\n')
  121. text = open(filename).readlines()[1:]
  122. filename = filename.name
  123. if filename[0] != '.':
  124. filename = filename.split('.', 1)[0]
  125. content_string += '<item>\n'
  126. content_string += '<title>' + title + '</title>\n'
  127. content_string += '<guid>' + config.WEBSITE + \
  128. '/index.html#' + filename + '</guid>\n'
  129. content_string += '<pubDate>' + \
  130. datetime.fromtimestamp(os.path.getctime(file)).strftime(
  131. '%Y-%m-%d') + '</pubDate>\n'
  132. content_string += '<description>'
  133. for line in text:
  134. content_string += line
  135. content_string += '</description>\n'
  136. content_string += '</item>\n'
  137. return content_string