foiegras r67 - in trunk: . src/foiegras src/foiegras/buffer



Author: denisw
Date: Sun Mar 16 11:46:54 2008
New Revision: 67
URL: http://svn.gnome.org/viewvc/foiegras?rev=67&view=rev

Log:
2008-03-11  Denis Washington  <denisw svn gnome org>

	* src/foiegras/document.py:
	* src/foiegras/buffer/docbuffer.py:
	* src/foiegras/buffer/open.py:
	Support opening of files to the extent that saving supports.


Added:
   trunk/src/foiegras/buffer/open.py
Modified:
   trunk/ChangeLog
   trunk/src/foiegras/buffer/docbuffer.py
   trunk/src/foiegras/document.py

Modified: trunk/src/foiegras/buffer/docbuffer.py
==============================================================================
--- trunk/src/foiegras/buffer/docbuffer.py	(original)
+++ trunk/src/foiegras/buffer/docbuffer.py	Sun Mar 16 11:46:54 2008
@@ -21,6 +21,7 @@
 
 from block_handler import block_handler
 from tags import INLINE_TAGS, BLOCK_TAGS, SIMPLE_BLOCK_TAGS, TAG_TABLE
+from open import open_document
 from save import save_document
 
 def _(string):
@@ -41,13 +42,14 @@
         self.__block_tag = "p"
         self.__activated_tags = set()
         self.__deactivated_tags = set()
+        self.__manual_insert = False
 
         self.connect("insert-text", self.__insert_text_cb)
         self.connect_after("insert-text", self.__insert_text_after_cb)
      
         if filename:
             # TODO: File loading
-            pass
+            open_document(self)
         else:
             self.set_text(_("New Topic\nAdd the topic's content here."))
 
@@ -211,22 +213,31 @@
         return tags
 
 
+    def insert_with_tags(self, textiter, data, *tags):
+        """
+        """
+        self.__manual_insert = True
+        gtk.TextBuffer.insert_with_tags(self, textiter, data, *tags)
+        self.__manual_insert = False
+
+
     def __insert_text_cb(self, textbuffer, location, text, length):
         """
         "insert-text" signal handler.
         """
-        block_tag = None
+        if not self.__manual_insert:
+            block_tag = None
 
-        for tag_name in BLOCK_TAGS:
-            tag = self.get_tag_table().lookup(tag_name)
+            for tag_name in BLOCK_TAGS:
+                tag = self.get_tag_table().lookup(tag_name)
 
-            if location.has_tag(tag):
-                block_tag = tag_name
+                if location.has_tag(tag):
+                    block_tag = tag_name
 
-        if block_tag:
-            self.__block_tag = block_tag
-        else:
-            self.__block_tag = "p"
+            if block_tag:
+                self.__block_tag = block_tag
+            else:
+                self.__block_tag = "p"
             
 
 
@@ -234,24 +245,25 @@
         """
         "insert-text" signal handler. (Runs after the default hander).
         """
-        num_lines_back = 0
+        if not self.__manual_insert:
+            num_lines_back = 0
 
-        for i in range(length):
-            if text[i] == "\n" or (text[i] == "\r" and text[i + 1] != "\n"):
-                 num_lines_back += 1
+            for i in range(length):
+                if text[i] == "\n" or (text[i] == "\r" and text[i + 1] != "\n"):
+                     num_lines_back += 1
 
-        for i in range(num_lines_back):
-            location.backward_line()
+            for i in range(num_lines_back):
+                location.backward_line()
 
-        #self.apply_block_tag(self.__block_tag, location)
+            #self.apply_block_tag(self.__block_tag, location)
 
-        start_iter = location.copy()
-        start_iter.backward_chars(length)
+            start_iter = location.copy()
+            start_iter.backward_chars(length)
 
-        for tag_name in self.__get_active_tags(start_iter):
-            self.apply_tag_by_name(tag_name, start_iter, location)
+            for tag_name in self.__get_active_tags(start_iter):
+                self.apply_tag_by_name(tag_name, start_iter, location)
 
-        self.notify("cursor-position")
+            self.notify("cursor-position")
 
 
     def __get_active_tags(self, location):

Added: trunk/src/foiegras/buffer/open.py
==============================================================================
--- (empty file)
+++ trunk/src/foiegras/buffer/open.py	Sun Mar 16 11:46:54 2008
@@ -0,0 +1,67 @@
+# Copyright (C) 2008 by Denis Washington <denisw svn gnome org>
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 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 General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+
+import xml.parsers.expat
+
+from tags import BLOCK_TAGS, INLINE_TAGS, get_text_tag
+
+def open_document(doc_buffer):
+    """
+    Opens the passed document buffer.
+    """
+    DocumentLoader(doc_buffer).parse()
+
+
+class DocumentLoader (object):
+
+    def __init__(self, doc_buffer):
+        self.buffer = doc_buffer
+        self._active_tags = []
+        self._tag_names = []
+
+
+    def parse(self):
+        parser = xml.parsers.expat.ParserCreate()
+
+        parser.StartElementHandler = self._start_element
+        parser.EndElementHandler = self._end_element
+        parser.CharacterDataHandler = self._char_data
+
+        f = file(self.buffer.get_filename())
+        parser.ParseFile(f)
+        f.close()
+
+
+    def _start_element(self, name, attrs):
+        if name in BLOCK_TAGS or name in INLINE_TAGS:
+            self._active_tags.append(get_text_tag(name))
+            self._tag_names.append(name)
+
+
+    def _end_element(self, name):
+        if name in BLOCK_TAGS or name in INLINE_TAGS:
+            self._active_tags.remove(get_text_tag(name))
+            self._tag_names.remove(name)
+
+        if name in BLOCK_TAGS:
+            self.buffer.insert_with_tags(self.buffer.get_end_iter(), "\n", *self._active_tags)
+
+
+    def _char_data(self, data):
+        print "'%s': %s" % (data, self._tag_names)
+        self.buffer.insert_with_tags(self.buffer.get_end_iter(), data, *self._active_tags)
+
+

Modified: trunk/src/foiegras/document.py
==============================================================================
--- trunk/src/foiegras/document.py	(original)
+++ trunk/src/foiegras/document.py	Sun Mar 16 11:46:54 2008
@@ -30,7 +30,7 @@
         """ Constructor for Document. """
         gtk.VBox.__init__(self)
 
-        self._buffer = docbuffer.DocumentBuffer()
+        self._buffer = docbuffer.DocumentBuffer(filename)
 
         self._view = gtk.TextView(self._buffer)
         self._view.set_pixels_above_lines(6)



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