[snowy] Split code for converting a note to html into notes.utils.note_to_html()
- From: Jeff Schroeder <jschroeder src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [snowy] Split code for converting a note to html into notes.utils.note_to_html()
- Date: Tue, 2 Nov 2010 04:52:52 +0000 (UTC)
commit c751df9a414a26368d1f80565895a5a30fe93fc6
Author: Jeff Schroeder <jeffschroeder computer org>
Date: Thu Oct 21 20:01:31 2010 -0700
Split code for converting a note to html into notes.utils.note_to_html()
notes/utils.py | 57 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
notes/views.py | 37 +----------------------------------
2 files changed, 59 insertions(+), 35 deletions(-)
---
diff --git a/notes/utils.py b/notes/utils.py
new file mode 100644
index 0000000..d7e31e3
--- /dev/null
+++ b/notes/utils.py
@@ -0,0 +1,57 @@
+# 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/>.
+#
+from snowy.notes.models import Note
+from django.conf import settings
+from django.core.exceptions import ObjectDoesNotExist
+
+from snowy.notes.templates import CONTENT_TEMPLATES, DEFAULT_CONTENT_TEMPLATE
+
+def note_to_html(note, author):
+ """
+ Convert a note into a html string
+ """
+ from lxml import etree
+ import os.path
+
+ # Extension function for XSL. Called twice per link,
+ # so we keep a little cache to save on lookups
+ link_cache = {}
+ def get_url_for_title(dummy, link_text):
+ if link_text in link_cache:
+ return link_cache[link_text]
+ try:
+ note = Note.objects.get(author=author, title=link_text)
+ note_url = note.get_absolute_url()
+ link_cache[link_text] = note_url
+ return note_url
+ except ObjectDoesNotExist:
+ return None
+
+ ns = etree.FunctionNamespace("http://tomboy-online.org/stuff")
+ ns.prefix = "tomboyonline"
+ ns['get_url_for_title'] = get_url_for_title
+
+ style = etree.parse(os.path.join(settings.PROJECT_ROOT,
+ 'data/note2xhtml.xsl'))
+ transform = etree.XSLT(style)
+
+ template = CONTENT_TEMPLATES.get(note.content_version, DEFAULT_CONTENT_TEMPLATE)
+ complete_xml = template.replace('%%%CONTENT%%%', note.content)
+ doc = etree.fromstring(complete_xml)
+
+ result = transform(doc)
+ body = str(result)
+ return body
diff --git a/notes/views.py b/notes/views.py
index de82df2..e2d7951 100644
--- a/notes/views.py
+++ b/notes/views.py
@@ -17,16 +17,14 @@
from django.http import HttpResponseRedirect, HttpResponseForbidden, Http404
from django.shortcuts import render_to_response, get_object_or_404
-from django.core.exceptions import ObjectDoesNotExist
from django.core.urlresolvers import reverse
from django.core.paginator import Paginator
from django.contrib.auth.models import User
from django.template import RequestContext
from django.db.models import Q
-from snowy.notes.templates import CONTENT_TEMPLATES, DEFAULT_CONTENT_TEMPLATE
+from snowy.notes.utils import note_to_html
from snowy.notes.models import *
-from snowy import settings
def note_index(request, username,
template_name='notes/note_index.html'):
@@ -67,38 +65,7 @@ def note_detail(request, username, note_id, slug='',
if note.slug != slug:
return HttpResponseRedirect(note.get_absolute_url())
- # break this out into a function
- from lxml import etree
- import os.path
-
- # Extension function for XSL. Called twice per link,
- # so we keep a little cache to save on lookups
- link_cache = {}
- def get_url_for_title(dummy, link_text):
- if link_text in link_cache:
- return link_cache[link_text]
- try:
- note = Note.objects.get(author=author, title=link_text)
- note_url = note.get_absolute_url()
- link_cache[link_text] = note_url
- return note_url
- except ObjectDoesNotExist:
- return None
-
- ns = etree.FunctionNamespace("http://tomboy-online.org/stuff")
- ns.prefix = "tomboyonline"
- ns['get_url_for_title'] = get_url_for_title
-
- style = etree.parse(os.path.join(settings.PROJECT_ROOT,
- 'data/note2xhtml.xsl'))
- transform = etree.XSLT(style)
-
- template = CONTENT_TEMPLATES.get(note.content_version, DEFAULT_CONTENT_TEMPLATE)
- complete_xml = template.replace('%%%CONTENT%%%', note.content)
- doc = etree.fromstring(complete_xml)
-
- result = transform(doc)
- body = str(result)
+ body = note_to_html(note, author)
return render_to_response(template_name,
{'title': note.title,
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]