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.

147 lines
5.5 KiB

7 months ago
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. 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 += 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 + '">' + 'standalone' + '</a>]<br>\n'
  63. if file.endswith('.html'):
  64. for line in text:
  65. content_string += line
  66. content_string += '<br>'
  67. if file.endswith('.md'):
  68. content_string += gen_md_content(file, 2)
  69. content_string += '<small>' + \
  70. datetime.fromtimestamp(os.path.getctime(
  71. file)).strftime('%Y-%m-%d') + '</small>'
  72. content_string += '</div>'
  73. return content_string
  74. def gen_stand_string(path_ex):
  75. filename = os.path.join(ENTRY_DIR, path_ex)
  76. content_string = ''
  77. if path.exists(filename):
  78. title = open(filename).readline().rstrip('\n')
  79. text = open(filename).readlines()[1:]
  80. filename_no_end = filename.split('.', 1)[0]
  81. content_string += '<h1>' + title + '</h1>\n'
  82. content_string += '['
  83. content_string += '<a href="' + '/index.html#' + \
  84. filename_no_end + '">' + 'link' + '</a>'
  85. content_string += ']<br>\n'
  86. if filename.endswith('.html'):
  87. for line in text:
  88. content_string += line
  89. content_string += '<br>'
  90. if filename.endswith('.md'):
  91. content_string += gen_md_content(filename, 1)
  92. return content_string
  93. def gen_md_content(path_ex, depth):
  94. content_string = ''
  95. if path.exists(path_ex):
  96. filename = path_ex.split('.', 1)
  97. fileend = filename[len(filename) - 1]
  98. header = '#'
  99. for i in range(depth):
  100. header += '#'
  101. header += ' '
  102. markdown_lines = open(path_ex, "r").readlines()[1:]
  103. markdown_text = ''
  104. for line in markdown_lines:
  105. markdown_text += line.replace('# ', header)
  106. content_string = markdown.markdown(
  107. markdown_text, extensions=["fenced_code", "tables"]
  108. )
  109. return content_string
  110. def get_rss_string():
  111. path_ex = ENTRY_DIR
  112. if path.exists(path_ex):
  113. name_list = os.listdir(path_ex)
  114. full_list = [os.path.join(path_ex, i) for i in name_list]
  115. contents = sorted(full_list, key=os.path.getctime)
  116. content_string = ''
  117. for file in reversed(contents):
  118. filename = pathlib.PurePath(file)
  119. title = open(filename).readline().rstrip('\n')
  120. text = open(filename).readlines()[1:]
  121. filename = filename.name
  122. if filename[0] != '.':
  123. filename = filename.split('.', 1)[0]
  124. content_string += '<item>\n'
  125. content_string += '<title>' + title + '</title>\n'
  126. content_string += '<guid>' + '/index.html#' + filename + '</guid>\n'
  127. content_string += '<pubDate>' + \
  128. datetime.fromtimestamp(os.path.getctime(file)).strftime(
  129. '%Y-%m-%d') + '</pubDate>\n'
  130. content_string += '<description>'
  131. for line in text:
  132. content_string += line
  133. content_string += '</description>\n'
  134. content_string += '</item>\n'
  135. return content_string