category_meta.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. '''Copyright 2014, 2015 Zack Weinberg
  2. Category Metadata
  3. -----------------
  4. A plugin to read metadata for each category from an index file in that
  5. category's directory.
  6. For this plugin to work properly, your articles should not have a
  7. Category: tag in their metadata; instead, they should be stored in
  8. (subdirectories of) per-category directories. Each per-category
  9. directory must have a file named 'index.ext' at its top level, where
  10. .ext is any extension that will be picked up by an article reader.
  11. The metadata of that article becomes the metadata for the category,
  12. copied over verbatim, with three special cases:
  13. * The category's name is set to the article's title.
  14. * The category's slug is set to the name of the parent directory
  15. of the index.ext file.
  16. * The _text_ of the article is stored as category.description.
  17. '''
  18. from pelican import signals
  19. import os
  20. import re
  21. import logging
  22. logger = logging.getLogger(__name__)
  23. ### CORE BUG: https://github.com/getpelican/pelican/issues/1547
  24. ### Content.url_format does not honor category.slug (or author.slug).
  25. ### The sanest way to work around this is to dynamically redefine each
  26. ### article's class to a subclass of itself with the bug fixed.
  27. ###
  28. ### Core was fixed in rev 822fb134e041c6938c253dd4db71813c4d0dc74a,
  29. ### which is not yet in any release, so we dynamically detect whether
  30. ### the installed version of Pelican still has the bug.
  31. patched_subclasses = {}
  32. def make_patched_subclass(klass):
  33. if klass.__name__ not in patched_subclasses:
  34. class PatchedContent(klass):
  35. @property
  36. def url_format(self):
  37. metadata = super(PatchedContent, self).url_format
  38. if hasattr(self, 'author'):
  39. metadata['author'] = self.author.slug
  40. if hasattr(self, 'category'):
  41. metadata['category'] = self.category.slug
  42. return metadata
  43. # Code in core uses Content class names as keys for things.
  44. PatchedContent.__name__ = klass.__name__
  45. patched_subclasses[klass.__name__] = PatchedContent
  46. return patched_subclasses[klass.__name__]
  47. def patch_urlformat(cont):
  48. # Test whether this content object needs to be patched.
  49. md = cont.url_format
  50. if ((hasattr(cont, 'author') and cont.author.slug != md['author']) or
  51. (hasattr(cont, 'category') and cont.category.slug != md['category'])):
  52. logger.debug("Detected bug 1547, applying workaround.")
  53. cont.__class__ = make_patched_subclass(cont.__class__)
  54. ### END OF BUG WORKAROUND
  55. def make_category(article, slug):
  56. # Reuse the article's existing category object.
  57. category = article.category
  58. # Setting a category's name resets its slug, so do that first.
  59. category.name = article.title
  60. try:
  61. category.slug = slug
  62. except AttributeError:
  63. category._slug = slug
  64. # Description from article text.
  65. # XXX Relative URLs in the article content may not be handled correctly.
  66. setattr(category, 'description', article.content)
  67. # Metadata, to the extent that this makes sense.
  68. for k, v in article.metadata.items():
  69. if k not in ('path', 'slug', 'category', 'name', 'title',
  70. 'description', 'reader'):
  71. setattr(category, k, v)
  72. logger.debug("Category: %s -> %s", category.slug, category.name)
  73. return category
  74. def pretaxonomy_hook(generator):
  75. """This hook is invoked before the generator's .categories property is
  76. filled in. Each article has already been assigned a category
  77. object, but these objects are _not_ unique per category and so are
  78. not safe to tack metadata onto (as is).
  79. The category metadata we're looking for is represented as an
  80. Article object, one per directory, whose filename is 'index.ext'.
  81. """
  82. category_objects = {}
  83. real_articles = []
  84. for article in generator.articles:
  85. dirname, fname = os.path.split(article.source_path)
  86. fname, _ = os.path.splitext(fname)
  87. if fname == 'index':
  88. category_objects[dirname] = \
  89. make_category(article, os.path.basename(dirname))
  90. else:
  91. real_articles.append(article)
  92. category_assignment = \
  93. re.compile("^(" +
  94. "|".join(re.escape(prefix)
  95. for prefix in category_objects.keys()) +
  96. ")/")
  97. for article in real_articles:
  98. m = category_assignment.match(article.source_path)
  99. if not m or m.group(1) not in category_objects:
  100. logger.error("No category assignment for %s (%s)",
  101. article, article.source_path)
  102. continue
  103. article.category = category_objects[m.group(1)]
  104. patch_urlformat(article)
  105. generator.articles = real_articles
  106. def register():
  107. signals.article_generator_pretaxonomy.connect(pretaxonomy_hook)