diff --git a/kallithea/lib/vcs/utils/annotate.py b/kallithea/lib/vcs/utils/annotate.py deleted file mode 100644 --- a/kallithea/lib/vcs/utils/annotate.py +++ /dev/null @@ -1,176 +0,0 @@ -from pygments import highlight -from pygments.formatters import HtmlFormatter - -from kallithea.lib.vcs.exceptions import VCSError -from kallithea.lib.vcs.nodes import FileNode -from kallithea.lib.vcs.utils import safe_unicode - - -def annotate_highlight(filenode, annotate_from_changeset_func=None, - order=None, headers=None, **options): - """ - Returns html portion containing annotated table with 3 columns: line - numbers, changeset information and pygmentized line of code. - - :param filenode: FileNode object - :param annotate_from_changeset_func: function taking changeset and - returning single annotate cell; needs break line at the end - :param order: ordered sequence of ``ls`` (line numbers column), - ``annotate`` (annotate column), ``code`` (code column); Default is - ``['ls', 'annotate', 'code']`` - :param headers: dictionary with headers (keys are whats in ``order`` - parameter) - """ - options['linenos'] = True - formatter = AnnotateHtmlFormatter(filenode=filenode, order=order, - headers=headers, - annotate_from_changeset_func=annotate_from_changeset_func, **options) - return highlight(safe_unicode(filenode.content), filenode.lexer, formatter) - - -class AnnotateHtmlFormatter(HtmlFormatter): - - def __init__(self, filenode, annotate_from_changeset_func=None, - order=None, **options): - """ - If ``annotate_from_changeset_func`` is passed it should be a function - which returns string from the given changeset. For example, we may pass - following function as ``annotate_from_changeset_func``:: - - def changeset_to_anchor(changeset): - return '%s\n' % \ - (changeset.id, changeset.id) - - :param annotate_from_changeset_func: see above - :param order: (default: ``['ls', 'annotate', 'code']``); order of - columns; - :param options: standard pygment's HtmlFormatter options, there is - extra option tough, ``headers``. For instance we can pass:: - - formatter = AnnotateHtmlFormatter(filenode, headers={ - 'ls': '#', - 'annotate': 'Annotate', - 'code': 'Code', - }) - - """ - super(AnnotateHtmlFormatter, self).__init__(**options) - self.annotate_from_changeset_func = annotate_from_changeset_func - self.order = order or ('ls', 'annotate', 'code') - headers = options.pop('headers', None) - if headers and not ('ls' in headers and 'annotate' in headers and - 'code' in headers - ): - raise ValueError("If headers option dict is specified it must " - "all 'ls', 'annotate' and 'code' keys") - self.headers = headers - if isinstance(filenode, FileNode): - self.filenode = filenode - else: - raise VCSError("This formatter expect FileNode parameter, not %r" - % type(filenode)) - - def annotate_from_changeset(self, changeset): - """ - Returns full html line for single changeset per annotated line. - """ - if self.annotate_from_changeset_func: - return self.annotate_from_changeset_func(changeset) - else: - return ''.join((changeset.id, '\n')) - - def _wrap_tablelinenos(self, inner): - inner_lines = [] - lncount = 0 - for t, line in inner: - if t: - lncount += 1 - inner_lines.append(line) - - fl = self.linenostart - mw = len(str(lncount + fl - 1)) - sp = self.linenospecial - st = self.linenostep - la = self.lineanchors - aln = self.anchorlinenos - if sp: - lines = [] - - for i in range(fl, fl + lncount): - if i % st == 0: - if i % sp == 0: - if aln: - lines.append('' - '%*d' % - (la, i, mw, i)) - else: - lines.append('' - '%*d' % (mw, i)) - else: - if aln: - lines.append('' - '%*d' % (la, i, mw, i)) - else: - lines.append('%*d' % (mw, i)) - else: - lines.append('') - ls = '\n'.join(lines) - else: - lines = [] - for i in range(fl, fl + lncount): - if i % st == 0: - if aln: - lines.append('%*d' - % (la, i, mw, i)) - else: - lines.append('%*d' % (mw, i)) - else: - lines.append('') - ls = '\n'.join(lines) - - annotate_changesets = [tup[1] for tup in self.filenode.annotate] - # If pygments cropped last lines break we need do that too - ln_cs = len(annotate_changesets) - ln_ = len(ls.splitlines()) - if ln_cs > ln_: - annotate_changesets = annotate_changesets[:ln_ - ln_cs] - annotate = ''.join((self.annotate_from_changeset(changeset) - for changeset in annotate_changesets)) - # in case you wonder about the seemingly redundant
' + - ls + ' | ' +
- '') - yield 0, ''.join(inner_lines) - yield 0, ' |
' + - ls + '
' + - annotate + '