category_meta.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. category.slug = slug
  61. # Description from article text.
  62. # XXX Relative URLs in the article content may not be handled correctly.
  63. setattr(category, 'description', article.content)
  64. # Metadata, to the extent that this makes sense.
  65. for k, v in article.metadata.items():
  66. if k not in ('path', 'slug', 'category', 'name', 'title',
  67. 'description', 'reader'):
  68. setattr(category, k, v)
  69. logger.debug("Category: %s -> %s", category.slug, category.name)
  70. return category
  71. def pretaxonomy_hook(generator):
  72. """This hook is invoked before the generator's .categories property is
  73. filled in. Each article has already been assigned a category
  74. object, but these objects are _not_ unique per category and so are
  75. not safe to tack metadata onto (as is).
  76. The category metadata we're looking for is represented as an
  77. Article object, one per directory, whose filename is 'index.ext'.
  78. """
  79. category_objects = {}
  80. real_articles = []
  81. for article in generator.articles:
  82. dirname, fname = os.path.split(article.source_path)
  83. fname, _ = os.path.splitext(fname)
  84. if fname == 'index':
  85. category_objects[dirname] = \
  86. make_category(article, os.path.basename(dirname))
  87. else:
  88. real_articles.append(article)
  89. category_assignment = \
  90. re.compile("^(" +
  91. "|".join(re.escape(prefix)
  92. for prefix in category_objects.keys()) +
  93. ")/")
  94. for article in real_articles:
  95. m = category_assignment.match(article.source_path)
  96. if not m or m.group(1) not in category_objects:
  97. logger.error("No category assignment for %s (%s)",
  98. article, article.source_path)
  99. continue
  100. article.category = category_objects[m.group(1)]
  101. patch_urlformat(article)
  102. generator.articles = real_articles
  103. def register():
  104. signals.article_generator_pretaxonomy.connect(pretaxonomy_hook)