<s> from itertools import count <EOL> import re , os , cStringIO , time , cgi , urlparse <EOL> from xml . dom import minidom as dom <EOL> from xml . sax . handler import ErrorHandler , feature_validation <EOL> from xml . dom . pulldom import SAX2DOM <EOL> from xml . sax import make_parser <EOL> from xml . sax . xmlreader import InputSource <EOL> from twisted . python import htmlizer <EOL> from twisted . python . filepath import FilePath <EOL> from twisted . web import domhelpers <EOL> import process , latex , indexer , numberer , htmlbook <EOL> def fixLinks ( document , ext ) : <EOL> """<STR_LIT>""" <EOL> supported_schemes = [ '<STR_LIT:http>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> for node in domhelpers . findElementsWithAttribute ( document , '<STR_LIT>' ) : <EOL> href = node . getAttribute ( "<STR_LIT>" ) <EOL> if urlparse . urlparse ( href ) [ <NUM_LIT:0> ] in supported_schemes : <EOL> continue <EOL> if node . getAttribute ( "<STR_LIT:class>" ) == "<STR_LIT>" : <EOL> continue <EOL> if node . getAttribute ( "<STR_LIT:class>" ) . find ( '<STR_LIT>' ) != - <NUM_LIT:1> : <EOL> continue <EOL> if href . endswith ( '<STR_LIT:html>' ) or href [ : href . rfind ( '<STR_LIT:#>' ) ] . endswith ( '<STR_LIT:html>' ) : <EOL> fname , fext = os . path . splitext ( href ) <EOL> if '<STR_LIT:#>' in fext : <EOL> fext = ext + '<STR_LIT:#>' + fext . split ( '<STR_LIT:#>' , <NUM_LIT:1> ) [ <NUM_LIT:1> ] <EOL> else : <EOL> fext = ext <EOL> node . setAttribute ( "<STR_LIT>" , fname + fext ) <EOL> def addMtime ( document , fullpath ) : <EOL> """<STR_LIT>""" <EOL> for node in domhelpers . findElementsWithAttribute ( document , "<STR_LIT:class>" , "<STR_LIT>" ) : <EOL> txt = dom . Text ( ) <EOL> txt . data = time . ctime ( os . path . getmtime ( fullpath ) ) <EOL> node . appendChild ( txt ) <EOL> def _getAPI ( node ) : <EOL> """<STR_LIT>""" <EOL> base = "<STR_LIT>" <EOL> if node . hasAttribute ( "<STR_LIT>" ) : <EOL> base = node . getAttribute ( "<STR_LIT>" ) + "<STR_LIT:.>" <EOL> return base + node . childNodes [ <NUM_LIT:0> ] . nodeValue <EOL> def fixAPI ( document , url ) : <EOL> """<STR_LIT>""" <EOL> for node in domhelpers . findElementsWithAttribute ( document , "<STR_LIT:class>" , "<STR_LIT>" ) : <EOL> fullname = _getAPI ( node ) <EOL> anchor = dom . Element ( '<STR_LIT:a>' ) <EOL> anchor . setAttribute ( '<STR_LIT>' , url % ( fullname , ) ) <EOL> anchor . setAttribute ( '<STR_LIT:title>' , fullname ) <EOL> while node . childNodes : <EOL> child = node . childNodes [ <NUM_LIT:0> ] <EOL> node . removeChild ( child ) <EOL> anchor . appendChild ( child ) <EOL> node . appendChild ( anchor ) <EOL> if node . hasAttribute ( '<STR_LIT>' ) : <EOL> node . removeAttribute ( '<STR_LIT>' ) <EOL> def fontifyPython ( document ) : <EOL> """<STR_LIT>""" <EOL> def matcher ( node ) : <EOL> return ( node . nodeName == '<STR_LIT>' and node . hasAttribute ( '<STR_LIT:class>' ) and <EOL> node . getAttribute ( '<STR_LIT:class>' ) == '<STR_LIT>' ) <EOL> for node in domhelpers . findElements ( document , matcher ) : <EOL> fontifyPythonNode ( node ) <EOL> def fontifyPythonNode ( node ) : <EOL> """<STR_LIT>""" <EOL> oldio = cStringIO . StringIO ( ) <EOL> latex . getLatexText ( node , oldio . write , <EOL> entities = { '<STR_LIT>' : '<STR_LIT:<>' , '<STR_LIT>' : '<STR_LIT:>>' , '<STR_LIT>' : '<STR_LIT:&>' } ) <EOL> oldio = cStringIO . StringIO ( oldio . getvalue ( ) . strip ( ) + '<STR_LIT:\n>' ) <EOL> howManyLines = len ( oldio . getvalue ( ) . splitlines ( ) ) <EOL> newio = cStringIO . StringIO ( ) <EOL> htmlizer . filter ( oldio , newio , writer = htmlizer . SmallerHTMLWriter ) <EOL> lineLabels = _makeLineNumbers ( howManyLines ) <EOL> newel = dom . parseString ( newio . getvalue ( ) ) . documentElement <EOL> newel . setAttribute ( "<STR_LIT:class>" , "<STR_LIT>" ) <EOL> node . parentNode . replaceChild ( newel , node ) <EOL> newel . insertBefore ( lineLabels , newel . firstChild ) <EOL> def addPyListings ( document , dir ) : <EOL> """<STR_LIT>""" <EOL> for node in domhelpers . findElementsWithAttribute ( document , "<STR_LIT:class>" , <EOL> "<STR_LIT>" ) : <EOL> filename = node . getAttribute ( "<STR_LIT>" ) <EOL> outfile = cStringIO . StringIO ( ) <EOL> lines = map ( str . rstrip , open ( os . path . join ( dir , filename ) ) . readlines ( ) ) <EOL> skip = node . getAttribute ( '<STR_LIT>' ) or <NUM_LIT:0> <EOL> lines = lines [ int ( skip ) : ] <EOL> howManyLines = len ( lines ) <EOL> data = '<STR_LIT:\n>' . join ( lines ) <EOL> data = cStringIO . StringIO ( _removeLeadingTrailingBlankLines ( data ) ) <EOL> htmlizer . filter ( data , outfile , writer = htmlizer . SmallerHTMLWriter ) <EOL> sourceNode = dom . parseString ( outfile . getvalue ( ) ) . documentElement <EOL> sourceNode . insertBefore ( _makeLineNumbers ( howManyLines ) , sourceNode . firstChild ) <EOL> _replaceWithListing ( node , sourceNode . toxml ( ) , filename , "<STR_LIT>" ) <EOL> def _makeLineNumbers ( howMany ) : <EOL> """<STR_LIT>""" <EOL> width = len ( str ( howMany ) ) <EOL> labels = [ '<STR_LIT>' % ( width , i ) for i in range ( <NUM_LIT:1> , howMany + <NUM_LIT:1> ) ] <EOL> p = dom . Element ( '<STR_LIT:p>' ) <EOL> p . setAttribute ( '<STR_LIT:class>' , '<STR_LIT>' ) <EOL> t = dom . Text ( ) <EOL> t . data = '<STR_LIT:\n>' . join ( labels ) + '<STR_LIT:\n>' <EOL> p . appendChild ( t ) <EOL> return p <EOL> def _replaceWithListing ( node , val , filename , class_ ) : <EOL> captionTitle = domhelpers . getNodeText ( node ) <EOL> if captionTitle == os . path . basename ( filename ) : <EOL> captionTitle = '<STR_LIT>' <EOL> text = ( '<STR_LIT>' <EOL> '<STR_LIT>' % <EOL> ( class_ , val , captionTitle , filename , filename ) ) <EOL> newnode = dom . parseString ( text ) . documentElement <EOL> node . parentNode . replaceChild ( newnode , node ) <EOL> def _removeLeadingBlankLines ( lines ) : <EOL> """<STR_LIT>""" <EOL> ret = [ ] <EOL> for line in lines : <EOL> if ret or line . strip ( ) : <EOL> ret . append ( line ) <EOL> return ret <EOL> def _removeLeadingTrailingBlankLines ( inputString ) : <EOL> """<STR_LIT>""" <EOL> lines = _removeLeadingBlankLines ( inputString . split ( '<STR_LIT:\n>' ) ) <EOL> lines . reverse ( ) <EOL> lines = _removeLeadingBlankLines ( lines ) <EOL> lines . reverse ( ) <EOL> return '<STR_LIT:\n>' . join ( lines ) + '<STR_LIT:\n>' <EOL> def addHTMLListings ( document , dir ) : <EOL> """<STR_LIT>""" <EOL> for node in domhelpers . findElementsWithAttribute ( document , "<STR_LIT:class>" , <EOL> "<STR_LIT>" ) : <EOL> filename = node . getAttribute ( "<STR_LIT>" ) <EOL> val = ( '<STR_LIT>' % <EOL> cgi . escape ( open ( os . path . join ( dir , filename ) ) . read ( ) ) ) <EOL> _replaceWithListing ( node , val , filename , "<STR_LIT>" ) <EOL> def addPlainListings ( document , dir ) : <EOL> """<STR_LIT>""" <EOL> for node in domhelpers . findElementsWithAttribute ( document , "<STR_LIT:class>" , <EOL> "<STR_LIT>" ) : <EOL> filename = node . getAttribute ( "<STR_LIT>" ) <EOL> val = ( '<STR_LIT>' % <EOL> cgi . escape ( open ( os . path . join ( dir , filename ) ) . read ( ) ) ) <EOL> _replaceWithListing ( node , val , filename , "<STR_LIT>" ) <EOL> def getHeaders ( document ) : <EOL> """<STR_LIT>""" <EOL> return domhelpers . findElements ( <EOL> document , <EOL> lambda n , m = re . compile ( '<STR_LIT>' ) . match : m ( n . nodeName ) ) <EOL> def generateToC ( document ) : <EOL> """<STR_LIT>""" <EOL> subHeaders = None <EOL> headers = [ ] <EOL> for element in getHeaders ( document ) : <EOL> if element . tagName == '<STR_LIT>' : <EOL> subHeaders = [ ] <EOL> headers . append ( ( element , subHeaders ) ) <EOL> elif subHeaders is None : <EOL> raise ValueError ( <EOL> "<STR_LIT>" ) <EOL> else : <EOL> subHeaders . append ( element ) <EOL> auto = count ( ) . next <EOL> def addItem ( headerElement , parent ) : <EOL> anchor = dom . Element ( '<STR_LIT:a>' ) <EOL> name = '<STR_LIT>' % ( auto ( ) , ) <EOL> anchor . setAttribute ( '<STR_LIT>' , '<STR_LIT:#>' + name ) <EOL> text = dom . Text ( ) <EOL> text . data = domhelpers . getNodeText ( headerElement ) <EOL> anchor . appendChild ( text ) <EOL> headerNameItem = dom . Element ( '<STR_LIT>' ) <EOL> headerNameItem . appendChild ( anchor ) <EOL> parent . appendChild ( headerNameItem ) <EOL> anchor = dom . Element ( '<STR_LIT:a>' ) <EOL> anchor . setAttribute ( '<STR_LIT:name>' , name ) <EOL> headerElement . appendChild ( anchor ) <EOL> toc = dom . Element ( '<STR_LIT>' ) <EOL> for headerElement , subHeaders in headers : <EOL> addItem ( headerElement , toc ) <EOL> if subHeaders : <EOL> subtoc = dom . Element ( '<STR_LIT>' ) <EOL> toc . appendChild ( subtoc ) <EOL> for subHeaderElement in subHeaders : <EOL> addItem ( subHeaderElement , subtoc ) <EOL> return toc <EOL> def putInToC ( document , toc ) : <EOL> """<STR_LIT>""" <EOL> tocOrig = domhelpers . findElementsWithAttribute ( document , '<STR_LIT:class>' , '<STR_LIT>' ) <EOL> if tocOrig : <EOL> tocOrig = tocOrig [ <NUM_LIT:0> ] <EOL> tocOrig . childNodes = [ toc ] <EOL> def removeH1 ( document ) : <EOL> """<STR_LIT>""" <EOL> h1 = domhelpers . findNodesNamed ( document , '<STR_LIT>' ) <EOL> empty = dom . Element ( '<STR_LIT>' ) <EOL> for node in h1 : <EOL> node . parentNode . replaceChild ( empty , node ) <EOL> def footnotes ( document ) : <EOL> """<STR_LIT>""" <EOL> footnotes = domhelpers . findElementsWithAttribute ( document , "<STR_LIT:class>" , <EOL> "<STR_LIT>" ) <EOL> if not footnotes : <EOL> return <EOL> footnoteElement = dom . Element ( '<STR_LIT>' ) <EOL> id = <NUM_LIT:1> <EOL> for footnote in footnotes : <EOL> href = dom . parseString ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> % vars ( ) ) . documentElement <EOL> text = '<STR_LIT:U+0020>' . join ( domhelpers . getNodeText ( footnote ) . split ( ) ) <EOL> href . setAttribute ( '<STR_LIT:title>' , text ) <EOL> target = dom . Element ( '<STR_LIT:a>' ) <EOL> target . setAttribute ( '<STR_LIT:name>' , '<STR_LIT>' % ( id , ) ) <EOL> target . childNodes = [ footnote ] <EOL> footnoteContent = dom . Element ( '<STR_LIT>' ) <EOL> footnoteContent . childNodes = [ target ] <EOL> footnoteElement . childNodes . append ( footnoteContent ) <EOL> footnote . parentNode . replaceChild ( href , footnote ) <EOL> id += <NUM_LIT:1> <EOL> body = domhelpers . findNodesNamed ( document , "<STR_LIT:body>" ) [ <NUM_LIT:0> ] <EOL> header = dom . parseString ( '<STR_LIT>' ) . documentElement <EOL> body . childNodes . append ( header ) <EOL> body . childNodes . append ( footnoteElement ) <EOL> def notes ( document ) : <EOL> """<STR_LIT>""" <EOL> notes = domhelpers . findElementsWithAttribute ( document , "<STR_LIT:class>" , "<STR_LIT>" ) <EOL> notePrefix = dom . parseString ( '<STR_LIT>' ) . documentElement <EOL> for note in notes : <EOL> note . childNodes . insert ( <NUM_LIT:0> , notePrefix ) <EOL> def findNodeJustBefore ( target , nodes ) : <EOL> """<STR_LIT>""" <EOL> while target is not None : <EOL> node = target . previousSibling <EOL> while node is not None : <EOL> if node in nodes : <EOL> return node <EOL> node = node . previousSibling <EOL> target = target . parentNode <EOL> raise RuntimeError ( "<STR_LIT>" ) <EOL> def getFirstAncestorWithSectionHeader ( entry ) : <EOL> """<STR_LIT>""" <EOL> for a in domhelpers . getParents ( entry ) [ <NUM_LIT:1> : ] : <EOL> headers = domhelpers . findNodesNamed ( a , "<STR_LIT>" ) <EOL> if len ( headers ) > <NUM_LIT:0> : <EOL> return headers <EOL> return [ ] <EOL> def getSectionNumber ( header ) : <EOL> """<STR_LIT>""" <EOL> if not header : <EOL> return None <EOL> return domhelpers . gatherTextNodes ( header . childNodes [ <NUM_LIT:0> ] ) <EOL> def getSectionReference ( entry ) : <EOL> """<STR_LIT>""" <EOL> headers = getFirstAncestorWithSectionHeader ( entry ) <EOL> myHeader = findNodeJustBefore ( entry , headers ) <EOL> return getSectionNumber ( myHeader ) <EOL> def index ( document , filename , chapterReference ) : <EOL> """<STR_LIT>""" <EOL> entries = domhelpers . findElementsWithAttribute ( document , "<STR_LIT:class>" , "<STR_LIT:index>" ) <EOL> if not entries : <EOL> return <EOL> i = <NUM_LIT:0> ; <EOL> for entry in entries : <EOL> i += <NUM_LIT:1> <EOL> anchor = '<STR_LIT>' % i <EOL> if chapterReference : <EOL> ref = getSectionReference ( entry ) or chapterReference <EOL> else : <EOL> ref = '<STR_LIT>' <EOL> indexer . addEntry ( filename , anchor , entry . getAttribute ( '<STR_LIT:value>' ) , ref ) <EOL> entry . nodeName = entry . tagName = entry . endTagName = '<STR_LIT:a>' <EOL> for attrName in entry . attributes . keys ( ) : <EOL> entry . removeAttribute ( attrName ) <EOL> entry . setAttribute ( '<STR_LIT:name>' , anchor ) <EOL> def setIndexLink ( template , indexFilename ) : <EOL> """<STR_LIT>""" <EOL> indexLinks = domhelpers . findElementsWithAttribute ( template , <EOL> "<STR_LIT:class>" , <EOL> "<STR_LIT>" ) <EOL> for link in indexLinks : <EOL> if indexFilename is None : <EOL> link . parentNode . removeChild ( link ) <EOL> else : <EOL> link . nodeName = link . tagName = link . endTagName = '<STR_LIT:a>' <EOL> for attrName in link . attributes . keys ( ) : <EOL> link . removeAttribute ( attrName ) <EOL> link . setAttribute ( '<STR_LIT>' , indexFilename ) <EOL> def numberDocument ( document , chapterNumber ) : <EOL> """<STR_LIT>""" <EOL> i = <NUM_LIT:1> <EOL> for node in domhelpers . findNodesNamed ( document , "<STR_LIT>" ) : <EOL> label = dom . Text ( ) <EOL> label . data = "<STR_LIT>" % ( chapterNumber , i ) <EOL> node . insertBefore ( label , node . firstChild ) <EOL> i += <NUM_LIT:1> <EOL> def fixRelativeLinks ( document , linkrel ) : <EOL> """<STR_LIT>""" <EOL> for attr in '<STR_LIT:src>' , '<STR_LIT>' : <EOL> for node in domhelpers . findElementsWithAttribute ( document , attr ) : <EOL> href = node . getAttribute ( attr ) <EOL> if not href . startswith ( '<STR_LIT:http>' ) and not href . startswith ( '<STR_LIT:/>' ) : <EOL> node . setAttribute ( attr , linkrel + node . getAttribute ( attr ) ) <EOL> def setTitle ( template , title , chapterNumber ) : <EOL> """<STR_LIT>""" <EOL> if numberer . getNumberSections ( ) and chapterNumber : <EOL> titleNode = dom . Text ( ) <EOL> titleNode . ownerDocument = template . ownerDocument <EOL> titleNode . data = '<STR_LIT>' % ( chapterNumber , ) <EOL> title . insert ( <NUM_LIT:0> , titleNode ) <EOL> for nodeList in ( domhelpers . findNodesNamed ( template , "<STR_LIT:title>" ) , <EOL> domhelpers . findElementsWithAttribute ( template , "<STR_LIT:class>" , <EOL> '<STR_LIT:title>' ) ) : <EOL> if nodeList : <EOL> for titleNode in title : <EOL> nodeList [ <NUM_LIT:0> ] . appendChild ( titleNode . cloneNode ( True ) ) <EOL> def setAuthors ( template , authors ) : <EOL> """<STR_LIT>""" <EOL> for node in domhelpers . findElementsWithAttribute ( template , <EOL> "<STR_LIT:class>" , '<STR_LIT>' ) : <EOL> container = dom . Element ( '<STR_LIT>' ) <EOL> for name , href in authors : <EOL> anchor = dom . Element ( '<STR_LIT:a>' ) <EOL> anchor . setAttribute ( '<STR_LIT>' , href ) <EOL> anchorText = dom . Text ( ) <EOL> anchorText . data = name <EOL> anchor . appendChild ( anchorText ) <EOL> if ( name , href ) == authors [ - <NUM_LIT:1> ] : <EOL> if len ( authors ) == <NUM_LIT:1> : <EOL> container . appendChild ( anchor ) <EOL> else : <EOL> andText = dom . Text ( ) <EOL> andText . data = '<STR_LIT>' <EOL> container . appendChild ( andText ) <EOL> container . appendChild ( anchor ) <EOL> else : <EOL> container . appendChild ( anchor ) <EOL> commaText = dom . Text ( ) <EOL> commaText . data = '<STR_LIT:U+002CU+0020>' <EOL> container . appendChild ( commaText ) <EOL> node . appendChild ( container ) <EOL> head = domhelpers . findNodesNamed ( template , '<STR_LIT>' ) [ <NUM_LIT:0> ] <EOL> authors = [ dom . parseString ( '<STR_LIT>' <EOL> % ( href , name ) ) . childNodes [ <NUM_LIT:0> ] <EOL> for name , href in authors ] <EOL> head . childNodes . extend ( authors ) <EOL> def setVersion ( template , version ) : <EOL> """<STR_LIT>""" <EOL> for node in domhelpers . findElementsWithAttribute ( template , "<STR_LIT:class>" , <EOL> "<STR_LIT:version>" ) : <EOL> text = dom . Text ( ) <EOL> text . data = version <EOL> node . appendChild ( text ) <EOL> def getOutputFileName ( originalFileName , outputExtension , index = None ) : <EOL> """<STR_LIT>""" <EOL> return os . path . splitext ( originalFileName ) [ <NUM_LIT:0> ] + outputExtension <EOL> def munge ( document , template , linkrel , dir , fullpath , ext , url , config , outfileGenerator = getOutputFileName ) : <EOL> """<STR_LIT>""" <EOL> fixRelativeLinks ( template , linkrel ) <EOL> addMtime ( template , fullpath ) <EOL> removeH1 ( document ) <EOL> if not config . get ( '<STR_LIT>' , False ) : <EOL> fixAPI ( document , url ) <EOL> fontifyPython ( document ) <EOL> fixLinks ( document , ext ) <EOL> addPyListings ( document , dir ) <EOL> addHTMLListings ( document , dir ) <EOL> addPlainListings ( document , dir ) <EOL> putInToC ( template , generateToC ( document ) ) <EOL> footnotes ( document ) <EOL> notes ( document ) <EOL> setIndexLink ( template , indexer . getIndexFilename ( ) ) <EOL> setVersion ( template , config . get ( '<STR_LIT:version>' , '<STR_LIT>' ) ) <EOL> chapterNumber = htmlbook . getNumber ( fullpath ) <EOL> title = domhelpers . findNodesNamed ( document , '<STR_LIT:title>' ) [ <NUM_LIT:0> ] . childNodes <EOL> setTitle ( template , title , chapterNumber ) <EOL> if numberer . getNumberSections ( ) and chapterNumber : <EOL> numberDocument ( document , chapterNumber ) <EOL> index ( document , outfileGenerator ( os . path . split ( fullpath ) [ <NUM_LIT:1> ] , ext ) , <EOL> htmlbook . getReference ( fullpath ) ) <EOL> authors = domhelpers . findNodesNamed ( document , '<STR_LIT>' ) <EOL> authors = [ ( node . getAttribute ( '<STR_LIT:title>' ) or '<STR_LIT>' , <EOL> node . getAttribute ( '<STR_LIT>' ) or '<STR_LIT>' ) <EOL> for node in authors <EOL> if node . getAttribute ( '<STR_LIT>' ) == '<STR_LIT>' ] <EOL> setAuthors ( template , authors ) <EOL> body = domhelpers . findNodesNamed ( document , "<STR_LIT:body>" ) [ <NUM_LIT:0> ] <EOL> tmplbody = domhelpers . findElementsWithAttribute ( template , "<STR_LIT:class>" , <EOL> "<STR_LIT:body>" ) [ <NUM_LIT:0> ] <EOL> tmplbody . childNodes = body . childNodes <EOL> tmplbody . setAttribute ( "<STR_LIT:class>" , "<STR_LIT:content>" ) <EOL> class _LocationReportingErrorHandler ( ErrorHandler ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , contentHandler ) : <EOL> self . contentHandler = contentHandler <EOL> def fatalError ( self , err ) : <EOL> if err . getMessage ( ) == '<STR_LIT>' : <EOL> expect , begLine , begCol = self . contentHandler . _locationStack [ - <NUM_LIT:1> ] <EOL> endLine , endCol = err . getLineNumber ( ) , err . getColumnNumber ( ) <EOL> raise process . ProcessingFailure ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" % ( <EOL> endLine , endCol , expect , begLine , begCol ) ) <EOL> raise process . ProcessingFailure ( <EOL> '<STR_LIT>' % ( err . getMessage ( ) , <EOL> err . getLineNumber ( ) , <EOL> err . getColumnNumber ( ) ) ) <EOL> class _TagTrackingContentHandler ( SAX2DOM ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self ) : <EOL> SAX2DOM . __init__ ( self ) <EOL> self . _locationStack = [ ] <EOL> def setDocumentLocator ( self , locator ) : <EOL> self . _docLocator = locator <EOL> SAX2DOM . setDocumentLocator ( self , locator ) <EOL> def startElement ( self , name , attrs ) : <EOL> self . _locationStack . append ( ( name , self . _docLocator . getLineNumber ( ) , self . _docLocator . getColumnNumber ( ) ) ) <EOL> SAX2DOM . startElement ( self , name , attrs ) <EOL> def endElement ( self , name ) : <EOL> self . _locationStack . pop ( ) <EOL> SAX2DOM . endElement ( self , name ) <EOL> class _LocalEntityResolver ( object ) : <EOL> """<STR_LIT>""" <EOL> s = FilePath ( __file__ ) . sibling <EOL> knownDTDs = { <EOL> None : s ( "<STR_LIT>" ) , <EOL> "<STR_LIT>" : s ( "<STR_LIT>" ) , <EOL> "<STR_LIT>" : s ( "<STR_LIT>" ) , <EOL> "<STR_LIT>" : s ( "<STR_LIT>" ) , <EOL> "<STR_LIT>" : s ( "<STR_LIT>" ) , <EOL> "<STR_LIT>" : s ( "<STR_LIT>" ) , <EOL> } <EOL> del s <EOL> def __init__ ( self , filename ) : <EOL> self . filename = filename <EOL> def resolveEntity ( self , publicId , systemId ) : <EOL> source = InputSource ( ) <EOL> source . setSystemId ( systemId ) <EOL> try : <EOL> dtdPath = self . knownDTDs [ systemId ] <EOL> except KeyError : <EOL> raise process . ProcessingFailure ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" % ( systemId , self . filename ) ) <EOL> source . setByteStream ( dtdPath . open ( ) ) <EOL> return source <EOL> def parseFileAndReport ( filename , _open = file ) : <EOL> """<STR_LIT>""" <EOL> content = _TagTrackingContentHandler ( ) <EOL> error = _LocationReportingErrorHandler ( content ) <EOL> parser = make_parser ( ) <EOL> parser . setContentHandler ( content ) <EOL> parser . setErrorHandler ( error ) <EOL> parser . reset ( ) <EOL> parser . reset = lambda : None <EOL> parser . _parser . UseForeignDTD ( True ) <EOL> parser . setEntityResolver ( _LocalEntityResolver ( filename ) ) <EOL> parser . setFeature ( feature_validation , False ) <EOL> fObj = _open ( filename ) <EOL> try : <EOL> try : <EOL> parser . parse ( fObj ) <EOL> except IOError , e : <EOL> raise process . ProcessingFailure ( <EOL> e . strerror + "<STR_LIT>" + filename + "<STR_LIT:'>" ) <EOL> finally : <EOL> fObj . close ( ) <EOL> return content . document <EOL> def makeSureDirectoryExists ( filename ) : <EOL> filename = os . path . abspath ( filename ) <EOL> dirname = os . path . dirname ( filename ) <EOL> if ( not os . path . exists ( dirname ) ) : <EOL> os . makedirs ( dirname ) <EOL> def doFile ( filename , linkrel , ext , url , templ , options = { } , outfileGenerator = getOutputFileName ) : <EOL> """<STR_LIT>""" <EOL> doc = parseFileAndReport ( filename ) <EOL> clonedNode = templ . cloneNode ( <NUM_LIT:1> ) <EOL> munge ( doc , clonedNode , linkrel , os . path . dirname ( filename ) , filename , ext , <EOL> url , options , outfileGenerator ) <EOL> newFilename = outfileGenerator ( filename , ext ) <EOL> _writeDocument ( newFilename , clonedNode ) <EOL> def _writeDocument ( newFilename , clonedNode ) : <EOL> """<STR_LIT>""" <EOL> makeSureDirectoryExists ( newFilename ) <EOL> f = open ( newFilename , '<STR_LIT:w>' ) <EOL> f . write ( clonedNode . toxml ( '<STR_LIT:utf-8>' ) ) <EOL> f . close ( ) </s>
