Explorar el Código

subcategory plugin update, sub_path and sub_url

Alistair Magee hace 10 años
padre
commit
4cf80e5ae7
Se han modificado 2 ficheros con 20 adiciones y 0 borrados
  1. 8 0
      subcategory/README.md
  2. 12 0
      subcategory/subcategory.py

+ 8 - 0
subcategory/README.md

@@ -74,3 +74,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

@@ -83,6 +83,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,