<s> """<STR_LIT>""" <EOL> import sys <EOL> from pycopia . XML import POM <EOL> from pycopia . aid import partial <EOL> Text = POM . Text <EOL> ANYCONTENT = POM . ContentModel ( ( True , ) ) <EOL> attribClass = POM . XMLAttribute ( '<STR_LIT:class>' , <NUM_LIT:8> , <NUM_LIT:12> , None ) <EOL> attribHref = POM . XMLAttribute ( '<STR_LIT>' , <NUM_LIT:1> , <NUM_LIT:12> , None ) <EOL> attribLevel = POM . XMLAttribute ( '<STR_LIT>' , <NUM_LIT:1> , <NUM_LIT:12> , None ) <EOL> def check_object ( obj ) : <EOL> if type ( obj ) in ( str , unicode ) : <EOL> return POM . Text ( obj ) <EOL> if isinstance ( obj , POM . ElementNode ) : <EOL> return obj <EOL> raise POM . ValidationError , "<STR_LIT>" <EOL> class InlineMixin ( object ) : <EOL> def _init ( self , dtd ) : <EOL> self . dtd = dtd <EOL> def emit ( self , fo ) : <EOL> fo . write ( self . __str__ ( ) ) <EOL> class ContainerMixin ( object ) : <EOL> def _init ( self , dtd ) : <EOL> self . dtd = dtd <EOL> def make_node ( self , name , attribs , * content ) : <EOL> if name in ( "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ) : <EOL> elemclass = getattr ( POM , name ) <EOL> else : <EOL> elemclass = Plaintext <EOL> if attribs : <EOL> elem = elemclass ( ** attribs ) <EOL> else : <EOL> elem = elemclass ( ) <EOL> for cont in content : <EOL> if isinstance ( cont , ( POM . ElementNode , POM . Text , POM . ASIS ) ) : <EOL> elem . append ( cont ) <EOL> else : <EOL> elem . add_text ( cont ) <EOL> return elem <EOL> def get_nodemaker ( self ) : <EOL> def _make_node ( container , name , attribs , * contents ) : <EOL> return container . make_node ( name , attribs , * contents ) <EOL> return partial ( _make_node , self ) <EOL> nodemaker = property ( get_nodemaker ) <EOL> def get_section ( self , _name , ** kwargs ) : <EOL> sect = Section ( ** kwargs ) <EOL> sect . _init ( self . dtd ) <EOL> return sect <EOL> def add_section ( self , _name , ** kwargs ) : <EOL> sect = self . get_section ( _name , ** kwargs ) <EOL> self . append ( sect ) <EOL> return sect <EOL> def add_break ( self ) : <EOL> self . append ( Br ( ) ) <EOL> def get_para ( self , ** attribs ) : <EOL> p = Para ( ** attribs ) <EOL> p . _init ( self . dtd ) <EOL> return p <EOL> def add_para ( self , ** attribs ) : <EOL> p = self . get_para ( ** attribs ) <EOL> self . append ( p ) <EOL> return p <EOL> def new_para ( self , text , ** attribs ) : <EOL> p = self . get_para ( ** attribs ) <EOL> t = check_object ( text ) <EOL> p . append ( t ) <EOL> self . append ( p ) <EOL> return p <EOL> def get_header ( self , level , text , ** kwargs ) : <EOL> hobj = Heading ( level = level ) <EOL> hobj . append ( POM . Text ( text ) ) <EOL> return hobj <EOL> def add_header ( self , level , text , ** kwargs ) : <EOL> hobj = self . get_header ( level , text , ** kwargs ) <EOL> self . append ( hobj ) <EOL> return hobj <EOL> def get_unordered_list ( self , items , ** attribs ) : <EOL> rv = [ ] <EOL> for item in items : <EOL> self . append ( Plaintext ( "<STR_LIT>" % item ) ) <EOL> return rv <EOL> def add_unordered_list ( self , items , ** attribs ) : <EOL> for item in items : <EOL> self . append ( Plaintext ( "<STR_LIT>" % item ) ) <EOL> def get_ordered_list ( self , items ) : <EOL> rv = [ ] <EOL> for i , item in enumerate ( items ) : <EOL> rv . append ( Plaintext ( "<STR_LIT>" % ( i , item ) ) ) <EOL> return rv <EOL> def add_ordered_list ( self , items ) : <EOL> for i , item in enumerate ( items ) : <EOL> self . append ( Plaintext ( "<STR_LIT>" % ( i , item ) ) ) <EOL> def add_anchor ( self , ** attribs ) : <EOL> raise NotImplementedError <EOL> def new_anchor ( self , obj , ** attribs ) : <EOL> raise NotImplementedError <EOL> def get_table ( self , ** kwargs ) : <EOL> raise NotImplementedError <EOL> def add_table ( self , ** kwargs ) : <EOL> raise NotImplementedError <EOL> def emit ( self , fo , encoding = "<STR_LIT:ascii>" ) : <EOL> if not self . CONTENTMODEL or self . CONTENTMODEL . is_empty ( ) : <EOL> fo . write ( self . __str__ ( ) ) <EOL> else : <EOL> fo . write ( self . __str__ ( ) ) <EOL> map ( lambda o : o . emit ( fo ) , self . _children ) <EOL> class Plaintext ( ContainerMixin , POM . ElementNode ) : <EOL> _name = "<STR_LIT:text>" <EOL> CONTENTMODEL = ANYCONTENT <EOL> ATTRIBUTES = { <EOL> '<STR_LIT:class>' : attribClass , <EOL> } <EOL> KWATTRIBUTES = { <EOL> '<STR_LIT>' : attribClass , <EOL> } <EOL> def __str__ ( self ) : <EOL> s = map ( str , self . _children ) <EOL> return "<STR_LIT>" . join ( s ) <EOL> def __iadd__ ( self , text ) : <EOL> self . add_text ( str ( text ) ) <EOL> return self <EOL> class Title ( InlineMixin , POM . ElementNode ) : <EOL> _name = "<STR_LIT:title>" <EOL> CONTENTMODEL = ANYCONTENT <EOL> def __str__ ( self ) : <EOL> t = self . get_text ( ) <EOL> line = "<STR_LIT:=>" * len ( t ) <EOL> return "<STR_LIT>" % ( line , t , line ) <EOL> class Br ( ContainerMixin , POM . ElementNode ) : <EOL> _name = "<STR_LIT>" <EOL> def __str__ ( self ) : <EOL> return "<STR_LIT:\n>" <EOL> class A ( ContainerMixin , POM . ElementNode ) : <EOL> _name = "<STR_LIT:a>" <EOL> CONTENTMODEL = ANYCONTENT <EOL> ATTRIBUTES = { <EOL> '<STR_LIT>' : attribHref , <EOL> } <EOL> KWATTRIBUTES = { <EOL> '<STR_LIT>' : attribHref , <EOL> } <EOL> def __str__ ( self ) : <EOL> t = self . get_text ( ) <EOL> return "<STR_LIT>" % ( t , self . href ) <EOL> class Heading ( ContainerMixin , POM . ElementNode ) : <EOL> _name = "<STR_LIT>" <EOL> CONTENTMODEL = ANYCONTENT <EOL> ATTRIBUTES = { <EOL> '<STR_LIT>' : attribLevel , <EOL> } <EOL> KWATTRIBUTES = { <EOL> '<STR_LIT>' : attribLevel , <EOL> } <EOL> def __str__ ( self ) : <EOL> t = self . get_text ( ) <EOL> try : <EOL> level = int ( self . level ) <EOL> except ( ValueError , TypeError ) : <EOL> level = <NUM_LIT:0> <EOL> indent = "<STR_LIT:U+0020>" * level <EOL> lc = { <NUM_LIT:0> : "<STR_LIT:=>" , <NUM_LIT:1> : "<STR_LIT:->" , <NUM_LIT:2> : "<STR_LIT:_>" , <NUM_LIT:3> : "<STR_LIT:*>" } . get ( level , "<STR_LIT:+>" ) <EOL> line = lc * len ( t ) <EOL> return "<STR_LIT>" % ( indent , t , indent , line ) <EOL> class Para ( Plaintext ) : <EOL> _name = "<STR_LIT>" <EOL> def __str__ ( self ) : <EOL> s = map ( str , self . _children ) <EOL> return "<STR_LIT>" . join ( s ) + "<STR_LIT>" <EOL> class Section ( Plaintext ) : <EOL> _name = "<STR_LIT>" <EOL> class Chapter ( Plaintext ) : <EOL> _name = "<STR_LIT>" <EOL> class TextDocument ( POM . POMDocument , ContainerMixin ) : <EOL> """<STR_LIT>""" <EOL> XMLHEADER = "<STR_LIT>" <EOL> DOCTYPE = "<STR_LIT>" <EOL> MIMETYPE = "<STR_LIT>" <EOL> def __init__ ( self , encoding = "<STR_LIT:ascii>" ) : <EOL> dtd = sys . modules [ __name__ ] <EOL> super ( TextDocument , self ) . __init__ ( dtd = dtd , encoding = encoding ) <EOL> def initialize ( self ) : <EOL> self . set_root ( Plaintext ( ) ) <EOL> def __iadd__ ( self , text ) : <EOL> self . root . add_text ( text ) <EOL> return self <EOL> def add_text ( self , text ) : <EOL> self . root . add_text ( text ) <EOL> def __str__ ( self ) : <EOL> return str ( self . root ) <EOL> def emit ( self , fo ) : <EOL> self . root . emit ( fo ) <EOL> def add_title ( self , title ) : <EOL> ti = Title ( ) <EOL> ti . append ( Text ( title ) ) <EOL> self . root . insert ( <NUM_LIT:0> , ti ) <EOL> return ti <EOL> def _get_title ( self ) : <EOL> return self . get_path ( "<STR_LIT>" ) <EOL> title = property ( _get_title , add_title ) <EOL> def append ( self , obj , ** kwargs ) : <EOL> self . root . append ( obj ) <EOL> def insert ( self , ind , obj , ** kwargs ) : <EOL> self . root . insert ( ind , obj ) <EOL> def new_document ( encoding = POM . DEFAULT_ENCODING ) : <EOL> doc = TextDocument ( encoding ) <EOL> doc . initialize ( ) <EOL> return doc </s>
