<s> """<STR_LIT>""" <EOL> import re <EOL> import ast <EOL> from collections import OrderedDict <EOL> from . . lib . base_lexer import BaseGrammar , BaseLexer <EOL> from . . lib . markdown import MarkdownFilter <EOL> from . . lib . python import _is_python <EOL> from . . ext . six import StringIO <EOL> from . . utils . utils import _ensure_string , _preprocess <EOL> class PythonSplitGrammar ( BaseGrammar ) : <EOL> """<STR_LIT>""" <EOL> _triple_quotes = "<STR_LIT>" <EOL> _triple_doublequotes = '<STR_LIT>' <EOL> _triple = _triple_quotes + '<STR_LIT:|>' + _triple_doublequotes <EOL> text_var = re . compile ( r"<STR_LIT>" . format ( _triple ) ) <EOL> newline = re . compile ( r'<STR_LIT>' ) <EOL> linebreak = re . compile ( r'<STR_LIT>' ) <EOL> other = re . compile ( r'<STR_LIT>' . format ( _triple ) ) <EOL> class PythonSplitLexer ( BaseLexer ) : <EOL> """<STR_LIT>""" <EOL> grammar_class = PythonSplitGrammar <EOL> default_rules = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> def __init__ ( self ) : <EOL> super ( PythonSplitLexer , self ) . __init__ ( ) <EOL> self . _chunks = [ '<STR_LIT>' ] <EOL> @ property <EOL> def current ( self ) : <EOL> if not self . _chunks : <EOL> return None <EOL> else : <EOL> return self . _chunks [ - <NUM_LIT:1> ] <EOL> @ property <EOL> def chunks ( self ) : <EOL> return [ chunk for chunk in self . _chunks if chunk ] <EOL> @ current . setter <EOL> def current ( self , value ) : <EOL> self . _chunks [ - <NUM_LIT:1> ] = value <EOL> def new_chunk ( self ) : <EOL> self . _chunks . append ( '<STR_LIT>' ) <EOL> def append ( self , text ) : <EOL> self . current += text <EOL> def parse_newline ( self , m ) : <EOL> self . new_chunk ( ) <EOL> def parse_linebreak ( self , m ) : <EOL> self . append ( m . group ( <NUM_LIT:0> ) ) <EOL> def parse_text_var ( self , m ) : <EOL> self . append ( m . group ( <NUM_LIT:0> ) ) <EOL> def parse_other ( self , m ) : <EOL> self . append ( m . group ( <NUM_LIT:0> ) ) <EOL> def _split_python ( python ) : <EOL> """<STR_LIT>""" <EOL> python = _preprocess ( python ) <EOL> if not python : <EOL> return [ ] <EOL> lexer = PythonSplitLexer ( ) <EOL> lexer . read ( python ) <EOL> return lexer . chunks <EOL> def _is_chunk_markdown ( source ) : <EOL> """<STR_LIT>""" <EOL> lines = source . splitlines ( ) <EOL> if all ( line . startswith ( '<STR_LIT>' ) for line in lines ) : <EOL> source = '<STR_LIT:\n>' . join ( line [ <NUM_LIT:2> : ] for line in lines <EOL> if not line [ <NUM_LIT:2> : ] . startswith ( '<STR_LIT:#>' ) ) <EOL> if not source : <EOL> return True <EOL> return not _is_python ( source ) <EOL> return False <EOL> def _remove_hash ( source ) : <EOL> """<STR_LIT>""" <EOL> return '<STR_LIT:\n>' . join ( line [ <NUM_LIT:2> : ] . rstrip ( ) for line in source . splitlines ( ) ) <EOL> def _add_hash ( source ) : <EOL> """<STR_LIT>""" <EOL> source = '<STR_LIT:\n>' . join ( '<STR_LIT>' + line . rstrip ( ) <EOL> for line in source . splitlines ( ) ) <EOL> return source <EOL> class PythonReader ( object ) : <EOL> """<STR_LIT>""" <EOL> def read ( self , python ) : <EOL> chunks = _split_python ( python ) <EOL> for chunk in chunks : <EOL> if _is_chunk_markdown ( chunk ) : <EOL> yield self . _markdown_cell ( _remove_hash ( chunk ) ) <EOL> else : <EOL> yield self . _code_cell ( chunk ) <EOL> def _code_cell ( self , source ) : <EOL> return { '<STR_LIT>' : '<STR_LIT:code>' , <EOL> '<STR_LIT:input>' : source , <EOL> '<STR_LIT>' : None } <EOL> def _markdown_cell ( self , source ) : <EOL> return { '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:source>' : source } <EOL> class PythonWriter ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , keep_markdown = None ) : <EOL> self . _output = StringIO ( ) <EOL> self . _markdown_filter = MarkdownFilter ( keep_markdown ) <EOL> def _new_paragraph ( self ) : <EOL> self . _output . write ( '<STR_LIT>' ) <EOL> def append_comments ( self , source ) : <EOL> source = source . rstrip ( ) <EOL> source = self . _markdown_filter ( source ) <EOL> if not source : <EOL> return <EOL> comments = _add_hash ( source ) <EOL> self . _output . write ( comments ) <EOL> self . _new_paragraph ( ) <EOL> def append_code ( self , input ) : <EOL> self . _output . write ( input ) <EOL> self . _new_paragraph ( ) <EOL> def write ( self , cell ) : <EOL> if cell [ '<STR_LIT>' ] == '<STR_LIT>' : <EOL> self . append_comments ( cell [ '<STR_LIT:source>' ] ) <EOL> elif cell [ '<STR_LIT>' ] == '<STR_LIT:code>' : <EOL> self . append_code ( cell [ '<STR_LIT:input>' ] ) <EOL> @ property <EOL> def contents ( self ) : <EOL> return self . _output . getvalue ( ) . rstrip ( ) + '<STR_LIT:\n>' <EOL> def close ( self ) : <EOL> self . _output . close ( ) <EOL> def __del__ ( self ) : <EOL> self . close ( ) <EOL> PYTHON_FORMAT = dict ( <EOL> reader = PythonReader , <EOL> writer = PythonWriter , <EOL> file_extension = '<STR_LIT>' , <EOL> file_type = '<STR_LIT:text>' , <EOL> ) </s>
