<s> """<STR_LIT>""" <EOL> import os <EOL> import sys <EOL> from datetime import datetime <EOL> from cgi import escape <EOL> from docutils import nodes <EOL> from docutils . parsers . rst import directives <EOL> from docutils . core import publish_parts <EOL> from docutils . writers import html4css1 <EOL> from jinja2 import Template <EOL> sys . path . insert ( <NUM_LIT:0> , os . path . join ( os . path . dirname ( __file__ ) , '<STR_LIT:..>' ) ) <EOL> from pygments import highlight , __version__ <EOL> from pygments . lexers import get_lexer_by_name <EOL> from pygments . formatters import HtmlFormatter <EOL> LEXERDOC = '''<STR_LIT>''' <EOL> def generate_lexer_docs ( ) : <EOL> from pygments . lexers import LEXERS <EOL> out = [ ] <EOL> modules = { } <EOL> moduledocstrings = { } <EOL> for classname , data in sorted ( LEXERS . iteritems ( ) , key = lambda x : x [ <NUM_LIT:0> ] ) : <EOL> module = data [ <NUM_LIT:0> ] <EOL> mod = __import__ ( module , None , None , [ classname ] ) <EOL> cls = getattr ( mod , classname ) <EOL> if not cls . __doc__ : <EOL> print "<STR_LIT>" % classname <EOL> modules . setdefault ( module , [ ] ) . append ( ( <EOL> classname , <EOL> cls . __doc__ , <EOL> '<STR_LIT:U+002CU+0020>' . join ( data [ <NUM_LIT:2> ] ) or '<STR_LIT:None>' , <EOL> '<STR_LIT:U+002CU+0020>' . join ( data [ <NUM_LIT:3> ] ) . replace ( '<STR_LIT:*>' , '<STR_LIT>' ) or '<STR_LIT:None>' , <EOL> '<STR_LIT:U+002CU+0020>' . join ( data [ <NUM_LIT:4> ] ) or '<STR_LIT:None>' ) ) <EOL> if module not in moduledocstrings : <EOL> moduledocstrings [ module ] = mod . __doc__ <EOL> for module , lexers in sorted ( modules . iteritems ( ) , key = lambda x : x [ <NUM_LIT:0> ] ) : <EOL> heading = moduledocstrings [ module ] . splitlines ( ) [ <NUM_LIT:4> ] . strip ( ) . rstrip ( '<STR_LIT:.>' ) <EOL> out . append ( '<STR_LIT:\n>' + heading + '<STR_LIT:\n>' + '<STR_LIT:->' * len ( heading ) + '<STR_LIT:\n>' ) <EOL> for data in lexers : <EOL> out . append ( LEXERDOC % data ) <EOL> return '<STR_LIT>' . join ( out ) . decode ( '<STR_LIT:utf-8>' ) <EOL> def generate_formatter_docs ( ) : <EOL> from pygments . formatters import FORMATTERS <EOL> out = [ ] <EOL> for cls , data in sorted ( FORMATTERS . iteritems ( ) , <EOL> key = lambda x : x [ <NUM_LIT:0> ] . __name__ ) : <EOL> heading = cls . __name__ <EOL> out . append ( '<STR_LIT>' + heading + '<STR_LIT>' + '<STR_LIT:->' * ( <NUM_LIT:2> + len ( heading ) ) + '<STR_LIT:\n>' ) <EOL> out . append ( cls . __doc__ ) <EOL> out . append ( '''<STR_LIT>''' % ( '<STR_LIT:U+002CU+0020>' . join ( data [ <NUM_LIT:1> ] ) or '<STR_LIT:None>' , '<STR_LIT:U+002CU+0020>' . join ( data [ <NUM_LIT:2> ] ) . replace ( '<STR_LIT:*>' , '<STR_LIT>' ) or '<STR_LIT:None>' ) ) <EOL> return '<STR_LIT>' . join ( out ) . decode ( '<STR_LIT:utf-8>' ) <EOL> def generate_filter_docs ( ) : <EOL> from pygments . filters import FILTERS <EOL> out = [ ] <EOL> for name , cls in FILTERS . iteritems ( ) : <EOL> out . append ( '''<STR_LIT>''' % ( cls . __name__ , cls . __doc__ , name ) ) <EOL> return '<STR_LIT>' . join ( out ) . decode ( '<STR_LIT:utf-8>' ) <EOL> def generate_changelog ( ) : <EOL> fn = os . path . abspath ( os . path . join ( os . path . dirname ( __file__ ) , '<STR_LIT:..>' , <EOL> '<STR_LIT>' ) ) <EOL> f = file ( fn ) <EOL> result = [ ] <EOL> in_header = False <EOL> header = True <EOL> for line in f : <EOL> if header : <EOL> if not in_header and line . strip ( ) : <EOL> in_header = True <EOL> elif in_header and not line . strip ( ) : <EOL> header = False <EOL> else : <EOL> result . append ( line . rstrip ( ) ) <EOL> f . close ( ) <EOL> return '<STR_LIT:\n>' . join ( result ) . decode ( '<STR_LIT:utf-8>' ) <EOL> def generate_authors ( ) : <EOL> fn = os . path . abspath ( os . path . join ( os . path . dirname ( __file__ ) , '<STR_LIT:..>' , <EOL> '<STR_LIT>' ) ) <EOL> f = file ( fn ) <EOL> r = f . read ( ) . rstrip ( ) . decode ( '<STR_LIT:utf-8>' ) <EOL> f . close ( ) <EOL> return r <EOL> LEXERDOCS = generate_lexer_docs ( ) <EOL> FORMATTERDOCS = generate_formatter_docs ( ) <EOL> FILTERDOCS = generate_filter_docs ( ) <EOL> CHANGELOG = generate_changelog ( ) <EOL> AUTHORS = generate_authors ( ) <EOL> PYGMENTS_FORMATTER = HtmlFormatter ( style = '<STR_LIT>' , cssclass = '<STR_LIT>' ) <EOL> USAGE = '''<STR_LIT>''' % sys . argv [ <NUM_LIT:0> ] <EOL> TEMPLATE = '''<STR_LIT>''' <EOL> STYLESHEET = '''<STR_LIT>''' <EOL> def pygments_directive ( name , arguments , options , content , lineno , <EOL> content_offset , block_text , state , state_machine ) : <EOL> try : <EOL> lexer = get_lexer_by_name ( arguments [ <NUM_LIT:0> ] ) <EOL> except ValueError : <EOL> lexer = get_lexer_by_name ( '<STR_LIT:text>' ) <EOL> parsed = highlight ( u'<STR_LIT:\n>' . join ( content ) , lexer , PYGMENTS_FORMATTER ) <EOL> return [ nodes . raw ( '<STR_LIT>' , parsed , format = "<STR_LIT:html>" ) ] <EOL> pygments_directive . arguments = ( <NUM_LIT:1> , <NUM_LIT:0> , <NUM_LIT:1> ) <EOL> pygments_directive . content = <NUM_LIT:1> <EOL> directives . register_directive ( '<STR_LIT>' , pygments_directive ) <EOL> def create_translator ( link_style ) : <EOL> class Translator ( html4css1 . HTMLTranslator ) : <EOL> def visit_reference ( self , node ) : <EOL> refuri = node . get ( '<STR_LIT>' ) <EOL> if refuri is not None and '<STR_LIT:/>' not in refuri and refuri . endswith ( '<STR_LIT>' ) : <EOL> node [ '<STR_LIT>' ] = link_style ( refuri [ : - <NUM_LIT:4> ] ) <EOL> html4css1 . HTMLTranslator . visit_reference ( self , node ) <EOL> return Translator <EOL> class DocumentationWriter ( html4css1 . Writer ) : <EOL> def __init__ ( self , link_style ) : <EOL> html4css1 . Writer . __init__ ( self ) <EOL> self . translator_class = create_translator ( link_style ) <EOL> def translate ( self ) : <EOL> html4css1 . Writer . translate ( self ) <EOL> contents = self . build_contents ( self . document ) <EOL> contents_doc = self . document . copy ( ) <EOL> contents_doc . children = contents <EOL> contents_visitor = self . translator_class ( contents_doc ) <EOL> contents_doc . walkabout ( contents_visitor ) <EOL> self . parts [ '<STR_LIT>' ] = self . _generated_toc <EOL> def build_contents ( self , node , level = <NUM_LIT:0> ) : <EOL> sections = [ ] <EOL> i = len ( node ) - <NUM_LIT:1> <EOL> while i >= <NUM_LIT:0> and isinstance ( node [ i ] , nodes . section ) : <EOL> sections . append ( node [ i ] ) <EOL> i -= <NUM_LIT:1> <EOL> sections . reverse ( ) <EOL> toc = [ ] <EOL> for section in sections : <EOL> try : <EOL> reference = nodes . reference ( '<STR_LIT>' , '<STR_LIT>' , refid = section [ '<STR_LIT>' ] [ <NUM_LIT:0> ] , * section [ <NUM_LIT:0> ] ) <EOL> except IndexError : <EOL> continue <EOL> ref_id = reference [ '<STR_LIT>' ] <EOL> text = escape ( reference . astext ( ) ) <EOL> toc . append ( ( ref_id , text ) ) <EOL> self . _generated_toc = [ ( '<STR_LIT>' % href , caption ) for href , caption in toc ] <EOL> return [ ] <EOL> def generate_documentation ( data , link_style ) : <EOL> writer = DocumentationWriter ( link_style ) <EOL> data = data . replace ( '<STR_LIT>' , LEXERDOCS ) . replace ( '<STR_LIT>' , FORMATTERDOCS ) . replace ( '<STR_LIT>' , FILTERDOCS ) . replace ( '<STR_LIT>' , CHANGELOG ) . replace ( '<STR_LIT>' , AUTHORS ) <EOL> parts = publish_parts ( <EOL> data , <EOL> writer = writer , <EOL> settings_overrides = { <EOL> '<STR_LIT>' : <NUM_LIT:3> , <EOL> '<STR_LIT>' : <NUM_LIT:50> , <EOL> } <EOL> ) <EOL> return { <EOL> '<STR_LIT:title>' : parts [ '<STR_LIT:title>' ] , <EOL> '<STR_LIT:body>' : parts [ '<STR_LIT:body>' ] , <EOL> '<STR_LIT>' : parts [ '<STR_LIT>' ] <EOL> } <EOL> def handle_python ( filename , fp , dst ) : <EOL> now = datetime . now ( ) <EOL> title = os . path . basename ( filename ) [ : - <NUM_LIT:4> ] <EOL> content = fp . read ( ) <EOL> def urlize ( href ) : <EOL> if href == '<STR_LIT>' : <EOL> return '<STR_LIT>' <EOL> else : <EOL> return '<STR_LIT>' % href <EOL> parts = generate_documentation ( content , urlize ) <EOL> result = file ( os . path . join ( dst , title + '<STR_LIT>' ) , '<STR_LIT:w>' ) <EOL> result . write ( '<STR_LIT>' ) <EOL> result . write ( '<STR_LIT>' % title ) <EOL> result . write ( '<STR_LIT>' % ( '<STR_LIT>' * ( <NUM_LIT> + len ( title ) ) ) ) <EOL> result . write ( '<STR_LIT>' % now ) <EOL> result . write ( '<STR_LIT>' ) <EOL> result . write ( '<STR_LIT>' % now ) <EOL> result . write ( '<STR_LIT>' % parts [ '<STR_LIT:title>' ] ) <EOL> result . write ( '<STR_LIT>' % parts [ '<STR_LIT>' ] ) <EOL> result . write ( '<STR_LIT>' % parts [ '<STR_LIT:body>' ] ) <EOL> result . close ( ) <EOL> def handle_html ( filename , fp , dst ) : <EOL> now = datetime . now ( ) <EOL> title = os . path . basename ( filename ) [ : - <NUM_LIT:4> ] <EOL> content = fp . read ( ) . decode ( '<STR_LIT:utf-8>' ) <EOL> c = generate_documentation ( content , ( lambda x : '<STR_LIT>' % x ) ) <EOL> result = file ( os . path . join ( dst , title + '<STR_LIT>' ) , '<STR_LIT:w>' ) <EOL> c [ '<STR_LIT>' ] = STYLESHEET + PYGMENTS_FORMATTER . get_style_defs ( '<STR_LIT>' ) <EOL> c [ '<STR_LIT>' ] = now <EOL> c [ '<STR_LIT>' ] = title <EOL> t = Template ( TEMPLATE ) <EOL> result . write ( t . render ( c ) . encode ( '<STR_LIT:utf-8>' ) ) <EOL> result . close ( ) <EOL> def run ( handle_file , dst , sources = ( ) ) : <EOL> path = os . path . abspath ( os . path . join ( os . path . dirname ( __file__ ) , '<STR_LIT:src>' ) ) <EOL> if not sources : <EOL> sources = [ os . path . join ( path , fn ) for fn in os . listdir ( path ) ] <EOL> if not os . path . isdir ( dst ) : <EOL> os . makedirs ( dst ) <EOL> print '<STR_LIT>' % ( __version__ , dst ) <EOL> for fn in sources : <EOL> if not os . path . isfile ( fn ) : <EOL> continue <EOL> print '<STR_LIT>' % fn <EOL> f = open ( fn ) <EOL> try : <EOL> handle_file ( fn , f , dst ) <EOL> finally : <EOL> f . close ( ) <EOL> def main ( mode , dst = '<STR_LIT>' , * sources ) : <EOL> try : <EOL> handler = { <EOL> '<STR_LIT:html>' : handle_html , <EOL> '<STR_LIT>' : handle_python <EOL> } [ mode ] <EOL> except KeyError : <EOL> print '<STR_LIT>' % mode <EOL> sys . exit ( <NUM_LIT:1> ) <EOL> run ( handler , os . path . realpath ( dst ) , sources ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> if len ( sys . argv ) == <NUM_LIT:1> : <EOL> print USAGE <EOL> else : <EOL> main ( * sys . argv [ <NUM_LIT:1> : ] ) </s>
