[dots] Add ODT support using odf.odf2xhtml package to translate it to xhtml



commit f3aec3dd37af2d0e984e5d2256b43f6176a5fa96
Author: Fernando Herrera <fherrera onirica com>
Date:   Wed Jun 2 16:19:04 2010 +0200

    Add ODT support using odf.odf2xhtml package to translate it to xhtml

 TODO               |    4 +++-
 dots/document.py   |   23 +++++++++++++++++++++--
 dots/translator.py |   14 +++++++++-----
 3 files changed, 33 insertions(+), 8 deletions(-)
---
diff --git a/TODO b/TODO
index 8e6f130..3554ab9 100644
--- a/TODO
+++ b/TODO
@@ -1,6 +1,8 @@
 - Put cursor at beginning of textviews when they are exposed.
 - Add application window.
 
+- Add semantics for odt xml files
+- Add pdfextractor 
+
 For model/view branch:
 	- Save the text in the model, not the buffer in the view
-	- 
diff --git a/dots/document.py b/dots/document.py
index 676fa9a..100871a 100644
--- a/dots/document.py
+++ b/dots/document.py
@@ -16,6 +16,7 @@
 # You should have received a copy of the GNU General Public License
 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
+import mimetypes
 from translator import Translator
 
 class Document():
@@ -28,8 +29,26 @@ class Document():
 	self.output_file = output_file
 
     def translate(self, config):
-	translator = Translator(config)
-	self.braille_text = translator.translate (self.input_file)
+	self.translator = Translator(config)
+	mime_type, encoding = mimetypes.guess_type (self.input_file)
+	if mime_type == "application/pdf":
+		self.translate_pdf ()
+	elif mime_type == "application/vnd.oasis.opendocument.text":
+		self.translate_odt ()
+	else:
+		self.translate_xml ()
+
+    def translate_pdf(self):
+	return
+
+    def translate_odt(self):
+	from odf.odf2xhtml import ODF2XHTML
+	odhandler = ODF2XHTML (False, False)
+	result = odhandler.odf2xhtml(self.input_file).encode('UTF-8','xmlcharrefreplace')
+	self.braille_text = self.translator.translate_string (result)
+
+    def translate_xml(self):
+	self.braille_text = self.translator.translate_file (self.input_file)
 
     def get_braille_text(self):
 	return self.braille_text
diff --git a/dots/translator.py b/dots/translator.py
index 0c01d58..4e6b94d 100644
--- a/dots/translator.py
+++ b/dots/translator.py
@@ -24,14 +24,18 @@ class Translator():
     def __init__(self, config):
         self.config = config
 
-    def translate(self, input_file):
+    def translate_file(self, input_file):
+	f = open (input_file)
+	res = self.translate_string (f.read())
+	f.close()
+	return res
+
+    def translate_string(self, buffer):
 	cfgfile = tempfile.NamedTemporaryFile (delete=False)
 	cfgfile.write (str (self.config))
 	cfgfile.close()
-	f = open (input_file)
-	buf = louisxml.translateString (cfgfile.name, f.read(),0)
-	f.close()
+	res = louisxml.translateString (cfgfile.name, buffer, 0)
 	os.unlink(cfgfile.name)
-	return buf
+	return res
 	
 



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