本文整理汇总了Python中trac.mimeview.api.Mimeview.render方法的典型用法代码示例。如果您正苦于以下问题:Python Mimeview.render方法的具体用法?Python Mimeview.render怎么用?Python Mimeview.render使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类trac.mimeview.api.Mimeview
的用法示例。
在下文中一共展示了Mimeview.render方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: expand_macro
# 需要导入模块: from trac.mimeview.api import Mimeview [as 别名]
# 或者: from trac.mimeview.api.Mimeview import render [as 别名]
def expand_macro(self, formatter, name, content):
add_stylesheet(formatter.req, 'lineno/css/lineno.css')
i = 1
self._anchor = 'a1'
while self._anchor in formatter._anchors:
self._anchor = 'a' + str(i)
i += 1
formatter._anchors[self._anchor] = True
mt = 'txt'
match = WikiParser._processor_re.match(content)
if match:
try: #Trac 0.12+
mt = match.group(2)
content = content[match.end(2)+1:]
except IndexError: #Trac 0.11
mt = match.group(1)
content = content[match.end(1)+1:]
mimeview = Mimeview(formatter.env)
mimetype = mimeview.get_mimetype(mt) or mimeview.get_mimetype('txt')
return mimeview.render(formatter.context, mimetype,
content, annotations=['codeblock-lineno'])
示例2: FilePage
# 需要导入模块: from trac.mimeview.api import Mimeview [as 别名]
# 或者: from trac.mimeview.api.Mimeview import render [as 别名]
class FilePage(Page):
def __init__(self, env, node, root, base):
super(FilePage, self).__init__(env, node, root, base)
self.mimeview = Mimeview(self.env)
self.exists = (node is not None)
self.mime_type = None
self.chunk = None
def get_html(self, req):
"""
Get the raw content from the repository and convert to html.
"""
mime_type, chunk = self.get_raw()
if not mime_type.startswith('text'):
raise TracError("Invalid mime-type: %s" % mime_type)
# Hack to support images, we change the path from relative
# the document being requested to absolute.
# 1: Ignore http and ftp urls to allow images to be fetched
# 2: Assume URLS beginning with "/" are relative to top-level
# 3: Assume URLS that do not include "http/ftp" are relative to
# current path.
def fixup(m):
text = m.group(1)
if text.startswith('http:') or text.startswith('ftp:'):
return m.group(0)
if text.startswith('/'):
text = text[1:]
dir = self.dir
if dir.endswith('/'):
dir = dir[:-1]
return '.. image:: %s/%s' % (req.href.docs(dir), text)
chunk = re.sub('\.\. image\:\:\s*(\S+)', fixup, chunk, re.MULTILINE)
# Assume all wiki pages are ReStructuredText documents
result = self.mimeview.render(req, mime_type, chunk)
if not isinstance(result, (str, unicode)):
result = unicode(result)
# Hack to pretty-print source code (assumes all literal-blocks
# contain source code).
result = result.replace('<pre class="literal-block">',
'<pre class="literal-block prettyprint">')
if 'prettyprint' in result:
# FIXME: Add as an event listener instead?
result += """
<script type="text/javascript">
var origOnLoad = window.onload;
function onLoad() {
if (origOnLoad) {
if(typeof(origOnLoad) == "string") {
eval(origOnLoad);
}
else {
origOnLoad();
}
}
prettyPrint();
}
window.onload = onLoad;
</script>
"""
return Markup(result)
def get_raw(self):
"""
Load the raw content from the repository.
"""
if self.mime_type is not None:
return self.mime_type, self.chunk
node = self.node
content_length = node.get_content_length()
if content_length > (1024 * 1024):
raise TracError("Docs is too large: %d" % content_length)
content = node.get_content()
chunk = content.read(content_length)
mime_type = node.content_type
# Guess the mime-type when possible to be text/plain.
if not mime_type or mime_type == 'application/octet-stream':
mime_type = self.mimeview.get_mimetype(node.name, chunk) or \
mime_type or 'text/plain'
if mime_type.startswith('text/plain'):
mime_type = 'text/x-rst; charset=utf8'
self.mime_type = mime_type
self.chunk = chunk
return mime_type, chunk
def save(self, req, text, comment):
"""
Save the specified text into this document.
#.........这里部分代码省略.........
示例3: process_request
# 需要导入模块: from trac.mimeview.api import Mimeview [as 别名]
# 或者: from trac.mimeview.api.Mimeview import render [as 别名]
def process_request(self, req):
req.perm.require('WIKI_VIEW')
action = req.args.get('action', 'view')
version = req.args.get('version')
root = self.config.get('docs', 'root', '')
base = req.args.get('path')
if base[-1] == '/':
base = base[:-1]
path = root + base
title = base or 'Docs'
if action != 'view':
title += ' (%s)' % action
data = {}
data['title'] = title
repos = self.env.get_repository(authname=req.authname)
node = repos.get_node(path, None)
if node.isdir:
page = DirPage(self.env, node, root, base)
else:
page = FilePage(self.env, node, root, base)
data['editable'] = not node.isdir
if req.method == 'POST':
if action == 'edit':
if page.node.isdir:
raise TracError("Cannot edit a directory")
latest_version = page.version
if req.args.has_key('cancel'):
req.redirect(req.href.docs(page.base))
elif int(version) != latest_version:
action = 'collision'
self._render_editor(req, page, data)
elif req.args.has_key('preview'):
action = 'preview'
self._render_editor(req, page, data, preview=True)
else:
self._do_save(req, page)
elif action == 'edit':
self._render_editor(req, page, data)
else:
req.perm.assert_permission('WIKI_VIEW')
if node.isdir:
data['entries'] = page.get_entries(req)
if page.index is not None:
data['index'] = page.index.get_html(req)
else:
mimeview = Mimeview(self.env)
text = []
text.append('=' * (len(page.name) + 6))
text.append(' %s' % page.name)
text.append('=' * (len(page.name) + 6))
text = '\n'.join(text)
mimetype = 'text/x-rst; charset=utf8'
result = mimeview.render(req, mimetype, text)
data['index'] = result
else:
mime_type, chunk = page.get_raw()
# When possible, send with non-text mimetype
# Perhaps we should embed images...?
if not mime_type.startswith('text') or \
mime_type.startswith('text/html'):
req.send_response(200)
req.send_header('Content-Type', mime_type)
req.end_headers()
req.write(chunk)
return
format = req.args.get('format')
if format:
Mimeview(self.env).send_converted(req, 'text/x-rst', chunk, format, page.name)
for conversion in Mimeview(self.env).get_supported_conversions('text/x-rst'):
conversion_href = req.href.docs(page.base, format=conversion[0])
add_link(req, 'alternate', conversion_href, conversion[1],
conversion[3])
# Render the content into HTML
data['content'] = page.get_html(req)
data['action'] = action
data['current_href'] = req.href.docs(page.base)
data['log_href'] = req.href.log(page.path)
# Include trac wiki stylesheet
add_stylesheet(req, 'common/css/wiki.css')
# Include trac docs stylesheet
add_stylesheet(req, 'docs/common.css')
#.........这里部分代码省略.........
示例4: PygmentsRendererTestCase
# 需要导入模块: from trac.mimeview.api import Mimeview [as 别名]
# 或者: from trac.mimeview.api.Mimeview import render [as 别名]
class PygmentsRendererTestCase(unittest.TestCase):
def setUp(self):
self.env = EnvironmentStub(enable=[Chrome, PygmentsRenderer])
self.pygments = Mimeview(self.env).renderers[0]
self.req = Mock(base_path='', chrome={}, args={},
abs_href=Href('/'), href=Href('/'),
session={}, perm=None, authname=None, tz=None)
self.context = web_context(self.req)
pygments_html = open(os.path.join(os.path.split(__file__)[0],
'pygments.html'))
self.pygments_html = Stream(list(HTMLParser(pygments_html, encoding='utf-8')))
def _expected(self, expected_id):
return self.pygments_html.select(
'//div[@id="%s"]/*|//div[@id="%s"]/text())' %
(expected_id, expected_id))
def _test(self, expected_id, result):
expected = str(self._expected(expected_id))
result = str(result)
#print "\nE: " + repr(expected)
#print "\nR: " + repr(result)
expected, result = expected.splitlines(), result.splitlines()
for exp, res in zip(expected, result):
self.assertEqual(exp, res)
self.assertEqual(len(expected), len(result))
def test_python_hello(self):
"""
Simple Python highlighting with Pygments (direct)
"""
result = self.pygments.render(self.context, 'text/x-python', """
def hello():
return "Hello World!"
""")
self.assertTrue(result)
self._test('python_hello', result)
def test_python_hello_mimeview(self):
"""
Simple Python highlighting with Pygments (through Mimeview.render)
"""
result = Mimeview(self.env).render(self.context, 'text/x-python', """
def hello():
return "Hello World!"
""")
self.assertTrue(result)
self._test('python_hello_mimeview', result)
def test_newline_content(self):
"""
The behavior of Pygments changed post-Pygments 0.11.1, and now
contains all four newlines. In Pygments 0.11.1 and prior, it only
has three since stripnl defaults to True.
See http://trac.edgewall.org/ticket/7705.
"""
from pkg_resources import parse_version, get_distribution
result = self.pygments.render(self.context, 'text/x-python', '\n\n\n\n')
self.assertTrue(result)
t = "".join([r[1] for r in result if r[0] is TEXT])
if parse_version(pygments.__version__) > parse_version('0.11.1') \
or pygments.__version__ == '0.11.1' and 'dev' in \
get_distribution('Pygments').version:
self.assertEqual("\n\n\n\n", t)
else:
self.assertEqual("\n\n\n", t)
def test_empty_content(self):
"""
A '\n' token is generated for an empty file, so we have to bypass
pygments when rendering empty files.
"""
result = self.pygments.render(self.context, 'text/x-python', '')
self.assertIsNone(result)
def test_extra_mimetypes(self):
"""
The text/x-ini mimetype is normally not known by Trac, but
Pygments supports it.
"""
mimeview = Mimeview(self.env)
self.assertEqual('text/x-ini; charset=utf-8',
mimeview.get_mimetype('file.ini'))
self.assertEqual('text/x-ini; charset=utf-8',
mimeview.get_mimetype('file.cfg'))
self.assertEqual('text/x-ini; charset=utf-8',
mimeview.get_mimetype('file.text/x-ini'))
示例5: format_wiki_text
# 需要导入模块: from trac.mimeview.api import Mimeview [as 别名]
# 或者: from trac.mimeview.api.Mimeview import render [as 别名]
def format_wiki_text(text):
from trac.mimeview.api import Mimeview
mimeview = Mimeview(self.env)
mimetype = 'text/x-trac-wiki'
return mimeview.render(req, mimetype, text)
示例6: PatchRendererTestCase
# 需要导入模块: from trac.mimeview.api import Mimeview [as 别名]
# 或者: from trac.mimeview.api.Mimeview import render [as 别名]
class PatchRendererTestCase(unittest.TestCase):
def setUp(self):
env = EnvironmentStub(enable=[Chrome, PatchRenderer])
req = Mock(base_path='', chrome={'static_hash': None}, args={},
session={}, abs_href=Href('/'), href=Href('/'), locale='',
perm=MockPerm(), authname=None, tz=None)
self.context = web_context(req)
self.patch = Mimeview(env).renderers[0]
patch_html = open(os.path.join(os.path.split(__file__)[0],
'patch.html'))
self.patch_html = Stream(list(HTMLParser(patch_html, encoding='utf-8')))
def _expected(self, expected_id):
return self.patch_html.select('//div[@id="%s"]/div' % expected_id)
def _test(self, expected_id, result):
expected = self._expected(expected_id).render(encoding='utf-8')
result = XML(result.render(encoding='utf-8')).render(encoding='utf-8')
expected, result = expected.splitlines(), result.splitlines()
for exp, res in zip(expected, result):
self.assertEquals(exp, res)
self.assertEquals(len(expected), len(result))
def test_simple(self):
"""
Simple patch rendering
"""
result = self.patch.render(self.context, None, """
--- README.orig 2006-10-27 14:42:04.062500000 +0200
+++ README 2006-10-27 14:42:28.125000000 +0200
@@ -1,5 +1,5 @@
----
-base
-base
-base
+be
+the base
+base modified
.
""")
self.assertTrue(result)
self._test('simple', result)
def test_no_newline_in_base(self):
"""
Simple regression test for #4027 ("No newline at end of file")
"""
result = self.patch.render(self.context, None, """
--- nonewline 2006-10-27 08:36:48.453125000 +0200
+++ newline 2006-10-27 08:36:57.187500000 +0200
@@ -1 +1 @@
-ONELINE
\ No newline at end of file
+ONELINE
""")
self.assertTrue(result)
self._test('no_newline_in_base', result)
def test_no_newline_in_changed(self):
"""
Another simple regression test for #4027 ("No newline at end of file")
"""
result = self.patch.render(self.context, None, """
--- newline 2006-10-27 08:36:57.187500000 +0200
+++ nonewline 2006-10-27 08:36:48.453125000 +0200
@@ -1 +1 @@
-ONELINE
+ONELINE
\ No newline at end of file
""")
self.assertTrue(result)
self._test('no_newline_in_changed', result)
def test_diff_to_hdf_expandtabs(self):
"""Regression test related to #4557"""
changes = self.patch._diff_to_hdf(
['--- hello.c 1',
'+++ hello.c 2',
'@@ -1 +1 @@',
'-aa\tb',
'+aaxb'], 8)
self.assertEquals('aa<del> </del>b',
str(changes[0]['diffs'][0][0]['base']['lines'][0]))
self.assertEquals('aa<ins>x</ins>b',
str(changes[0]['diffs'][0][0]['changed']['lines'][0]))
def test_diff_to_hdf_leading_ws(self):
"""Regression test related to #5795"""
changes = self.patch._diff_to_hdf(
['--- hello.c 1',
'+++ hello.c 2',
'@@ -1 +1 @@',
'-*a',
'+ *a'], 8)
self.assertEquals('<del></del>*a',
str(changes[0]['diffs'][0][0]['base']['lines'][0]))
self.assertEquals('<ins> </ins>*a',
str(changes[0]['diffs'][0][0]['changed']['lines'][0]))
示例7: render
# 需要导入模块: from trac.mimeview.api import Mimeview [as 别名]
# 或者: from trac.mimeview.api.Mimeview import render [as 别名]
def render(self, req):
"""Render the data."""
context = Context.from_request(req)
mimeview = Mimeview(self.env)
return mimeview.render(context, self.mimetype, self.data,
annotations=['lineno'])
示例8: expand_macro
# 需要导入模块: from trac.mimeview.api import Mimeview [as 别名]
# 或者: from trac.mimeview.api.Mimeview import render [as 别名]
def expand_macro(self, formatter, name, content):
self.log.info('Begin expand_macro for req: ' + repr(content))
largs, kwargs = parse_args(content)
if len(largs) == 0:
raise TracError("File name to include is required parameter!")
orig_file_name = file_name = largs[0]
global multirepos
if not multirepos:
repos = self.env.get_repository(formatter.req.authname)
else:
if (orig_file_name[0] == '/'): orig_file_name = orig_file_name[1:]
splitpath = file_name.split('/')
if (file_name[0] == '/'):
reponame = splitpath[1]
else:
reponame = splitpath[0]
repos = self.env.get_repository(reponame)
if (repos):
l = len(reponame)
if (file_name[0] == '/'):
file_name = file_name[1:]
file_name = file_name[l:]
else:
repos = self.env.get_repository()
rev = kwargs.get('rev', None)
if kwargs.has_key('header'):
header = kwargs.get('header') # user specified header
else:
href = '../browser/%s%s' % (orig_file_name, make_rev_str(rev))
header = tag.a(file_name, href=href)
if not header:
header = u'\xa0' # default value from trac.mimeview.api.py
# TODO - 'content' is default from mimeview.api.py, but it picks
# up text-align: center, which probably isn't the best thing if
# we are adding a file name in the header. There isn't an obvious
# replacement in the delivered CSS to pick over this for now though
header_class = kwargs.get('header_class', 'content')
src = repos.get_node(file_name, rev).get_content().read()
context = formatter.context
# put these into context object so annotator sees them
context.file_name = file_name
context.rev = rev
context.startline = 1
# we generally include line numbers in the output, unless it has been
# explicitly requested otherwise. 0, no, false, none will suppress
line_numbers = kwargs.get('line_numbers', None)
if line_numbers is None:
line_numbers = True
else:
try:
line_numbers = int(line_numbers)
except:
negatory = ('no', 'false', 'none')
line_numbers = str(line_numbers).lower() not in negatory
# lines added up front to "trick" renderer when rendering partial
render_prepend = []
start, end = kwargs.get('start', None), kwargs.get('end', None)
if start or end:
src, start, end = self._handle_partial(src, start, end)
context.startline = start
if start > 2 and file_name.endswith('.php'):
render_prepend = [ '#!/usr/bin/php -f', '<?' ]
if render_prepend:
src = '\n'.join(render_prepend) + '\n' + src
# ensure accurate start number after this gets stripped
context.startline = start - len(render_prepend)
mimetype = kwargs.get('mimetype', None)
url = None # render method doesn't seem to use this
mv = Mimeview(self.env)
annotations = line_numbers and ['givenlineno'] or None
src = mv.render(formatter.context, mimetype, src, file_name, url, annotations)
if line_numbers:
# handle the case where only one line of code was included
# and we get back an XHTML string
if not hasattr(src, 'generate'):
from genshi.input import XML
src = XML(src)
# the _render_source method will always set the CSS class
# of the annotator to it's name; there isn't an easy way
# to override that. We could create our own CSS class for
# givenlineno that mimics lineno, but it's cleaner to just
#.........这里部分代码省略.........