diff --git a/.gitignore b/.gitignore index 95036ea..0030193 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,6 @@ repos/ forks/ -wikis/ +docs/ dist/ build/ *.pyc diff --git a/progit/__init__.py b/progit/__init__.py index 9741496..931aad7 100644 --- a/progit/__init__.py +++ b/progit/__init__.py @@ -192,7 +192,7 @@ def __get_file_in_tree(repo_obj, tree, filepath): ## Import the application import progit.app +import progit.docs import progit.fork import progit.issues import progit.repo -import progit.wiki diff --git a/progit/app.py b/progit/app.py index fc86bb4..0cf6e6d 100644 --- a/progit/app.py +++ b/progit/app.py @@ -180,7 +180,7 @@ def new_project(): description=description, user=flask.g.fas_user.username, gitfolder=APP.config['GIT_FOLDER'], - docfolder=APP.config['WIKI_FOLDER'], + docfolder=APP.config['DOCS_FOLDER'], ) SESSION.commit() flask.flash(message) diff --git a/progit/default_config.py b/progit/default_config.py index dd29316..81ea836 100644 --- a/progit/default_config.py +++ b/progit/default_config.py @@ -42,9 +42,9 @@ FORK_FOLDER = os.path.join( 'forks' ) -# Folder containing the wiki repos -WIKI_FOLDER = os.path.join( +# Folder containing the docs repos +DOCS_FOLDER = os.path.join( os.path.abspath(os.path.dirname(__file__)), '..', - 'wikis' + 'docs' ) diff --git a/progit/docs.py b/progit/docs.py new file mode 100644 index 0000000..caf1d1c --- /dev/null +++ b/progit/docs.py @@ -0,0 +1,141 @@ +#-*- coding: utf-8 -*- + +""" + (c) 2014 - Copyright Red Hat Inc + + Authors: + Pierre-Yves Chibon + +""" + +import flask +import os +from math import ceil + +import pygit2 +from sqlalchemy.exc import SQLAlchemyError +from pygments import highlight +from pygments.lexers import guess_lexer +from pygments.lexers.text import DiffLexer +from pygments.formatters import HtmlFormatter + + +import progit.doc_utils +import progit.lib +import progit.forms +from progit import APP, SESSION, LOG, cla_required + + +def __get_tree(repo_obj, tree, filepath, startswith=False): + ''' Retrieve the entry corresponding to the provided filename in a + given tree. + ''' + filename = filepath[0] + if isinstance(tree, pygit2.Blob): + return (tree, None) + cnt = 0 + for el in tree: + cnt += 1 + ok = False + if cnt == len(tree) and el.name.startswith(filename): + ok = True + if el.name == filename: + ok = True + if ok: + if len(filepath) == 1: + return (el, tree) + else: + return __get_tree( + repo_obj, repo_obj[el.oid], filepath[1:]) + + +def __get_tree_and_content(repo_obj, commit, path, startswith): + ''' Return the tree and the content of the specified file. ''' + + (blob_or_tree, tree_obj) = __get_tree( + repo_obj, commit.tree, path, startswith=startswith) + + if not repo_obj[blob_or_tree.oid]: + flask.abort(404, 'File not found') + + blob_or_tree_obj = repo_obj[blob_or_tree.oid] + blob = repo_obj[blob_or_tree.oid] + if isinstance(blob, pygit2.Blob): # Returned a file + name, ext = os.path.splitext(blob_or_tree.name) + content = progit.doc_utils.convert_readme(blob_or_tree_obj.data, ext) + else: # Returned a tree + path.append('index') + return __get_tree_and_content(repo_obj, commit, path, startswith) + + tree = sorted(tree_obj, key=lambda x: x.filemode) + return (tree, content) + + +## URLs + + +@APP.route('//docs') +@APP.route('//docs/') +@APP.route('//docs/') +@APP.route('//docs//') +@APP.route('/fork///docs') +@APP.route('/fork///docs/') +@APP.route('/fork///docs/') +@APP.route('/fork///docs//') +def view_docs(repo, username=None, branchname=None, filename=None): + """ Display the documentation + """ + status = flask.request.args.get('status', None) + + repo = progit.lib.get_project(SESSION, repo, user=username) + + if not repo: + flask.abort(404, 'Project not found') + + if not repo.project_docs: + flask.abort(404, 'No documentation found for this project') + + reponame = os.path.join(APP.config['DOCS_FOLDER'], repo.path) + if not os.path.exists(reponame): + flask.flash( + 'No docs repository could be found, please contact an admin', + 'error') + return flask.redirect(flask.url_for( + 'view_repo', repo=repo.name, username=username)) + + repo_obj = pygit2.Repository(reponame) + + if branchname in repo_obj.listall_branches(): + branch = repo_obj.lookup_branch(branchname) + commit = branch.get_object() + else: + if not repo_obj.is_empty: + commit = repo_obj[repo_obj.head.target] + else: + commit = None + branchname = 'master' + + content = None + tree = None + startswith = False + if not filename: + path = ['index'] + startswith = True + else: + path = filename.split('/') + + if commit: + (tree, content) = __get_tree_and_content( + repo_obj, commit, path, startswith) + + return flask.render_template( + 'docs.html', + select='docs', + repo_obj=repo_obj, + repo=repo, + username=username, + branchname=branchname, + filename=filename, + tree=tree, + content=content, + ) diff --git a/progit/fork.py b/progit/fork.py index 1498df8..890365b 100644 --- a/progit/fork.py +++ b/progit/fork.py @@ -230,7 +230,7 @@ def fork_project(repo, username=None): repo=repo, repofolder=APP.config['GIT_FOLDER'], forkfolder=APP.config['FORK_FOLDER'], - docfolder=APP.config['WIKI_FOLDER'], + docfolder=APP.config['DOCS_FOLDER'], user=flask.g.fas_user.username) SESSION.commit() diff --git a/progit/lib.py b/progit/lib.py index 669849c..a8a5d9f 100644 --- a/progit/lib.py +++ b/progit/lib.py @@ -95,7 +95,7 @@ def new_project(session, user, name, gitfolder, docfolder, gitrepo = os.path.join(docfolder, project.path) if os.path.exists(gitrepo): raise progit.exceptions.RepoExistsException( - 'The wiki "%s" already exists' % project.path + 'The docs "%s" already exists' % project.path ) pygit2.init_repository(gitrepo, bare=True) @@ -181,7 +181,7 @@ def fork_project(session, user, repo, gitfolder, forkfolder, docfolder): gitrepo = os.path.join(docfolder, project.path) if os.path.exists(gitrepo): raise progit.exceptions.RepoExistsException( - 'The wiki "%s" already exists' % project.path + 'The docs "%s" already exists' % project.path ) pygit2.init_repository(gitrepo, bare=True) diff --git a/progit/model.py b/progit/model.py index 3957b1b..20f9813 100644 --- a/progit/model.py +++ b/progit/model.py @@ -114,7 +114,7 @@ class Project(BASE): sa.ForeignKey('projects.id', onupdate='CASCADE'), nullable=True) issue_tracker = sa.Column(sa.Boolean, nullable=False, default=True) - project_wiki = sa.Column(sa.Boolean, nullable=False, default=True) + project_focs = sa.Column(sa.Boolean, nullable=False, default=True) date_created = sa.Column(sa.DateTime, nullable=False, default=datetime.datetime.utcnow) diff --git a/progit/templates/docs.html b/progit/templates/docs.html new file mode 100644 index 0000000..6a935ca --- /dev/null +++ b/progit/templates/docs.html @@ -0,0 +1,83 @@ +{% extends "repo_master.html" %} + +{% block title %}Docs - {{ repo.name }}{% endblock %} +{%block tag %}home{% endblock %} + + +{% block repo %} + +

Docs

+ +{% if repo_obj and repo_obj.is_empty %} + +
+

This repo is brand new!

+ +

If you already have a git repo:

+
+      git remote add origin https://githosted.org/{{ repo.name }}.git
+      git push -u origin master
+    
+ +

If you have no create your git repo yet:

+
+      touch README.rst
+      git init
+      git add README.rst
+      git commit -m "Add README file"
+      git remote add origin https://githosted.org/{{ repo.name }}.git
+      git push -u origin master
+    
+ +
+ +{% else %} +

+ + {{ branchname }}/{% if filename %}{% + for file in filename.split('/') %} + {% if loop.first %} + {% set path = file %} + {% else %} + {% set path = path + '/' + file %} + {% endif %} + {% if loop.index != loop.length %}{{ file }}/{% else %}{{ file }}{% endif %} + {% endfor %}{% endif %} +

+ + {% if tree %} +
+ +
+ {% endif %} + + {% if content %} +
+ {% autoescape false %} + {{ content }} + {% endautoescape %} +
+ {% endif %} +{% endif %} + +{% endblock %} diff --git a/progit/templates/repo_master.html b/progit/templates/repo_master.html index 62b94b8..e01ae22 100644 --- a/progit/templates/repo_master.html +++ b/progit/templates/repo_master.html @@ -27,9 +27,9 @@ repo=repo.name) }}">Overview - {% if repo.project_wiki %} + {% if repo.project_docs %}
  • - Docs
  • {% endif %} diff --git a/progit/templates/wiki.html b/progit/templates/wiki.html deleted file mode 100644 index ec63a26..0000000 --- a/progit/templates/wiki.html +++ /dev/null @@ -1,83 +0,0 @@ -{% extends "repo_master.html" %} - -{% block title %}Docs - {{ repo.name }}{% endblock %} -{%block tag %}home{% endblock %} - - -{% block repo %} - -

    Docs

    - -{% if repo_obj and repo_obj.is_empty %} - -
    -

    This repo is brand new!

    - -

    If you already have a git repo:

    -
    -      git remote add origin https://githosted.org/{{ repo.name }}.git
    -      git push -u origin master
    -    
    - -

    If you have no create your git repo yet:

    -
    -      touch README.rst
    -      git init
    -      git add README.rst
    -      git commit -m "Add README file"
    -      git remote add origin https://githosted.org/{{ repo.name }}.git
    -      git push -u origin master
    -    
    - -
    - -{% else %} -

    - - {{ branchname }}/{% if filename %}{% - for file in filename.split('/') %} - {% if loop.first %} - {% set path = file %} - {% else %} - {% set path = path + '/' + file %} - {% endif %} - {% if loop.index != loop.length %}{{ file }}/{% else %}{{ file }}{% endif %} - {% endfor %}{% endif %} -

    - - {% if tree %} -
    - -
    - {% endif %} - - {% if content %} -
    - {% autoescape false %} - {{ content }} - {% endautoescape %} -
    - {% endif %} -{% endif %} - -{% endblock %} diff --git a/progit/wiki.py b/progit/wiki.py deleted file mode 100644 index 1cfa9ff..0000000 --- a/progit/wiki.py +++ /dev/null @@ -1,141 +0,0 @@ -#-*- coding: utf-8 -*- - -""" - (c) 2014 - Copyright Red Hat Inc - - Authors: - Pierre-Yves Chibon - -""" - -import flask -import os -from math import ceil - -import pygit2 -from sqlalchemy.exc import SQLAlchemyError -from pygments import highlight -from pygments.lexers import guess_lexer -from pygments.lexers.text import DiffLexer -from pygments.formatters import HtmlFormatter - - -import progit.doc_utils -import progit.lib -import progit.forms -from progit import APP, SESSION, LOG, cla_required - - -def __get_tree(repo_obj, tree, filepath, startswith=False): - ''' Retrieve the entry corresponding to the provided filename in a - given tree. - ''' - filename = filepath[0] - if isinstance(tree, pygit2.Blob): - return (tree, None) - cnt = 0 - for el in tree: - cnt += 1 - ok = False - if cnt == len(tree) and el.name.startswith(filename): - ok = True - if el.name == filename: - ok = True - if ok: - if len(filepath) == 1: - return (el, tree) - else: - return __get_tree( - repo_obj, repo_obj[el.oid], filepath[1:]) - - -def __get_tree_and_content(repo_obj, commit, path, startswith): - ''' Return the tree and the content of the specified file. ''' - - (blob_or_tree, tree_obj) = __get_tree( - repo_obj, commit.tree, path, startswith=startswith) - - if not repo_obj[blob_or_tree.oid]: - flask.abort(404, 'File not found') - - blob_or_tree_obj = repo_obj[blob_or_tree.oid] - blob = repo_obj[blob_or_tree.oid] - if isinstance(blob, pygit2.Blob): # Returned a file - name, ext = os.path.splitext(blob_or_tree.name) - content = progit.doc_utils.convert_readme(blob_or_tree_obj.data, ext) - else: # Returned a tree - path.append('index') - return __get_tree_and_content(repo_obj, commit, path, startswith) - - tree = sorted(tree_obj, key=lambda x: x.filemode) - return (tree, content) - - -## URLs - - -@APP.route('//docs') -@APP.route('//docs/') -@APP.route('//docs/') -@APP.route('//docs//') -@APP.route('/fork///docs') -@APP.route('/fork///docs/') -@APP.route('/fork///docs/') -@APP.route('/fork///docs//') -def view_wiki(repo, username=None, branchname=None, filename=None): - """ Display the documentation - """ - status = flask.request.args.get('status', None) - - repo = progit.lib.get_project(SESSION, repo, user=username) - - if not repo: - flask.abort(404, 'Project not found') - - if not repo.project_wiki: - flask.abort(404, 'No documentation found for this project') - - reponame = os.path.join(APP.config['WIKI_FOLDER'], repo.path) - if not os.path.exists(reponame): - flask.flash( - 'No wiki repository could be found, please contact an admin', - 'error') - return flask.redirect(flask.url_for( - 'view_repo', repo=repo.name, username=username)) - - repo_obj = pygit2.Repository(reponame) - - if branchname in repo_obj.listall_branches(): - branch = repo_obj.lookup_branch(branchname) - commit = branch.get_object() - else: - if not repo_obj.is_empty: - commit = repo_obj[repo_obj.head.target] - else: - commit = None - branchname = 'master' - - content = None - tree = None - startswith = False - if not filename: - path = ['index'] - startswith = True - else: - path = filename.split('/') - - if commit: - (tree, content) = __get_tree_and_content( - repo_obj, commit, path, startswith) - - return flask.render_template( - 'wiki.html', - select='docs', - repo_obj=repo_obj, - repo=repo, - username=username, - branchname=branchname, - filename=filename, - tree=tree, - content=content, - )