Commits

Mike Bayer committed eb20d00

- version 0.3.0
- move in SQLAlchemy's version here, removes unused architecture for other kinds
of directives, adds support for changelog imports
- support render strings as callables also, e.g. to support custom schemes
like github vs. bitbucket

Comments (0)

Files changed (1)

-__version__ = '0.2.0'
+__version__ = '0.3.0'
+
 import re
 from sphinx.util.compat import Directive
 from docutils.statemachine import StringList
     def env(self):
         return self.state.document.settings.env
 
+    @classmethod
+    def changes(cls, env):
+        return env.temp_data['ChangeLogDirective_changes']
+
 class ChangeLogDirective(EnvDirective, Directive):
     has_content = True
 
-    type_ = "change"
-
     default_section = 'misc'
 
     def _organize_by_section(self, changes):
         bysection = collections.defaultdict(list)
         all_sections = set()
         for rec in changes:
+            if self.version not in rec['versions']:
+                continue
             inner_tag = rec['tags'].intersection(self.inner_tag_sort)
             if inner_tag:
                 inner_tag = inner_tag.pop()
                     bysection[(self.default_section, inner_tag)].append(rec)
         return bysection, all_sections
 
-    @classmethod
-    def changes(cls, env):
-        return env.temp_data['ChangeLogDirective_%s_changes' % cls.type_]
-
     def _setup_run(self):
         self.sections = self.env.config.changelog_sections
         self.inner_tag_sort = self.env.config.changelog_inner_tag_sort + [""]
-        self.env.temp_data['ChangeLogDirective_%s_changes' % self.type_] = []
+        if 'ChangeLogDirective_changes' not in self.env.temp_data:
+            self.env.temp_data['ChangeLogDirective_changes'] = []
         self._parsed_content = _parse_content(self.content)
 
+        self.version = version = self._parsed_content.get('version', '')
+        self.env.temp_data['ChangeLogDirective_version'] = version
+
         p = nodes.paragraph('', '',)
         self.state.nested_parse(self.content[1:], 0, p)
 
     def run(self):
         self._setup_run()
+
+        if 'ChangeLogDirective_includes' in self.env.temp_data:
+            return []
+
         changes = self.changes(self.env)
         output = []
 
-        self.version = version = self._parsed_content.get('version', '')
-        id_prefix = "%s-%s" % (self.type_, version)
+        id_prefix = "change-%s" % (self.version, )
         topsection = self._run_top(id_prefix)
         output.append(topsection)
 
             topsection.append(nodes.Text("no release date"))
 
         intro_para = nodes.paragraph('', '')
+        len_ = -1
         for len_, text in enumerate(self._parsed_content['text']):
             if ".. change::" in text:
                 break
-        if len_:
+
+        # if encountered any text elements that didn't start with
+        # ".. change::", those become the intro
+        if len_ > 0:
             self.state.nested_parse(self._parsed_content['text'][0:len_], 0,
                             intro_para)
             topsection.append(intro_para)
         text = _text_rawsource_from_node(para)
 
         to_hash = "%s %s" % (self.version, text[0:100])
-        targetid = "%s-%s" % (self.type_,
+        targetid = "change-%s" % (
                         md5.md5(to_hash.encode('ascii', 'ignore')
                             ).hexdigest())
         targetnode = nodes.target('', '', ids=[targetid])
                     )
         para.append(permalink)
 
+        if len(rec['versions']) > 1:
+
+            backported_changes = rec['sorted_versions'][rec['sorted_versions'].index(self.version) + 1:]
+            if backported_changes:
+                backported = nodes.paragraph('')
+                backported.append(nodes.Text("This change is also ", ""))
+                backported.append(nodes.strong("", "backported"))
+                backported.append(nodes.Text(" to: %s" % ", ".join(backported_changes), ""))
+                para.append(backported)
+
         insert_ticket = nodes.paragraph('')
         para.append(insert_ticket)
 
                 if i > 0:
                     insert_ticket.append(nodes.Text(", ", ", "))
                 else:
-                    insert_ticket.append(nodes.Text(" ", " "))
+                    insert_ticket.append(nodes.Text("References: """))
                 i += 1
                 if render is not None:
-                    refuri = render % refname
+                    if callable(render):
+                        refuri = render(refname)
+                    else:
+                        refuri = render % refname
                     node = nodes.reference('', '',
                             nodes.Text(prefix % refname, prefix % refname),
                             refuri=refuri
             )
         )
 
+class ChangeLogImportDirective(EnvDirective, Directive):
+    has_content = True
+
+    def _setup_run(self):
+        if 'ChangeLogDirective_changes' not in self.env.temp_data:
+            self.env.temp_data['ChangeLogDirective_changes'] = []
+
+    def run(self):
+        self._setup_run()
+
+        # tell ChangeLogDirective we're here, also prevent
+        # nested .. include calls
+        if 'ChangeLogDirective_includes' not in self.env.temp_data:
+            self.env.temp_data['ChangeLogDirective_includes'] = True
+            p = nodes.paragraph('', '',)
+            self.state.nested_parse(self.content, 0, p)
+            del self.env.temp_data['ChangeLogDirective_includes']
+
+        return []
 
 class ChangeDirective(EnvDirective, Directive):
     has_content = True
 
-    type_ = "change"
-    parent_cls = ChangeLogDirective
-
     def run(self):
         content = _parse_content(self.content)
         p = nodes.paragraph('', '',)
         sorted_tags = _comma_list(content.get('tags', ''))
+        declared_version = self.env.temp_data['ChangeLogDirective_version']
+        versions = set(_comma_list(content.get("versions", ""))).difference(['']).\
+                            union([declared_version])
+
+        # if we don't refer to any other versions and we're in an include,
+        # skip
+        if len(versions) == 1 and 'ChangeLogDirective_includes' in self.env.temp_data:
+            return []
+
+        def int_ver(ver):
+            out = []
+            for dig in ver.split("."):
+                try:
+                    out.append(int(dig))
+                except ValueError:
+                    out.append(0)
+            return tuple(out)
+
         rec = {
             'tags': set(sorted_tags).difference(['']),
             'tickets': set(_comma_list(content.get('tickets', ''))).difference(['']),
             'pullreq': set(_comma_list(content.get('pullreq', ''))).difference(['']),
             'changeset': set(_comma_list(content.get('changeset', ''))).difference(['']),
             'node': p,
-            'type': self.type_,
+            'type': "change",
             "title": content.get("title", None),
-            'sorted_tags': sorted_tags
+            'sorted_tags': sorted_tags,
+            "versions": versions,
+            "sorted_versions": list(reversed(sorted(versions, key=int_ver)))
         }
 
         self.state.nested_parse(content['text'], 0, p)
-        self.parent_cls.changes(self.env).append(rec)
+        ChangeLogDirective.changes(self.env).append(rec)
 
         return []
 
 def setup(app):
     app.add_directive('changelog', ChangeLogDirective)
     app.add_directive('change', ChangeDirective)
+    app.add_directive('changelog_imports', ChangeLogImportDirective)
     app.add_config_value("changelog_sections", [], 'env')
     app.add_config_value("changelog_inner_tag_sort", [], 'env')
     app.add_config_value("changelog_render_ticket",