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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1
5574cfc2b28d Adding the sphinx builder script simplecomment
amit
parents:
diff changeset
     1
# -*- coding: utf-8 -*-
5574cfc2b28d Adding the sphinx builder script simplecomment
amit
parents:
diff changeset
     2
"""
5574cfc2b28d Adding the sphinx builder script simplecomment
amit
parents:
diff changeset
     3
    sphinx.builders.webapp
5574cfc2b28d Adding the sphinx builder script simplecomment
amit
parents:
diff changeset
     4
    ~~~~~~~~~~~~~~~~~~~~~~
5574cfc2b28d Adding the sphinx builder script simplecomment
amit
parents:
diff changeset
     5
5574cfc2b28d Adding the sphinx builder script simplecomment
amit
parents:
diff changeset
     6
    A web application builder.
5574cfc2b28d Adding the sphinx builder script simplecomment
amit
parents:
diff changeset
     7
5574cfc2b28d Adding the sphinx builder script simplecomment
amit
parents:
diff changeset
     8
    :copyright: Copyright 2007-2009 by the Sphinx team, see AUTHORS.
5574cfc2b28d Adding the sphinx builder script simplecomment
amit
parents:
diff changeset
     9
    :license: BSD, see LICENSE for details.
5574cfc2b28d Adding the sphinx builder script simplecomment
amit
parents:
diff changeset
    10
"""
5574cfc2b28d Adding the sphinx builder script simplecomment
amit
parents:
diff changeset
    11
5574cfc2b28d Adding the sphinx builder script simplecomment
amit
parents:
diff changeset
    12
import os
5574cfc2b28d Adding the sphinx builder script simplecomment
amit
parents:
diff changeset
    13
import sys
5574cfc2b28d Adding the sphinx builder script simplecomment
amit
parents:
diff changeset
    14
import codecs
5574cfc2b28d Adding the sphinx builder script simplecomment
amit
parents:
diff changeset
    15
import shutil
5574cfc2b28d Adding the sphinx builder script simplecomment
amit
parents:
diff changeset
    16
import cPickle
5574cfc2b28d Adding the sphinx builder script simplecomment
amit
parents:
diff changeset
    17
from os import path
5574cfc2b28d Adding the sphinx builder script simplecomment
amit
parents:
diff changeset
    18
from hashlib import md5
5574cfc2b28d Adding the sphinx builder script simplecomment
amit
parents:
diff changeset
    19
from types import ListType, TupleType
5574cfc2b28d Adding the sphinx builder script simplecomment
amit
parents:
diff changeset
    20
import re
5574cfc2b28d Adding the sphinx builder script simplecomment
amit
parents:
diff changeset
    21
5574cfc2b28d Adding the sphinx builder script simplecomment
amit
parents:
diff changeset
    22
5574cfc2b28d Adding the sphinx builder script simplecomment
amit
parents:
diff changeset
    23
import jinja2 as j2
5574cfc2b28d Adding the sphinx builder script simplecomment
amit
parents:
diff changeset
    24
5574cfc2b28d Adding the sphinx builder script simplecomment
amit
parents:
diff changeset
    25
#from mercurial import commands, ui
5574cfc2b28d Adding the sphinx builder script simplecomment
amit
parents:
diff changeset
    26
#from mercurial.hg import repository
5574cfc2b28d Adding the sphinx builder script simplecomment
amit
parents:
diff changeset
    27
5574cfc2b28d Adding the sphinx builder script simplecomment
amit
parents:
diff changeset
    28
from sphinx.builders.html import StandaloneHTMLBuilder
5574cfc2b28d Adding the sphinx builder script simplecomment
amit
parents:
diff changeset
    29
from sphinx.errors import SphinxError
5574cfc2b28d Adding the sphinx builder script simplecomment
amit
parents:
diff changeset
    30
from sphinx.web.dbutils import PidDb
5574cfc2b28d Adding the sphinx builder script simplecomment
amit
parents:
diff changeset
    31
from sphinx.web.webconfig import WebConfig
5574cfc2b28d Adding the sphinx builder script simplecomment
amit
parents:
diff changeset
    32
from sphinx.writers.html import HTMLTranslator
5574cfc2b28d Adding the sphinx builder script simplecomment
amit
parents:
diff changeset
    33
5574cfc2b28d Adding the sphinx builder script simplecomment
amit
parents:
diff changeset
    34
5574cfc2b28d Adding the sphinx builder script simplecomment
amit
parents:
diff changeset
    35
5574cfc2b28d Adding the sphinx builder script simplecomment
amit
parents:
diff changeset
    36
5574cfc2b28d Adding the sphinx builder script simplecomment
amit
parents:
diff changeset
    37
5574cfc2b28d Adding the sphinx builder script simplecomment
amit
parents:
diff changeset
    38
5574cfc2b28d Adding the sphinx builder script simplecomment
amit
parents:
diff changeset
    39
class SimpleCommentHTMLBuilder(StandaloneHTMLBuilder):
5574cfc2b28d Adding the sphinx builder script simplecomment
amit
parents:
diff changeset
    40
    """ Static html pages with paragraph ids. 
5574cfc2b28d Adding the sphinx builder script simplecomment
amit
parents:
diff changeset
    41
    """
5574cfc2b28d Adding the sphinx builder script simplecomment
amit
parents:
diff changeset
    42
    def init(self):
5574cfc2b28d Adding the sphinx builder script simplecomment
amit
parents:
diff changeset
    43
        self.app.add_javascript('simplecomment.js')
5574cfc2b28d Adding the sphinx builder script simplecomment
amit
parents:
diff changeset
    44
        self.id_file_loc=os.path.join(self.outdir,'paragraph_id.py')
5574cfc2b28d Adding the sphinx builder script simplecomment
amit
parents:
diff changeset
    45
        self.id_file=open(self.id_file_loc,'w')
5574cfc2b28d Adding the sphinx builder script simplecomment
amit
parents:
diff changeset
    46
        self.beginning="p_list= "
5574cfc2b28d Adding the sphinx builder script simplecomment
amit
parents:
diff changeset
    47
        self.id_dictionary={}
5574cfc2b28d Adding the sphinx builder script simplecomment
amit
parents:
diff changeset
    48
5574cfc2b28d Adding the sphinx builder script simplecomment
amit
parents:
diff changeset
    49
        StandaloneHTMLBuilder.init(self)
5574cfc2b28d Adding the sphinx builder script simplecomment
amit
parents:
diff changeset
    50
5574cfc2b28d Adding the sphinx builder script simplecomment
amit
parents:
diff changeset
    51
    def get_target_uri(self, docname, typ=None):
5574cfc2b28d Adding the sphinx builder script simplecomment
amit
parents:
diff changeset
    52
        return docname + self.link_suffix
5574cfc2b28d Adding the sphinx builder script simplecomment
amit
parents:
diff changeset
    53
5574cfc2b28d Adding the sphinx builder script simplecomment
amit
parents:
diff changeset
    54
    def write_doc(self, docname, doctree):
5574cfc2b28d Adding the sphinx builder script simplecomment
amit
parents:
diff changeset
    55
        StandaloneHTMLBuilder.write_doc(self, docname, doctree)
5574cfc2b28d Adding the sphinx builder script simplecomment
amit
parents:
diff changeset
    56
5574cfc2b28d Adding the sphinx builder script simplecomment
amit
parents:
diff changeset
    57
    def prepare_writing(self, docnames):
5574cfc2b28d Adding the sphinx builder script simplecomment
amit
parents:
diff changeset
    58
        StandaloneHTMLBuilder.prepare_writing(self, docnames)
5574cfc2b28d Adding the sphinx builder script simplecomment
amit
parents:
diff changeset
    59
5574cfc2b28d Adding the sphinx builder script simplecomment
amit
parents:
diff changeset
    60
    def finish(self):
5574cfc2b28d Adding the sphinx builder script simplecomment
amit
parents:
diff changeset
    61
        StandaloneHTMLBuilder.finish(self)
5574cfc2b28d Adding the sphinx builder script simplecomment
amit
parents:
diff changeset
    62
        self.add_pids_to_paragraphs()
5574cfc2b28d Adding the sphinx builder script simplecomment
amit
parents:
diff changeset
    63
        self.id_file.write(self.beginning+str(self.id_dictionary))
5574cfc2b28d Adding the sphinx builder script simplecomment
amit
parents:
diff changeset
    64
       
5574cfc2b28d Adding the sphinx builder script simplecomment
amit
parents:
diff changeset
    65
5574cfc2b28d Adding the sphinx builder script simplecomment
amit
parents:
diff changeset
    66
    def add_pids_to_paragraphs(self):
5574cfc2b28d Adding the sphinx builder script simplecomment
amit
parents:
diff changeset
    67
        all_files=[]
5574cfc2b28d Adding the sphinx builder script simplecomment
amit
parents:
diff changeset
    68
        for path,dir,filenames in os.walk(self.outdir):
5574cfc2b28d Adding the sphinx builder script simplecomment
amit
parents:
diff changeset
    69
            for filename in filenames:
5574cfc2b28d Adding the sphinx builder script simplecomment
amit
parents:
diff changeset
    70
                all_files.append(os.path.join(path, filename))
5574cfc2b28d Adding the sphinx builder script simplecomment
amit
parents:
diff changeset
    71
5574cfc2b28d Adding the sphinx builder script simplecomment
amit
parents:
diff changeset
    72
            for element in all_files :
5574cfc2b28d Adding the sphinx builder script simplecomment
amit
parents:
diff changeset
    73
                if element.split('.')[1]=='html':
5574cfc2b28d Adding the sphinx builder script simplecomment
amit
parents:
diff changeset
    74
                    self.id_list=[]
5574cfc2b28d Adding the sphinx builder script simplecomment
amit
parents:
diff changeset
    75
                    self.taggen(element)
5574cfc2b28d Adding the sphinx builder script simplecomment
amit
parents:
diff changeset
    76
                    self.id_dictionary[self.chapter.split('/')[-1]]=self.id_list
5574cfc2b28d Adding the sphinx builder script simplecomment
amit
parents:
diff changeset
    77
                
5574cfc2b28d Adding the sphinx builder script simplecomment
amit
parents:
diff changeset
    78
                else :
5574cfc2b28d Adding the sphinx builder script simplecomment
amit
parents:
diff changeset
    79
                    pass
5574cfc2b28d Adding the sphinx builder script simplecomment
amit
parents:
diff changeset
    80
5574cfc2b28d Adding the sphinx builder script simplecomment
amit
parents:
diff changeset
    81
    def retag(self,s):
5574cfc2b28d Adding the sphinx builder script simplecomment
amit
parents:
diff changeset
    82
        self.biggest_id += 1
5574cfc2b28d Adding the sphinx builder script simplecomment
amit
parents:
diff changeset
    83
        id_name="%s_%x" % (self.chapter.split('/')[-1],self.biggest_id)   
5574cfc2b28d Adding the sphinx builder script simplecomment
amit
parents:
diff changeset
    84
        self.id_list.append(id_name) 
5574cfc2b28d Adding the sphinx builder script simplecomment
amit
parents:
diff changeset
    85
         
5574cfc2b28d Adding the sphinx builder script simplecomment
amit
parents:
diff changeset
    86
        return '<p id="%s">' %id_name
5574cfc2b28d Adding the sphinx builder script simplecomment
amit
parents:
diff changeset
    87
5574cfc2b28d Adding the sphinx builder script simplecomment
amit
parents:
diff changeset
    88
5574cfc2b28d Adding the sphinx builder script simplecomment
amit
parents:
diff changeset
    89
5574cfc2b28d Adding the sphinx builder script simplecomment
amit
parents:
diff changeset
    90
5574cfc2b28d Adding the sphinx builder script simplecomment
amit
parents:
diff changeset
    91
    def taggen(self,html_file_name):
5574cfc2b28d Adding the sphinx builder script simplecomment
amit
parents:
diff changeset
    92
        tagged = re.compile('<p id="x_([0-9a-f]+)"[^>]*>', re.M)
5574cfc2b28d Adding the sphinx builder script simplecomment
amit
parents:
diff changeset
    93
        self.biggest_id=0 
5574cfc2b28d Adding the sphinx builder script simplecomment
amit
parents:
diff changeset
    94
        self.chapter=html_file_name.split('.')[0]    
5574cfc2b28d Adding the sphinx builder script simplecomment
amit
parents:
diff changeset
    95
       
5574cfc2b28d Adding the sphinx builder script simplecomment
amit
parents:
diff changeset
    96
        try:    
5574cfc2b28d Adding the sphinx builder script simplecomment
amit
parents:
diff changeset
    97
            f = open(html_file_name).read()
5574cfc2b28d Adding the sphinx builder script simplecomment
amit
parents:
diff changeset
    98
            f1 = re.sub('<p>',self.retag, f )
5574cfc2b28d Adding the sphinx builder script simplecomment
amit
parents:
diff changeset
    99
            
5574cfc2b28d Adding the sphinx builder script simplecomment
amit
parents:
diff changeset
   100
           
5574cfc2b28d Adding the sphinx builder script simplecomment
amit
parents:
diff changeset
   101
            if f1 != f:
5574cfc2b28d Adding the sphinx builder script simplecomment
amit
parents:
diff changeset
   102
                tmpname = html_file_name+ '.tmp'
5574cfc2b28d Adding the sphinx builder script simplecomment
amit
parents:
diff changeset
   103
                fp = open(tmpname, 'w')
5574cfc2b28d Adding the sphinx builder script simplecomment
amit
parents:
diff changeset
   104
                fp.write(f1)
5574cfc2b28d Adding the sphinx builder script simplecomment
amit
parents:
diff changeset
   105
                fp.close()
5574cfc2b28d Adding the sphinx builder script simplecomment
amit
parents:
diff changeset
   106
                os.rename(tmpname,html_file_name)
5574cfc2b28d Adding the sphinx builder script simplecomment
amit
parents:
diff changeset
   107
        
5574cfc2b28d Adding the sphinx builder script simplecomment
amit
parents:
diff changeset
   108
        except IOError:
5574cfc2b28d Adding the sphinx builder script simplecomment
amit
parents:
diff changeset
   109
            pass
5574cfc2b28d Adding the sphinx builder script simplecomment
amit
parents:
diff changeset
   110
            
5574cfc2b28d Adding the sphinx builder script simplecomment
amit
parents:
diff changeset
   111
5574cfc2b28d Adding the sphinx builder script simplecomment
amit
parents:
diff changeset
   112