wiki.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. from pelican import signals, utils
  2. from collections import namedtuple, OrderedDict
  3. import os
  4. import re
  5. import logging
  6. def add_to_structure(structure, path_list):
  7. folders = structure["folders"]
  8. articles = structure["articles"]
  9. subdir = path_list[0]
  10. rest = path_list[1:]
  11. if len(rest) > 1:
  12. if subdir in folders:
  13. folders[subdir] = add_to_structure(folders[subdir], rest)
  14. else:
  15. folders[subdir] = add_to_structure({"folders":{},"articles":[]}, rest)
  16. else:
  17. if subdir in folders:
  18. folders[subdir]["articles"] += rest
  19. else:
  20. folders[subdir] = { "folders": {}, "articles": rest }
  21. return { "folders": folders, "articles": articles }
  22. def parse_wiki_pages(generator):
  23. settings = generator.settings
  24. readers = generator.readers
  25. contentpath = settings.get("PATH", "content")
  26. root = os.path.realpath(
  27. os.path.abspath(os.path.join(contentpath, "wiki", "")))
  28. wikilist = []
  29. structure = {"folders":{}, "articles":[]}
  30. for (dirname, dirnames, filenames) in os.walk(root):
  31. for filename in filenames:
  32. if ".git" not in dirname and ".git" not in filename:
  33. parsedfile = readers.read_file(dirname, filename)
  34. metadata = parsedfile.metadata
  35. try:
  36. path = metadata["path"]
  37. org = metadata["path"].split("/")
  38. except KeyError:
  39. path = ""
  40. org = []
  41. org.append(filename)
  42. structure = add_to_structure(structure, org)
  43. wikilist.append((path,filename,parsedfile))
  44. structure = { "articles": structure["folders"]['']["articles"], "folders":structure["folders"] }
  45. del(structure["folders"][""])
  46. wikilist.sort()
  47. generator.context['wikilist'] = wikilist
  48. generator.context['wiki'] = structure
  49. def parse_dict(structure, level, nice_list):
  50. folders = OrderedDict(sorted(structure["folders"].items(), key=lambda t: t[0]))
  51. articles = sorted(structure["articles"])
  52. for key in folders.keys():
  53. if key + ".md" in articles:
  54. nice_list.append((key, "indexdir", level))
  55. articles.remove(key + ".md")
  56. else:
  57. nice_list.append((key, "noindexdir", level))
  58. nice_list = parse_dict(folders[key], level + 1, nice_list)
  59. for item in articles:
  60. nice_list.append((item, "article", level))
  61. return nice_list
  62. def generate_wiki_pages(generator, writer):
  63. wiki_list = generator.context['wikilist']
  64. structure = generator.context['wiki']
  65. template = generator.get_template('wikiarticle')
  66. nice_list = parse_dict(structure, 0, [])
  67. for page in wiki_list:
  68. filename = os.path.join('wiki', page[1].replace('.md', '.html'))
  69. content = page[2].content
  70. metadata = page[2].metadata
  71. path = page[0]
  72. breadcrumbs = []
  73. for name in path.split('/'):
  74. name_match = [item[1] for item in nice_list if item[0] == name]
  75. if len(name_match) > 0 and name_match[0] == "indexdir":
  76. breadcrumbs.append((name, "a"))
  77. else:
  78. breadcrumbs.append((name, "p"))
  79. file = page[1]
  80. writer.write_file(filename, template, generator.context,
  81. meta=metadata, content=content, file=file, path=path, links=nice_list, breadcrumbs=breadcrumbs)
  82. def register():
  83. signals.article_generator_finalized.connect(parse_wiki_pages)
  84. signals.article_writer_finalized.connect(generate_wiki_pages)