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.

58 lines
1.6 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. content_string = ''
  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. content_string += '<h1>' + title + '</h1>\n'
  19. if filename.endswith('.md'):
  20. content_string += gen_md_content(filename, 1)
  21. return content_string
  22. def gen_md_content(path_ex, depth):
  23. content_string = ''
  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. content_string = markdown.markdown(
  36. markdown_text, extensions=["fenced_code", "tables"]
  37. )
  38. return content_string
  39. def gen_query_res_string(query_str):
  40. src_results = search.search(query_str)
  41. res_string = ''
  42. res_string += '<ul>\n'
  43. for result in src_results:
  44. title = result['title']
  45. path = result['path']
  46. path = '/entry/' + path.split('/', 2)[2]
  47. res_string += '<li><a href="' + path + '">' + title + '</a></li>'
  48. res_string += '</ul>\n'
  49. return res_string