simplecomment.py
author amit
Thu, 30 Sep 2010 15:59:32 +0530
changeset 1 5574cfc2b28d
permissions -rwxr-xr-x
Adding the sphinx builder script simplecomment

# -*- coding: utf-8 -*-
"""
    sphinx.builders.webapp
    ~~~~~~~~~~~~~~~~~~~~~~

    A web application builder.

    :copyright: Copyright 2007-2009 by the Sphinx team, see AUTHORS.
    :license: BSD, see LICENSE for details.
"""

import os
import sys
import codecs
import shutil
import cPickle
from os import path
from hashlib import md5
from types import ListType, TupleType
import re


import jinja2 as j2

#from mercurial import commands, ui
#from mercurial.hg import repository

from sphinx.builders.html import StandaloneHTMLBuilder
from sphinx.errors import SphinxError
from sphinx.web.dbutils import PidDb
from sphinx.web.webconfig import WebConfig
from sphinx.writers.html import HTMLTranslator






class SimpleCommentHTMLBuilder(StandaloneHTMLBuilder):
    """ Static html pages with paragraph ids. 
    """
    def init(self):
        self.app.add_javascript('simplecomment.js')
        self.id_file_loc=os.path.join(self.outdir,'paragraph_id.py')
        self.id_file=open(self.id_file_loc,'w')
        self.beginning="p_list= "
        self.id_dictionary={}

        StandaloneHTMLBuilder.init(self)

    def get_target_uri(self, docname, typ=None):
        return docname + self.link_suffix

    def write_doc(self, docname, doctree):
        StandaloneHTMLBuilder.write_doc(self, docname, doctree)

    def prepare_writing(self, docnames):
        StandaloneHTMLBuilder.prepare_writing(self, docnames)

    def finish(self):
        StandaloneHTMLBuilder.finish(self)
        self.add_pids_to_paragraphs()
        self.id_file.write(self.beginning+str(self.id_dictionary))
       

    def add_pids_to_paragraphs(self):
        all_files=[]
        for path,dir,filenames in os.walk(self.outdir):
            for filename in filenames:
                all_files.append(os.path.join(path, filename))

            for element in all_files :
                if element.split('.')[1]=='html':
                    self.id_list=[]
                    self.taggen(element)
                    self.id_dictionary[self.chapter.split('/')[-1]]=self.id_list
                
                else :
                    pass

    def retag(self,s):
        self.biggest_id += 1
        id_name="%s_%x" % (self.chapter.split('/')[-1],self.biggest_id)   
        self.id_list.append(id_name) 
         
        return '<p id="%s">' %id_name




    def taggen(self,html_file_name):
        tagged = re.compile('<p id="x_([0-9a-f]+)"[^>]*>', re.M)
        self.biggest_id=0 
        self.chapter=html_file_name.split('.')[0]    
       
        try:    
            f = open(html_file_name).read()
            f1 = re.sub('<p>',self.retag, f )
            
           
            if f1 != f:
                tmpname = html_file_name+ '.tmp'
                fp = open(tmpname, 'w')
                fp.write(f1)
                fp.close()
                os.rename(tmpname,html_file_name)
        
        except IOError:
            pass