<s> import devon . renderers , devon . stream <EOL> from devon import tags <EOL> import cStringIO <EOL> class TextRenderer ( devon . renderers . Renderer ) : <EOL> def __init__ ( self , lineWidth = <NUM_LIT:100> ) : <EOL> self . rendererStack = [ ] <EOL> def getRendererForTag ( self , tag ) : <EOL> if tag . __class__ in textTagRenderers : <EOL> return textTagRenderers [ tag . __class__ ] <EOL> for baseClass in tag . __class__ . __bases__ : <EOL> if baseClass in textTagRenderers : <EOL> return textTagRenderers [ baseClass ] <EOL> def getTopRenderer ( self ) : <EOL> if len ( self . rendererStack ) : <EOL> return self . rendererStack [ - <NUM_LIT:1> ] <EOL> else : <EOL> return None <EOL> def renderOpenTag ( self , tag , out ) : <EOL> renderer = self . getRendererForTag ( tag ) <EOL> self . rendererStack . append ( renderer ) <EOL> if renderer : <EOL> renderer . renderOpenTag ( tag , out ) <EOL> def renderCloseTag ( self , tag , out ) : <EOL> renderer = self . rendererStack . pop ( ) <EOL> if renderer : <EOL> renderer . renderCloseTag ( tag , out ) <EOL> def renderText ( self , text , out ) : <EOL> renderer = self . getTopRenderer ( ) <EOL> if renderer : <EOL> renderer . renderText ( text , out ) <EOL> else : <EOL> out . write ( text ) <EOL> class NodeRenderer ( devon . renderers . Renderer ) : <EOL> def __init__ ( self , textBefore = None , textAfter = None ) : <EOL> self . textBefore = textBefore <EOL> self . textAfter = textAfter <EOL> def renderOpenTag ( self , tag , out ) : <EOL> pass <EOL> def renderCloseTag ( self , tag , out ) : <EOL> pass <EOL> def renderText ( self , text , out ) : <EOL> out . write ( text ) <EOL> def getTextBefore ( self , tag ) : <EOL> if callable ( self . textBefore ) : <EOL> return self . textBefore ( self , tag ) <EOL> else : <EOL> return self . textBefore <EOL> def getTextAfter ( self , tag ) : <EOL> if callable ( self . textAfter ) : <EOL> return self . textAfter ( self , tag ) <EOL> else : <EOL> return self . textAfter <EOL> class Block ( NodeRenderer ) : <EOL> def renderCloseTag ( self , tag , out ) : <EOL> out . write ( "<STR_LIT:\n>" ) <EOL> if self . textAfter : <EOL> out . write ( self . getTextAfter ( tag ) ) <EOL> class LineBlock ( Block ) : pass <EOL> class NextLineBlock ( Block ) : <EOL> def renderOpenTag ( self , tag , out ) : <EOL> if self . textBefore : <EOL> out . write ( self . getTextBefore ( tag ) + "<STR_LIT:\n>" ) <EOL> def renderText ( self , text , out ) : <EOL> out . write ( text ) <EOL> class Span ( NodeRenderer ) : <EOL> def renderOpenTag ( self , tag , out ) : <EOL> if self . textBefore : <EOL> out . write ( self . getTextBefore ( tag ) ) <EOL> def renderCloseTag ( self , tag , out ) : <EOL> if self . textAfter : <EOL> out . write ( self . getTextAfter ( tag ) ) <EOL> class Link ( NodeRenderer ) : <EOL> def renderOpenTag ( self , tag , out ) : <EOL> out . write ( "<STR_LIT>" ) <EOL> if "<STR_LIT>" in tag . attributes : <EOL> out . write ( tag . attributes [ "<STR_LIT>" ] ) <EOL> if "<STR_LIT:title>" in tag . attributes : <EOL> out . write ( "<STR_LIT:|>" + tag . attributes [ "<STR_LIT:title>" ] ) <EOL> def renderCloseTag ( self , tag , out ) : <EOL> out . write ( "<STR_LIT>" ) <EOL> def renderText ( self , text , out ) : <EOL> pass <EOL> class FileLink ( NodeRenderer ) : <EOL> def renderOpenTag ( self , tag , out ) : <EOL> pass <EOL> def renderCloseTag ( self , tag , out ) : <EOL> pass <EOL> def renderText ( self , text , out ) : <EOL> out . write ( text ) <EOL> class EmptyBlock ( NodeRenderer ) : <EOL> def renderText ( self , text , out ) : <EOL> pass <EOL> def mapHeaderTagLineChar ( renderer , tag ) : <EOL> char = "<STR_LIT:=>" <EOL> return char * ( <NUM_LIT:100> / len ( char ) ) <EOL> textTagRenderers = { <EOL> tags . Tag : None , <EOL> tags . Block : Block ( ) , <EOL> tags . Paragraph : Block ( ) , <EOL> tags . Section : None , <EOL> tags . Header : NextLineBlock ( mapHeaderTagLineChar , "<STR_LIT:\n>" ) , <EOL> tags . Separator : LineBlock ( "<STR_LIT:_>" ) , <EOL> tags . QuoteBlock : Block ( "<STR_LIT>" ) , <EOL> tags . CodeBlock : Block ( "<STR_LIT>" ) , <EOL> tags . Script : EmptyBlock ( ) , <EOL> tags . Style : EmptyBlock ( ) , <EOL> tags . Span : Span ( ) , <EOL> tags . Emph : Span ( "<STR_LIT:*>" , "<STR_LIT:*>" ) , <EOL> tags . Strong : Span ( "<STR_LIT:_>" , "<STR_LIT:_>" ) , <EOL> tags . Code : Span ( "<STR_LIT>" , "<STR_LIT>" ) , <EOL> tags . Link : Link ( ) , <EOL> tags . FileLink : FileLink ( ) , <EOL> tags . Image : None , <EOL> tags . Anchor : None , <EOL> tags . Field : None , <EOL> tags . OrderedList : None , <EOL> tags . UnorderedList : None , <EOL> tags . ListItem : Block ( "<STR_LIT>" ) , <EOL> tags . Table : None , <EOL> tags . Row : None , <EOL> tags . Cell : None , <EOL> tags . HTMLTag : None <EOL> } <EOL> def createTextStream ( out = None ) : <EOL> if not out : <EOL> out = cStringIO . StringIO ( ) <EOL> renderer = devon . renderers . text . TextRenderer ( ) <EOL> return devon . stream . OutStream ( out , renderer ) </s>
