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 _get_tmpl_tree(tmpl_target, ref, path, project_name, request): project = CodeDoubanProject.get_by_name(project_name) if not project: raise TraversalError("Wrong path for tree %s" % path) if project.is_mirror_project: mirror = CodeDoubanMirror.get_by_project_id(project.id) if not mirror.is_clone_completed: return st("/projects/mirror_cloning.html", **locals()) if not project.repo: raise TraversalError("Wrong path for tree %s" % path) if not ref: ref = project.default_branch tree_path = path.decode("utf-8") last_commit = project.repo.get_last_commit(ref, path=path) if ref else "" tdt = _tmpl_common_data(ref, path, project_name, request) user = tdt["user"] if user is not None: username = user.username cur_user_project = project.get_forked_project(username) if cur_user_project is not None: latest_branch = _latest_update_branch(cur_user_project, ref, user) if latest_branch: cur_user_latest_branch_name = "{0}:{1}".format(username, latest_branch) tdt["latest_update_branch"].append((cur_user_project, cur_user_latest_branch_name, latest_branch)) tdt.update({"lastcommit": last_commit}) tree = [] is_empty = True if last_commit: tree = project.repo.get_tree(ref, path) is_empty = False if tree else True if is_empty and not project.repo.is_empty: raise TraversalError("Wrong path for tree %s" % path) if isinstance(tree, basestring): raise TraversalError("Got a blob instead of a tree") # Add README code to tree if any for item in tree: if item["type"] == "blob" and (item["name"] == "README" or item["name"].startswith("README.")): readme_content = project.repo.get_file_by_ref("%s:%s" % (ref, item["path"])) tdt.update({"readme_content": format_md_or_rst(item["path"], readme_content, project.name)}) break tdt.update({"tree": tree, "tree_path": tree_path, "is_empty": is_empty}) return st(tmpl_target, **tdt)
def readme(self): # TODO: remove tree loop ref = self.default_branch repo = self.repo try: tree = repo.get_tree(ref) except JagareError as e: logging.warning("JagareError: %r" % e) return '' for item in tree: if (item['type'] == 'blob' and (item['name'] == 'README' or item['name'].startswith('README.'))): readme_content = repo.get_file_by_ref("%s:%s" % ( ref, item['path'])) return format_md_or_rst(item['path'], readme_content) return ''
def _get_tmpl_tree(tmpl_target, ref, path, project_name, request): project = CodeDoubanProject.get_by_name(project_name) if not project: raise TraversalError("Wrong path for tree %s" % path) if project.is_mirror_project: mirror = CodeDoubanMirror.get_by_project_id(project.id) if not mirror.is_clone_completed: return st('/projects/mirror_cloning.html', **locals()) if not project.repo: raise TraversalError("Wrong path for tree %s" % path) if not ref: ref = project.default_branch tree_path = path.decode('utf-8') last_commit = project.repo.get_last_commit(ref, path=path) if ref else '' tdt = _tmpl_common_data(ref, path, project_name, request) user = tdt['user'] if user is not None: username = user.username cur_user_project = project.get_forked_project(username) if cur_user_project is not None: latest_branch = _latest_update_branch( cur_user_project, ref, user) if latest_branch: cur_user_latest_branch_name = '{0}:{1}'.format( username, latest_branch) tdt['latest_update_branch'].append( (cur_user_project, cur_user_latest_branch_name, latest_branch)) tdt.update({ 'lastcommit': last_commit, }) tree = [] is_empty = True if last_commit: tree = project.repo.get_tree(ref, path) is_empty = False if tree else True if is_empty and not project.repo.is_empty: raise TraversalError("Wrong path for tree %s" % path) if isinstance(tree, basestring): raise TraversalError("Got a blob instead of a tree") # Add README code to tree if any for item in tree: if (item['type'] == 'blob' and (item['name'] == 'README' or item['name'].startswith('README.'))): readme_content = project.repo.get_file_by_ref( "%s:%s" % (ref, item['path']) ) tdt.update({ 'readme_content': format_md_or_rst(item['path'], readme_content, project.name), }) break tdt.update({ 'tree': tree, 'tree_path': tree_path, 'is_empty': is_empty, }) return st(tmpl_target, **tdt)
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)