[Zope-Checkins] CVS: Zope/lib/python/App - ProductContext.py:1.42.22.1

Jeffrey P Shell jeffrey@cuemedia.com
Wed, 13 Nov 2002 17:01:24 -0500


Update of /cvs-repository/Zope/lib/python/App
In directory cvs.zope.org:/tmp/cvs-serv13071

Modified Files:
      Tag: ajung-restructuredtext-integration-branch
	ProductContext.py 
Log Message:
Added support in ``registerHelp()`` for reStructuredText help topic
files with the ``.rst`` extension.  Includes some simple support for
ReST titling conventions.


=== Zope/lib/python/App/ProductContext.py 1.42 => 1.42.22.1 ===
--- Zope/lib/python/App/ProductContext.py:1.42	Tue Aug 20 15:37:52 2002
+++ Zope/lib/python/App/ProductContext.py	Wed Nov 13 17:01:24 2002
@@ -35,6 +35,12 @@
 
 _marker = []  # Create a new marker object
 
+## _punc_line is used to catch lines of the character set
+## [=-`~], commonly used to surround the first headers in
+## reStructuredText.  This regex matches two or more such
+## characters spanning an entire line of text.
+_punc_line = re.compile(r"^[=-`~]{2,}$")
+
 class ProductContext:
 
     def __init__(self, product, app, package):
@@ -277,13 +283,14 @@
         .dtml            -- DTMLHelpTopic
         .html .htm       -- TextHelpTopic
         .stx .txt        -- STXHelpTopic
+        .rst             -- ReSTHelpTopic (reStructuredText)
         .jpg .png .gif   -- ImageHelpTopic
         .py              -- APIHelpTopic
         """
 
         if not doInstall():
             return
-        
+
         help=self.getProductHelp()
         path=os.path.join(Globals.package_home(self.__pack.__dict__),
                           directory)
@@ -329,6 +336,31 @@
             elif ext in ('.stx', '.txt'):
                 title=(open(os.path.join(path,file),'rb').readline()).split(':')[0]
                 ht=HelpTopic.STXTopic(file, title, os.path.join(path, file))
+                self.registerHelpTopic(file, ht)
+            elif ext in ('.rst',):
+                """
+                Restructured Text Support
+                =========================
+                
+                Try to find the title out of a reStructuredText
+                document.  It would probably be better to do at least
+                some parsing of the ReST document and use its DOM to
+                get the title, but it would probably be too expensive
+                to parse the file twice.  Just read the first line(s)
+                looking for a title.
+                """
+                fullpath = os.path.join(path, file)
+                f = open(fullpath, 'rb')
+                while 1:
+                    line = f.readline().strip()
+                    if _punc_line.match(line):
+                        ## Line looks like '============'
+                        continue
+                    elif line:
+                        title = line.split(':')[0]
+                        break
+                f.close()
+                ht = HelpTopic.ReSTTopic(file, title, os.path.join(path, file))
                 self.registerHelpTopic(file, ht)
             elif ext in ('.jpg', '.gif', '.png'):
                 ht=HelpTopic.ImageTopic(file, '', os.path.join(path, file))