--- text_html-1.5b2.py	2005-10-31 08:39:10.000000000 +0900
+++ text_html.py	2005-11-24 11:11:23.384242300 +0900
@@ -39,6 +39,16 @@
         self._is_included = kw.get('is_included',False)
         self.request = request
         self.cfg = request.cfg
+        
+        # for section editing
+        self._allow_section_edit = None
+        if not hasattr(request, 'sectionindex'):
+            request.sectionindex = 0
+        
+        self.sectionstack = []
+        self.sectionpool = {}
+        self.sectionlastdepth = 0
+        self.lineno = 0
 
         if not hasattr(request, '_fmt_hd_counters'):
             request._fmt_hd_counters = []
@@ -285,6 +295,7 @@
         return '<a id="%s"></a>' % (id, ) # do not add a \n here, it breaks pre sections with line_anchordef
 
     def line_anchordef(self, lineno):
+        self.lineno = lineno
         return self.anchordef("line-%d" % lineno)
 
     def anchorlink(self, on, name='', id=None):
@@ -689,6 +700,21 @@
         if not self._base_depth:
             self._base_depth = depth
 
+        # section editing configuration
+        if self._allow_section_edit is None:
+            # need to add config
+            self._allow_section_edit = self.cfg.allow_section_edit
+            # self._allow_section_edit = 1
+            sectionediting = self.request.getPragma('section-edit', '').lower()
+            if sectionediting in ['off']:
+                self._allow_section_edit = 0
+            elif sectionediting in ['on']:
+                self._allow_section_edit = 1
+        
+        if self._allow_section_edit:
+            if not self.request.user.may.write(self.page.page_name):
+                self._allow_section_edit = 0
+        
         count_depth = max(depth - (self._base_depth - 1), 1)
 
         # check numbering, possibly changing the default
@@ -707,7 +733,32 @@
 
         # closing tag, with empty line after, to make source more readable
         if not on:
-            return self.close('h%d' % heading_depth) + '\n'
+            result = self.close('h%d' % heading_depth)
+            
+            sectionscript = ''
+            if self._allow_section_edit:
+                
+                self.request.sectionindex += 1
+                sectionindex = self.request.sectionindex
+                sectionscript = self.savesectioninfor(sectionindex, depth, self.lineno)
+                
+                attr = 'id="sectionedit%d"' % sectionindex
+                
+                backto = u''
+                if self._is_included and hasattr(self.request, "_Include_backto"):
+                    backto = u'&backto=%s' % wikiutil.quoteWikinameURL(self.request._Include_backto)
+                
+                sectioneditpage = u'%s/%s' % (self.request.getScriptname(), wikiutil.quoteWikinameURL(self.page.page_name))
+                srev = self.page.current_rev()
+                querystring = u'%s?action=edit%s&srev=%d&startline=%d' % (sectioneditpage, backto, srev, self.lineno)
+                
+                result = ('%s%s%s%s' %
+                    (self.url(1, querystring, unescaped=1, title='Edit this section', attrs=attr),
+                     self.icon('edit'),
+                     self.url(0),
+                     result))
+
+            return ' %s%s' % (result, sectionscript)
             
         # create section number
         number = ''
@@ -840,6 +891,53 @@
             return self.open(tag, newline=1, attr=attrs)
         return self.close(tag)
 
+    def savesectioninfor(self, index, depth, lineno, save=1):
+        # store section information
+        section = {}
+        sectionscriptlist = []
+        sectionscript = u'document.getElementById("sectionedit%d").href += "&endline=%d";'
+        scriptresult = ''
+        
+        lastdepth = self.sectionlastdepth
+        sectionindex = index
+        
+        if lastdepth >= depth:
+            while 1:
+                if len(self.sectionstack):
+                    lastsection = self.sectionstack.pop()
+                    lastdepth = lastsection['depth']    
+                    if lastdepth >= depth:
+                        self.sectionpool[lastsection['index']]['endlineno'] = lineno - 1
+                        sectionscriptlist.append(sectionscript % (lastsection['index'], lineno - 1))
+                    else:
+                        self.sectionstack.append(lastsection)
+                        break
+                else:
+                    break
+                    
+        if save:
+            section['index'] = sectionindex
+            section['startlineno'] = lineno
+            section['depth'] = depth
+            section['endlineno'] = -1
+            self.sectionlastdepth = depth
+            self.sectionpool[sectionindex] = section
+            self.sectionstack.append(section)
+        
+        if len(sectionscriptlist) > 0:
+            
+            scriptresult = u"""
+<script type="text/javascript">
+<!--
+%s
+//-->
+</script>
+""" % u'\n'.join(sectionscriptlist)
+
+        return scriptresult
+
+        
+
     def escapedText(self, text):
         return wikiutil.escape(text)
 
