<s> """<STR_LIT>""" <EOL> from xml . dom import minidom as dom <EOL> import os . path , re <EOL> from cStringIO import StringIO <EOL> from twisted . lore import default <EOL> from twisted . web import domhelpers <EOL> from twisted . lore . latex import BaseLatexSpitter , LatexSpitter , processFile <EOL> from twisted . lore . latex import getLatexText , HeadingLatexSpitter <EOL> from twisted . lore . tree import getHeaders , _removeLeadingTrailingBlankLines <EOL> from twisted . lore . tree import removeH1 , fixAPI , fontifyPython <EOL> from twisted . lore . tree import addPyListings , addHTMLListings , setTitle <EOL> hacked_entities = { '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' } <EOL> entities = { '<STR_LIT>' : '<STR_LIT:&>' , '<STR_LIT>' : '<STR_LIT:>>' , '<STR_LIT>' : '<STR_LIT:<>' , '<STR_LIT>' : '<STR_LIT:">' , <EOL> '<STR_LIT>' : '<STR_LIT>' } <EOL> class MagicpointOutput ( BaseLatexSpitter ) : <EOL> bulletDepth = <NUM_LIT:0> <EOL> def writeNodeData ( self , node ) : <EOL> buf = StringIO ( ) <EOL> getLatexText ( node , buf . write , entities = hacked_entities ) <EOL> data = buf . getvalue ( ) . rstrip ( ) . replace ( '<STR_LIT:\n>' , '<STR_LIT:U+0020>' ) <EOL> self . writer ( re . sub ( '<STR_LIT>' , '<STR_LIT:U+0020>' , data ) ) <EOL> def visitNode_title ( self , node ) : <EOL> self . title = domhelpers . getNodeText ( node ) <EOL> def visitNode_body ( self , node ) : <EOL> self . fontStack = [ ( '<STR_LIT>' , None ) ] <EOL> self . writer ( self . start_h2 ) <EOL> self . writer ( self . title ) <EOL> self . writer ( self . end_h2 ) <EOL> self . writer ( '<STR_LIT>' ) <EOL> for authorNode in domhelpers . findElementsWithAttribute ( node , '<STR_LIT:class>' , '<STR_LIT>' ) : <EOL> getLatexText ( authorNode , self . writer , entities = entities ) <EOL> self . writer ( '<STR_LIT:\n>' ) <EOL> self . writer ( self . start_h2 ) <EOL> self . writer ( self . title ) <EOL> self . writer ( self . end_h2 ) <EOL> for element in getHeaders ( node ) : <EOL> level = int ( element . tagName [ <NUM_LIT:1> ] ) - <NUM_LIT:1> <EOL> self . writer ( level * '<STR_LIT:\t>' ) <EOL> self . writer ( domhelpers . getNodeText ( element ) ) <EOL> self . writer ( '<STR_LIT:\n>' ) <EOL> self . visitNodeDefault ( node ) <EOL> def visitNode_div_author ( self , node ) : <EOL> pass <EOL> def visitNode_div_pause ( self , node ) : <EOL> self . writer ( '<STR_LIT>' ) <EOL> def visitNode_pre ( self , node ) : <EOL> """<STR_LIT>""" <EOL> buf = StringIO ( ) <EOL> getLatexText ( node , buf . write , entities = entities ) <EOL> data = buf . getvalue ( ) <EOL> data = _removeLeadingTrailingBlankLines ( data ) <EOL> lines = data . split ( '<STR_LIT:\n>' ) <EOL> self . fontStack . append ( ( '<STR_LIT>' , <NUM_LIT:4> ) ) <EOL> self . writer ( '<STR_LIT:%>' + self . fontName ( ) + '<STR_LIT:\n>' ) <EOL> for line in lines : <EOL> self . writer ( '<STR_LIT:U+0020>' + line + '<STR_LIT:\n>' ) <EOL> del self . fontStack [ - <NUM_LIT:1> ] <EOL> self . writer ( '<STR_LIT:%>' + self . fontName ( ) + '<STR_LIT:\n>' ) <EOL> def visitNode_ul ( self , node ) : <EOL> if self . bulletDepth > <NUM_LIT:0> : <EOL> self . writer ( self . _start_ul ) <EOL> self . bulletDepth += <NUM_LIT:1> <EOL> self . start_li = self . _start_li * self . bulletDepth <EOL> self . visitNodeDefault ( node ) <EOL> self . bulletDepth -= <NUM_LIT:1> <EOL> self . start_li = self . _start_li * self . bulletDepth <EOL> def visitNode_strong ( self , node ) : <EOL> self . doFont ( node , '<STR_LIT>' ) <EOL> def visitNode_em ( self , node ) : <EOL> self . doFont ( node , '<STR_LIT>' ) <EOL> def visitNode_code ( self , node ) : <EOL> self . doFont ( node , '<STR_LIT>' ) <EOL> def doFont ( self , node , style ) : <EOL> self . fontStack . append ( ( style , None ) ) <EOL> self . writer ( '<STR_LIT>' + self . fontName ( ) + '<STR_LIT:\n>' ) <EOL> self . visitNodeDefault ( node ) <EOL> del self . fontStack [ - <NUM_LIT:1> ] <EOL> self . writer ( '<STR_LIT>' + self . fontName ( ) + '<STR_LIT:\n>' ) <EOL> def fontName ( self ) : <EOL> names = [ x [ <NUM_LIT:0> ] for x in self . fontStack ] <EOL> if '<STR_LIT>' in names : <EOL> name = '<STR_LIT>' <EOL> else : <EOL> name = '<STR_LIT>' <EOL> if '<STR_LIT>' in names : <EOL> name += '<STR_LIT>' <EOL> if '<STR_LIT>' in names : <EOL> name += '<STR_LIT>' <EOL> if name == '<STR_LIT>' : <EOL> name = '<STR_LIT>' <EOL> sizes = [ x [ <NUM_LIT:1> ] for x in self . fontStack ] <EOL> sizes . reverse ( ) <EOL> for size in sizes : <EOL> if size : <EOL> return '<STR_LIT>' % ( name , size ) <EOL> return '<STR_LIT>' % name <EOL> start_h2 = "<STR_LIT>" <EOL> end_h2 = '<STR_LIT>' <EOL> _start_ul = '<STR_LIT:\n>' <EOL> _start_li = "<STR_LIT:\t>" <EOL> end_li = "<STR_LIT:\n>" <EOL> def convertFile ( filename , outputter , template , ext = "<STR_LIT>" ) : <EOL> fout = open ( os . path . splitext ( filename ) [ <NUM_LIT:0> ] + ext , '<STR_LIT:w>' ) <EOL> fout . write ( open ( template ) . read ( ) ) <EOL> spitter = outputter ( fout . write , os . path . dirname ( filename ) , filename ) <EOL> fin = open ( filename ) <EOL> processFile ( spitter , fin ) <EOL> fin . close ( ) <EOL> fout . close ( ) <EOL> def splitIntoSlides ( document ) : <EOL> body = domhelpers . findNodesNamed ( document , '<STR_LIT:body>' ) [ <NUM_LIT:0> ] <EOL> slides = [ ] <EOL> slide = [ ] <EOL> title = '<STR_LIT>' <EOL> for child in body . childNodes : <EOL> if isinstance ( child , dom . Element ) and child . tagName == '<STR_LIT>' : <EOL> if slide : <EOL> slides . append ( ( title , slide ) ) <EOL> slide = [ ] <EOL> title = domhelpers . getNodeText ( child ) <EOL> else : <EOL> slide . append ( child ) <EOL> slides . append ( ( title , slide ) ) <EOL> return slides <EOL> def insertPrevNextLinks ( slides , filename , ext ) : <EOL> for slide in slides : <EOL> for name , offset in ( ( "<STR_LIT>" , - <NUM_LIT:1> ) , ( "<STR_LIT>" , + <NUM_LIT:1> ) ) : <EOL> if ( slide . pos > <NUM_LIT:0> and name == "<STR_LIT>" ) or ( slide . pos < len ( slides ) - <NUM_LIT:1> and name == "<STR_LIT>" ) : <EOL> for node in domhelpers . findElementsWithAttribute ( slide . dom , "<STR_LIT:class>" , name ) : <EOL> if node . tagName == '<STR_LIT:a>' : <EOL> node . setAttribute ( '<STR_LIT>' , '<STR_LIT>' <EOL> % ( filename [ <NUM_LIT:0> ] , slide . pos + offset , ext ) ) <EOL> else : <EOL> text = dom . Text ( ) <EOL> text . data = slides [ slide . pos + offset ] . title <EOL> node . appendChild ( text ) <EOL> else : <EOL> for node in domhelpers . findElementsWithAttribute ( slide . dom , "<STR_LIT:class>" , name ) : <EOL> pos = <NUM_LIT:0> <EOL> for child in node . parentNode . childNodes : <EOL> if child is node : <EOL> del node . parentNode . childNodes [ pos ] <EOL> break <EOL> pos += <NUM_LIT:1> <EOL> class HTMLSlide : <EOL> def __init__ ( self , dom , title , pos ) : <EOL> self . dom = dom <EOL> self . title = title <EOL> self . pos = pos <EOL> def munge ( document , template , linkrel , d , fullpath , ext , url , config ) : <EOL> removeH1 ( document ) <EOL> fixAPI ( document , url ) <EOL> fontifyPython ( document ) <EOL> addPyListings ( document , d ) <EOL> addHTMLListings ( document , d ) <EOL> template = template . cloneNode ( <NUM_LIT:1> ) <EOL> slides = [ ] <EOL> pos = <NUM_LIT:0> <EOL> for title , slide in splitIntoSlides ( document ) : <EOL> t = template . cloneNode ( <NUM_LIT:1> ) <EOL> text = dom . Text ( ) <EOL> text . data = title <EOL> setTitle ( t , [ text ] ) <EOL> tmplbody = domhelpers . findElementsWithAttribute ( t , "<STR_LIT:class>" , "<STR_LIT:body>" ) [ <NUM_LIT:0> ] <EOL> tmplbody . childNodes = slide <EOL> tmplbody . setAttribute ( "<STR_LIT:class>" , "<STR_LIT:content>" ) <EOL> slides . append ( HTMLSlide ( t , title , pos ) ) <EOL> pos += <NUM_LIT:1> <EOL> insertPrevNextLinks ( slides , os . path . splitext ( os . path . basename ( fullpath ) ) , ext ) <EOL> return slides <EOL> from tree import makeSureDirectoryExists <EOL> def getOutputFileName ( originalFileName , outputExtension , index ) : <EOL> return os . path . splitext ( originalFileName ) [ <NUM_LIT:0> ] + '<STR_LIT:->' + str ( index ) + outputExtension <EOL> def doFile ( filename , linkrel , ext , url , templ , options = { } , outfileGenerator = getOutputFileName ) : <EOL> from tree import parseFileAndReport <EOL> doc = parseFileAndReport ( filename ) <EOL> slides = munge ( doc , templ , linkrel , os . path . dirname ( filename ) , filename , ext , url , options ) <EOL> for slide , index in zip ( slides , range ( len ( slides ) ) ) : <EOL> newFilename = outfileGenerator ( filename , ext , index ) <EOL> makeSureDirectoryExists ( newFilename ) <EOL> f = open ( newFilename , '<STR_LIT:wb>' ) <EOL> slide . dom . writexml ( f ) <EOL> f . close ( ) <EOL> class ProsperSlides ( LatexSpitter ) : <EOL> firstSlide = <NUM_LIT:1> <EOL> start_html = '<STR_LIT>' <EOL> start_body = '<STR_LIT>' <EOL> start_div_author = '<STR_LIT>' <EOL> end_div_author = '<STR_LIT:}>' <EOL> def visitNode_h2 ( self , node ) : <EOL> if self . firstSlide : <EOL> self . firstSlide = <NUM_LIT:0> <EOL> self . end_body = '<STR_LIT>' + self . end_body <EOL> else : <EOL> self . writer ( '<STR_LIT>' ) <EOL> self . writer ( '<STR_LIT>' ) <EOL> spitter = HeadingLatexSpitter ( self . writer , self . currDir , self . filename ) <EOL> spitter . visitNodeDefault ( node ) <EOL> self . writer ( '<STR_LIT:}>' ) <EOL> def _write_img ( self , target ) : <EOL> self . writer ( '<STR_LIT>' <EOL> '<STR_LIT>' % target ) <EOL> class PagebreakLatex ( LatexSpitter ) : <EOL> everyN = <NUM_LIT:1> <EOL> currentN = <NUM_LIT:0> <EOL> seenH2 = <NUM_LIT:0> <EOL> start_html = LatexSpitter . start_html + "<STR_LIT>" <EOL> start_body = '<STR_LIT>' <EOL> def visitNode_h2 ( self , node ) : <EOL> if not self . seenH2 : <EOL> self . currentN = <NUM_LIT:0> <EOL> self . seenH2 = <NUM_LIT:1> <EOL> else : <EOL> self . currentN += <NUM_LIT:1> <EOL> self . currentN %= self . everyN <EOL> if not self . currentN : <EOL> self . writer ( '<STR_LIT>' ) <EOL> level = ( int ( node . tagName [ <NUM_LIT:1> ] ) - <NUM_LIT:2> ) + self . baseLevel <EOL> self . writer ( '<STR_LIT>' + level * '<STR_LIT>' + '<STR_LIT>' ) <EOL> spitter = HeadingLatexSpitter ( self . writer , self . currDir , self . filename ) <EOL> spitter . visitNodeDefault ( node ) <EOL> self . writer ( '<STR_LIT>' ) <EOL> class TwoPagebreakLatex ( PagebreakLatex ) : <EOL> everyN = <NUM_LIT:2> <EOL> class SlidesProcessingFunctionFactory ( default . ProcessingFunctionFactory ) : <EOL> latexSpitters = default . ProcessingFunctionFactory . latexSpitters . copy ( ) <EOL> latexSpitters [ '<STR_LIT>' ] = ProsperSlides <EOL> latexSpitters [ '<STR_LIT>' ] = PagebreakLatex <EOL> latexSpitters [ '<STR_LIT>' ] = TwoPagebreakLatex <EOL> def getDoFile ( self ) : <EOL> return doFile <EOL> def generate_mgp ( self , d , fileNameGenerator = None ) : <EOL> template = d . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> df = lambda file , linkrel : convertFile ( file , MagicpointOutput , template , ext = "<STR_LIT>" ) <EOL> return df <EOL> factory = SlidesProcessingFunctionFactory ( ) </s>
