From: W. Trevor King Date: Tue, 20 Jul 2010 16:46:29 +0000 (-0400) Subject: Add preview ability to dir page. X-Git-Url: http://git.tremily.us/?a=commitdiff_plain;h=f990ae9554e1207ffe208a364987594d574d6d58;p=dirtag.git Add preview ability to dir page. Currently justs stuffs the file into an iframe and leaves it up to the browser to handle it. However, the iframe is constructed in dirtag.web.WebInterface._preview so you can construct mime-type dependent preview holders if you want. I changed b1.html -> b1.svg to demonstrate the ability to preview images, etc. --- diff --git a/README b/README index 323d9d2..c2fc95a 100644 --- a/README +++ b/README @@ -14,7 +14,7 @@ For example, with a raw directory like this:: | |-- a2.html | `-- a3.html `-- b - |-- b1.html + |-- b1.svg |-- b2.html `-- b3.html @@ -23,19 +23,19 @@ You could create a tag directory like this:: tag/ |-- 1 | |-- a1.html -> ../../raw/a/a1.html - | `-- b1.html -> ../../raw/b/b1.html + | `-- b1.svg -> ../../raw/b/b1.svg `-- x |-- a2.html -> ../../raw/a/a2.html |-- b3.html -> ../../raw/b/b3.html `-- y - `-- b1.html -> ../../../raw/b/b1.html + `-- b1.svg -> ../../../raw/b/b1.svg Corresponding to the following tags:: a1.html 1 a2.html x a3.html - b1.html 1, x/y + b1.svg 1, x/y b2.html b3.html x diff --git a/dirtag/__init__.py b/dirtag/__init__.py index e572446..045c37c 100644 --- a/dirtag/__init__.py +++ b/dirtag/__init__.py @@ -201,16 +201,16 @@ def dir_tree(root_dir): >>> print '\\n'.join(['|'.join(x) for x in t.traverse(depth=1)]) 1 1|a1.html - 1|b1.html + 1|b1.svg x x|y - x|y|b1.html + x|y|b1.svg x|a2.html x|b3.html >>> print '\\n'.join(['|'.join(x) for x in t.traverse(depth=1, type='files')]) 1|a1.html - 1|b1.html - x|y|b1.html + 1|b1.svg + x|y|b1.svg x|a2.html x|b3.html >>> print '\\n'.join(['|'.join(x) for x in t.traverse(depth=1, type='dirs')]) @@ -247,12 +247,12 @@ class Dirtag (object): file test/raw a|a2.html x file test/raw a|a3.html dir test/raw b - file test/raw b|b1.html 1,x/y + file test/raw b|b1.svg 1,x/y file test/raw b|b2.html file test/raw b|b3.html x >>> print '\\n'.join(['|'.join(x) for x in d.elements(['x'])]) x|y - x|y|b1.html + x|y|b1.svg x|a2.html x|b3.html >>> print '\\n'.join(['|'.join(x) for x in d.tags()]) @@ -261,21 +261,21 @@ class Dirtag (object): x|y >>> print '\\n'.join(['|'.join(x) for x in d.tags(['b', 'b2.html'])]) - >>> print '\\n'.join(['|'.join(x) for x in d.tags(['b', 'b1.html'])]) + >>> print '\\n'.join(['|'.join(x) for x in d.tags(['b', 'b1.svg'])]) 1 x|y >>> print d.tag_path(['a', 'a3.html'], ['x', 'y']) test/tag/x/y/a3.html >>> os.listdir('test/tag/x/y') - ['b1.html'] + ['b1.svg'] >>> d.add_tag(['a', 'a3.html'], ['x', 'y']) >>> sorted(os.listdir('test/tag/x/y')) - ['a3.html', 'b1.html'] + ['a3.html', 'b1.svg'] >>> print 'Z'+os.path.realpath('test/tag/x/y/a3.html') # doctest: +ELLIPSIS Z.../test/raw/a/a3.html >>> d.remove_tag(['a', 'a3.html'], ['x', 'y']) >>> os.listdir('test/tag/x/y') - ['b1.html'] + ['b1.svg'] """ def __init__(self, raw_dir, tag_dir): self.raw_dir = raw_dir diff --git a/dirtag/web.py b/dirtag/web.py index e676e04..59063ec 100755 --- a/dirtag/web.py +++ b/dirtag/web.py @@ -1,5 +1,6 @@ #!/usr/bin/env python +from mimetypes import guess_type from optparse import OptionParser import os from os import path @@ -95,7 +96,8 @@ class WebInterface: '/'.join(f), self._dir_tags(f), 'file?%s' % urlencode({'selected':'/'.join( - self.dirtag.raw_node(f))})) + self.dirtag.raw_node(f))}), + self._preview(f)) for f in s.tree.traverse(prefix=[s.root]+s[:-1], depth=1, type='files', @@ -108,6 +110,16 @@ class WebInterface: else: return '-' + def _preview(self, node): + style = 'style="height=200px; width=200px;"' + type,encoding = guess_type(node[-1]) + return ('' + % (style, '/'.join(node))) + #elif node[-1].endswith('.jpg'): + # return ('' + # % (style, '/'.join(node))) + return 'Unknown filetype for %s' % node[-1] + @cherrypy.expose def file(self, selected): # Disable form value caching in Firefox. See diff --git a/template/dir.html b/template/dir.html index aa727ae..dfbcb3f 100644 --- a/template/dir.html +++ b/template/dir.html @@ -10,11 +10,13 @@ File Tags + Preview - {% for file,path,tags,tag_url in files %} + {% for file,path,tags,tag_url,preview in files %} {{ file }} {{ tags }} + {{ preview }} {% endfor %} diff --git a/template/file.html b/template/file.html index b083ef9..8a78b8b 100644 --- a/template/file.html +++ b/template/file.html @@ -17,5 +17,5 @@ - diff --git a/test/raw/b/b1.html b/test/raw/b/b1.html deleted file mode 100644 index e5f45b0..0000000 --- a/test/raw/b/b1.html +++ /dev/null @@ -1 +0,0 @@ -

b1.html

diff --git a/test/raw/b/b1.svg b/test/raw/b/b1.svg new file mode 100644 index 0000000..bafb194 --- /dev/null +++ b/test/raw/b/b1.svg @@ -0,0 +1,14 @@ + + + + + + b1.svg + + + b1.svg + + diff --git a/test/tag/1/b1.html b/test/tag/1/b1.html deleted file mode 120000 index b0603cc..0000000 --- a/test/tag/1/b1.html +++ /dev/null @@ -1 +0,0 @@ -../../raw/b/b1.html \ No newline at end of file diff --git a/test/tag/1/b1.svg b/test/tag/1/b1.svg new file mode 120000 index 0000000..4f7ef48 --- /dev/null +++ b/test/tag/1/b1.svg @@ -0,0 +1 @@ +../../raw/b/b1.svg \ No newline at end of file diff --git a/test/tag/x/y/b1.html b/test/tag/x/y/b1.html deleted file mode 120000 index fcb3b67..0000000 --- a/test/tag/x/y/b1.html +++ /dev/null @@ -1 +0,0 @@ -../../../raw/b/b1.html \ No newline at end of file diff --git a/test/tag/x/y/b1.svg b/test/tag/x/y/b1.svg new file mode 120000 index 0000000..3d0e13e --- /dev/null +++ b/test/tag/x/y/b1.svg @@ -0,0 +1 @@ +../../../raw/b/b1.svg \ No newline at end of file