def _render_file(self, req, context, repos, node, rev=None): """ trac.versioncontrol.web_ui.browser.BrowserModule._render_file() copy with just the essentials needed for our purpose. """ req.perm(context.resource).require('FILE_VIEW') mimeview = Mimeview(self.env) # MIME type detection CHUNK_SIZE = 4096 content = node.get_content() chunk = content.read(CHUNK_SIZE) mime_type = node.content_type if not mime_type or mime_type == 'application/octet-stream': mime_type = mimeview.get_mimetype(node.name, chunk) or \ mime_type or 'text/plain' self.log.debug( "Rendering ReposReadMe of node %s@%s with mime-type %s" % (node.name, str(rev), mime_type)) del content # the remainder of that content is not needed add_stylesheet(req, 'common/css/code.css') annotations = [] force_source = False raw_href = '' return mimeview.preview_data(context, node.get_content(), node.get_content_length(), mime_type, node.created_path, raw_href, annotations=annotations, force_source=force_source)
def _render_file(self, req, context, repos, node, rev=None): """ trac.versioncontrol.web_ui.browser.BrowserModule._render_file() copy with just the essentials needed for our purpose. """ req.perm(context.resource).require('FILE_VIEW') mimeview = Mimeview(self.env) # MIME type detection CHUNK_SIZE = 4096 content = node.get_content() chunk = content.read(CHUNK_SIZE) mime_type = node.content_type if not mime_type or mime_type == 'application/octet-stream': mime_type = mimeview.get_mimetype(node.name, chunk) or \ mime_type or 'text/plain' self.log.debug("Rendering ReposReadMe of node %s@%s with mime-type %s" % (node.name, str(rev), mime_type)) del content # the remainder of that content is not needed add_stylesheet(req, 'common/css/code.css') annotations = [] force_source = False raw_href = '' return mimeview.preview_data(context, node.get_content(), node.get_content_length(), mime_type, node.created_path, raw_href, annotations=annotations, force_source=force_source)
def _render_file(self, req, context, repos, node, rev=None): req.perm(node.resource).require('FILE_VIEW') mimeview = Mimeview(self.env) # MIME type detection content = node.get_content() chunk = content.read(CHUNK_SIZE) mime_type = node.content_type if not mime_type or mime_type == 'application/octet-stream': mime_type = mimeview.get_mimetype(node.name, chunk) or \ mime_type or 'text/plain' # Eventually send the file directly format = req.args.get('format') if format in ('raw', 'txt'): req.send_response(200) req.send_header('Content-Type', 'text/plain' if format == 'txt' else mime_type) req.send_header('Content-Length', node.content_length) req.send_header('Last-Modified', http_date(node.last_modified)) if rev is None: req.send_header('Pragma', 'no-cache') req.send_header('Cache-Control', 'no-cache') req.send_header('Expires', 'Fri, 01 Jan 1999 00:00:00 GMT') if not self.render_unsafe_content: # Force browser to download files instead of rendering # them, since they might contain malicious code enabling # XSS attacks req.send_header('Content-Disposition', 'attachment') req.end_headers() while 1: if not chunk: raise RequestDone req.write(chunk) chunk = content.read(CHUNK_SIZE) else: # The changeset corresponding to the last change on `node` # is more interesting than the `rev` changeset. changeset = repos.get_changeset(node.created_rev) # add ''Plain Text'' alternate link if needed if not is_binary(chunk) and mime_type != 'text/plain': plain_href = req.href.browser(repos.reponame or None, node.path, rev=rev, format='txt') add_link(req, 'alternate', plain_href, _('Plain Text'), 'text/plain') # add ''Original Format'' alternate link (always) raw_href = req.href.export(rev or repos.youngest_rev, repos.reponame or None, node.path) add_link(req, 'alternate', raw_href, _('Original Format'), mime_type) self.log.debug("Rendering preview of node %s@%s with mime-type %s" % (node.name, str(rev), mime_type)) del content # the remainder of that content is not needed add_stylesheet(req, 'common/css/code.css') annotations = ['lineno'] annotate = req.args.get('annotate') if annotate: annotations.insert(0, annotate) preview_data = mimeview.preview_data(context, node.get_content(), node.get_content_length(), mime_type, node.created_path, raw_href, annotations=annotations, force_source=bool(annotate)) return { 'changeset': changeset, 'size': node.content_length, 'preview': preview_data, 'annotate': annotate, }
def filter_stream(self, req, method, template, stream, data): if not (template == 'browser.html' and data.get('dir')): if ((not data.get('dir')) and (data.get('path')) and (data.get('path').endswith('.md'))): # Rendering single markdown file preview stream = stream | Transformer("//head/script[not(@src)][1]").after( tag.script( Markup( "jQuery(document).ready(function($) {" " $('#preview').each(function() {" " $(this).html(marked( $(this).children('pre').first().text() ));" " });" "});" ), type = "text/javascript" ) ) return stream add_stylesheet(req, 'common/css/code.css') repos = data.get('repos') or self.env.get_repository() rev = req.args.get('rev', None) for entry in data['dir']['entries']: # Rendering all READMEs in a directory preview try: if not entry.isdir and entry.name.lower().startswith('readme'): node = repos.get_node(entry.path, rev) req.perm(data['context'].resource).require('FILE_VIEW') mimeview = Mimeview(self.env) content = node.get_content() mimetype = node.content_type divclass = 'searchable' if entry.name.lower().endswith('.wiki'): mimetype = 'text/x-trac-wiki' divclass = 'searchable wiki' elif entry.name.lower().endswith('.md'): mimetype = 'text/x-markdown' divclass = 'searchable markdown' if not mimetype or mimetype == 'application/octet-stream': mimetype = mimeview.get_mimetype(node.name, content.read(4096)) or mimetype or 'text/plain' del content self.log.debug("ReadmeRenderer: rendering node %s@%s as %s" % (node.name, str(rev), mimetype)) output = mimeview.preview_data( data['context'], node.get_content(), node.get_content_length(), mimetype, node.created_path, '', annotations = [], force_source = False ) if output: if isinstance(output['rendered'], Stream): #content = output['rendered'].select('./pre/node()') #content = output['rendered'].select('./pre') content = output['rendered'].select('.') else: self.log.debug("GOT THERE") content = output['rendered'] insert = tag.div( tag.h1(entry.name, tag.a(Markup(' ¶'), class_ = "anchor", href = '#' + entry.name, title = 'Link to file' ), id_ = entry.name ), tag.div( content, #xml:space = "preserve", class_ = divclass, title = entry.name ), class_ = "readme", style = "padding-top: 1em;" ) stream = stream | Transformer("//div[@id='content']/div[@id='help']").before(insert) except Exception, e: self.log.debug(to_unicode(e))
def _render_readme(self, req, stream, data): add_stylesheet(req, 'common/css/code.css') repos = data.get('repos') or self.env.get_repository() rev = req.args.get('rev', None) # Rendering all READMEs in a directory preview for entry in data['dir']['entries']: try: if not entry.isdir and entry.name.lower().startswith('readme'): node = repos.get_node(entry.path, rev) req.perm(data['context'].resource).require('FILE_VIEW') mimeview = Mimeview(self.env) content = node.get_content() mimetype = node.content_type divclass = 'searchable' if entry.name.lower().endswith('.wiki'): mimetype = 'text/x-trac-wiki' divclass = 'searchable wiki' elif entry.name.lower().endswith('.md'): mimetype = 'text/x-markdown' divclass = 'searchable markdown' if not mimetype or mimetype == 'application/octet-stream': mimetype = mimeview.get_mimetype( node.name, content.read(4096)) or \ mimetype or 'text/plain' del content self.log.debug( "ReadmeRenderer: rendering node %s@%s as %s", node.name, rev, mimetype) output = mimeview.preview_data( data['context'], node.get_content(), node.get_content_length(), mimetype, node.created_path, '', annotations=[], force_source=False) if output: if isinstance(output['rendered'], Stream): content = output['rendered'].select('.') else: content = output['rendered'] insert = tag.div( tag.h1(entry.name, tag.a(Markup(' ¶'), class_="anchor", href='#' + entry.name, title='Link to file'), id_=entry.name), tag.div(content, class_=divclass, title=entry.name), class_="readme", style="padding-top: 1em;" ) xpath = "//div[@id='content']/div[@id='help']" stream |= Transformer(xpath).before(insert) except Exception, e: self.log.debug(to_unicode(e))
def _render_file(self, req, context, repos, node, rev=None): req.perm(node.resource).require("FILE_VIEW") mimeview = Mimeview(self.env) # MIME type detection content = node.get_processed_content() chunk = content.read(CHUNK_SIZE) mime_type = node.content_type if not mime_type or mime_type == "application/octet-stream": mime_type = mimeview.get_mimetype(node.name, chunk) or mime_type or "text/plain" # Eventually send the file directly format = req.args.get("format") if format in ("raw", "txt"): req.send_response(200) req.send_header("Content-Type", "text/plain" if format == "txt" else mime_type) req.send_header("Last-Modified", http_date(node.last_modified)) if rev is None: req.send_header("Pragma", "no-cache") req.send_header("Cache-Control", "no-cache") req.send_header("Expires", "Fri, 01 Jan 1999 00:00:00 GMT") if not self.render_unsafe_content: # Force browser to download files instead of rendering # them, since they might contain malicious code enabling # XSS attacks req.send_header("Content-Disposition", "attachment") req.end_headers() # Note: don't pass an iterable instance to RequestDone, instead # call req.write() with each chunk here to avoid SEGVs (#11805) while chunk: req.write(chunk) chunk = content.read(CHUNK_SIZE) raise RequestDone else: # The changeset corresponding to the last change on `node` # is more interesting than the `rev` changeset. changeset = repos.get_changeset(node.created_rev) # add ''Plain Text'' alternate link if needed if not is_binary(chunk) and mime_type != "text/plain": plain_href = req.href.browser(repos.reponame or None, node.path, rev=rev, format="txt") add_link(req, "alternate", plain_href, _("Plain Text"), "text/plain") # add ''Original Format'' alternate link (always) raw_href = req.href.export(rev or repos.youngest_rev, repos.reponame or None, node.path) add_link(req, "alternate", raw_href, _("Original Format"), mime_type) self.log.debug("Rendering preview of node %s@%s with mime-type %s", node.name, rev, mime_type) content = None # the remainder of that content is not needed add_stylesheet(req, "common/css/code.css") annotations = ["lineno"] annotate = req.args.get("annotate") if annotate: annotations.insert(0, annotate) preview_data = mimeview.preview_data( context, node.get_processed_content(), node.get_content_length(), mime_type, node.created_path, raw_href, annotations=annotations, force_source=bool(annotate), ) return {"changeset": changeset, "size": node.content_length, "preview": preview_data, "annotate": annotate}
def filter_stream(self, req, method, template, stream, data): if not (template == 'browser.html' and data.get('dir')): if ((not data.get('dir')) and (data.get('path')) and (data.get('path').endswith('.md')) ): # Rendering single markdown file preview stream = stream | Transformer( "//head/script[not(@src)][1]" ).after( tag.script(Markup( "jQuery(document).ready(function($) {" " $('#preview').each(function() {" " $(this).html(marked( $(this).children('pre').first().text() ));" " });" "});"), type="text/javascript")) return stream add_stylesheet(req, 'common/css/code.css') repos = data.get('repos') or self.env.get_repository() rev = req.args.get('rev', None) for entry in data['dir'][ 'entries']: # Rendering all READMEs in a directory preview try: if not entry.isdir and entry.name.lower().startswith('readme'): node = repos.get_node(entry.path, rev) req.perm(data['context'].resource).require('FILE_VIEW') mimeview = Mimeview(self.env) content = node.get_content() mimetype = node.content_type divclass = 'searchable' if entry.name.lower().endswith('.wiki'): mimetype = 'text/x-trac-wiki' divclass = 'searchable wiki' elif entry.name.lower().endswith('.md'): mimetype = 'text/x-markdown' divclass = 'searchable markdown' if not mimetype or mimetype == 'application/octet-stream': mimetype = mimeview.get_mimetype( node.name, content.read(4096)) or mimetype or 'text/plain' del content self.log.debug( "ReadmeRenderer: rendering node %s@%s as %s" % (node.name, str(rev), mimetype)) output = mimeview.preview_data(data['context'], node.get_content(), node.get_content_length(), mimetype, node.created_path, '', annotations=[], force_source=False) if output: if isinstance(output['rendered'], Stream): #content = output['rendered'].select('./pre/node()') #content = output['rendered'].select('./pre') content = output['rendered'].select('.') else: self.log.debug("GOT THERE") content = output['rendered'] insert = tag.div( tag.h1(entry.name, tag.a(Markup(' ¶'), class_="anchor", href='#' + entry.name, title='Link to file'), id_=entry.name), tag.div( content, #xml:space = "preserve", class_=divclass, title=entry.name), class_="readme", style="padding-top: 1em;") stream = stream | Transformer( "//div[@id='content']/div[@id='help']").before( insert) except Exception, e: self.log.debug(to_unicode(e))