dirbrowser: ditch .lstrip, add file_url function
This commit is contained in:
parent
700756aa16
commit
375e60627a
@ -44,9 +44,7 @@ def get_file_list(basedir, all_files, filterfunc):
|
|||||||
for filename in all_files:
|
for filename in all_files:
|
||||||
absname = os.path.join(basedir, filename)
|
absname = os.path.join(basedir, filename)
|
||||||
if filterfunc(absname):
|
if filterfunc(absname):
|
||||||
# Absolute paths in Unix start with a slash ('/'), but we already
|
items.append({'name': filename, 'absname': absname})
|
||||||
# have enough slashes in the template, so we don't need it here
|
|
||||||
items.append({'name': filename, 'absname': absname.lstrip('/')})
|
|
||||||
return sorted(items, key=lambda v: v['name'].lower())
|
return sorted(items, key=lambda v: v['name'].lower())
|
||||||
|
|
||||||
|
|
||||||
@ -84,7 +82,7 @@ def dirbrowser_html(path):
|
|||||||
if is_root(path):
|
if is_root(path):
|
||||||
parent = None
|
parent = None
|
||||||
else:
|
else:
|
||||||
parent = os.path.normpath(os.path.join(path, '..')).lstrip('/')
|
parent = os.path.normpath(os.path.join(path, '..'))
|
||||||
|
|
||||||
try:
|
try:
|
||||||
all_files = os.listdir(path)
|
all_files = os.listdir(path)
|
||||||
|
@ -48,19 +48,19 @@ ul.files > li {
|
|||||||
|
|
||||||
{% if parent is not none %}
|
{% if parent is not none %}
|
||||||
<ul class="parent">
|
<ul class="parent">
|
||||||
<li><a href="file:///{{parent}}">..</a></li>
|
<li><a href="{{ file_url(parent) }}">..</a></li>
|
||||||
</ul>
|
</ul>
|
||||||
{% endif %}
|
{% endif %}
|
||||||
|
|
||||||
<ul class="folders">
|
<ul class="folders">
|
||||||
{% for item in directories %}
|
{% 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 %}
|
{% endfor %}
|
||||||
</ul>
|
</ul>
|
||||||
|
|
||||||
<ul class="files">
|
<ul class="files">
|
||||||
{% for item in 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 %}
|
{% endfor %}
|
||||||
</ul>
|
</ul>
|
||||||
</div>
|
</div>
|
||||||
|
@ -74,6 +74,15 @@ def resource_url(path):
|
|||||||
return QUrl.fromLocalFile(image).toString(QUrl.FullyEncoded)
|
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):
|
def render(template, **kwargs):
|
||||||
"""Render the given template and pass the given arguments to it."""
|
"""Render the given template and pass the given arguments to it."""
|
||||||
try:
|
try:
|
||||||
@ -85,5 +94,7 @@ def render(template, **kwargs):
|
|||||||
tb = traceback.format_exc()
|
tb = traceback.format_exc()
|
||||||
return err_template.format(pagename=template, traceback=tb)
|
return err_template.format(pagename=template, traceback=tb)
|
||||||
|
|
||||||
|
|
||||||
_env = jinja2.Environment(loader=Loader('html'), autoescape=_guess_autoescape)
|
_env = jinja2.Environment(loader=Loader('html'), autoescape=_guess_autoescape)
|
||||||
_env.globals['resource_url'] = resource_url
|
_env.globals['resource_url'] = resource_url
|
||||||
|
_env.globals['file_url'] = file_url
|
||||||
|
Loading…
Reference in New Issue
Block a user