diff options
| -rw-r--r-- | pysite/views/wiki/source.py | 45 | ||||
| -rw-r--r-- | static/css/pygments-monokai.css | 73 | ||||
| -rw-r--r-- | templates/wiki/base.html | 24 | ||||
| -rw-r--r-- | templates/wiki/page_source.html | 13 | 
4 files changed, 149 insertions, 6 deletions
| diff --git a/pysite/views/wiki/source.py b/pysite/views/wiki/source.py new file mode 100644 index 00000000..7038dad9 --- /dev/null +++ b/pysite/views/wiki/source.py @@ -0,0 +1,45 @@ +# coding=utf-8 +from flask import redirect, url_for +from pygments import highlight +from pygments.formatters.html import HtmlFormatter +from pygments.lexers import get_lexer_by_name +from werkzeug.exceptions import NotFound + +from pysite.base_route import RouteView +from pysite.constants import DEBUG_MODE, EDITOR_ROLES +from pysite.mixins import DBMixin + + +class PageView(RouteView, DBMixin): +    path = "/source/<path:page>"  # "path" means that it accepts slashes +    name = "source" + +    table_name = "wiki" +    table_primary_key = "slug" + +    def get(self, page): +        obj = self.db.get(self.table_name, page) + +        if obj is None: +            if self.is_staff(): +                return redirect(url_for("wiki.edit", page=page, can_edit=False)) + +            raise NotFound() + +        rst = obj["rst"] +        rst = highlight(rst, get_lexer_by_name("rst"), HtmlFormatter(preclass="code", linenos="inline")) +        return self.render("wiki/page_source.html", page=page, data=obj, rst=rst, can_edit=self.is_staff()) + +    def is_staff(self): +        if DEBUG_MODE: +            return True +        if not self.logged_in: +            return False + +        roles = self.user_data.get("roles", []) + +        for role in roles: +            if role in EDITOR_ROLES: +                return True + +        return False diff --git a/static/css/pygments-monokai.css b/static/css/pygments-monokai.css index 250c8d44..756a5850 100644 --- a/static/css/pygments-monokai.css +++ b/static/css/pygments-monokai.css @@ -68,3 +68,76 @@  .code .vi { color: #f8f8f2 } /* Name.Variable.Instance */  .code .vm { color: #f8f8f2 } /* Name.Variable.Magic */  .code .il { color: #ae81ff } /* Literal.Number.Integer.Long */ + +/* Extra CSS for non-docutils output */ + +.highlight pre .hll { background-color: #49483e } +.highlight pre  { background: #272822; color: #f8f8f2 } +.highlight pre .c { color: #75715e } /* Comment */ +.highlight pre .err { color: #960050; background-color: #1e0010 } /* Error */ +.highlight pre .k { color: #66d9ef } /* Keyword */ +.highlight pre .l { color: #ae81ff } /* Literal */ +.highlight pre .n { color: #f8f8f2 } /* Name */ +.highlight pre .o { color: #f92672 } /* Operator */ +.highlight pre .p { color: #f8f8f2 } /* Punctuation */ +.highlight pre .ch { color: #75715e } /* Comment.Hashbang */ +.highlight pre .cm { color: #75715e } /* Comment.Multiline */ +.highlight pre .cp { color: #75715e } /* Comment.Preproc */ +.highlight pre .cpf { color: #75715e } /* Comment.PreprocFile */ +.highlight pre .c1 { color: #75715e } /* Comment.Single */ +.highlight pre .cs { color: #75715e } /* Comment.Special */ +.highlight pre .gd { color: #f92672 } /* Generic.Deleted */ +.highlight pre .ge { font-style: italic } /* Generic.Emph */ +.highlight pre .gi { color: #a6e22e } /* Generic.Inserted */ +.highlight pre .gs { font-weight: bold } /* Generic.Strong */ +.highlight pre .gu { color: #75715e } /* Generic.Subheading */ +.highlight pre .kc { color: #66d9ef } /* Keyword.Constant */ +.highlight pre .kd { color: #66d9ef } /* Keyword.Declaration */ +.highlight pre .kn { color: #f92672 } /* Keyword.Namespace */ +.highlight pre .kp { color: #66d9ef } /* Keyword.Pseudo */ +.highlight pre .kr { color: #66d9ef } /* Keyword.Reserved */ +.highlight pre .kt { color: #66d9ef } /* Keyword.Type */ +.highlight pre .ld { color: #e6db74 } /* Literal.Date */ +.highlight pre .m { color: #ae81ff } /* Literal.Number */ +.highlight pre .s { color: #e6db74 } /* Literal.String */ +.highlight pre .na { color: #a6e22e } /* Name.Attribute */ +.highlight pre .nb { color: #f8f8f2 } /* Name.Builtin */ +.highlight pre .nc { color: #a6e22e } /* Name.Class */ +.highlight pre .no { color: #66d9ef } /* Name.Constant */ +.highlight pre .nd { color: #a6e22e } /* Name.Decorator */ +.highlight pre .ni { color: #f8f8f2 } /* Name.Entity */ +.highlight pre .ne { color: #a6e22e } /* Name.Exception */ +.highlight pre .nf { color: #a6e22e } /* Name.Function */ +.highlight pre .nl { color: #f8f8f2 } /* Name.Label */ +.highlight pre .nn { color: #f8f8f2 } /* Name.Namespace */ +.highlight pre .nx { color: #a6e22e } /* Name.Other */ +.highlight pre .py { color: #f8f8f2 } /* Name.Property */ +.highlight pre .nt { color: #f92672 } /* Name.Tag */ +.highlight pre .nv { color: #f8f8f2 } /* Name.Variable */ +.highlight pre .ow { color: #f92672 } /* Operator.Word */ +.highlight pre .w { color: #f8f8f2 } /* Text.Whitespace */ +.highlight pre .mb { color: #ae81ff } /* Literal.Number.Bin */ +.highlight pre .mf { color: #ae81ff } /* Literal.Number.Float */ +.highlight pre .mh { color: #ae81ff } /* Literal.Number.Hex */ +.highlight pre .mi { color: #ae81ff } /* Literal.Number.Integer */ +.highlight pre .mo { color: #ae81ff } /* Literal.Number.Oct */ +.highlight pre .sa { color: #e6db74 } /* Literal.String.Affix */ +.highlight pre .sb { color: #e6db74 } /* Literal.String.Backtick */ +.highlight pre .sc { color: #e6db74 } /* Literal.String.Char */ +.highlight pre .dl { color: #e6db74 } /* Literal.String.Delimiter */ +.highlight pre .sd { color: #e6db74 } /* Literal.String.Doc */ +.highlight pre .s2 { color: #e6db74 } /* Literal.String.Double */ +.highlight pre .se { color: #ae81ff } /* Literal.String.Escape */ +.highlight pre .sh { color: #e6db74 } /* Literal.String.Heredoc */ +.highlight pre .si { color: #e6db74 } /* Literal.String.Interpol */ +.highlight pre .sx { color: #e6db74 } /* Literal.String.Other */ +.highlight pre .sr { color: #e6db74 } /* Literal.String.Regex */ +.highlight pre .s1 { color: #e6db74 } /* Literal.String.Single */ +.highlight pre .ss { color: #e6db74 } /* Literal.String.Symbol */ +.highlight pre .bp { color: #f8f8f2 } /* Name.Builtin.Pseudo */ +.highlight pre .fm { color: #a6e22e } /* Name.Function.Magic */ +.highlight pre .vc { color: #f8f8f2 } /* Name.Variable.Class */ +.highlight pre .vg { color: #f8f8f2 } /* Name.Variable.Global */ +.highlight pre .vi { color: #f8f8f2 } /* Name.Variable.Instance */ +.highlight pre .vm { color: #f8f8f2 } /* Name.Variable.Magic */ +.highlight pre .il { color: #ae81ff } /* Literal.Number.Integer.Long */ diff --git a/templates/wiki/base.html b/templates/wiki/base.html index 4bb227df..9464782b 100644 --- a/templates/wiki/base.html +++ b/templates/wiki/base.html @@ -33,21 +33,33 @@              <div class="uk-flex uk-flex-row uk-flex-1">                  <div class="uk-card uk-card-body uk-flex-left uk-flex uk-card-primary">                      <ul class="uk-nav-default uk-nav-parent-icon" uk-nav> -                        {% if (can_edit or debug) and current_page == "page" %} +                        {% if (can_edit or debug) and current_page != "edit" %}                              <li>                                  <a href="{{ url_for("wiki.edit", page=page) }}"> -                                    <i class="fas fa-pencil-alt"></i>  Edit +                                    <i class="fas fa-fw fa-pencil-alt"></i>  Edit                                  </a>                              </li> -                            <li class="uk-nav-divider"></li> -                        {% elif current_page == "edit" %} +                        {% else %}                              <li>                                  <a href="{{ url_for("wiki.page", page=page) }}"> -                                    <i class="fas fa-arrow-left fa-fw"></i>  Back +                                    <i class="fas fa-fw fa-arrow-left"></i>  Back                                  </a>                              </li> -                            <li class="uk-nav-divider"></li>                          {% endif %} +                        {% if current_page != "source" %} +                            <li> +                                <a href="{{ url_for("wiki.source", page=page) }}"> +                                    <i class="fas fa-fw fa-code"></i>  Source +                                </a> +                            </li> +                        {% else %} +                            <li> +                                <a href="{{ url_for("wiki.page", page=page) }}"> +                                    <i class="fas fa-fw fa-arrow-left"></i>  Back +                                </a> +                            </li> +                        {% endif %} +                        <li class="uk-nav-divider"></li>                          <li class="uk-active"><a href="#">Placeholder</a></li>                          <li class="uk-parent">                              <a href="#">Parent</a> diff --git a/templates/wiki/page_source.html b/templates/wiki/page_source.html new file mode 100644 index 00000000..15384aa9 --- /dev/null +++ b/templates/wiki/page_source.html @@ -0,0 +1,13 @@ +{% extends "wiki/base.html" %} +{% block title %}Wiki | {{ data["title"] }}{% endblock %} +{% block og_title %}Wiki | {{ data["title"] }}{% endblock %} +{% block og_description %}{% endblock %} +{% block content %} +    <div class="uk-container uk-container-small"> +        <h2 class="uk-title"> +            {{ data["title"] }} +        </h2> + +        {{ rst | safe }} +    </div> +{% endblock %}
\ No newline at end of file | 
