Some suggestions (comments and utf-8, pyarchives_enhanced, config file location)

"Joern P. Meier" <[email protected]> Sun, 21 Sep 2008 15:15:38 +0200
Newsgroups gmane.comp.web.pyblosxom.devel
Message-ID <[email protected]>
Hi,

I am in the process of creating a blog using Pyblosxom. I really enjoy
working with it but I ran into some problems. I tried to fix most of
those myself but I would like to know if there was some mistake on my
part. Also, if those are indeed bugs, maybe my changes are helpful.

1) comments.py and UTF-8

Since my blog language is German, I tried to use UTF-8 throughout. I
have not noticed any broblems with entries so far, but comments did not
work as expected.

The first problem is that the comments.js does not actually encode the
form data as UTF-8 even if it is declared as such, which yields a broken
preview. I applied the following fix:

@@ -66,7 +66,8 @@
   for (i = 0; i < form.elements.length; i++) {
     elem = form.elements[i];
     if (elem.type != 'submit' && elem.type != 'button')
-      post_data += '&' + elem.name + '=' + escape(elem.value);
+      post_data += '&' + elem.name + '='
+        + encodeURIComponent(elem.value);
   }

   // send the request and tell the user.

The second problem is that in comments.py, there is a section where
values from the form are passed (via tools.urlencode_text) to
urllib.quote, which tries to use the ASCII codec by default if an
unicode string is passed. This fails if the unicode string actually
contains a character that cannot be encoded in ASCII.
My fix tries to encode the string using UTF-8 before it is passed if it
is a unicode string.

@@ -995,8 +995,12 @@
             del comment_entry_base['comments']
             for key in entry.keys():
                 if isinstance(entry[key], types.StringTypes):
-                    comment_entry_base[key + '_escaped'] =
tools.escape_text(entry[key])
-                    comment_entry_base[key + '_urlencoded'] =
tools.urlencode_text(entry[key])
+                    if (isinstance(entry[key], unicode)):
+                        encodedEntry = entry[key].encode('utf-8')
+                    else:
+                        encodedEntry = entry[key]
+                    comment_entry_base[key + '_escaped'] =
tools.escape_text(encodedEntry)
+                    comment_entry_base[key + '_urlencoded'] =
tools.urlencode_text(encodedEntry)
             for comment in entry['comments']:
                 comment_entry = dict(comment_entry_base)
                 comment_entry.update(comment)

Maybe it is not so good an idea to hardcode the encoding. Unfortunately
I do not understand the code well enough yet to come up with a better
solution. However, this works in my setup.

2) pyarchives_enhanced

The 'post' variable as used in the template returns the filename of the
blog entry. This cannot be used to create a valid link to the entry,
like it is suggested in the docstring:

py['archive_template_post'] = '<div class="archivePost"><a
href="%(base_url)s/%(post)s">%(title)s</a> </div>'

It also does not include the category or a reference to the flavour.

I applied the following changes in pyarchives_enhanced.py:

@@ -188,7 +188,11 @@
             post_archives = month_entry.archives
             text = open(mem).readline()
             title = re.match(".+", text).group()
-            post = os.path.basename(mem)
+            if (mem[:len(root)] == root):
+                post, ext = os.path.splitext(mem[len(root):])
+                post = post.lstrip('/')
+            else:
+                post = ''
             fulldict.update({ 'title' : title, 'post' : post})
             post_archives[timedict['d'] + timedict['H'] + timedict['M']
+ timedict['S']] = post_template % fulldict

This sets the 'post' variable to the complete path relative to the
datadir, minus the extension.

I then use this template to create the links:

<a href="%(base_url)s/%(post)s.%(default_flavour)s">%(title)s</a>

Maybe I did something wrong here with my setup. I almost cannot believe
that the default solution should not work. ;)

3) config file location

I also modified pyblosxom.py to make the configuration file location
configurable:

@@ -445,7 +443,8 @@
     """
     This class is the WSGI application for PyBlosxom.
     """
-    def __init__(self, environ=None, start_response=None, configini=None):
+    def __init__(self, environ=None, start_response=None, configini=None,
+        configpy = None):
         """
         Make WSGI app for PyBlosxom.

@@ -465,10 +464,12 @@
             configini = {}

         _config = tools.convert_configini_values(configini)
-
-        # FIXME - what if config is not named config?
-        import config
-        self.config = dict(config.py)
+
+        if (configpy is None):
+            configpy = 'config.py'
+        gl = {}
+        execfile(configpy, gl)
+        self.config = dict(gl['py'])

This uses config.py if no file has been specified when the application
was created. Otherwise, it loads whatever file has been specified. I
notice there is an configini argument, but using an INI file when I can
use Python just seems awkward. ;)

I hope any of this makes sense. :)

Cheers,

Jörn

-------------------------------------------------------------------------
This SF.Net email is sponsored by the Moblin Your Move Developer's challenge
Build the coolest Linux based applications with Moblin SDK & win great prizes
Grand prize is a trip for two to an Open Source event anywhere in the world
http://moblin-contest.org/redirect.php?banner_id=100&url=/
_______________________________________________
Pyblosxom-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/pyblosxom-devel