Commits

Georg Brandl committed a8abd74

#123: The ``glossary`` directive now supports a ``:sorted:``
flag that sorts glossary entries alphabetically.

Comments (0)

Files changed (3)

     the directive -- this allows you to define your document
     structure, but place the links yourself.
 
+  - #123: The ``glossary`` directive now supports a ``:sorted:``
+    flag that sorts glossary entries alphabetically.
+
   - Paths to images, literal include files and download files
     can now be absolute (like ``/images/foo.png``).  They are
     treated as relative to the top source directory.

doc/markup/para.rst

       .. glossary::
 
          environment
-            A structure where information about all documents under the root is saved,
-            and used for cross-referencing.  The environment is pickled after the
-            parsing stage, so that successive runs only need to read and parse new and
-            changed documents.
+            A structure where information about all documents under the root is
+            saved, and used for cross-referencing.  The environment is pickled
+            after the parsing stage, so that successive runs only need to read
+            and parse new and changed documents.
 
          source directory
-            The directory which, including its subdirectories, contains all source
-            files for one Sphinx project.
+            The directory which, including its subdirectories, contains all
+            source files for one Sphinx project.
+
+   .. versionadded:: 0.6
+      You can now give the glossary directive a ``:sorted:`` flag that will
+      automatically sort the entries alphabetically.
 
 
 Grammar production displays

sphinx/directives/other.py

     required_arguments = 0
     optional_arguments = 0
     final_argument_whitespace = False
-    option_spec = {}
+    option_spec = {
+        'sorted': directives.flag,
+    }
 
     def run(self):
         env = self.state.document.settings.env
         dls = [child for child in node
                if isinstance(child, nodes.definition_list)]
         # now, extract definition terms to enable cross-reference creation
+        new_dl = nodes.definition_list()
+        new_dl['classes'].append('glossary')
+        items = []
         for dl in dls:
-            dl['classes'].append('glossary')
             for li in dl.children:
                 if not li.children or not isinstance(li[0], nodes.term):
                     continue
                 indexnode = addnodes.index()
                 indexnode['entries'] = [('single', termtext, new_id, termtext)]
                 li.insert(0, indexnode)
+                items.append((termtext, li))
+        if 'sorted' in self.options:
+            items.sort()
+        new_dl.extend(item[1] for item in items)
+        node.children = [new_dl]
         return [node]