Add preview ability to dir page.
authorW. Trevor King <wking@drexel.edu>
Tue, 20 Jul 2010 16:46:29 +0000 (12:46 -0400)
committerW. Trevor King <wking@drexel.edu>
Tue, 20 Jul 2010 16:48:17 +0000 (12:48 -0400)
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.

README
dirtag/__init__.py
dirtag/web.py
template/dir.html
template/file.html
test/raw/b/b1.html [deleted file]
test/raw/b/b1.svg [new file with mode: 0644]
test/tag/1/b1.html [deleted symlink]
test/tag/1/b1.svg [new symlink]
test/tag/x/y/b1.html [deleted symlink]
test/tag/x/y/b1.svg [new symlink]

diff --git a/README b/README
index 323d9d28b5d875d911d74cf89e9605ceb65e0e33..c2fc95a312d522f2ff4f0c0ccd6dead6dab17f47 100644 (file)
--- 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
 
index e572446d17997f273996b84ec30b8e73a2b283cd..045c37cabc04af18f5b52df014e9b32a0eb666c6 100644 (file)
@@ -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'])])
     <BLANKLINE>
-    >>> 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
index e676e044d7b4f3affbb2b02a7c19bc658b601e82..59063ec5ca39b21a32ab4f791f27eb8e0c045844 100755 (executable)
@@ -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 ('<iframe %s src="static/raw/%s"></iframe>'
+                % (style, '/'.join(node)))
+        #elif node[-1].endswith('.jpg'):
+        #    return ('<img %s " src="static/raw/%.s"/>'
+        #            % (style, '/'.join(node)))
+        return 'Unknown filetype for %s' % node[-1]
+
     @cherrypy.expose
     def file(self, selected):
         # Disable form value caching in Firefox.  See
index aa727ae1cf73dc4190de4725742d09b4e590f843..dfbcb3f83a5d71cea7f0a1d5a4f73d186377cb9a 100644 (file)
   <tr>
     <th>File</th>
     <th>Tags</th>
+    <th>Preview</th>
   </tr>
-  {% for file,path,tags,tag_url in files %}
+  {% for file,path,tags,tag_url,preview in files %}
   <tr>
     <td><a href="static/raw/{{ path }}">{{ file }}</></td>
     <td><a href="{{ tag_url }}">{{ tags  }}</a></td>
+    <td>{{ preview }}</td>
   </tr>
   {% endfor %}
 </table>
index b083ef902a8cf703b7263d296ff602364bc9e054..8a78b8b7a443c008e5453a12dbb36044f8efb63f 100644 (file)
@@ -17,5 +17,5 @@
   </ul>
   <input type="submit" value="Set tags" />
 </form>
-<iframe style="width:100%; height:500px" src="{{ selected_url }}"/>
+<iframe style="width:100%; height:500px" src="{{ selected_url }}"></iframe>
 </body>
diff --git a/test/raw/b/b1.html b/test/raw/b/b1.html
deleted file mode 100644 (file)
index e5f45b0..0000000
+++ /dev/null
@@ -1 +0,0 @@
-<html><head/><body><h1>b1.html</h1></body></html>
diff --git a/test/raw/b/b1.svg b/test/raw/b/b1.svg
new file mode 100644 (file)
index 0000000..bafb194
--- /dev/null
@@ -0,0 +1,14 @@
+<?xml version="1.0" standalone="no"?>
+<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN"
+"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
+
+
+<svg xmlns="http://www.w3.org/2000/svg" version="1.1"
+     width="100%" height="100%" viewBox="0 0 300 300">
+  <title>b1.svg</title>
+  <circle cx="150" cy="150" r="100" stroke="black"
+         stroke-width="2" fill="yellow"/>
+  <text x="150" y="150" text-anchor="middle">
+    b1.svg
+  </text>
+</svg>
diff --git a/test/tag/1/b1.html b/test/tag/1/b1.html
deleted file mode 120000 (symlink)
index b0603cc..0000000
+++ /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 (symlink)
index 0000000..4f7ef48
--- /dev/null
@@ -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 (symlink)
index fcb3b67..0000000
+++ /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 (symlink)
index 0000000..3d0e13e
--- /dev/null
@@ -0,0 +1 @@
+../../../raw/b/b1.svg
\ No newline at end of file