Commits

dan mackinlay  committed 2ba05a1

this doesn't work , i think because of ToC being examined at a special point in proceedings. perhpas it would be better to globally alter the toc behaviour?

  • Participants
  • Parent commits 463ccfe

Comments (0)

Files changed (5)

File feed/sphinxcontrib/feed/__init__.py

 import feedgenerator
 from urllib import quote_plus
 import os.path
-import directives
+import feeddirectives
+import feedtransforms
+import feednodes
+from sphinx.addnodes import toctree
 
 #global
 feed_entries = None
     app.add_config_value('feed_description', '', 'html')
     app.add_config_value('feed_filename', 'rss.xml', 'html')
     
+    #app.add_transform(feedtransforms.SortLastestTree)
+    app.add_node(feednodes.latesttree)
+    app.add_directive('latest', feeddirectives.Latest)
+    
     app.connect('html-page-context', create_feed_item)
     app.connect('build-finished', emit_feed)
     app.connect('builder-inited', create_feed_container)
     app.connect('env-purge-doc', remove_dead_feed_item)
-    
+    #app.connect('doctree-resolved', process_latest_toc)
+    app.connect('doctree-read', process_latest_toc)
+
+def process_latest_toc(app, doctree, docname=None):
+    env = app.builder.env
+    for node in doctree.traverse(feednodes.latesttree):
+        new_node = toctree()
+        import pdb; pdb.set_trace()
+
+        new_node['parent'] = node['parent']
+        # entries contains all entries (self references, external links etc.)
+        new_node['entries'] = node['entries']
+        # includefiles only entries that are documents
+        new_node['includefiles'] = node['includefiles']
+        new_node['maxdepth'] = node['maxdepth']
+        
+        new_node['glob'] = node['glob']
+        new_node['hidden'] = node['hidden']
+        new_node['numbered'] = node['numbered']
+        new_node['titlesonly'] = node['titlesonly']
+        
+        node.replace_self(new_node)
+        pass
+
 def create_feed_container(app):
     """
     create lazy filesystem stash for keeping RSS entry fragments, since we
         return #don't index dateless articles
     try:
         pub_date = parse_date(metadata['date'])
-        app.builder.env.metadata.get(pagename, {})
+        metadata['pub_date'] = pub_date
     except ValueError, exc:
         #probably a nonsensical date
         app.builder.warn('date parse error: ' + str(exc) + ' in ' + pagename)
     }
     if 'author' in metadata:
         item['author'] = metadata['author']
-    feed_entries[nice_name(pagename, pub_date)] = item
+        
+    feed_entries[dated_name(pagename, pub_date)] = item
     
     #Now, useful variables to keep in context
     ctx['rss_link'] = app.builder.env.feed_url 
     feed.write(fp, 'utf-8')
     fp.close()
 
-def nice_name(docname, date):
+def dated_name(docname, date):
     """
-    we need convenient filenames which incorporate dates for ease of sorting and
-    guid for uniqueness, plus will work in the FS without inconvenient
+    we need convenient filenames which incorporate dates for ease of sorting
+    andguid for uniqueness, plus will work in the FS without inconvenient
     characters. NB, at the moment, hour of publication is ignored.
     """
     return quote_plus(MAGIC_SEPARATOR.join([date.isoformat(), docname]))

File feed/sphinxcontrib/feed/directives.py

-# -*- coding: utf-8 -*-
-"""
-    sphinxcontrib.feed.directives
-    ~~~~~~~~~~~~~~~~~~~~~~~
-
-    :copyright: Copyright 2007-2011 the Sphinx team, see AUTHORS.
-    :license: BSD, see LICENSE for details.
-    
-    Latest is based on sphinx.directives.other.TocTree
-"""
-
-import os
-
-from docutils import nodes
-from docutils.parsers.rst import Directive, directives
-
-from sphinx import addnodes
-from sphinx.locale import _
-from sphinx.util import url_re, docname_join
-from sphinx.util.nodes import explicit_title_re
-from sphinx.util.matching import patfilter
-
-def int_or_nothing(argument):
-    if not argument:
-        return 999
-    return int(argument)
-
-
-class Latest(Directive):
-    """
-    Directive to notify Sphinx about the hierarchical structure of the docs,
-    and to include a table-of-contents-like tree in the current document.
-    
-    Originally absed on the `sphinx.directives.other.TocTree` class.
-    """
-    has_content = True
-    required_arguments = 0
-    optional_arguments = 0
-    final_argument_whitespace = False
-    option_spec = {
-        'maxdepth': int,
-        'glob': directives.flag,
-        'hidden': directives.flag,
-        'numbered': int_or_nothing,
-        'titlesonly': directives.flag,
-    }
-
-    def run(self):
-        env = self.state.document.settings.env
-        suffix = env.config.source_suffix
-        glob = 'glob' in self.options
-
-        ret = []
-        # (title, ref) pairs, where ref may be a document, or an external link,
-        # and title may be None if the document's title is to be used
-        entries = []
-        includefiles = []
-        all_docnames = env.found_docs.copy()
-        # don't add the currently visited file in catch-all patterns
-        all_docnames.remove(env.docname)
-        for entry in self.content:
-            if not entry:
-                continue
-            if not glob:
-                # look for explicit titles ("Some Title <document>")
-                m = explicit_title_re.match(entry)
-                if m:
-                    ref = m.group(2)
-                    title = m.group(1)
-                    docname = ref
-                else:
-                    ref = docname = entry
-                    title = None
-                # remove suffixes (backwards compatibility)
-                if docname.endswith(suffix):
-                    docname = docname[:-len(suffix)]
-                # absolutize filenames
-                docname = docname_join(env.docname, docname)
-                if url_re.match(ref) or ref == 'self':
-                    entries.append((title, ref))
-                elif docname not in env.found_docs:
-                    ret.append(self.state.document.reporter.warning(
-                        'toctree contains reference to nonexisting '
-                        'document %r' % docname, line=self.lineno))
-                    env.note_reread()
-                else:
-                    entries.append((title, docname))
-                    includefiles.append(docname)
-            else:
-                patname = docname_join(env.docname, entry)
-                docnames = sorted(patfilter(all_docnames, patname))
-                for docname in docnames:
-                    all_docnames.remove(docname) # don't include it again
-                    entries.append((None, docname))
-                    includefiles.append(docname)
-                if not docnames:
-                    ret.append(self.state.document.reporter.warning(
-                        'toctree glob pattern %r didn\'t match any documents'
-                        % entry, line=self.lineno))
-        subnode = addnodes.toctree()
-        subnode['parent'] = env.docname
-        # entries contains all entries (self references, external links etc.)
-        subnode['entries'] = entries
-        # includefiles only entries that are documents
-        subnode['includefiles'] = includefiles
-        subnode['maxdepth'] = self.options.get('maxdepth', -1)
-        
-        subnode['glob'] = glob
-        subnode['hidden'] = 'hidden' in self.options
-        subnode['numbered'] = self.options.get('numbered', 0)
-        subnode['titlesonly'] = 'titlesonly' in self.options
-        wrappernode = nodes.compound(classes=['toctree-wrapper'])
-        wrappernode.append(subnode)
-        ret.append(wrappernode)
-        return ret
-
-directives.register_directive('latest', Latest)
-

File feed/sphinxcontrib/feed/feeddirectives.py

+# -*- coding: utf-8 -*-
+"""
+    sphinxcontrib.feed.directives
+    ~~~~~~~~~~~~~~~~~~~~~~~
+
+    :copyright: Copyright 2007-2011 the Sphinx team, see AUTHORS.
+    :license: BSD, see LICENSE for details.
+    
+    Latest is based on sphinx.directives.other.TocTree
+"""
+
+import os
+
+from docutils import nodes
+from docutils.parsers.rst import Directive, directives
+
+from sphinx.locale import _
+from sphinx.util import url_re, docname_join
+from sphinx.util.nodes import explicit_title_re
+from sphinx.util.matching import patfilter
+
+from feednodes import latesttree
+
+
+def int_or_nothing(argument):
+    if not argument:
+        return 999
+    return int(argument)
+
+
+class Latest(Directive):
+    """
+    Directive to notify Sphinx about the hierarchical structure of the docs,
+    and to include a table-of-contents-like tree in the current document.
+    
+    Originally based on the `sphinx.directives.other.TocTree` class.
+    """
+    has_content = True
+    required_arguments = 0
+    optional_arguments = 0
+    final_argument_whitespace = False
+    option_spec = {
+        'maxdepth': int,
+        'glob': directives.flag,
+        'hidden': directives.flag,
+        'numbered': int_or_nothing,
+        'titlesonly': directives.flag,
+    }
+
+    def run(self):
+        env = self.state.document.settings.env
+        suffix = env.config.source_suffix
+        glob = 'glob' in self.options
+
+        ret = []
+        # (title, ref) pairs, where ref may be a document, or an external link,
+        # and title may be None if the document's title is to be used
+        entries = []
+        includefiles = []
+        all_docnames = env.found_docs.copy()
+        # don't add the currently visited file in catch-all patterns
+        all_docnames.remove(env.docname)
+        #the following logic is traight from TocTree
+        for entry in self.content:
+            if not entry:
+                continue
+            if not glob:
+                # look for explicit titles ("Some Title <document>")
+                m = explicit_title_re.match(entry)
+                if m:
+                    ref = m.group(2)
+                    title = m.group(1)
+                    docname = ref
+                else:
+                    ref = docname = entry
+                    title = None
+                # remove suffixes (backwards compatibility)
+                if docname.endswith(suffix):
+                    docname = docname[:-len(suffix)]
+                # absolutize filenames
+                docname = docname_join(env.docname, docname)
+                if url_re.match(ref) or ref == 'self':
+                    entries.append((title, ref))
+                elif docname not in env.found_docs:
+                    ret.append(self.state.document.reporter.warning(
+                        'toctree contains reference to nonexisting '
+                        'document %r' % docname, line=self.lineno))
+                    env.note_reread()
+                else:
+                    entries.append((title, docname))
+                    includefiles.append(docname)
+            else:
+                #For globs only, we differ from TocTree by sorting
+                patname = docname_join(env.docname, entry)
+                docnames = sorted(patfilter(all_docnames, patname))
+                for docname in docnames:
+                    all_docnames.remove(docname) # don't include it again
+                    entries.append((None, docname))
+                    includefiles.append(docname)
+                if not docnames:
+                    ret.append(self.state.document.reporter.warning(
+                        'toctree glob pattern %r didn\'t match any documents'
+                        % entry, line=self.lineno))
+        
+        subnode = latesttree()
+        subnode['parent'] = env.docname
+        # entries contains all entries (self references, external links etc.)
+        subnode['entries'] = entries
+        # includefiles only entries that are documents
+        subnode['includefiles'] = includefiles
+        subnode['maxdepth'] = self.options.get('maxdepth', -1)
+        
+        subnode['glob'] = glob
+        subnode['hidden'] = 'hidden' in self.options
+        subnode['numbered'] = self.options.get('numbered', 0)
+        subnode['titlesonly'] = 'titlesonly' in self.options
+        wrappernode = nodes.compound(classes=['toctree-wrapper'])
+        wrappernode.append(subnode)
+        ret.append(wrappernode)
+        return ret
+
+directives.register_directive('latest', Latest)
+

File feed/sphinxcontrib/feed/feednodes.py

+#from docutils import nodes
+from sphinx.addnodes import toctree
+
+class latesttree(toctree):
+    """toctree which will be sortd by date"""
+    

File feed/sphinxcontrib/feed/feedtransforms.py

+# -*- coding: utf-8 -*-
+"""
+    sphinx.environment
+    ~~~~~~~~~~~~~~~~~~
+
+    Global creation environment.
+
+    :copyright: Copyright 2007-2011 by the Sphinx team, see AUTHORS.
+    :license: BSD, see LICENSE for details.
+"""
+# 
+# import re
+# import os
+# import sys
+# import time
+# import types
+# import codecs
+# import imghdr
+# import string
+# import unicodedata
+# import cPickle as pickle
+from os import path
+# from glob import glob
+# from itertools import izip, groupby
+# 
+# from docutils import nodes
+# from docutils.io import FileInput, NullOutput
+# from docutils.core import Publisher
+# from docutils.utils import Reporter, relative_path, new_document, \
+#      get_source_line
+# from docutils.readers import standalone
+# from docutils.parsers.rst import roles, directives, Parser as RSTParser
+# from docutils.parsers.rst.languages import en as english
+# from docutils.parsers.rst.directives.html import MetaBody
+# from docutils.writers import UnfilteredWriter
+from docutils.transforms import Transform
+# from docutils.transforms.parts import ContentsFilter
+
+from feednodes import latesttree
+
+class SortLastestTree(Transform):
+    """
+    Replace translatable nodes with their translated doctree.
+    """
+    default_priority = 800
+    
+    def apply(self):
+        env = self.document.settings.env
+        settings, source = self.document.settings, self.document['source']
+        # XXX check if this is reliable
+        assert source.startswith(env.srcdir)
+        docname = path.splitext(path.relpath(source, env.srcdir))[0]
+
+        for node in self.document.traverse(latesttree):
+            import pdb; pdb.set_trace()
+            
+            if len(node['ids']) > 1 and node['ids'][0].startswith('id'):
+                node['ids'] = node['ids'][1:] + [node['ids'][0]]
+
+        # for child in patch.children: # update leaves
+        #     child.parent = node
+        # node.children = patch.children
+