Browse Source

Merge pull request #808 from mfitzp/master

Subcategory: Add support for folder-based subcategories.
Justin Mayer 7 years ago
parent
commit
55f0084e91
2 changed files with 23 additions and 2 deletions
  1. 17 0
      subcategory/README.md
  2. 6 2
      subcategory/subcategory.py

+ 17 - 0
subcategory/README.md

@@ -9,6 +9,8 @@ Feeds can be generated for each subcategory, just like categories and tags.
 
 ##Usage##
 
+###Metadata###
+
 Subcategories are an extension to categories. Add subcategories to an article's
 category metadata using a `/` like this:
 
@@ -34,6 +36,21 @@ breadcrumb-style navigation you might try something like this:
     </ol>
     </nav>
 
+###Subcategory folders###
+
+To specify subcategories using folders you can configure `PATH_METADATA`  
+to extract the article path (containing all category and subcategory folders) 
+into the `subcategory_path` metadata. The following settings would use all available 
+subcategories for the hierarchy:
+
+    PATH_METADATA= '(?P<subcategory_path>.*)/.*'
+
+You can limit the depth of generated subcategories by adjusting the regular expression
+to only include a specific number of path separators (`/`). For example, the following 
+would generate only a single level of subcategories regardless of the folder tree depth:
+
+    PATH_METADATA= '(?P<subcategory_path>[^/]*/[^/]*)/.*'
+
 ##Subcategory Names##
 
 Each subcategory's full name is a `/`-separated list of it parents and itself.

+ 6 - 2
subcategory/subcategory.py

@@ -49,8 +49,12 @@ def get_subcategories(generator, metadata):
                 'subcategory', '{savepath}.html')
     if 'SUBCATEGORY_URL' not in generator.settings:
         generator.settings['SUBCATEGORY_URL'] = 'subcategory/{fullurl}.html'
-    
-    category_list = text_type(metadata.get('category')).split('/')
+
+    if 'subcategory_path' in metadata:
+        category_list = text_type(metadata.get('subcategory_path')).split('/')
+    else:
+        category_list = text_type(metadata.get('category')).split('/')
+
     category = (category_list.pop(0)).strip()
     category = Category(category, generator.settings)
     metadata['category'] = category