<s> from __future__ import ( absolute_import , division , generators , nested_scopes , print_function , <EOL> unicode_literals , with_statement ) <EOL> import os <EOL> import sys <EOL> import markdown <EOL> from docutils . core import publish_parts <EOL> from pygments import highlight <EOL> from pygments . formatters . html import HtmlFormatter <EOL> from pygments . lexers import PythonLexer , TextLexer , guess_lexer_for_filename <EOL> from pygments . util import ClassNotFound <EOL> from six . moves import range <EOL> from pants . base . exceptions import TaskError <EOL> from pants . util . dirutil import safe_open <EOL> def emit_codehighlight_css ( path , style ) : <EOL> with safe_open ( path , '<STR_LIT:w>' ) as css : <EOL> css . write ( ( HtmlFormatter ( style = style ) ) . get_style_defs ( '<STR_LIT>' ) ) <EOL> return path <EOL> WIKILINKS_PATTERN = r'<STR_LIT>' <EOL> class WikilinksPattern ( markdown . inlinepatterns . Pattern ) : <EOL> def __init__ ( self , build_url , markdown_instance = None ) : <EOL> markdown . inlinepatterns . Pattern . __init__ ( self , WIKILINKS_PATTERN , markdown_instance ) <EOL> self . build_url = build_url <EOL> def handleMatch ( self , m ) : <EOL> alias , url = self . build_url ( m . group ( <NUM_LIT:2> ) . strip ( ) ) <EOL> el = markdown . util . etree . Element ( '<STR_LIT:a>' ) <EOL> el . set ( '<STR_LIT>' , url ) <EOL> el . text = markdown . util . AtomicString ( alias ) <EOL> return el <EOL> class WikilinksExtension ( markdown . Extension ) : <EOL> def __init__ ( self , build_url , configs = None ) : <EOL> markdown . Extension . __init__ ( self , configs or { } ) <EOL> self . build_url = build_url <EOL> def extendMarkdown ( self , md , md_globals ) : <EOL> md . inlinePatterns [ '<STR_LIT>' ] = WikilinksPattern ( self . build_url , md ) <EOL> INCLUDE_PATTERN = r'<STR_LIT>' + '<STR_LIT:\n>' + r'<STR_LIT>' <EOL> def choose_include_text ( s , params , source_path ) : <EOL> """<STR_LIT>""" <EOL> lines = s . splitlines ( ) <EOL> start_after = None <EOL> start_at = None <EOL> end_before = None <EOL> end_at = None <EOL> for term in params . split ( "<STR_LIT:&>" ) : <EOL> if '<STR_LIT:=>' in term : <EOL> param , value = [ p . strip ( ) for p in term . split ( '<STR_LIT:=>' , <NUM_LIT:1> ) ] <EOL> else : <EOL> param , value = term . strip ( ) , '<STR_LIT>' <EOL> if not param : continue <EOL> if param == "<STR_LIT>" : <EOL> start_after = value <EOL> elif param == "<STR_LIT>" : <EOL> start_at = value <EOL> elif param == "<STR_LIT>" : <EOL> end_before = value <EOL> elif param == "<STR_LIT>" : <EOL> end_at = value <EOL> else : <EOL> raise TaskError ( '<STR_LIT>' <EOL> '<STR_LIT>' . format ( params , source_path ) ) <EOL> chosen_lines = [ ] <EOL> for line_ix in range ( <NUM_LIT:0> , len ( lines ) ) : <EOL> line = lines [ line_ix ] <EOL> if ( not start_at ) and ( not start_after ) : <EOL> break <EOL> if start_at is not None and start_at in line : <EOL> break <EOL> if start_after is not None and start_after in line : <EOL> line_ix += <NUM_LIT:1> <EOL> break <EOL> else : <EOL> return '<STR_LIT>' <EOL> for line_ix in range ( line_ix , len ( lines ) ) : <EOL> line = lines [ line_ix ] <EOL> if end_before is not None and end_before in line : <EOL> break <EOL> chosen_lines . append ( line ) <EOL> if end_at is not None and end_at in line : <EOL> break <EOL> else : <EOL> if ( end_before or end_at ) : <EOL> return '<STR_LIT>' <EOL> return '<STR_LIT:\n>' . join ( chosen_lines ) <EOL> class IncludeExcerptPattern ( markdown . inlinepatterns . Pattern ) : <EOL> def __init__ ( self , source_path = None ) : <EOL> """<STR_LIT>""" <EOL> markdown . inlinepatterns . Pattern . __init__ ( self , INCLUDE_PATTERN ) <EOL> self . source_path = source_path <EOL> def handleMatch ( self , match ) : <EOL> params = match . group ( '<STR_LIT>' ) or '<STR_LIT>' <EOL> rel_include_path = match . group ( '<STR_LIT:path>' ) <EOL> source_dir = os . path . dirname ( self . source_path ) <EOL> include_path = os . path . join ( source_dir , rel_include_path ) <EOL> try : <EOL> with open ( include_path ) as include_file : <EOL> file_text = include_file . read ( ) <EOL> except IOError as e : <EOL> raise IOError ( '<STR_LIT>' <EOL> '<STR_LIT>' . format ( self . source_path , <EOL> rel_include_path , <EOL> e . strerror ) ) <EOL> include_text = choose_include_text ( file_text , params , self . source_path ) <EOL> if not include_text : <EOL> raise TaskError ( '<STR_LIT>' <EOL> '<STR_LIT>' . format ( self . source_path , <EOL> rel_include_path ) ) <EOL> el = markdown . util . etree . Element ( '<STR_LIT>' ) <EOL> el . set ( '<STR_LIT:class>' , '<STR_LIT>' ) <EOL> try : <EOL> lexer = guess_lexer_for_filename ( include_path , file_text ) <EOL> except ClassNotFound : <EOL> if '<STR_LIT>' in include_path : <EOL> lexer = PythonLexer ( ) <EOL> else : <EOL> lexer = TextLexer ( ) <EOL> html_snippet = highlight ( include_text , <EOL> lexer , <EOL> HtmlFormatter ( cssclass = '<STR_LIT>' ) ) <EOL> el . text = html_snippet <EOL> return el <EOL> class IncludeExcerptExtension ( markdown . Extension ) : <EOL> def __init__ ( self , source_path , configs = None ) : <EOL> markdown . Extension . __init__ ( self , configs or { } ) <EOL> self . source_path = source_path <EOL> def extendMarkdown ( self , md , md_globals ) : <EOL> md . inlinePatterns . add ( '<STR_LIT>' , <EOL> IncludeExcerptPattern ( source_path = self . source_path ) , <EOL> '<STR_LIT>' ) <EOL> def page_to_html_path ( page ) : <EOL> """<STR_LIT>""" <EOL> source_path = page . sources_relative_to_buildroot ( ) [ <NUM_LIT:0> ] <EOL> return os . path . splitext ( source_path ) [ <NUM_LIT:0> ] + "<STR_LIT>" <EOL> def rst_to_html ( in_rst , stderr ) : <EOL> """<STR_LIT>""" <EOL> if not in_rst : <EOL> return '<STR_LIT>' , <NUM_LIT:0> <EOL> orig_sys_exit = sys . exit <EOL> orig_sys_stderr = sys . stderr <EOL> returncodes = [ ] <EOL> try : <EOL> sys . exit = returncodes . append <EOL> sys . stderr = stderr <EOL> pp = publish_parts ( in_rst , <EOL> writer_name = '<STR_LIT:html>' , <EOL> settings_overrides = dict ( exit_status_level = <NUM_LIT:2> , report_level = <NUM_LIT:2> ) , <EOL> enable_exit_status = True ) <EOL> finally : <EOL> sys . exit = orig_sys_exit <EOL> sys . stderr = orig_sys_stderr <EOL> return_value = '<STR_LIT>' <EOL> if '<STR_LIT:title>' in pp and pp [ '<STR_LIT:title>' ] : <EOL> return_value += '<STR_LIT>' . format ( pp [ '<STR_LIT:title>' ] ) <EOL> return_value += pp [ '<STR_LIT:body>' ] . strip ( ) <EOL> return return_value , returncodes . pop ( ) if returncodes else <NUM_LIT:0> </s>
