<s> """<STR_LIT>""" <EOL> import markdown <EOL> from markdown import etree <EOL> class TableProcessor ( markdown . blockprocessors . BlockProcessor ) : <EOL> """<STR_LIT>""" <EOL> def test ( self , parent , block ) : <EOL> rows = block . split ( '<STR_LIT:\n>' ) <EOL> return ( len ( rows ) > <NUM_LIT:2> and '<STR_LIT:|>' in rows [ <NUM_LIT:0> ] and <EOL> '<STR_LIT:|>' in rows [ <NUM_LIT:1> ] and '<STR_LIT:->' in rows [ <NUM_LIT:1> ] and <EOL> rows [ <NUM_LIT:1> ] [ <NUM_LIT:0> ] in [ '<STR_LIT:|>' , '<STR_LIT::>' , '<STR_LIT:->' ] ) <EOL> def run ( self , parent , blocks ) : <EOL> """<STR_LIT>""" <EOL> block = blocks . pop ( <NUM_LIT:0> ) . split ( '<STR_LIT:\n>' ) <EOL> header = block [ : <NUM_LIT:2> ] <EOL> rows = block [ <NUM_LIT:2> : ] <EOL> border = False <EOL> if header [ <NUM_LIT:0> ] . startswith ( '<STR_LIT:|>' ) : <EOL> border = True <EOL> align = [ ] <EOL> for c in self . _split_row ( header [ <NUM_LIT:1> ] , border ) : <EOL> if c . startswith ( '<STR_LIT::>' ) and c . endswith ( '<STR_LIT::>' ) : <EOL> align . append ( '<STR_LIT>' ) <EOL> elif c . startswith ( '<STR_LIT::>' ) : <EOL> align . append ( '<STR_LIT:left>' ) <EOL> elif c . endswith ( '<STR_LIT::>' ) : <EOL> align . append ( '<STR_LIT:right>' ) <EOL> else : <EOL> align . append ( None ) <EOL> table = etree . SubElement ( parent , '<STR_LIT>' ) <EOL> thead = etree . SubElement ( table , '<STR_LIT>' ) <EOL> self . _build_row ( header [ <NUM_LIT:0> ] , thead , align , border ) <EOL> tbody = etree . SubElement ( table , '<STR_LIT>' ) <EOL> for row in rows : <EOL> self . _build_row ( row , tbody , align , border ) <EOL> def _build_row ( self , row , parent , align , border ) : <EOL> """<STR_LIT>""" <EOL> tr = etree . SubElement ( parent , '<STR_LIT>' ) <EOL> tag = '<STR_LIT>' <EOL> if parent . tag == '<STR_LIT>' : <EOL> tag = '<STR_LIT>' <EOL> cells = self . _split_row ( row , border ) <EOL> for i , a in enumerate ( align ) : <EOL> c = etree . SubElement ( tr , tag ) <EOL> try : <EOL> c . text = cells [ i ] . strip ( ) <EOL> except IndexError : <EOL> c . text = "<STR_LIT>" <EOL> if a : <EOL> c . set ( '<STR_LIT>' , a ) <EOL> def _split_row ( self , row , border ) : <EOL> """<STR_LIT>""" <EOL> if border : <EOL> if row . startswith ( '<STR_LIT:|>' ) : <EOL> row = row [ <NUM_LIT:1> : ] <EOL> if row . endswith ( '<STR_LIT:|>' ) : <EOL> row = row [ : - <NUM_LIT:1> ] <EOL> return row . split ( '<STR_LIT:|>' ) <EOL> class TableExtension ( markdown . Extension ) : <EOL> """<STR_LIT>""" <EOL> def extendMarkdown ( self , md , md_globals ) : <EOL> """<STR_LIT>""" <EOL> md . parser . blockprocessors . add ( '<STR_LIT>' , <EOL> TableProcessor ( md . parser ) , <EOL> '<STR_LIT>' ) <EOL> def makeExtension ( configs = { } ) : <EOL> return TableExtension ( configs = configs ) </s>
