我们从Python开源项目中,提取了以下10个代码示例,用于说明如何使用docutils.statemachine.string2lines()。
def run(self): oldStdout, sys.stdout = sys.stdout, StringIO() tab_width = self.options.get('tab-width', self.state.document.settings.tab_width) source = self.state_machine.input_lines.source(self.lineno - self.state_machine.input_offset - 1) try: exec('\n'.join(self.content)) text = sys.stdout.getvalue() lines = statemachine.string2lines(text, tab_width, convert_whitespace=True) self.state_machine.insert_input(lines, source) return [] except Exception: return [nodes.error(None, nodes.paragraph(text = "Unable to execute python code at %s:%d:" % (basename(source), self.lineno)), nodes.paragraph(text = str(sys.exc_info()[1])))] finally: sys.stdout = oldStdout
def parse(self, viewlist, filename): env = self.state.document.settings.env compat = self.options.get('compat', env.config.cautodoc_compat) clang = self.options.get('clang', env.config.cautodoc_clang) comments = parse(filename, compat=compat, clang=clang) for (comment, meta) in comments: lineoffset = meta['line'] - 1 lines = statemachine.string2lines(comment, 8, convert_whitespace=True) for line in lines: viewlist.append(line, filename, lineoffset) lineoffset += 1
def run(self): rst_file = self.state_machine.document.attributes['source'] data = subprocess.check_output( self.arguments[0].split(" ") + ['-h']).decode('utf8').split('\n') ind = next((i for i, val in enumerate(data) if re.match('\s{0,3}\{.*\}\s*$', val))) lines = ['.. code-block:: none', ''] + data[ind + 1:] self.state_machine.insert_input( statemachine.string2lines("\n".join(lines)), rst_file) return []
def _format_description(ctx): """Format the description for a given `click.Command`. We parse this as reStructuredText, allowing users to embed rich information in their help messages if they so choose. """ if not ctx.command.help: return for line in statemachine.string2lines( ctx.command.help, tab_width=4, convert_whitespace=True): yield line yield ''
def _format_option(opt): """Format the output for a `click.Option`.""" opt = _get_help_record(opt) yield '.. option:: {}'.format(opt[0]) if opt[1]: yield '' for line in statemachine.string2lines( opt[1], tab_width=4, convert_whitespace=True): yield _indent(line)
def _format_subcommand(command): """Format a sub-command of a `click.Command` or `click.Group`.""" yield '.. object:: {}'.format(command[0]) if command[1].short_help: yield '' for line in statemachine.string2lines( command[1].short_help, tab_width=4, convert_whitespace=True): yield _indent(line)
def run(self): """Most of this method is from ``docutils.parser.rst.Directive``. docutils version: 0.12 """ if not self.state.document.settings.file_insertion_enabled: raise self.warning('"%s" directive disabled.' % self.name) source = self.state_machine.input_lines.source( self.lineno - self.state_machine.input_offset - 1) source_dir = os.path.dirname(os.path.abspath(source)) path = rst.directives.path(self.arguments[0]) path = os.path.normpath(os.path.join(source_dir, path)) path = utils.relative_path(None, path) path = nodes.reprunicode(path) # get options (currently not use directive-specific options) encoding = self.options.get( 'encoding', self.state.document.settings.input_encoding) e_handler = self.state.document.settings.input_encoding_error_handler tab_width = self.options.get( 'tab-width', self.state.document.settings.tab_width) # open the inclding file try: self.state.document.settings.record_dependencies.add(path) include_file = io.FileInput(source_path=path, encoding=encoding, error_handler=e_handler) except UnicodeEncodeError as error: raise self.severe('Problems with "%s" directive path:\n' 'Cannot encode input file path "%s" ' '(wrong locale?).' % (self.name, SafeString(path))) except IOError as error: raise self.severe('Problems with "%s" directive path:\n%s.' % (self.name, ErrorString(error))) # read from the file try: rawtext = include_file.read() except UnicodeError as error: raise self.severe('Problem with "%s" directive:\n%s' % (self.name, ErrorString(error))) config = self.state.document.settings.env.config converter = M2R(no_underscore_emphasis=config.no_underscore_emphasis) include_lines = statemachine.string2lines(converter(rawtext), tab_width, convert_whitespace=True) self.state_machine.insert_input(include_lines, path) return []