John Mulligan avatar John Mulligan committed daea4c9

add support for tag summary pages

Comments (0)

Files changed (9)

muyhomepage2/app/build.py

 def build(config):
     website = site.Site()
     website.pages = site.read_site_pages(config.page_cache())
+    site.update_site_tags(website, config.tag_cache())
     rebuild = set()
     cached = [p.filename for p in website.pages]
     for pagefile in page.walkpages(config.root()):
             rebuild.add(page.parse(spage.filename))
     compile_pages(config, website, rebuild)
     site.write_site_pages(website, config.page_cache())
+    site.write_site_tags(website, config.tag_cache())
 
 
 def compile_pages(config, website, rebuild):
 def compile_page(config, website, pctx):
     print 'Building:', pctx.page.title
     tname = pctx.template()
-    scope = dict(current=pctx, site=website, config=config)
+    scope = dict(current=pctx, site=website, config=config,
+            ext=pctx.extension(website, config))
     env = config.templateenv()
     template = env.get_template(tname)
     fp = open(os.path.join(config.destination(), pctx.finalname()), 'w')

muyhomepage2/app/configuration.py

 
 from muyhomepage2.pagehandlers.standard import StandardHandler
+from muyhomepage2.pagehandlers.tagsummary import TagSummaryHandler
 
 
 DEFAULT_HANDLERS = {
     'standard': StandardHandler,
     'blog': StandardHandler,
-    'tagsummary': StandardHandler,
+    'tagsummary': TagSummaryHandler,
     'tagglobal': StandardHandler,
     'software': StandardHandler,
     'atom': StandardHandler,
         return StandardHandler
     
     def always_rebuild(self):
-        return []
+        return ['tagsummary']
 

muyhomepage2/metadata.py

 
 
 def writetagcache(data, fp):
-    pass
+    for ccode, tags in data:
+        ccode = urllib.quote(ccode)
+        tags = ' '.join(urllib.quote(tag) for tag in tags)
+        fp.write('%s: %s\n' % (ccode, tags))
+
 
 def readtagcache(fp):
-    pass
+    for line in fp:
+        ccode, tags = [v.strip() for v in line.split(':', 1)]
+        ccode = urllib.unquote(ccode)
+        tags = [urllib.unquote(t) for t in tags.split()]
+        yield ccode, tags

muyhomepage2/page.py

     def contentcode(self):
         return util.contentcode(self.filename)
 
+    @property
+    def tags(self):
+        return [v.strip() for v in (self.headers.get('tags') or '').split(',')]
+
 
 class PseduoPage(object):
     def __init__(self, filename, contentcode, title, action):
         self.contentcode = contentcode
         self.title = title
         self.action = action
+        self.tags = []
+        self.headers = {}

muyhomepage2/pagehandlers/handler.py

     def template(self):
         return None
 
+    def extension(self, website, config):
+        raise NotImplementedError('PageHandler.extension')
+
 
 def handle_action(registry, default, page):
     """ Return a PageHandler object based on the given ``page``'s action

muyhomepage2/pagehandlers/standard.py

 
     def template(self):
         return 'page.html'
+
+    def extension(self, website, config):
+        return None

muyhomepage2/pagehandlers/tagsummary.py

+
+from muyhomepage2.pagehandlers import handler
+
+class TagSummaryHandler(handler.PageHandler):
+    def content(self):
+        return self.gettag()
+
+    def template(self):
+        return 'tagsummary.html'
+
+    def extension(self, website, config):
+        tp = self._taggedpages(self.gettag(), website)
+        return {
+            'tagcount': len(tp),
+            'taggedpages': tp
+            }
+
+    def gettag(self):
+        return self.page.title.split(':', 1)[-1]
+
+    def _taggedpages(self, tag, website):
+        tl = []
+        for page in website.tagmap[tag]:
+            url = './%s' % handler.PageHandler(page).finalname()
+            tl.append((url, page.title))
+        return sorted(tl, key=lambda v: v[1])

muyhomepage2/site.py

 class Site(object):
     def __init__(self):
         self.statemap = {}
-        self.tagmap = {}
         self.pages = set()
 
     def getpagefile(self, filename):
                 return page
         raise KeyError(filename)
 
+    @property
+    def tagmap(self):
+        tmap = {}
+        for page in self.pages:
+            for tag in page.tags:
+                tmap.setdefault(tag, []).append(page)
+        return tmap
+
 
 def write_site_pages(wsite, filename):
     pdata = ((p.filename, p.contentcode, p.title, p.action)
     finally:
         fp.close()
 
+
+def write_site_tags(wsite, filename):
+    tdata = ((p.contentcode, p.tags) for p in wsite.pages)
+    fp = open(filename, 'w')
+    try:
+        metadata.writetagcache(tdata, fp)
+    finally:
+        fp.close()
+
+
 def read_site_pages(filename):
     pages = set()
     if not os.path.isfile(filename):
     finally:
         fp.close()
     return pages
+
+
+def read_site_tags(filename):
+    tagmap = {}
+    if not os.path.isfile(filename):
+        return tagmap
+    fp = open(filename)
+    try:
+        for ccode, tags in metadata.readtagcache(fp):
+            tagmap[ccode] = tags
+    finally:
+        fp.close()
+    return tagmap
+
+
+def update_site_tags(wsite, filename):
+    tagmap = read_site_tags(filename)
+    for page in wsite.pages:
+        page.tags = tagmap.get(page.contentcode, [])
+

skins/default/tagsummary.html

+{% extends "base.html" %}
+
+{% block main %}
+<div id="page-content">
+  <h2>{{current.page.title}}</h2>
+  <p>
+    This page lists the <em>{{ext.tagcount}}</em> page(s)
+    that have been tagged: <span>{{current.content()}}</span>.
+  </p>
+  
+  <ul>
+  {% for purl, ptitle in ext.taggedpages %}
+    <li><a href="{{purl}}">{{ptitle}}</a></li>
+  {% endfor %}
+  </ul>
+
+  <ul class="tidbits">
+    </li>
+    <li>
+        Created: <span class="value">{{current.created}}</span>
+    </li>
+    <li>
+        Modified: <span class="value">{{current.modified}}</span>
+    </li>
+  </ul>
+</div>
+{% endblock %}
+
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.