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.

188 lines
6.6 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. import config
  8. ENTRY_DIR = 'templates/entry'
  9. def gen_arch_string():
  10. """
  11. Creates and returns a archive string of every file in ENTRY_DIR.
  12. Returns:
  13. string: html-formatted archive-string
  14. """
  15. path_ex = ENTRY_DIR
  16. if path.exists(path_ex):
  17. name_list = os.listdir(path_ex)
  18. full_list = [os.path.join(path_ex, i) for i in name_list]
  19. contents = sorted(full_list, key=os.path.getctime)
  20. content_string = ''
  21. last_month = ''
  22. for file in reversed(contents):
  23. curr_date = datetime.fromtimestamp(
  24. os.path.getctime(file)).strftime('%Y-%m-%d')
  25. curr_month = datetime.fromtimestamp(
  26. os.path.getctime(file)).strftime('%b %Y')
  27. if curr_month != last_month:
  28. if last_month != '':
  29. content_string += '</ul>\n'
  30. content_string += '<h2>' + curr_month + '</h2>\n'
  31. content_string += '<ul>\n'
  32. last_month = curr_month
  33. filename = pathlib.PurePath(file)
  34. title = open(filename).readline().rstrip('\n')
  35. filename = filename.name
  36. if filename[0] != '.':
  37. filename = filename.split('.', 1)[0]
  38. content_string += '<li>'
  39. content_string += curr_date + ' - '
  40. content_string += title + ' ['
  41. content_string += '<a href="' + '/index.html#' + \
  42. filename + '">' + 'link' + '</a> - '
  43. content_string += '<a href="' + '/entry/' + \
  44. pathlib.PurePath(file).name + '">' + 'standalone' + '</a>'
  45. content_string += '] <br>'
  46. content_string += '</li>\n'
  47. content_string += '</ul>\n'
  48. return content_string
  49. def gen_index_string():
  50. """
  51. Create and returns a string including every file in the ENTRY_DIR as an index.
  52. Returns:
  53. string: html-formatted index string
  54. """
  55. path_ex = ENTRY_DIR
  56. content_string = ''
  57. if path.exists(path_ex):
  58. name_list = os.listdir(path_ex)
  59. full_list = [os.path.join(path_ex, i) for i in name_list]
  60. contents = sorted(full_list, key=os.path.getctime)
  61. for file in reversed(contents):
  62. filename = pathlib.PurePath(file)
  63. purefile = filename
  64. title = open(filename).readline().rstrip('\n')
  65. text = open(filename).readlines()[1:]
  66. filename = filename.name
  67. if filename[0] != '.':
  68. filename = filename.split('.', 1)[0]
  69. content_string += '<div class=\'entry\'>\n'
  70. content_string += '<h2 id=\'' + filename + '\'>' + title + '</h2>\n'
  71. content_string += '[<a href="' + '/entry/' + \
  72. pathlib.PurePath(file).name + '">' + \
  73. 'standalone' + '</a>]<br>\n'
  74. if file.endswith('.html'):
  75. for line in text:
  76. content_string += line
  77. content_string += '<br>'
  78. if file.endswith('.md'):
  79. content_string += gen_md_content(file, 2)
  80. content_string += '<small>' + \
  81. datetime.fromtimestamp(os.path.getctime(
  82. file)).strftime('%Y-%m-%d') + '</small>'
  83. content_string += '</div>'
  84. return content_string
  85. def gen_stand_string(path_ex):
  86. """
  87. Creates a html-string for a file.
  88. If the file is markdown it will convert it.
  89. This functions ensures upscaling for future formats.
  90. Parameters:
  91. path_ex: path to a file.
  92. Returns:
  93. string: html-formatted string string equivalent to the file
  94. """
  95. filename = os.path.join(ENTRY_DIR, path_ex)
  96. content_string = ''
  97. if path.exists(filename):
  98. title = open(filename).readline().rstrip('\n')
  99. text = open(filename).readlines()[1:]
  100. filename_no_end = filename.split('.', 1)[0]
  101. content_string += '<h1>' + title + '</h1>\n'
  102. content_string += '['
  103. content_string += '<a href="' + '/index.html#' + \
  104. filename_no_end + '">' + 'link' + '</a>'
  105. content_string += ']<br>\n'
  106. if filename.endswith('.html'):
  107. for line in text:
  108. content_string += line
  109. content_string += '<br>'
  110. if filename.endswith('.md'):
  111. content_string += gen_md_content(filename, 1)
  112. return content_string
  113. def gen_md_content(path_ex, depth):
  114. """
  115. Convert a markdown file to a html string.
  116. Parameters:
  117. path_ex (string): path to the markdown file
  118. depth (int): starting depth for markdown headings
  119. Returns:
  120. string: html-formatted string string equivalent to the markdown file
  121. """
  122. content_string = ''
  123. if path.exists(path_ex):
  124. filename = path_ex.split('.', 1)
  125. fileend = filename[len(filename) - 1]
  126. header = '#'
  127. for i in range(depth):
  128. header += '#'
  129. header += ' '
  130. markdown_lines = open(path_ex, "r").readlines()[1:]
  131. markdown_text = ''
  132. for line in markdown_lines:
  133. markdown_text += line.replace('# ', header)
  134. content_string = markdown.markdown(
  135. markdown_text, extensions=["fenced_code", "tables"]
  136. )
  137. return content_string
  138. def get_rss_string():
  139. """
  140. Create a rss-string of the blog and return it.
  141. Returns:
  142. string: rss-string of everything that is in the ENTRY_DIR.
  143. """
  144. path_ex = ENTRY_DIR
  145. if path.exists(path_ex):
  146. name_list = os.listdir(path_ex)
  147. full_list = [os.path.join(path_ex, i) for i in name_list]
  148. contents = sorted(full_list, key=os.path.getctime)
  149. content_string = ''
  150. for file in reversed(contents):
  151. filename = pathlib.PurePath(file)
  152. title = open(filename).readline().rstrip('\n')
  153. text = open(filename).readlines()[1:]
  154. filename = filename.name
  155. if filename[0] != '.':
  156. filename = filename.split('.', 1)[0]
  157. content_string += '<item>\n'
  158. content_string += '<title>' + title + '</title>\n'
  159. content_string += '<guid>' + config.WEBSITE + \
  160. '/index.html#' + filename + '</guid>\n'
  161. content_string += '<pubDate>' + \
  162. datetime.fromtimestamp(os.path.getctime(file)).strftime(
  163. '%Y-%m-%d') + '</pubDate>\n'
  164. content_string += '<description>'
  165. for line in text:
  166. content_string += line
  167. content_string += '</description>\n'
  168. content_string += '</item>\n'
  169. return content_string