Simple file-based wiki with fulltext-search.
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.

75 lines
2.0 KiB

7 months ago
7 months ago
  1. import config
  2. import search
  3. import datetime
  4. from datetime import datetime
  5. import os
  6. from os import path
  7. import pathlib
  8. import markdown
  9. ENTRY_DIR = config.ENTRY_DIR
  10. WEBSITE = config.WEBSITE
  11. def gen_stand_string(path_ex):
  12. filename = os.path.join(ENTRY_DIR, path_ex)
  13. result = ''
  14. if path.exists(filename):
  15. title = open(filename).readline().rstrip('\n')
  16. text = open(filename).readlines()[1:]
  17. filename_no_end = filename.split('.', 1)[0]
  18. result += '<h1>' + title + '</h1>\n'
  19. if filename.endswith('.md'):
  20. result += gen_md_content(filename, 1)
  21. return result
  22. def gen_md_content(path_ex, depth):
  23. result = ''
  24. if path.exists(path_ex):
  25. filename = path_ex.split('.', 1)
  26. fileend = filename[len(filename) - 1]
  27. header = '#'
  28. for i in range(depth):
  29. header += '#'
  30. header += ' '
  31. markdown_lines = open(path_ex, 'r').readlines()[1:]
  32. markdown_text = ''
  33. for line in markdown_lines:
  34. markdown_text += line.replace('# ', header)
  35. result = markdown.markdown(
  36. markdown_text, extensions=["fenced_code", "tables", "nl2br"])
  37. return result
  38. def gen_query_res_string(query_str):
  39. src_results = search.search(query_str)
  40. res_string = ''
  41. res_string += '<ul>\n'
  42. for result in src_results:
  43. title = result['title']
  44. path = result['path']
  45. preview = create_preview(path)
  46. path = '/entry/' + path.split('/', 2)[2]
  47. res_string += '<li><a href="' + path + '">' + title + '</a><br>'
  48. res_string += '<div class="description">' + preview + '</div>'
  49. res_string += '</li>'
  50. res_string += '</ul>\n'
  51. return res_string
  52. def create_preview(path):
  53. file = open(path, 'r')
  54. first_lines = file.readlines()
  55. preview = ''
  56. preview_length = 3
  57. for i, line in enumerate(first_lines):
  58. if i > preview_length:
  59. break
  60. if not line.isspace():
  61. preview += line + '<br>'
  62. else:
  63. preview_length += 1
  64. return preview