wiki.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. # SPDX-License-Identifier: AGPL-3.0-or-later
  2. #
  3. # Copyright (C) 2023 Leo Gavilieau <xmoo@vern.cc>
  4. from pycmarkgfm import gfm_to_html as markdown
  5. from api.base import api_call
  6. def get_project_wiki_sitemap(instance, full_reponame):
  7. recv = api_call('https://%s/api/v4/projects/%s/wikis' \
  8. % (instance, full_reponame))
  9. if 'message' in recv:
  10. return "Could not retrieve pages: " + recv["message"]
  11. pages_html = "" # Convert JSON to HTML
  12. categories = {} # For storing links to pages inside categories
  13. individual = {} # For storing links to individual pages
  14. for subdict in recv:
  15. if "/" in subdict["slug"]:
  16. slashcount = subdict["slug"].count("/")
  17. slashlist = subdict["slug"].split("/")
  18. category = ""
  19. for i in range(slashcount):
  20. if i - 1 == slashcount:
  21. continue
  22. category += slashlist[i]
  23. try:
  24. tmp = categories[category]
  25. except:
  26. tmp = ""
  27. tmp += '&#9;<a class="project_wiki_category_link" \
  28. href="/%s/%s/-/wikis/%s">%s</a> (%s)<br>\n' \
  29. % (instance, full_reponame.replace("%2F", "/"), \
  30. subdict["slug"], subdict["title"], subdict["format"])
  31. categories[category] = tmp
  32. else:
  33. if subdict["slug"] in categories:
  34. continue
  35. individual[subdict["slug"]] = '<a class="project_wiki_link" \
  36. href="/%s/%s/-/wikis/%s">%s</a> (%s)<br>\n' \
  37. % (instance, full_reponame.replace("%2F","/"), \
  38. subdict["slug"], subdict["title"], subdict["format"])
  39. # We parse individual and categories at the end
  40. # So we can get rid of redundant entries
  41. categories_html = ""
  42. if len(categories) > 0:
  43. for key,val in categories.items():
  44. categories_html += '<h3><a class="project_wiki_category" \
  45. href="/%s/%s/-/wikis/%s">%s</a></h3>\n%s' \
  46. % (instance, full_reponame.replace("%2F","/"), key, key, val)
  47. if len(individual) > 0:
  48. for key,val in individual.items():
  49. if key in categories:
  50. continue
  51. pages_html += val
  52. return pages_html + categories_html
  53. def get_project_wiki_page(instance, full_reponame, wiki = ""):
  54. slug = '%s' % (wiki.replace('/', '%2F'))
  55. # Check if user is requesting a real wiki page
  56. # or the "Pages" wikipage, which is a sitemap with a special API
  57. recv = api_call('https://%s/api/v4/projects/%s/wikis/%s'\
  58. % (instance, full_reponame, slug))
  59. if 'message' in recv:
  60. # Error detected!
  61. return "Could not retrieve wiki: %s" % recv["message"]
  62. else:
  63. if 'content' in recv:
  64. # Now to decode the markdown...
  65. decode = markdown(recv["content"])
  66. # We want to replace all instance links with a link
  67. # to the frontend
  68. decode = decode.replace("https://" + instance, "/" + instance)
  69. decode = decode.replace("http://" + instance, "/" + instance)
  70. return decode
  71. else:
  72. return "Could not retrieve content..."