(name, arguments, options, content, lineno,
                             content_offset, block_text, state, state_machine)
return [retnode]
Like .. include:: :literal:, but only warns if the include file is not found.
Like .. include:: :literal:, but only warns if the include file is not found.
Like .. include :: : literal : but only warns if the include file is not found .
def literalinclude_directive(name, arguments, options, content, lineno,
                             content_offset, block_text, state, state_machine):
    """Like .. include:: :literal:, but only warns if the include file is not found."""
    if not state.document.settings.file_insertion_enabled:
        return [state.document.reporter.warning('File insertion disabled', line=lineno)]
    env = state.document.settings.env
    rel_fn = arguments[0]
    source_dir = path.dirname(path.abspath(state_machine.input_lines.source(
        lineno - state_machine.input_offset - 1)))
    fn = path.normpath(path.join(source_dir, rel_fn))

    if 'pyobject' in options and 'lines' in options:
        return [state.document.reporter.warning(
            'Cannot use both "pyobject" and "lines" options', line=lineno)]

    encoding = options.get('encoding', env.config.source_encoding)
    try:
        f = codecs.open(fn, 'r', encoding)
        lines = f.readlines()
        f.close()
    except (IOError, OSError):
        return [state.document.reporter.warning(
            'Include file %r not found or reading it failed' % arguments[0],
            line=lineno)]
    except UnicodeError:
        return [state.document.reporter.warning(
            'Encoding %r used for reading included file %r seems to '
            'be wrong, try giving an :encoding: option' %
            (encoding, arguments[0]))]

    objectname = options.get('pyobject')
    if objectname is not None:
        from sphinx.pycode import ModuleAnalyzer
        analyzer = ModuleAnalyzer.for_file(fn, '')
        tags = analyzer.find_tags()
        if objectname not in tags:
            return [state.document.reporter.warning(
                'Object named %r not found in include file %r' %
                (objectname, arguments[0]), line=lineno)]
        else:
            lines = lines[tags[objectname][1] - 1 : tags[objectname][2] - 1]

    linespec = options.get('lines')
    if linespec is not None:
        try:
            linelist = parselinenos(linespec, len(lines))
        except ValueError, err:
            return [state.document.reporter.warning(str(err), line=lineno)]
        lines = [lines[i] for i in linelist]

    startafter = options.get('start-after')
    endbefore = options.get('end-before')
    if startafter is not None or endbefore is not None:
        use = not startafter
        res = []
        for line in lines:
            if not use and startafter in line:
                use = True
            elif use and endbefore in line:
                use = False
                break
            elif use:
                res.append(line)
        lines = res

    text = ''.join(lines)
    text = re.sub("\r\n", "\n", text)
    
    retnode = nodes.literal_block(text, text, source=fn)
    retnode.line = 1
    if options.get('language', ''):
        retnode['language'] = options['language']
    if 'linenos' in options:
        retnode['linenos'] = True
    state.document.settings.env.note_dependency(rel_fn)
    return [retnode]
def literalinclude_directive ( name , arguments , options , content , lineno , content_offset , block_text , state , state_machine ) : if not state . document . settings . file_insertion_enabled : return [ state . document . reporter . warning ( 'File insertion disabled' , line = lineno ) ] env = state . document . settings . env rel_fn = arguments [ 0 ] source_dir = path . dirname ( path . abspath ( state_machine . input_lines . source ( lineno - state_machine . input_offset - 1 ) ) ) fn = path . normpath ( path . join ( source_dir , rel_fn ) ) if 'pyobject' in options and 'lines' in options : return [ state . document . reporter . warning ( 'Cannot use both "pyobject" and "lines" options' , line = lineno ) ] encoding = options . get ( 'encoding' , env . config . source_encoding ) try : f = codecs . open ( fn , 'r' , encoding ) lines = f . readlines ( ) f . close ( ) except ( IOError , OSError ) : return [ state . document . reporter . warning ( 'Include file %r not found or reading it failed' % arguments [ 0 ] , line = lineno ) ] except UnicodeError : return [ state . document . reporter . warning ( 'Encoding %r used for reading included file %r seems to ' 'be wrong, try giving an :encoding: option' % ( encoding , arguments [ 0 ] ) ) ] objectname = options . get ( 'pyobject' ) if objectname is not None : from sphinx . pycode import ModuleAnalyzer analyzer = ModuleAnalyzer . for_file ( fn , '' ) tags = analyzer . find_tags ( ) if objectname not in tags : return [ state . document . reporter . warning ( 'Object named %r not found in include file %r' % ( objectname , arguments [ 0 ] ) , line = lineno ) ] else : lines = lines [ tags [ objectname ] [ 1 ] - 1 : tags [ objectname ] [ 2 ] - 1 ] linespec = options . get ( 'lines' ) if linespec is not None : try : linelist = parselinenos ( linespec , len ( lines ) ) except ValueError , err : return [ state . document . reporter . warning ( str ( err ) , line = lineno ) ] lines = [ lines [ i ] for i in linelist ] startafter = options . get ( 'start-after' ) endbefore = options . get ( 'end-before' ) if startafter is not None or endbefore is not None : use = not startafter res = [ ] for line in lines : if not use and startafter in line : use = True elif use and endbefore in line : use = False break elif use : res . append ( line ) lines = res text = '' . join ( lines ) text = re . sub ( "\r\n" , "\n" , text ) retnode = nodes . literal_block ( text , text , source = fn ) retnode . line = 1 if options . get ( 'language' , '' ) : retnode [ 'language' ] = options [ 'language' ] if 'linenos' in options : retnode [ 'linenos' ] = True state . document . settings . env . note_dependency ( rel_fn ) return [ retnode ]