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.

160 lines
4.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. """
  13. Creates a html-string for a file.
  14. If the file is markdown it will convert it.
  15. This functions ensures upscaling for future formats.
  16. Parameters:
  17. path_ex: path to a file.
  18. Returns:
  19. string: html-formatted string string equivalent to the file
  20. """
  21. filename = os.path.join(ENTRY_DIR, path_ex)
  22. result = ''
  23. if path.exists(filename):
  24. title = open(filename,encoding='utf-8').readline().rstrip('\n').lstrip('#').lstrip(' ')
  25. text = open(filename,encoding='utf-8').readlines()[1:]
  26. filename_no_end = filename.split('.', 1)[0]
  27. result += '<h1>' + title + '</h1>\n'
  28. if filename.endswith('.md'):
  29. result += gen_md_content(filename, 1)
  30. return result
  31. def gen_md_content(path_ex, depth):
  32. """
  33. Convert a markdown file to a html string.
  34. Parameters:
  35. path_ex (string): path to the markdown file
  36. depth (int): starting depth for markdown headings
  37. Returns:
  38. string: html-formatted string string equivalent to the markdown file
  39. """
  40. result = ''
  41. if path.exists(path_ex):
  42. filename = path_ex.split('.', 1)
  43. fileend = filename[len(filename) - 1]
  44. header = '#'
  45. for i in range(depth):
  46. header += '#'
  47. header += ' '
  48. markdown_lines = open(path_ex, 'r',encoding='utf-8').readlines()[1:]
  49. markdown_text = ''
  50. for line in markdown_lines:
  51. markdown_text += line.replace('# ', header)
  52. result = markdown.markdown(
  53. markdown_text, extensions=["fenced_code", "tables", "nl2br"])
  54. return result
  55. def gen_arch_string(path_ex):
  56. """
  57. Creates and returns an index string of every file in the path_ex.
  58. Parameter:
  59. path_ex: path to the dir to show
  60. Returns:
  61. string: html-formatted archive-string
  62. """
  63. full_path = os.path.join(ENTRY_DIR, path_ex)
  64. if path.exists(full_path):
  65. name_list = os.listdir(full_path)
  66. full_list = [os.path.join(full_path, i) for i in name_list]
  67. contents = sorted(full_list, key=os.path.getctime)
  68. content_string = ''
  69. last_month = ''
  70. for file in reversed(contents):
  71. filename = pathlib.PurePath(file)
  72. if os.path.isfile(filename):
  73. title = open(filename).readline().rstrip('\n').lstrip('#').lstrip(' ')
  74. entry_or_namespace = 'entry'
  75. elif os.path.isdir(filename):
  76. title = filename.name
  77. entry_or_namespace = 'namespace'
  78. else:
  79. continue
  80. filename = filename.name
  81. if filename[0] != '.' and filename.__contains__('.'):
  82. filename = filename.split('.', 1)[0]
  83. content_string += '<li>'
  84. content_string += title + ' ['
  85. content_string += '<a href="' + '/'+ entry_or_namespace +'/' + \
  86. path_ex.rstrip("/") + '/' + pathlib.PurePath(file).name + '">' + 'standalone' + '</a>'
  87. content_string += '] <br>'
  88. content_string += '</li>\n'
  89. content_string += '</ul>\n'
  90. return content_string
  91. def gen_query_res_string(query_str):
  92. """
  93. Return the results of a query.
  94. Parameters:
  95. query_str (string): term to search
  96. Returns:
  97. string: html-formated search result
  98. """
  99. src_results = search.search(query_str)
  100. res_string = ''
  101. res_string += '<ul>\n'
  102. for result in src_results:
  103. title = result['title']
  104. path = result['path']
  105. preview = create_preview(path)
  106. path = '/entry/' + path.split('/', 2)[2]
  107. res_string += '<li><a href="' + path + '">' + markdown.markdown(title) + '</a><br>'
  108. res_string += '<div class="description">' + preview + '</div>'
  109. res_string += '</li>'
  110. res_string += '</ul>\n'
  111. return res_string
  112. def create_preview(path):
  113. """
  114. Create a preview of a given article and return it.
  115. Parameters:
  116. path (string): path to the article
  117. Returns:
  118. string: html-formated preview
  119. """
  120. file = open(path, 'r',encoding='utf-8')
  121. first_lines = file.readlines()
  122. preview = ''
  123. preview_length = 3
  124. for i, line in enumerate(first_lines):
  125. if i == 0:
  126. continue
  127. if i > preview_length:
  128. break
  129. if not line.isspace():
  130. preview += markdown.markdown(line) + '<br>'
  131. else:
  132. preview_length += 1
  133. preview += '...<br>'
  134. return preview
  135. print(gen_arch_string('system-software'))