Files
@ aaa7c3331186
Branch filter:
Location: kallithea/rhodecode/lib/annotate.py - annotation
aaa7c3331186
8.0 KiB
text/x-python
Rename paster command setup-rhodecode to setup-db
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 | 1d1ccb873d00 ffd45b185016 ffd45b185016 ffd45b185016 ffd45b185016 ffd45b185016 ffd45b185016 ffd45b185016 ffd45b185016 ffd45b185016 ffd45b185016 ffd45b185016 ffd45b185016 1d1ccb873d00 ffd45b185016 ffd45b185016 1d1ccb873d00 ffd45b185016 cf51bbfb120e ffd45b185016 ffd45b185016 ffd45b185016 ffd45b185016 1d1ccb873d00 ffd45b185016 7000fc4aa569 1d1ccb873d00 324ac367a4da 324ac367a4da 1d1ccb873d00 1d1ccb873d00 1d1ccb873d00 1d1ccb873d00 1d1ccb873d00 1d1ccb873d00 1d1ccb873d00 1d1ccb873d00 1d1ccb873d00 1d1ccb873d00 1d1ccb873d00 1d1ccb873d00 1d1ccb873d00 1d1ccb873d00 1d1ccb873d00 1d1ccb873d00 1d1ccb873d00 1d1ccb873d00 1d1ccb873d00 7000fc4aa569 1d1ccb873d00 1d1ccb873d00 1d1ccb873d00 1d1ccb873d00 7000fc4aa569 1d1ccb873d00 1d1ccb873d00 1d1ccb873d00 1d1ccb873d00 1d1ccb873d00 1d1ccb873d00 1d1ccb873d00 1d1ccb873d00 1d1ccb873d00 1d1ccb873d00 1d1ccb873d00 1d1ccb873d00 1d1ccb873d00 1d1ccb873d00 1d1ccb873d00 1d1ccb873d00 1d1ccb873d00 1d1ccb873d00 1d1ccb873d00 1d1ccb873d00 1d1ccb873d00 1d1ccb873d00 1d1ccb873d00 1d1ccb873d00 1d1ccb873d00 1d1ccb873d00 1d1ccb873d00 1d1ccb873d00 1d1ccb873d00 1d1ccb873d00 1d1ccb873d00 1d1ccb873d00 1d1ccb873d00 1d1ccb873d00 1d1ccb873d00 1d1ccb873d00 1d1ccb873d00 1d1ccb873d00 1d1ccb873d00 1d1ccb873d00 1d1ccb873d00 1d1ccb873d00 1d1ccb873d00 1d1ccb873d00 1d1ccb873d00 1d1ccb873d00 1d1ccb873d00 1d1ccb873d00 1d1ccb873d00 1d1ccb873d00 1d1ccb873d00 1d1ccb873d00 1d1ccb873d00 1d1ccb873d00 1d1ccb873d00 1d1ccb873d00 1d1ccb873d00 1d1ccb873d00 1d1ccb873d00 1d1ccb873d00 1d1ccb873d00 1d1ccb873d00 1d1ccb873d00 1d1ccb873d00 1d1ccb873d00 1d1ccb873d00 1d1ccb873d00 1d1ccb873d00 1d1ccb873d00 1d1ccb873d00 1d1ccb873d00 1d1ccb873d00 1d1ccb873d00 1d1ccb873d00 1d1ccb873d00 1d1ccb873d00 1d1ccb873d00 1d1ccb873d00 1d1ccb873d00 1d1ccb873d00 1d1ccb873d00 1d1ccb873d00 1d1ccb873d00 1d1ccb873d00 1d1ccb873d00 1d1ccb873d00 1d1ccb873d00 1d1ccb873d00 1d1ccb873d00 1d1ccb873d00 1d1ccb873d00 1d1ccb873d00 1d1ccb873d00 1d1ccb873d00 1d1ccb873d00 1d1ccb873d00 1d1ccb873d00 1d1ccb873d00 1d1ccb873d00 1d1ccb873d00 1d1ccb873d00 1d1ccb873d00 1d1ccb873d00 c57a37430dc9 c57a37430dc9 c57a37430dc9 c57a37430dc9 c57a37430dc9 c57a37430dc9 c57a37430dc9 c57a37430dc9 c57a37430dc9 1d1ccb873d00 1d1ccb873d00 1d1ccb873d00 1d1ccb873d00 1d1ccb873d00 1d1ccb873d00 1d1ccb873d00 1d1ccb873d00 1d1ccb873d00 1d1ccb873d00 1d1ccb873d00 1d1ccb873d00 1d1ccb873d00 1d1ccb873d00 1d1ccb873d00 1d1ccb873d00 1d1ccb873d00 1d1ccb873d00 1d1ccb873d00 1d1ccb873d00 1d1ccb873d00 1d1ccb873d00 1d1ccb873d00 1d1ccb873d00 1d1ccb873d00 1d1ccb873d00 1d1ccb873d00 1d1ccb873d00 1d1ccb873d00 1d1ccb873d00 1d1ccb873d00 1d1ccb873d00 1d1ccb873d00 1d1ccb873d00 1d1ccb873d00 1d1ccb873d00 1d1ccb873d00 1d1ccb873d00 | # -*- coding: utf-8 -*-
# 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/>.
"""
rhodecode.lib.annotate
~~~~~~~~~~~~~~~~~~~~~~
Anontation library for usage in rhodecode, previously part of vcs
:created_on: Dec 4, 2011
:author: marcink
:copyright: (c) 2013 RhodeCode GmbH.
:license: GPLv3, see LICENSE for more details.
"""
import StringIO
from rhodecode.lib.vcs.exceptions import VCSError
from rhodecode.lib.vcs.nodes import FileNode
from pygments.formatters import HtmlFormatter
from pygments import highlight
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)
"""
from rhodecode.lib.utils import get_custom_lexer
options['linenos'] = True
formatter = AnnotateHtmlFormatter(filenode=filenode, order=order,
headers=headers,
annotate_from_changeset_func=annotate_from_changeset_func, **options)
lexer = get_custom_lexer(filenode.extension) or filenode.lexer
highlighted = highlight(filenode.content, lexer, formatter)
return highlighted
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 '<a href="/changesets/%s/">%s</a>\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):
dummyoutfile = StringIO.StringIO()
lncount = 0
for t, line in inner:
if t:
lncount += 1
dummyoutfile.write(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('<a href="#%s-%d" class="special">'
'%*d</a>' %
(la, i, mw, i))
else:
lines.append('<span class="special">'
'%*d</span>' % (mw, i))
else:
if aln:
lines.append('<a href="#%s-%d">'
'%*d</a>' % (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('<a href="#%s-%d">%*d</a>' \
% (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]
## TODO: not sure what that fixes
# # 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(el[2]())
for el in self.filenode.annotate))
# in case you wonder about the seemingly redundant <div> here:
# since the content in the other cell also is wrapped in a div,
# some browsers in some configurations seem to mess up the formatting.
'''
yield 0, ('<table class="%stable">' % self.cssclass +
'<tr><td class="linenos"><div class="linenodiv"><pre>' +
ls + '</pre></div></td>' +
'<td class="code">')
yield 0, dummyoutfile.getvalue()
yield 0, '</td></tr></table>'
'''
headers_row = []
if self.headers:
headers_row = ['<tr class="annotate-header">']
for key in self.order:
td = ''.join(('<td>', self.headers[key], '</td>'))
headers_row.append(td)
headers_row.append('</tr>')
body_row_start = ['<tr>']
for key in self.order:
if key == 'ls':
body_row_start.append(
'<td class="linenos"><div class="linenodiv"><pre>' +
ls + '</pre></div></td>')
elif key == 'annotate':
body_row_start.append(
'<td class="annotate"><div class="annotatediv"><pre>' +
annotate + '</pre></div></td>')
elif key == 'code':
body_row_start.append('<td class="code">')
yield 0, ('<table class="%stable">' % self.cssclass +
''.join(headers_row) +
''.join(body_row_start)
)
yield 0, dummyoutfile.getvalue()
yield 0, '</td></tr></table>'
|