Source

sphinx-hg / sphinxcontrib / hg_changelog.py

# -*- coding: utf-8-unix -*-
# Copyright 2012 (C) Takayuki KONDO <tkondou@gmail.com>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

from docutils import nodes
from docutils.parsers.rst import directives
from sphinx.util.compat import Directive

from mercurial import ui, hg, util

class HgChangeLog(Directive):
    has_content = True
    required_arguments = 0
    optional_arguments = 2
    final_argument_whitespace = False
    option_spec = {
        'max_commits': directives.positive_int,
        'repo_root_path': directives.unchanged_required
    }
    
    def run(self):
        repo_root_path = "."
        if 'repo_root_path' in self.options:
            repo_root_path = self.options['repo_root_path']

        repo = hg.repository(ui.ui(), repo_root_path)
        l = nodes.bullet_list()

        commits = range(repo.changectx("tip").rev()+1)
        commits.reverse()

        max_commits = 10
        if 'max_commits' in self.options:
            max_commits = self.options['max_commits']
        
        for commit in commits[:max_commits]:
            ctx = repo.changectx(commit)
            item = nodes.list_item()
            item += [ nodes.strong(text=ctx.description()),
                      nodes.inline(text=" by "),
                      nodes.emphasis(text=ctx.user()),
                      nodes.inline(text=" at "),
                      nodes.emphasis(text=util.datestr(ctx.date()))
                      ]
            l.append(item)
        return [l]

def setup(app):
    app.add_directive('hg_changelog', HgChangeLog)