Преглед на файлове

Merge pull request #482 from Magnetic/autopages

Extend and rename AuthorPages to AutoPages
Justin Mayer преди 9 години
родител
ревизия
ef1ecf72fe
променени са 8 файла, в които са добавени 85 реда и са изтрити 58 реда
  1. 1 1
      Readme.rst
  2. 0 17
      authorpages/README.md
  3. 0 1
      authorpages/__init__.py
  4. 0 39
      authorpages/authorpages.py
  5. 0 0
      autopages/LICENSE
  6. 21 0
      autopages/README.md
  7. 1 0
      autopages/__init__.py
  8. 62 0
      autopages/autopages.py

+ 1 - 1
Readme.rst

@@ -40,7 +40,7 @@ AsciiDoc reader           Use AsciiDoc to write your posts.
 
 Asset management          Use the Webassets module to manage assets such as CSS and JS files.
                     
-Author Pages              Generate custom content for generated Author pages (e.g. author biography)
+Auto Pages                Generate custom content for generated Author, Category, and Tag pages (e.g. author biography)
 
 Better code samples       Wraps all `table` blocks with a class attribute `.codehilitetable` in an additional `div` of class `.hilitewrapper`. It thus permits to style codeblocks better, especially to make them scrollable.
                     

+ 0 - 17
authorpages/README.md

@@ -1,17 +0,0 @@
-# Author Pages
-
-This plugin adds an attribute `page` to the author object which can be used
-in templates by themes. The page is processed as an ordinary Pelican page,
-so it can be Markdown, reStructuredText, etc.
-
-## Configuration
-
-| Setting            | Default   | Notes                                                                                                                                                         |
-|--------------------|-----------|---------------------------------------------------------------------------------------------------------------------------------------------------------------|
-| `AUTHOR_PAGE_PATH` | `authors` | The location, relative to the project root where author pages can be found. The filename of the author page minus the extension must match the Author's slug. |
-
-## Template Variables
-
-| Setting       | Notes                                                                                                                                                         |
-|---------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------|
-| `author.page` | The location, relative to the project root where author pages can be found. The filename of the author page minus the extension must match the Author's slug. |

+ 0 - 1
authorpages/__init__.py

@@ -1 +0,0 @@
-from .authorpages import *

+ 0 - 39
authorpages/authorpages.py

@@ -1,39 +0,0 @@
-import os
-import os.path
-
-from pelican import signals
-
-
-def yield_files(root):
-    root = os.path.realpath(os.path.abspath(root))
-    for dirpath, dirnames, filenames in os.walk(root):
-        for dirname in list(dirnames):
-            try:
-                if dirname.startswith("."):
-                    dirnames.remove(dirname)
-            except IndexError:
-                # duplicate already removed?
-                pass
-        for filename in filenames:
-            if filename.startswith("."):
-                continue
-            yield os.path.join(dirpath, filename)
-
-def test(article_generator):
-    settings = article_generator.settings
-    readers = article_generator.readers
-    path = settings.get("AUTHOR_PAGE_PATH", "authors")
-
-    author_pages = {}
-    for filename in yield_files(path):
-        base_path, filename = os.path.split(filename)
-        page = readers.read_file(base_path, filename)
-        slug, _ = os.path.splitext(filename)
-        author_pages[slug] = page
-
-    for author, _ in article_generator.authors:
-        print "set author.page for %s to %r" % (author.slug, author_pages.get(author.slug, ""))
-        author.page = author_pages.get(author.slug, "")
-
-def register():
-    signals.article_generator_finalized.connect(test)

authorpages/LICENSE → autopages/LICENSE


+ 21 - 0
autopages/README.md

@@ -0,0 +1,21 @@
+# Auto Pages
+
+This plugin adds an attribute `page` to the author, category, and tag
+objects which can be used in templates by themes. The page is processed as
+an ordinary Pelican page, so it can be Markdown, reStructuredText, etc.
+
+## Configuration
+
+| Setting              | Default      | Notes                                                                                                                                                               |
+|----------------------|--------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------|
+| `AUTHOR_PAGE_PATH`   | `authors`    | The location, relative to the project root where author pages can be found. The filename of the author page minus the extension must match the Author's slug.       |
+| `CATEGORY_PAGE_PATH` | `categories` | The location, relative to the project root where category pages can be found. The filename of the category page minus the extension must match the Category's slug. |
+| `TAG_PAGE_PATH`      | `tags`       | The location, relative to the project root where tag pages can be found. The filename of the tag page minus the extension must match the Tag's slug.                |
+
+## Template Variables
+
+| Variable        | Notes                                      |
+|-----------------|--------------------------------------------|
+| `author.page`   | The rendered content of the author page.   |
+| `category.page` | The rendered content of the category page. |
+| `tag.page`      | The rendered content of the tag page.      |

+ 1 - 0
autopages/__init__.py

@@ -0,0 +1 @@
+from .autopages import *

+ 62 - 0
autopages/autopages.py

@@ -0,0 +1,62 @@
+import logging
+import os
+import os.path
+
+from pelican import signals
+
+
+logger = logging.getLogger("autopages")
+
+def yield_files(root):
+    root = os.path.realpath(os.path.abspath(root))
+    for dirpath, dirnames, filenames in os.walk(root):
+        for dirname in list(dirnames):
+            try:
+                if dirname.startswith("."):
+                    dirnames.remove(dirname)
+            except IndexError:
+                # duplicate already removed?
+                pass
+        for filename in filenames:
+            if filename.startswith("."):
+                continue
+            yield os.path.join(dirpath, filename)
+
+def make_page(readers, filename):
+    base_path, filename = os.path.split(filename)
+    page = readers.read_file(base_path, filename)
+    slug, _ = os.path.splitext(filename)
+    return slug, page
+
+def make_pages(readers, path):
+    pages = {}
+    for filename in yield_files(path):
+        try:
+            slug, page = make_page(readers, filename)
+        except Exception:
+            logger.exception("Could not make autopage for %r", filename)
+            continue
+        pages[slug] = page
+    return pages
+
+def create_autopages(article_generator):
+    settings = article_generator.settings
+    readers = article_generator.readers
+
+    authors_path = settings.get("AUTHOR_PAGE_PATH", "authors")
+    categories_path = settings.get("CATEGORY_PAGE_PATH", "categories")
+    tags_path = settings.get("TAG_PAGE_PATH", "tags")
+
+    author_pages = make_pages(readers, authors_path)
+    category_pages = make_pages(readers, categories_path)
+    tag_pages = make_pages(readers, tags_path)
+
+    for author, _ in article_generator.authors:
+        author.page = author_pages.get(author.slug, "")
+    for category, _ in article_generator.categories:
+        category.page = category_pages.get(category.slug, "")
+    for tag in article_generator.tags:
+        tag.page = tag_pages.get(tag.slug, "")
+
+def register():
+    signals.article_generator_finalized.connect(create_autopages)