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.

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