Commit 9cb4f967 authored by Antoine Beaupré's avatar Antoine Beaupré

follow flask's convention for template location

we create a separate templates directory where templates will live

this breaks the `--theme` option which we didn't really use, it can be
brought back later on i guess, if others are interested.

so far ubuntu declined to use this, so I don't see the urge to make
this uber-themable anymore, unfortunately.

still, this should serve as a future example of how this could be done
in the future
parent 570d87bf
......@@ -46,13 +46,11 @@ from debmans.search import serve
help='keep existing file if newer')
@click.option('--prefix', default='/', show_default=True,
help='prefix to prepend to internal URLs')
@click.option('-t', '--theme', default=find_static_file('static'),
show_default=True)
@click.option('-p', '--plugin', multiple=True, show_default=True,
default=['debmans.renderer.DefaultManpageRenderer'],
help='rendering plugins to load')
@click.pass_context
def debmans(ctx, loglevel, syslog, dryrun, plugin, theme,
def debmans(ctx, loglevel, syslog, dryrun, plugin,
progress, profile, output, mirror, cache, prefix):
'''Extract and render manuals from Debian packages'''
setup_logging(level=loglevel, syslog=syslog)
......@@ -78,8 +76,7 @@ def debmans(ctx, loglevel, syslog, dryrun, plugin, theme,
ctx.obj['dryrun'] = dryrun
ctx.obj['patterns'] = {}
ctx.obj['theme'] = theme
template = os.path.join(theme, 'template.html')
template = find_static_file(os.path.join('templates', 'template.html'))
for command in plugin:
logging.debug('looking for module %s', command)
try:
......
......@@ -419,8 +419,8 @@ def dispatch(job, destdir, dryrun=False, cache=True, prefix=None, mirror=None):
def site(obj):
'''render the whole static site'''
suites = obj['mirror'].releases
pattern = os.path.join(obj['theme'], '*.mdwn')
template = os.path.join(obj['theme'], 'template.html')
pattern = os.path.join('static', '*.mdwn')
template = os.path.join('templates', 'template.html')
logging.info('rendering files in %s with template %s',
pattern, template)
i = 0
......@@ -438,11 +438,11 @@ def site(obj):
i = 0
t = time.time()
for pattern in '*.css', '*.js':
for static in glob.glob(os.path.join(obj['theme'], pattern)):
for static in glob.glob(os.path.join('static', pattern)):
i += 1
logging.debug('copying %s to %s', static, obj['output'])
shutil.copy(static, obj['output'])
picsdir = os.path.join(obj['theme'], 'Pics')
picsdir = os.path.join('static', 'Pics')
pics = os.path.join(picsdir, '*')
targetdir = os.path.join(obj['output'], 'Pics')
mkdirp(targetdir)
......
......@@ -25,84 +25,52 @@ import os.path
import re
import click
from flask import Flask
from flask import Flask, render_template
from jinja2 import Template
from debmans.utils import find_static_file
from debmans.renderer import find_files, ManpageRenderer
app = Flask( __name__, static_url_path='')
app = Flask(__name__, static_url_path='')
@app.route('/search/<pattern>')
def search(pattern):
'''respond to searches requests
.. todo:: messy use of Jinja directly because Flask expects
templates to be in templates/ here. just follow the
convention in `the flask manual
<http://flask.pocoo.org/docs/0.11/quickstart/#rendering-templates>`_
.. todo:: Flask also expects static files to be in static/, so we
should copy them there instead of at the root
.. todo:: unit test this: http://flask.pocoo.org/docs/0.11/testing/
.. note:: inspired by http://manpages.ubuntu.com/cgi-bin/search.py?q=man
'''
with open(find_static_file('static/template.html'), 'r') as tmpl:
r = Template(tmpl.read().decode('utf-8'))
# XXX: this is getting a tad ridiculous now
# we shouldn't have to hack at the pattern like this
#
# there is a similar problem in dispatch - maybe file list
# should include the match pattern?
pattern = re.sub('\(\?P<name>[^)]*\)',
pattern, # XXX: wildcard or not?
ManpageRenderer.pattern)
logger.debug('searching for pattern: %s', pattern)
patterns = {}
# XXX: this doesn't belong here? should be command-level patterns
patterns[re.compile(pattern)] = None
# XXX: not sure this is the best structure
manpages = {} # suite -> (name, section)
i = 0
assert app.static_folder
for path in find_files(app.static_folder, patterns):
i += 1
# XXX: again
m = re.search(ManpageRenderer.pattern, path)
assert m
suite = m.group('suite')
name = m.group('name')
section = m.group('section')
if section not in manpages:
manpages[section] = set()
logger.debug('found manpage %s (%s) in %s', name, section, suite)
manpages[section].add((name, suite))
# XXX: should extend the normal template instead
# see http://flask.pocoo.org/docs/0.11/tutorial/templates/#login-html
# XXX: this should also know about all sections, probably?
t = Template('''
<table>
{% for section, mans in manpages.iteritems() %}
<tr>
{% for man, suite in mans %}
<td>{{ man }} ({{suite}})</td>
{% endfor %}
</tr>
{%endfor%}
</table>
''')
content = '%d manuals found' % i
content += t.render(manpages=manpages)
#for suite, mans in manpages.iteritems():
# content += 'suite: %s' % suite
# for man, sect in mans:
# app.logger.debug('go man: %s', man)
# content += 'man: %s sect %s' % (man, sect)
return r.render(content=content, suites={})
# XXX: this is getting a tad ridiculous now
# we shouldn't have to hack at the pattern like this
#
# there is a similar problem in dispatch - maybe file list
# should include the match pattern?
pattern = re.sub('\(\?P<name>[^)]*\)',
pattern, # XXX: wildcard or not?
ManpageRenderer.pattern)
logger.debug('searching for pattern: %s', pattern)
patterns = {}
# XXX: this doesn't belong here? should be command-level patterns
patterns[re.compile(pattern)] = None
# XXX: not sure this is the best structure
manpages = {} # suite -> (name, section)
i = 0
assert app.static_folder
for path in find_files(app.static_folder, patterns):
i += 1
# XXX: again
m = re.search(ManpageRenderer.pattern, path)
assert m
suite = m.group('suite')
name = m.group('name')
section = m.group('section')
if section not in manpages:
manpages[section] = set()
logger.debug('found manpage %s (%s) in %s', name, section, suite)
manpages[section].add((name, suite))
# XXX: this should also know about all sections, probably?
return render_template('search.html', manpages=manpages, count=i,
suites=app.config['releases'])
@app.route('/')
......@@ -133,11 +101,12 @@ def serve(obj, port):
this is used for development purposes and is not designed to be a
fully-fledged webserver
'''
main(obj['output'], port)
main(obj['output'], port, releases=obj['mirror'].releases)
def main(output, port=8000):
def main(output, port=8000, releases={}):
app.static_folder = os.path.abspath(output)
app.config['releases'] = releases
app.run(port=port)
......
{% extends "template.html" %}
{% block content %}
{{count}} manuals found
<table>
{% for section, mans in manpages.iteritems() %}
<tr>
{% for man, suite in mans %}
<td>{{ man }} ({{suite}})</td>
{% endfor %}
</tr>
{%endfor%}
</table>
{% endblock %}
......@@ -43,7 +43,9 @@
</div> <!-- end header -->
<!--/htdig_noindex-->
<div id="content">
{% block content %}
{{content}}
{% endblock %}
</div>
<!--htdig_noindex-->
<div id="footer">
......
......@@ -16,7 +16,7 @@ from debmans.utils import find_static_file
def test_jinja(tmpdir):
output = tmpdir.join('output.html')
template = find_static_file(os.path.join('static', 'template.html'))
template = find_static_file(os.path.join('templates', 'template.html'))
r = JinjaRenderer(template=template)
r.render(str(output), content="foo\nbar", suites={})
data = output.read().decode('utf-8')
......@@ -33,7 +33,7 @@ def test_jinja(tmpdir):
def test_command_render(tmpdir):
output = tmpdir.join('output.html')
template = find_static_file(os.path.join('static', 'template.html'))
template = find_static_file(os.path.join('templates', 'template.html'))
r = CommandRenderer(command='cat "%(source)s"', template=template)
r.render(__file__, str(output), suites={})
data = output.read().decode('utf-8')
......@@ -44,7 +44,7 @@ def test_static_render(tmpdir):
source = tmpdir.join('source.mdwn')
source.write('# Foo')
output = tmpdir.join('output.html')
template = find_static_file(os.path.join('static', 'template.html'))
template = find_static_file(os.path.join('templates', 'template.html'))
r = MarkdownRenderer(template)
r.render(str(source), str(output), suites={})
data = output.read().decode('utf-8')
......@@ -53,7 +53,7 @@ def test_static_render(tmpdir):
def test_manpage_rendering(fake_repo, tmpdir):
manpages, _ = test_extractor(fake_repo, tmpdir)
template = find_static_file(os.path.join('static', 'template.html'))
template = find_static_file(os.path.join('templates', 'template.html'))
suites = PackageMirror(str(fake_repo)).releases
print("releases: %s" % suites)
for manpage in manpages:
......@@ -76,7 +76,7 @@ def test_manpage_render_only(fake_repo, tmpdir):
def test_manpage_norender(tmpdir):
template = find_static_file(os.path.join('static', 'template.html'))
template = find_static_file(os.path.join('templates', 'template.html'))
r = DefaultManpageRenderer(template)
output = tmpdir.join('nosuchpage.1.html')
with pytest.raises(CommandRendererError):
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment