def src(self, request): if not self.project: raise api_errors.NotFoundError("project") ''' e.g. /api/testuser/testproj/src?path=test.md&ref=5c712b6712bec9c4ed1531b61fd7a4cbcbf3fe90 ''' # noqa path = request.get_form_var('path') ref = request.get_form_var('ref') or 'HEAD' t, src = '', '' _ref = ':'.join((ref, path or '')) try: # file_content = src.data src = self.project.repo.get_path_by_ref(_ref) if not src: raise ValueError t = src.type if t == 'blob': if src.binary: if path.endswith('.pdf'): src = '<a class="media" href="%s"></a>' % ( '/' + self.project.name + '/raw/' + ref + '/' + path) else: src = '<div class="rawfile">The content of %s appear to be raw binary, please use raw view instead</div>' % path # noqa elif path.endswith(('md', 'mkd', 'markdown')): src = '<div class="markdown-body">{}</div>'.format( format_md_or_rst(path, src.data, self.project.name)) else: src = highlight_code(path, src.data, div=True) elif t == 'tree': src = [dict(e) for e in src] except (KeyError, ValueError): t = 'blob' src = '<div class="error"><i class="icon-exclamation-sign"></i> File not found.</div>' # noqa data = {'path': path, 'type': t, 'src': src} return json.dumps(data)
def snippet(self): # TODO: fragment raw_content_lines = list(enumerate(self.content.split('\n'))) hl_content_lines = list(enumerate(self.hl_content.split('\n'))) fragment_lines = set(raw_content_lines) - set(hl_content_lines) line_nums = sorted([i for i, c in fragment_lines]) max_lines = 10 begin = line_nums[0] if line_nums else 0 end = line_nums[-1] if line_nums else max_lines delta = min(max_lines, end - begin + 1) snippets = [c for i, c in raw_content_lines[begin:begin + delta]] hl_name = self.name for line in snippets: if len(line) > 512: # don't highlight long line hl_name = 'xxx' highlighted = highlight_code( hl_name, '\n'.join(snippets), linenostart=begin + 1).decode('utf8') phrases = re.findall( ur'<em>(\w+)</em>', self.hl_content.decode('utf8'), flags=re.UNICODE) pattern = ur'|'.join(set(phrases)) if pattern: pattern = ur'(%s)(?![^<>]*>)' % pattern highlighted = re.sub( pattern, ur'<em>\g<0></em>', highlighted, flags=re.UNICODE).encode('utf8') else: logging.debug('Fail to highlight keywords in search result. \nraw_content: \n%s\n\nhl_content: \n%s', # noqa self.content, self.hl_content) return highlighted
def _blame_src_highlighted_lines(self, ref, path): HIGHLIGHT_PATN = re.compile( r'<a name="L-(\d+)"></a>(.*?)(?=<a name="L-(?:\d+)">)', re.DOTALL) source_code = self.cat('%s:%s' % (ref, path)) # TODO try to avoid having highlighted content here hl_source_code = highlight_code(path, source_code) hl_lines = dict(re.findall(HIGHLIGHT_PATN, hl_source_code)) return hl_lines
def __call__(self, request): resp = request.response resp.set_header("Content-Type", "text/javascript") resp.set_header('Expires', 'Sun, 1 Jan 2006 01:00:00 GMT') resp.set_header('Pragma', 'no-cache') resp.set_header('Cache-Control', 'must-revalidate, no-cache, private') if not self.gist_id.isdigit() or not Gist.get(self.gist_id): return "document.write('<span style=\"color:red;\">NOT EXIST GIST</span>')" # noqa gist = Gist.get(self.gist_id) html = EMBED_CSS + EMBED_HEAD % gist.id for path in gist.files: path = path.encode('utf8') # TODO: clean this src = gist.get_file(path, rev='HEAD') src = highlight_code(path, src) src = src.replace('"', '\"').replace("'", "\'") html += SRC_FORMAT % (src, DOMAIN, gist.id, path, DOMAIN, gist.id, path, path, gist.url, DOMAIN) html += EMBED_FOOTER html = html.replace('\n', '\\n') return "document.write('%s')" % html
def snippet(self): # TODO: fragment raw_content_lines = list(enumerate(self.content.split('\n'))) hl_content_lines = list(enumerate(self.hl_content.split('\n'))) fragment_lines = set(raw_content_lines) - set(hl_content_lines) line_nums = sorted([i for i, c in fragment_lines]) max_lines = 10 begin = line_nums[0] if line_nums else 0 end = line_nums[-1] if line_nums else max_lines delta = min(max_lines, end - begin + 1) snippets = [c for i, c in raw_content_lines[begin:begin + delta]] hl_name = self.name for line in snippets: if len(line) > 512: # don't highlight long line hl_name = 'xxx' highlighted = highlight_code(hl_name, '\n'.join(snippets), linenostart=begin + 1).decode('utf8') phrases = re.findall(ur'<em>(\w+)</em>', self.hl_content.decode('utf8'), flags=re.UNICODE) pattern = ur'|'.join(set(phrases)) if pattern: pattern = ur'(%s)(?![^<>]*>)' % pattern highlighted = re.sub(pattern, ur'<em>\g<0></em>', highlighted, flags=re.UNICODE).encode('utf8') else: logging.debug( 'Fail to highlight keywords in search result. \nraw_content: \n%s\n\nhl_content: \n%s', # noqa self.content, self.hl_content) return highlighted
def _render_src(self, path, src): if path.endswith(("md", "mkd", "markdown")): return '<div class="markdown-body">%s</div>' % format_md_or_rst(path, src) return highlight_code(path, src)
def _render_src(self, path, src): if path.endswith(('md', 'mkd', 'markdown')): return '<div class="markdown-body">%s</div>' % format_md_or_rst( path, src) return highlight_code(path, src)