dirbrowser: ditch .lstrip, add file_url function

This commit is contained in:
Daniel Schadt 2016-03-24 00:14:22 +01:00
parent 700756aa16
commit 375e60627a
3 changed files with 16 additions and 7 deletions

View File

@ -44,9 +44,7 @@ def get_file_list(basedir, all_files, filterfunc):
for filename in all_files:
absname = os.path.join(basedir, filename)
if filterfunc(absname):
# Absolute paths in Unix start with a slash ('/'), but we already
# have enough slashes in the template, so we don't need it here
items.append({'name': filename, 'absname': absname.lstrip('/')})
items.append({'name': filename, 'absname': absname})
return sorted(items, key=lambda v: v['name'].lower())
@ -84,7 +82,7 @@ def dirbrowser_html(path):
if is_root(path):
parent = None
else:
parent = os.path.normpath(os.path.join(path, '..')).lstrip('/')
parent = os.path.normpath(os.path.join(path, '..'))
try:
all_files = os.listdir(path)

View File

@ -48,19 +48,19 @@ ul.files > li {
{% if parent is not none %}
<ul class="parent">
<li><a href="file:///{{parent}}">..</a></li>
<li><a href="{{ file_url(parent) }}">..</a></li>
</ul>
{% endif %}
<ul class="folders">
{% for item in directories %}
<li><a href="file:///{{item.absname}}">{{item.name}}</a></li>
<li><a href="{{ file_url(item.absname) }}">{{item.name}}</a></li>
{% endfor %}
</ul>
<ul class="files">
{% for item in files %}
<li><a href="file:///{{item.absname}}">{{item.name}}</a></li>
<li><a href="{{ file_url(item.absname) }}">{{item.name}}</a></li>
{% endfor %}
</ul>
</div>

View File

@ -74,6 +74,15 @@ def resource_url(path):
return QUrl.fromLocalFile(image).toString(QUrl.FullyEncoded)
def file_url(path):
"""Return a file:// url (as string) to the given local path.
Arguments:
path: The absolute path to the local file
"""
return QUrl.fromLocalFile(path).toString(QUrl.FullyEncoded)
def render(template, **kwargs):
"""Render the given template and pass the given arguments to it."""
try:
@ -85,5 +94,7 @@ def render(template, **kwargs):
tb = traceback.format_exc()
return err_template.format(pagename=template, traceback=tb)
_env = jinja2.Environment(loader=Loader('html'), autoescape=_guess_autoescape)
_env.globals['resource_url'] = resource_url
_env.globals['file_url'] = file_url