<s> '''<STR_LIT>''' <EOL> import types <EOL> import inspect <EOL> from log import log , nodeid <EOL> from smartypants import smartyPants <EOL> import docutils . nodes <EOL> from flowables import BoundByWidth , TocEntry <EOL> class MetaHelper ( type ) : <EOL> '''<STR_LIT>''' <EOL> def __new__ ( clstype , name , bases , clsdict ) : <EOL> base = ( [ x for x in bases if type ( x ) is MetaHelper ] + [ None ] ) [ <NUM_LIT:0> ] <EOL> clsdict . setdefault ( '<STR_LIT>' , base ) <EOL> preinit = getattr ( base , '<STR_LIT>' , None ) <EOL> if preinit is not None : <EOL> clstype , name , bases , clsdict = preinit ( clstype , name , bases , clsdict ) <EOL> return type . __new__ ( clstype , name , bases , clsdict ) <EOL> def __init__ ( cls , name , bases , clsdict ) : <EOL> type . __init__ ( cls , name , bases , clsdict ) <EOL> if cls . _classinit is not None : <EOL> cls . _classinit ( ) <EOL> class NodeHandler ( object ) : <EOL> '''<STR_LIT>''' <EOL> __metaclass__ = MetaHelper <EOL> @ classmethod <EOL> def _classpreinit ( baseclass , clstype , name , bases , clsdict ) : <EOL> new_bases = [ ] <EOL> targets = [ ] <EOL> for target in bases : <EOL> if target is not object : <EOL> ( targets , new_bases ) [ issubclass ( target , NodeHandler ) ] . append ( target ) <EOL> clsdict [ '<STR_LIT>' ] = targets <EOL> return clstype , name , tuple ( new_bases ) , clsdict <EOL> @ classmethod <EOL> def _classinit ( cls ) : <EOL> if cls . _baseclass is None : <EOL> cls . dispatchdict = { } <EOL> return <EOL> self = cls ( ) <EOL> for target in cls . _targets : <EOL> if cls . dispatchdict . setdefault ( target , self ) is not self : <EOL> t = repr ( target ) <EOL> old = repr ( cls . dispatchdict [ target ] ) <EOL> new = repr ( self ) <EOL> log . debug ( '<STR_LIT>' % <EOL> ( old , t , new ) ) <EOL> cls . dispatchdict [ target ] = self <EOL> @ staticmethod <EOL> def getclassname ( obj ) : <EOL> cln = repr ( obj . __class__ ) <EOL> info = cln . split ( "<STR_LIT:'>" ) <EOL> if len ( info ) == <NUM_LIT:3> : <EOL> return info [ <NUM_LIT:1> ] <EOL> return cln <EOL> def log_unknown ( self , node , during ) : <EOL> if not hasattr ( self , '<STR_LIT>' ) : <EOL> self . unkn_node = set ( ) <EOL> cln = self . getclassname ( node ) <EOL> if not cln in self . unkn_node : <EOL> self . unkn_node . add ( cln ) <EOL> log . warning ( "<STR_LIT>" , <EOL> during , cln , nodeid ( node ) ) <EOL> try : <EOL> log . debug ( node ) <EOL> except ( UnicodeDecodeError , UnicodeEncodeError ) : <EOL> log . debug ( repr ( node ) ) <EOL> def findsubclass ( self , node , during ) : <EOL> handlerinfo = '<STR_LIT>' % ( self . getclassname ( self ) , during ) <EOL> log . debug ( "<STR_LIT>" , handlerinfo , self . getclassname ( node ) ) <EOL> log . debug ( "<STR_LIT>" , handlerinfo , nodeid ( node ) ) <EOL> try : <EOL> log . debug ( "<STR_LIT>" , handlerinfo , node ) <EOL> except ( UnicodeDecodeError , UnicodeEncodeError ) : <EOL> log . debug ( "<STR_LIT>" , handlerninfo , node ) <EOL> log . debug ( "<STR_LIT>" ) <EOL> dispatchdict = self . dispatchdict <EOL> for baseclass in inspect . getmro ( node . __class__ ) : <EOL> result = dispatchdict . get ( baseclass ) <EOL> if result is not None : <EOL> break <EOL> else : <EOL> self . log_unknown ( node , during ) <EOL> result = self <EOL> return result <EOL> def __call__ ( self , client ) : <EOL> '''<STR_LIT>''' <EOL> textdispatch = types . MethodType ( self . textdispatch , client ) <EOL> elemdispatch = types . MethodType ( self . elemdispatch , client ) <EOL> return textdispatch , elemdispatch <EOL> sphinxmode = False <EOL> def gather_elements ( self , client , node , style ) : <EOL> return client . gather_elements ( node , style = style ) <EOL> def getstyle ( self , client , node , style ) : <EOL> try : <EOL> if node [ '<STR_LIT>' ] and node [ '<STR_LIT>' ] [ <NUM_LIT:0> ] : <EOL> if client . styles . StyleSheet . has_key ( node [ '<STR_LIT>' ] [ <NUM_LIT:0> ] ) : <EOL> style = client . styles [ node [ '<STR_LIT>' ] [ <NUM_LIT:0> ] ] <EOL> else : <EOL> log . info ( "<STR_LIT>" , <EOL> node [ '<STR_LIT>' ] [ <NUM_LIT:0> ] , nodeid ( node ) ) <EOL> except TypeError : <EOL> pass <EOL> if style is None or style == client . styles [ '<STR_LIT>' ] : <EOL> style = client . styles . styleForNode ( node ) <EOL> return style <EOL> def getelements ( self , client , node , style ) : <EOL> style = self . getstyle ( client , node , style ) <EOL> elements = self . gather_elements ( client , node , style ) <EOL> if '<STR_LIT:width>' in style . __dict__ : <EOL> elements = [ BoundByWidth ( style . width , <EOL> elements , style , mode = "<STR_LIT>" ) ] <EOL> return elements <EOL> def elemdispatch ( self , client , node , style = None ) : <EOL> self = self . findsubclass ( node , '<STR_LIT>' ) <EOL> try : <EOL> for i in node [ '<STR_LIT>' ] : <EOL> client . pending_targets . append ( i ) <EOL> except TypeError : <EOL> pass <EOL> elements = self . getelements ( client , node , style ) <EOL> if node . line and client . debugLinesPdf : <EOL> elements . insert ( <NUM_LIT:0> , TocEntry ( client . depth - <NUM_LIT:1> , '<STR_LIT>' % node . line ) ) <EOL> node . elements = elements <EOL> return elements <EOL> pre = '<STR_LIT>' <EOL> post = '<STR_LIT>' <EOL> def get_pre_post ( self , client , node , replaceEnt ) : <EOL> return self . pre , self . post <EOL> def get_text ( self , client , node , replaceEnt ) : <EOL> return client . gather_pdftext ( node ) <EOL> def apply_smartypants ( self , text , smarty , node ) : <EOL> if node . __class__ in ( docutils . nodes . paragraph , <EOL> docutils . nodes . block_quote , docutils . nodes . title ) : <EOL> return smartyPants ( text , smarty ) <EOL> return text <EOL> def textdispatch ( self , client , node , replaceEnt = True ) : <EOL> self = self . findsubclass ( node , '<STR_LIT>' ) <EOL> pre , post = self . get_pre_post ( client , node , replaceEnt ) <EOL> text = self . get_text ( client , node , replaceEnt ) <EOL> text = pre + text + post <EOL> try : <EOL> log . debug ( "<STR_LIT>" % ( self . getclassname ( self ) , text ) ) <EOL> except UnicodeDecodeError : <EOL> pass <EOL> text = self . apply_smartypants ( text , client . smarty , node ) <EOL> node . pdftext = text <EOL> return text </s>
