Commits

Takafumi Arakaki committed d4380fc

added `recent-pages` directive to show recently update sub-pages.

  • Participants
  • Parent commits cea905b

Comments (0)

Files changed (4)

doc/source/changelog.rst

   :rst:dir:`table-data`, :rst:dir:`table-data-and-image` and
   :rst:dir:`dictdiff` so that they can read the data files
   with unsupported extensions.
+- Added :rst:dir:`recent-pages`.
 
 v0.0.3
 ^^^^^^

doc/source/index.rst

    Insert list of sub-pages.
 
 
+Recently Update Sub-pages - :rst:dir:`recent-pages`
+---------------------------------------------------
+
+.. rst:directive:: .. recent-pages::
+
+   Insert list of recently update sub-pages.
+
+   num : int
+       Number of pages to show (default is 10).
+
+
 .. _template-page:
 
 Template page - ``_temp_``
     return filter_descendants(path, path_list)
 
 
+def recent_pages(path, num):
+    """
+    Find the `num` most recently updated sub-pages of `path`
+    """
+    path_list = find_descendants(path)
+    date_list = [
+        g.db.execute(
+            "select max(updated) from page_history where page_path = ?",
+            [path]).fetchone()[0]
+        for path in path_list]
+    date_path_list = sorted(zip(date_list, path_list), reverse=True)[:num]
+    return date_path_list
+
+
 def has_descendants(path):
     return bool(find_descendants(path))
 
     pass
 
 
+class recent_pages(nodes.Admonition, nodes.Element):
+    pass
+
+
 class dictdiff(nodes.General, nodes.Element):
     pass
 
                 node.replace_self(admonition)
 
 
+class ProcessRecentPages(Transform):
+    _web = None  # needs override
+    default_priority = 0
+
+    def apply(self):
+        nodes_list_pages = list(self.document.traverse(recent_pages))
+        if nodes_list_pages:
+            page_path = self.document.settings.neorg_page_path
+            for node in nodes_list_pages:
+                date_page = self._web.recent_pages(page_path,
+                                                   node.get('num', 10))
+                title = 'Recently Updated Pages'
+                admonition = nodes.admonition()
+                admonition += nodes.title(title, title)
+                bullet_list = nodes.bullet_list(bullet='*')
+                bullet_list += [
+                    nodes.list_item(
+                        '',
+                        with_children(
+                            nodes.paragraph,
+                            [nodes.Text(u'{0}: '.format(d))] +
+                            list(gene_link(u'./{0}'.format(l)))
+                            ))
+                    for (d, l) in date_page]
+                admonition += bullet_list
+                node.replace_self(admonition)
+
+
 class ProcessDictDiff(Transform):
     _web = None  # needs override
     _DictTable = None  # needs override
 
 NEORG_TRANSFORMS = [
     AdHocInlineMarkup, ProcessListPages, ProcessDictDiff,
+    ProcessRecentPages,
     ]
 
 
         return [list_pages('')]
 
 
+class RecentPages(Directive):
+    _dirc_name = 'recent-pages'
+    option_spec = {'num': directives.positive_int}
+
+    def run(self):
+        return [recent_pages('', **self.options)]
+
+
 NEORG_DIRECTIVES = [
     TableData, TableDataAndImage, FindImages, ListPages, DictDiff,
-    GridImages,
+    GridImages, RecentPages,
     ]