--- PageEditor-1.5b2.py	2005-11-06 06:28:50.000000000 +0900
+++ PageEditor.py	2005-11-24 10:35:22.009242300 +0900
@@ -135,7 +135,28 @@
         edit_lock_message = None
         preview = kw.get('preview', None)
         staytop = kw.get('staytop', 0)
+        
+        # for section editing
+        issectionedit = kw.get('issectionedit', 1)
+        pagetext = kw.get('pagetext', None)
+        startline = int(form.get('startline', ['0'])[0])
+        endline = int(form.get('endline', ['0'])[0])
+        srev = int(form.get('srev', ['0'])[0])
+        
+        if startline or endline:
+            if not startline:
+                startline = 1
+        
+            if not endline:
+                endline = -1    
+        else:
+            issectionedit = 0
 
+        if issectionedit:
+            # need to add config
+            self._allow_section_edit = self.cfg.allow_section_edit
+            # self._allow_section_edit = 1
+            
         from MoinMoin.formatter.text_html import Formatter
         self.request.formatter = Formatter(self.request, store_pagelinks=1)
 
@@ -174,7 +195,10 @@
             title = _('Edit "%(pagename)s"')
         else:
             title = _('Preview of "%(pagename)s"')
-            self.set_raw_body(preview, modified=1)
+            if issectionedit and pagetext is not None:
+                self.set_raw_body(pagetext, modified=1)
+            else:
+                self.set_raw_body(preview, modified=1)
 
         # send header stuff
         lock_timeout = self.lock.timeout / 60
@@ -214,9 +238,19 @@
                 # We don't show preview when in conflict
                 preview = None
                 
+                # no section-editing any more
+                if issectionedit:
+                    conflict_msg = u'%s %s' % (conflict_msg, _('Section editing is canceled.'))
+                issectionedit = 0
+
         elif self.exists():
             # revision of existing page
             rev = self.current_rev()
+            
+            if issectionedit and preview is None:
+                if not (srev and srev == rev):
+                    conflict_msg = u'%s %s' % (_('Section editing is canceled.'), _('Someone else updated this page. You are editing the updated revision.'))
+                    issectionedit = 0
         else:
             # page creation
             rev = 0
@@ -273,6 +307,12 @@
         # Generate default content for new pages
         if not raw_body:
             raw_body = _('Describe %s here.') % (self.page_name,)
+        elif issectionedit:
+            # for section editing
+            if pagetext is not None:
+                raw_body = preview
+            else:
+                raw_body = self.fetchsection(raw_body, startline, endline)
 
         # send form
         self.request.write('<form id="editor" method="post" action="%s/%s#preview" onSubmit="flgChange = false;">' % (
@@ -290,6 +330,11 @@
         # Send revision of the page our edit is based on
         self.request.write('<input type="hidden" name="rev" value="%d">' % (rev,))
 
+        # Send section startline and endline for section-editing
+        if issectionedit:
+            self.request.write('<input type="hidden" name="startline" value="%d">' % startline)
+            self.request.write('<input type="hidden" name="endline" value="%d">' % endline)
+        
         # Save backto in a hidden input
         backto = form.get('backto', [None])[0]
         if backto:
@@ -391,6 +436,9 @@
 
         badwords_re = None
         if preview is not None:
+            if issectionedit:
+                self.set_raw_body(preview)
+                
             if SpellCheck and (
                     form.has_key('button_spellcheck') or
                     form.has_key('button_newwords')):
@@ -414,7 +462,12 @@
                 content_id = 'previewbelow'
             else:
                 content_id = 'preview'
-            self.send_page(self.request, content_id=content_id, content_only=1,
+
+            if issectionedit:
+                self.send_page(self.request, content_id=content_id, content_only=1,
+                           hilite_re=badwords_re, do_cache=False)
+            else:
+                self.send_page(self.request, content_id=content_id, content_only=1,
                            hilite_re=badwords_re)
 
         self.request.write(self.request.formatter.endContent())
@@ -860,7 +913,7 @@
                 if newtext==saved_page.get_raw_body():
                     msg = _("You already saved this page!")
                     return msg
-                
+
             msg = _("""Sorry, someone else saved the page while you edited it.
 
 Please do the following: Use the back button of your browser, and cut&paste
@@ -926,7 +979,17 @@
         self.lock.release(force=not msg) # XXX does "not msg" make any sense?
   
         return msg
-            
+
+    def fetchsection(self, pagetext, startline, endline):
+        
+        pagetext = pagetext.split('\n')
+        if endline == -1:
+            sectiontext = u'\n'.join(pagetext[startline-1:])
+        else:
+            sectiontext = u'\n'.join(pagetext[startline-1:endline])
+        
+        return sectiontext
+        
             
 class PageLock:
     """
