[snowy] Refactor view a bit so that the template is cached in memory



commit f7bab8876198292d53594d5fa97cd486c98e6e88
Author: Brad Taylor <brad getcoded net>
Date:   Mon May 18 18:07:48 2009 -0400

    Refactor view a bit so that the template is cached in memory
---
 data/note2xhtml.xsl |    2 +-
 notes/templates.py  |   30 ++++++++++++++++++++++++++++++
 notes/views.py      |    9 ++++-----
 3 files changed, 35 insertions(+), 6 deletions(-)

diff --git a/data/note2xhtml.xsl b/data/note2xhtml.xsl
index bde0f75..fc6e9be 100644
--- a/data/note2xhtml.xsl
+++ b/data/note2xhtml.xsl
@@ -96,6 +96,7 @@
 <xsl:template match="link:url">
 	<a style="color:#3465A4" href="{node()}"><xsl:value-of select="node()"/></a>
 </xsl:template>
+-->
 
 <xsl:template match="tomboy:list">
 	<ul>
@@ -114,7 +115,6 @@
 		<xsl:apply-templates select="node()" />
 	</li>
 </xsl:template>
--->
 
 <!-- Evolution.dll Plugin -->
 <!--
diff --git a/notes/templates.py b/notes/templates.py
new file mode 100644
index 0000000..36279fa
--- /dev/null
+++ b/notes/templates.py
@@ -0,0 +1,30 @@
+#
+# Copyright (c) 2009 Brad Taylor <brad getcoded net>
+#
+# This program is free software: you can redistribute it and/or modify it under
+# the terms of the GNU Affero General Public License as published by the Free
+# Software Foundation, either version 3 of the License, or (at your option) any
+# later version.
+#
+# This program is distributed in the hope that it will be useful, but WITHOUT
+# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
+# FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public License for more
+# details.
+#
+# You should have received a copy of the GNU Affero General Public License
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
+#
+
+# libxml2 doesn't munge encodings, so forcibly encode items to UTF-8
+# http://mail.gnome.org/archives/xml/2004-February/msg00363.html
+CONTENT_TEMPLATES = {
+    '0.1': """
+<note-content version="0.1" 
+ xmlns:link="http://beatniksoftware.com/tomboy/link";
+ xmlns:size="http://beatniksoftware.com/tomboy/size";
+ xmlns="http://beatniksoftware.com/tomboy";>
+ %%%CONTENT%%%
+</note-content>""".encode('UTF-8'),
+}
+
+DEFAULT_CONTENT_TEMPLATE = CONTENT_TEMPLATES['0.1']
diff --git a/notes/views.py b/notes/views.py
index 90944e0..2b642a5 100644
--- a/notes/views.py
+++ b/notes/views.py
@@ -20,6 +20,7 @@ from django.contrib.auth.models import User
 from django.http import HttpResponseRedirect, Http404
 from django.shortcuts import render_to_response, get_object_or_404
 
+from snowy.notes.templates import CONTENT_TEMPLATES, DEFAULT_CONTENT_TEMPLATE
 from snowy.notes.models import *
 
 def note_index(request, username,
@@ -55,12 +56,10 @@ def note_detail(request, username, note_id, slug='',
         styledoc = libxml2.parseFile('data/note2xhtml.xsl')
         style = libxslt.parseStylesheetDoc(styledoc)
     
-        # libxml2 doesn't munge encodings, so forcibly encode to UTF-8
-        # http://mail.gnome.org/archives/xml/2004-February/msg00363.html
-        # TODO: Check note.content_version so we can hypothetically apply different XSLT
-        content_to_parse = '<note-content version="1.0" xmlns:link="http://beatniksoftware.com/tomboy/link"; xmlns:size="http://beatniksoftware.com/tomboy/size"; xmlns="http://beatniksoftware.com/tomboy";>' + note.content + '</note-content>'
-        doc = libxml2.parseDoc(content_to_parse.encode('UTF-8'))
+        template = CONTENT_TEMPLATES.get(note.content_version, DEFAULT_CONTENT_TEMPLATE)
+        doc = libxml2.parseDoc(template.replace('%%%CONTENT%%%', note.content))
         result = style.applyStylesheet(doc, None)
+
         # libxml2 doesn't munge encodings, so forcibly decode from UTF-8
         body = unicode(style.saveResultToString(result), 'UTF-8')
     finally:



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]