[kupfer: 11/20] plugin.zim: Rearrange Zim Page and Source for encoding



commit 6119228068b3f8086f2294e63811b4e7271d9c83
Author: Ulrik Sverdrup <ulrik sverdrup gmail com>
Date:   Tue Oct 6 14:45:21 2009 +0200

    plugin.zim: Rearrange Zim Page and Source for encoding
    
    We try to do the right thing to handle arbitrary UTF-8 encoded
    notebook names. Zim does not seem to handle non-ascii names from the
    command line well, 'zim Notebook Page' simply won't do the right thing
    if Notebook or Page are non-ascii.
    
    For the Notebook part, we can workaround this by using
    NOTEBOOK/notebook.zim file instead, we call this the "notebook id".
    For the page part I don't know any workaround yet.

 kupfer/plugin/zim.py |   46 ++++++++++++++++++++++++++++++++--------------
 1 files changed, 32 insertions(+), 14 deletions(-)
---
diff --git a/kupfer/plugin/zim.py b/kupfer/plugin/zim.py
index 5913927..7805fa3 100644
--- a/kupfer/plugin/zim.py
+++ b/kupfer/plugin/zim.py
@@ -3,6 +3,8 @@ from __future__ import with_statement
 
 import os
 
+import glib
+
 from kupfer.objects import (Leaf, Action, Source, TextLeaf,
 		FilesystemWatchMixin, TextSource, AppLeafContentMixin)
 from kupfer import utils
@@ -12,7 +14,7 @@ __kupfer_sources__ = ("ZimPagesSource", )
 __kupfer_contents__ = ("ZimPagesSource", )
 __kupfer_actions__ = ("CreateZimPage", )
 __description__ = _("Access to Pages stored in Zim - A Desktop Wiki and Outliner")
-__version__ = "0.2"
+__version__ = "0.3"
 __author__ = "Karol BÄ?dkowski <karol bedkowski gmail com>"
 
 
@@ -30,18 +32,18 @@ def _start_zim(notebook, page):
 
 class ZimPage(Leaf):
 	""" Represent single Zim page """
-	def __init__(self, notebook, page):
-		full_name = notebook + " " + page
-		Leaf.__init__(self, full_name, page)
-		self.page = page
-		self.notebook = notebook
+	def __init__(self, page_id, page_name, notebook_id, notebook_name):
+		Leaf.__init__(self, page_id, page_name)
+		self.page = page_name
+		self.notebook = notebook_id
+		self.notebook_name = notebook_name
 
 	def get_actions(self):
 		yield OpenZimPage()
 		yield CreateZimSubPage()
 
 	def get_description(self):
-		return _('Zim Page from Notebook "%s"') % self.notebook
+		return _('Zim Page from Notebook "%s"') % self.notebook_name
 
 	def get_icon_name(self):
 		return "text-x-generic"
@@ -122,18 +124,28 @@ class ZimPagesSource(AppLeafContentMixin, Source):
 		Source.__init__(self, name)
 		# path to file with list notebooks
 		self._zim_notebooks_file = os.path.expanduser('~/.config/zim/notebooks.list')
+		self._version = 2
 
 	def get_items(self):
 		for notebook_name, notebook_path in self._get_notebooks():
-			notebook_path_len = len(notebook_path)
+			notebook_file = os.path.join(notebook_path, "notebook.zim")
 			for root, dirs, files in os.walk(notebook_path):
 				# find pages in notebook
 				for filename in files:
-					if filename.endswith('.txt'):
-						file_path = os.path.join(root, filename)
-						page_name = file_path[notebook_path_len:-4]
-						page_name = page_name.replace(os.path.sep, ':').replace('_', ' ')
-						yield ZimPage(notebook_name, page_name)
+					file_path = os.path.join(root, filename)
+					page_name, ext = os.path.splitext(file_path)
+					if not ext.lower() == ".txt":
+						continue
+					page_name = page_name.replace(notebook_path, "", 1)
+					# Ask GLib for the correct unicode representation
+					# of the page's filename
+					page_name = glib.filename_display_name(page_name)
+					page_name = (page_name
+							.lstrip(os.path.sep)
+							.replace(os.path.sep, u":")
+							.replace(u"_", u" "))
+					yield ZimPage(file_path, page_name, notebook_file,
+							notebook_name)
 
 	def get_description(self):
 		return _("Pages stored in Zim Notebooks")
@@ -145,12 +157,18 @@ class ZimPagesSource(AppLeafContentMixin, Source):
 		yield ZimPage
 
 	def _get_notebooks(self):
-		''' get (notebook name, notebook path) from zim config '''
+		''' Yield (notebook name, notebook path) from zim config
+
+		@notebook_name: Unicode name
+		@notebook_path: Filesystem byte string
+		'''
+		# We assume the notebook description is UTF-8 encoded
 		try:
 			with open(self._zim_notebooks_file, 'r') as noteboks_file:
 				for line in noteboks_file.readlines():
 					if not line.startswith('_default_'):
 						notebook_name, notebook_path = line.strip().split('\t', 2)
+						notebook_name = notebook_name.decode("UTF-8", "replace")
 						notebook_path = os.path.expanduser(notebook_path)
 						yield (notebook_name, notebook_path)
 



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