1. Wojtek Walczak
  2. sphinx-gsoc2009

Commits

Wojciech Walczak  committed 1d20fcd

Add prefixing paragraphs with paragraph IDs

  • Participants
  • Parent commits 143fa02
  • Branches default

Comments (0)

Files changed (1)

File sphinx/builders/webapp/webapp.py

View file
  • Ignore whitespace
 
 import os
 import sys
+import codecs
 import shutil
 import cPickle
 from os import path
 
 class WebAppBuilder(StandaloneHTMLBuilder):
     def init(self):
+        self.encoding = self.config.html_output_encoding or 'utf8'
+
         self.orig_outdir = self.outdir
 
         # _build/webapp/public - ordinary HTML files produced by Sphinx
         path.walk(self.srcdir, callback, '')
 
     def add_pids_to_paragraphs(self):
+        def add_pids_to_lines(filename):
+            dirbase = path.join(self.orig_outdir, self.webconfig.repodir)
+            fullpath = path.join(dirbase, filename)
+            try:
+                w = codecs.open(fullpath, 'r', self.encoding)
+            except Exception, err:
+                raise SphinxError(err)
+            contents = w.read()
+            # get MD5sums for paragraphs in file 'filename'
+            pids = self.piddb.get_pids(filename)
+            for pid in pids:
+                paragraph = self.piddb.get_paragraph(filename, pid)
+                new_p = '[%s]%s' % (pid, paragraph)
+                contents = contents.replace(paragraph, new_p)
+                # update the PidDb
+                self.piddb.add_record(filename, pid, new_p)
+            try:
+                w = codecs.open(fullpath, 'w', 'utf8')
+            except Exception, err:
+                raise SphinxError(err)
+            w.write(contents)
+            w.close()
+
         conffile = path.join(self.orig_outdir, 'webapp.conf')
-        dirbase = path.join(self.orig_outdir, self.webconfig.repodir)
         self.webconfig = WebConfig(conffile)
         self.piddb = PidDb(piddbfile)
         files = self.piddb.get_filenames()
         for f in files:
-            fullpath = path.join(dirbase, f)
-            print fullpath, path.isfile(fullpath)
-            # XXX: the logic for adding paragraph IDs to paragraphs in the
-            # repository goes here
+            add_pids_to_lines(f)
+
 
     ###
     ### general use functions
             data[filename][pid] = paragraph
         self.dbsave(self.dbfile, data)
 
-    def get_record(self, filename, pid):
+    def get_paragraph(self, filename, pid):
         data = self.dbread(self.dbfile)
         if filename in data and pid in data[filename]:
             return data[filename][pid]
         return None
 
+    def get_pids(self, filename):
+        data = self.dbread(self.dbfile)
+        return data.get(filename)
+
     def get_filenames(self):
         data = self.dbread(self.dbfile)
         return filter(lambda x: x.endswith('.rst'), data.keys())