UI: Add syntax highlighting to files, results, hosts and records

This implementation is mostly carried over from ara 0.x using
pygments.

Change-Id: I964a0410d088b53a28f55271b6610d8420ee8f8c
This commit is contained in:
David Moreau Simard 2019-07-24 16:04:02 -04:00
parent de360a0d41
commit 2072e86f08
No known key found for this signature in database
GPG Key ID: CBEB466764A9E621
8 changed files with 138 additions and 13 deletions

View File

@ -0,0 +1,63 @@
/* https://github.com/richleland/pygments-css */
.codehilite { padding-left: 10px; }
.codehilite .hll { background-color: #ffffcc }
.codehilite .c { color: #999988; font-style: italic } /* Comment */
.codehilite .err { color: #a61717; background-color: #e3d2d2 } /* Error */
.codehilite .k { color: #000000; font-weight: bold } /* Keyword */
.codehilite .o { color: #000000; font-weight: bold } /* Operator */
.codehilite .cm { color: #999988; font-style: italic } /* Comment.Multiline */
.codehilite .cp { color: #999999; font-weight: bold; font-style: italic } /* Comment.Preproc */
.codehilite .c1 { color: #999988; font-style: italic } /* Comment.Single */
.codehilite .cs { color: #999999; font-weight: bold; font-style: italic } /* Comment.Special */
.codehilite .gd { color: #000000; background-color: #ffdddd } /* Generic.Deleted */
.codehilite .ge { color: #000000; font-style: italic } /* Generic.Emph */
.codehilite .gr { color: #aa0000 } /* Generic.Error */
.codehilite .gh { color: #999999 } /* Generic.Heading */
.codehilite .gi { color: #000000; background-color: #ddffdd } /* Generic.Inserted */
.codehilite .go { color: #888888 } /* Generic.Output */
.codehilite .gp { color: #555555 } /* Generic.Prompt */
.codehilite .gs { font-weight: bold } /* Generic.Strong */
.codehilite .gu { color: #aaaaaa } /* Generic.Subheading */
.codehilite .gt { color: #aa0000 } /* Generic.Traceback */
.codehilite .kc { color: #000000; font-weight: bold } /* Keyword.Constant */
.codehilite .kd { color: #000000; font-weight: bold } /* Keyword.Declaration */
.codehilite .kn { color: #000000; font-weight: bold } /* Keyword.Namespace */
.codehilite .kp { color: #000000; font-weight: bold } /* Keyword.Pseudo */
.codehilite .kr { color: #000000; font-weight: bold } /* Keyword.Reserved */
.codehilite .kt { color: #445588; font-weight: bold } /* Keyword.Type */
.codehilite .m { color: #009999 } /* Literal.Number */
.codehilite .s { color: #d01040 } /* Literal.String */
.codehilite .na { color: #008080 } /* Name.Attribute */
.codehilite .nb { color: #0086B3 } /* Name.Builtin */
.codehilite .nc { color: #445588; font-weight: bold } /* Name.Class */
.codehilite .no { color: #008080 } /* Name.Constant */
.codehilite .nd { color: #3c5d5d; font-weight: bold } /* Name.Decorator */
.codehilite .ni { color: #800080 } /* Name.Entity */
.codehilite .ne { color: #990000; font-weight: bold } /* Name.Exception */
.codehilite .nf { color: #990000; font-weight: bold } /* Name.Function */
.codehilite .nl { color: #990000; font-weight: bold } /* Name.Label */
.codehilite .nn { color: #555555 } /* Name.Namespace */
.codehilite .nt { color: #000080 } /* Name.Tag */
.codehilite .nv { color: #008080 } /* Name.Variable */
.codehilite .ow { color: #000000; font-weight: bold } /* Operator.Word */
.codehilite .w { color: #bbbbbb } /* Text.Whitespace */
.codehilite .mf { color: #009999 } /* Literal.Number.Float */
.codehilite .mh { color: #009999 } /* Literal.Number.Hex */
.codehilite .mi { color: #009999 } /* Literal.Number.Integer */
.codehilite .mo { color: #009999 } /* Literal.Number.Oct */
.codehilite .sb { color: #d01040 } /* Literal.String.Backtick */
.codehilite .sc { color: #d01040 } /* Literal.String.Char */
.codehilite .sd { color: #d01040 } /* Literal.String.Doc */
.codehilite .s2 { color: #d01040 } /* Literal.String.Double */
.codehilite .se { color: #d01040 } /* Literal.String.Escape */
.codehilite .sh { color: #d01040 } /* Literal.String.Heredoc */
.codehilite .si { color: #d01040 } /* Literal.String.Interpol */
.codehilite .sx { color: #d01040 } /* Literal.String.Other */
.codehilite .sr { color: #009926 } /* Literal.String.Regex */
.codehilite .s1 { color: #d01040 } /* Literal.String.Single */
.codehilite .ss { color: #990073 } /* Literal.String.Symbol */
.codehilite .bp { color: #999999 } /* Name.Builtin.Pseudo */
.codehilite .vc { color: #008080 } /* Name.Variable.Class */
.codehilite .vg { color: #008080 } /* Name.Variable.Global */
.codehilite .vi { color: #008080 } /* Name.Variable.Instance */
.codehilite .il { color: #009999 } /* Literal.Number.Integer.Long */

View File

@ -6,6 +6,9 @@
<title>{% block title %}{% endblock %}</title>
<link rel="stylesheet" href="{% if page != "index" %}../{% endif %}static/css/patternfly.min.css">
<link rel="stylesheet" href="{% if page != "index" %}../{% endif %}static/css/ara.css">
{% if file.id or record.id or host.id %}
<link rel="stylesheet" href="{% if page != "index" %}../{% endif %}static/css/pygments.css">
{% endif %}
<link rel="shortcut icon" href="{% if page != "index" %}../{% endif %}static/images/favicon.ico">
{% block head %}
{% endblock %}

View File

@ -1,15 +1,14 @@
{% extends "base.html" %}
{% block body %}
{% include "partials/playbook_card.html" with playbook=file.playbook %}
{% load pygments_highlights %}
<div class="pf-c-card" style="margin: 1em 0;" id="properties">
<div class="pf-c-card" style="margin: 1em 0;">
<div class="pf-c-card__header pf-c-title pf-m-md">
File: {{ file.path }}
<strong>{{ file.path }}</strong>
</div>
</div>
<div class="pf-c-card" style="margin: 1em 0;" id="properties">
<div class="pf-c-card__body">
<pre>{% autoescape on %}{{ file.content | linebreaksbr }}{% endautoescape %}</pre>
{{ file.content | format_yaml | safe }}
</div>
</div>
{% endblock %}

View File

@ -1,13 +1,12 @@
{% extends "base.html" %}
{% block body %}
{% include "partials/playbook_card.html" with playbook=host.playbook %}
{% load pygments_highlights %}
<div class="pf-c-card" style="margin: 1em 0;">
<div class="pf-c-card__header pf-c-title pf-m-md">
Host: {{ host.name }}
</div>
</div>
<div class="pf-c-card" style="margin: 1em 0;">
<div class="pf-c-card__body">
<table class="pf-c-table pf-m-grid-md" role="grid">
<thead>
@ -20,7 +19,7 @@
{% for fact, value in host.facts.items %}
<tr>
<td id="{{ fact }}" style="white-space: nowrap"><a href="#{{ fact }}">{{ fact }}</a></td>
<td>{% autoescape on %}{{ value }}{% endautoescape %}</td>
<td>{{ value | format_data | safe }}</td>
</tr>
{% endfor %}
</tbody>

View File

@ -1,15 +1,14 @@
{% extends "base.html" %}
{% block body %}
{% include "partials/playbook_card.html" with playbook=record.playbook %}
{% load pygments_highlights %}
<div class="pf-c-card" style="margin: 1em 0;">
<div class="pf-c-card__header pf-c-title pf-m-md">
Record: {{ record.key }}
<strong>{{ record.key }}</strong>
</div>
</div>
<div class="pf-c-card" style="margin: 1em 0;">
<div class="pf-c-card__body">
<pre>{% autoescape on %}{{ record.value }}{% endautoescape %}</pre>
{{ record.value | format_data | safe }}
</div>
</div>
{% endblock %}

View File

@ -1,6 +1,7 @@
{% extends "base.html" %}
{% block body %}
{% include "partials/playbook_card.html" with playbook=result.playbook %}
{% load pygments_highlights %}
<div class="pf-c-card" style="margin: 1em 0;">
<div class="pf-c-card__header pf-c-title pf-m-md">
@ -20,7 +21,7 @@
{% for field, value in result.content.items %}
<tr>
<td id="{{ field }}" style="white-space: nowrap"><a href="#{{ field }}">{{ field }}</a></td>
<td>{% autoescape on %}{{ value }}{% endautoescape %}</td>
<td>{{ value | format_data | safe }}</td>
</tr>
{% endfor %}
</tbody>

View File

@ -0,0 +1,60 @@
# Copyright (c) 2019 Red Hat, Inc.
#
# This file is part of ARA Records Ansible.
#
# ARA is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# ARA is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with ARA. If not, see <http://www.gnu.org/licenses/>.
import json
from django import template
from pygments import highlight
from pygments.formatters import HtmlFormatter
from pygments.lexers import JsonLexer, YamlLexer
from pygments.lexers.special import TextLexer
register = template.Library()
@register.filter(name="format_yaml")
def format_yaml(code):
formatter = HtmlFormatter(
linenos="table", anchorlinenos=True, lineanchors="line", linespans="line", cssclass="codehilite"
)
if not code:
code = ""
return highlight(code, YamlLexer(stripall=True), formatter)
@register.filter(name="format_data")
def format_data(data):
formatter = HtmlFormatter(cssclass="codehilite")
if isinstance(data, bool) or isinstance(data, int) or isinstance(data, float):
return highlight(str(data), TextLexer(), formatter)
elif isinstance(data, str):
try:
data = json.dumps(json.loads(data), indent=4, sort_keys=True)
lexer = JsonLexer()
except (ValueError, TypeError):
lexer = TextLexer()
elif isinstance(data, dict) or isinstance(data, list):
data = json.dumps(data, indent=4, sort_keys=True)
lexer = JsonLexer()
else:
lexer = TextLexer()
lexer.stripall = True
return highlight(data, lexer, formatter)

View File

@ -39,6 +39,7 @@ server=
django-filter
dynaconf[yaml]
whitenoise
pygments
postgresql=
psycopg2