Преглед изворни кода

Merge pull request #319 from alistairmagee/subcat_patch

subcategory: Add sub_path and sub_url to metadata
Justin Mayer пре 8 година
родитељ
комит
50b6f4cba4
2 измењених фајлова са 20 додато и 0 уклоњено
  1. 8 0
      subcategory/README.md
  2. 12 0
      subcategory/subcategory.py

+ 8 - 0
subcategory/README.md

@@ -91,3 +91,11 @@ to your Pelican configuration file:
 … and this will create a feed with `fullurl` of the subcategory. For example:
 
     feeds/category/subcategory.atom.xml
+
+Article urls can also use the values of `subpath` and `suburl` in their
+definitions. These are equivalent to the `fullurl` and `savepath` of the most
+specific subcategory. If you have articles that don't have subcategories these
+values are set to the category slug.
+
+    ARTICLE_SAVE_AS = os.path.join('{subpath}' 'articles' '{slug}.html')
+    ARTICLE_URL = '{suburl}/articles/{slug}.html'

+ 12 - 0
subcategory/subcategory.py

@@ -87,6 +87,18 @@ def create_subcategories(generator):
                 parent = new_sub
                 actual_subcategories.append(parent)
         article.subcategories = actual_subcategories
+        """Add subpath and suburl to the article metadata. This allows the
+        the last subcategory's fullurl and savepath to be used when definining
+        Article URL's. If an article has no subcategories, the Category slug
+        is used instead
+        """
+        try:
+            last_subcat = article.subcategories[-1]
+            article.metadata['subpath'] = last_subcat.savepath
+            article.metadata['suburl'] = last_subcat.fullurl
+        except IndexError: #No Subcategory
+            article.metadata['subpath'] = article.category.slug
+            article.metadata['suburl'] = article.category.slug
 
 def generate_subcategories(generator, writer):
     write = partial(writer.write_file,