<s> """<STR_LIT>""" <EOL> from __future__ import absolute_import , print_function <EOL> import importlib <EOL> from os . path import basename <EOL> import re <EOL> import sys <EOL> import textwrap <EOL> from docutils import nodes <EOL> from docutils . statemachine import ViewList <EOL> import jinja2 <EOL> from sphinx . util . compat import Directive <EOL> from sphinx . util . nodes import nested_parse_with_titles <EOL> JINJA_TEMPLATE = jinja2 . Template ( u"""<STR_LIT>""" ) <EOL> DOCPAT = re . compile ( r"<STR_LIT>" , flags = re . MULTILINE | re . DOTALL ) <EOL> class BokehJinjaDirective ( Directive ) : <EOL> has_content = True <EOL> required_arguments = <NUM_LIT:1> <EOL> def run ( self ) : <EOL> env = self . state . document . settings . env <EOL> app = env . app <EOL> template_path = self . arguments [ <NUM_LIT:0> ] <EOL> module_path , template_name = template_path . rsplit ( '<STR_LIT:.>' , <NUM_LIT:1> ) <EOL> try : <EOL> module = importlib . import_module ( module_path ) <EOL> except ImportError : <EOL> msg = "<STR_LIT>" % module_path <EOL> app . warn ( msg ) <EOL> node = nodes . error ( None , <EOL> nodes . paragraph ( text = msg ) , <EOL> nodes . paragraph ( text = str ( sys . exc_info ( ) [ <NUM_LIT:1> ] ) ) ) <EOL> return [ node ] <EOL> template = getattr ( module , template_name , None ) <EOL> if template is None : <EOL> msg = "<STR_LIT>" % template_path <EOL> app . warn ( msg ) <EOL> node = nodes . error ( None , nodes . paragraph ( text = msg ) ) <EOL> return [ node ] <EOL> template_text = open ( template . filename ) . read ( ) <EOL> m = DOCPAT . match ( template_text ) <EOL> if m : doc = m . group ( <NUM_LIT:1> ) <EOL> else : doc = None <EOL> filename = basename ( template . filename ) <EOL> rst_text = JINJA_TEMPLATE . render ( <EOL> name = template_name , <EOL> module = module_path , <EOL> objrepr = repr ( template ) , <EOL> doc = "<STR_LIT>" if doc is None else textwrap . dedent ( doc ) , <EOL> lang = filename . rsplit ( '<STR_LIT:.>' , <NUM_LIT:1> ) [ - <NUM_LIT:1> ] , <EOL> filename = filename , <EOL> template_text = DOCPAT . sub ( "<STR_LIT>" , template_text ) , <EOL> ) <EOL> result = ViewList ( ) <EOL> for line in rst_text . split ( "<STR_LIT:\n>" ) : <EOL> result . append ( line , "<STR_LIT>" ) <EOL> node = nodes . paragraph ( ) <EOL> node . document = self . state . document <EOL> nested_parse_with_titles ( self . state , result , node ) <EOL> return node . children <EOL> def setup ( app ) : <EOL> app . add_directive_to_domain ( '<STR_LIT>' , '<STR_LIT>' , BokehJinjaDirective ) </s>
