Skip to content
Snippets Groups Projects
Verified Commit 4d0d2d8a authored by Nik | Klampfradler's avatar Nik | Klampfradler
Browse files

Implement cache list export to HTML.

parent ebb44ffd
No related branches found
No related tags found
No related merge requests found
......@@ -5,7 +5,7 @@ import logging
from .lib.adaptors.gpx import GpxImporter
from .lib.exporters.caches import CacheExporter
from .lib.exporters.formatters import caches_to_csv, caches_to_json
from .lib.exporters.formatters import caches_to_csv, caches_to_html, caches_to_json
from .lib.stats import all_stats
from .lib.stats.formatters import stats_to_html, stats_to_json
from .lib.config import get_default_config_path, get_config
......@@ -38,6 +38,7 @@ def export_caches():
parser = ArgumentParser(parents=[_PARENT_PARSER])
parser.add_argument('--found-by', help='limit caches found by cacher name', default=None)
parser.add_argument('--format', help='output format', default='json')
parser.add_argument('--title', help='title for output (only for html)', default=None)
args = parser.parse_args()
logging.basicConfig(level=args.loglevel, format='%(levelname)-8s %(message)s')
......@@ -52,6 +53,8 @@ def export_caches():
print(caches_to_json(data))
elif args.format == 'csv':
print(caches_to_csv(data))
elif args.format == 'html':
print(caches_to_html(data, title=args.title))
def stats():
""" Command for all kinds of statistics """
......
......@@ -2,6 +2,12 @@ import csv
import io
import json
try:
import jinja2
_HAVE_JINJA = True
except ImportError:
_HAVE_JINJA = False
from ..util import flatten_dict, json_serialise
def caches_to_json(data):
......@@ -19,3 +25,15 @@ def caches_to_csv(data):
writer.writerow(flatten_dict(entry))
return csv_file.getvalue()
def caches_to_html(data, main_template='simple', title=None):
if not _HAVE_JINJA:
return '<html><body>Jinja2 could not be imported.</body></html>'
env = jinja2.Environment(
loader=jinja2.PackageLoader('librestash', 'templates'),
autoescape=jinja2.select_autoescape(['html'])
)
template = env.get_template('caches/main_%s.html' % main_template)
return template.render(data=data, title=title)
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="generator" content="Librestash" />
<title>{{ title|default('Geocaches') }}</title>
</head>
<body>
<h1>{{ title|default('Geocaches') }}</h1>
{% include "caches/plugins/list.html" %}
</body>
</html>
<table>
<thead>
<th>Cache Name</th>
<th>D</th>
<th>T</th>
<th>Type</th>
<th>Size</th>
<th>Listings</th>
</thead>
<tbody>
{% for cache in data %}
<tr>
<td>{{ cache.name }}</td>
<td>{{ cache.difficulty }}</td>
<td>{{ cache.terrain }}</td>
<td>{{ cache.type }}</td>
<td>{{ cache.size }}</td>
<td>
{% for listing in cache.listings %}
{{ listing.code }}
{% endfor %}
</td>
</tr>
{% endfor %}
</tbody>
</table>
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment