我们从Python开源项目中,提取了以下34个代码示例,用于说明如何使用docutils.nodes.NodeVisitor()。
def __init__(self, document, termsize=None, **options): nodes.NodeVisitor.__init__(self, document) self.document = document self.output = '' self.lines = [''] self.line = 0 self.indent_width = 2 self.termsize = termsize or get_terminal_size((80,20)) self.options = options self.references = [] self.refcount = 0 self.ctx = self.Context() self.ctx_stack = [] self.style = self.StyleContext() self.style_stack = []
def __init__(self, document, builder): nodes.NodeVisitor.__init__(self, document) self.builder = builder newlines = builder.config.text_newlines if newlines == 'windows': self.nl = '\r\n' elif newlines == 'native': self.nl = os.linesep else: self.nl = '\n' self.sectionchars = builder.config.text_sectionchars self.states = [[]] self.stateindent = [0] self.list_counter = [] self.sectionlevel = 0 self.lineblocklevel = 0 self.table = None
def __init__(self, document, cols, rows, width): nodes.NodeVisitor.__init__(self, document) self.cols = cols self.rows = rows self.width = width self.height = 0
def __init__(self, document): nodes.NodeVisitor.__init__(self, document) self.level = 0 self.widths = [] self.heights = [] self.rows = 0
def __init__(self, props, document, **options): nodes.NodeVisitor.__init__(self, document) self.props = props self.level = 0 self.line = 0 self.cursor = 0 self.col = 0 self.row = 0 self.nb_rows = 0 self.options = options
def __init__(self, document, builder, destination, docname, basepath): nodes.NodeVisitor.__init__(self, document) self.builder = builder self.sectionlevel = 0 self.sectioncount = [0, 0, 0, 0, 0, 0] self.contents = []; #[ self.destination = destination self.docname = docname self.basepath = basepath self.state = ['regular']
def _make_visit_admonition(name): def visit_admonition(self, node): # type: (nodes.NodeVisitor, nodes.Node) -> None # self.end_state(first=admonitionlabels[name] + ': ') info("visit", node) return visit_admonition
def __init__(self, document): nodes.NodeVisitor.__init__(self, document) self.nl = os.linesep self.states = [[]] self.stateindent = [0] self.list_counter = [] self.sectionlevel = 0 self.table = None
def __init__(self, *args, **kw): nodes.NodeVisitor.__init__(self, *args, **kw) self.result = [] self.article = Article() self._main_title_visited = False
def __init__(self, document, builder): nodes.NodeVisitor.__init__(self, document) self.builder = builder self.init_settings() self.written_ids = set() # node names and anchors in output # node names and anchors that should be in output self.referenced_ids = set() self.indices = [] # (node name, content) self.short_ids = {} # anchors --> short ids self.node_names = {} # node name --> node's name to display self.node_menus = {} # node name --> node's menu entries self.rellinks = {} # node name --> (next, previous, up) self.collect_indices() self.collect_node_names() self.collect_node_menus() self.collect_rellinks() self.body = [] self.context = [] self.previous_section = None self.section_level = 0 self.seen_title = False self.next_section_ids = set() self.escape_newlines = 0 self.escape_hyphens = 0 self.curfilestack = [] self.footnotestack = [] self.in_footnote = 0 self.handled_abbrs = set()
def __init__(self, document): nodes.NodeVisitor.__init__(self, document) self.body = [] self.section_level = 0 self.par_level = -1 self.lists = [] # stack of the currents bullets self.listLevel = len(self.lists) self.bullet = '*' # next one to add to the next paragraph self.figures = 0 # Counts figures for reference targets self.inTable = False self.turnsInList = 0 self.inDesc = False self.inList = False
def __init__(self, document): nodes.NodeVisitor.__init__(self, document) self._found = False
def render_symbol_html(self, node, code, options, prefix='symbol', imgcls=None, alt=None): # type: (nodes.NodeVisitor, symbolator, unicode, Dict, unicode, unicode, unicode) -> Tuple[unicode, unicode] # NOQA format = self.builder.config.symbolator_output_format try: if format not in ('png', 'svg'): raise SymbolatorError("symbolator_output_format must be one of 'png', " "'svg', but is %r" % format) fname, outfn = render_symbol(self, code, options, format, prefix) except SymbolatorError as exc: logger.warning('symbolator code %r: ' % code + str(exc)) raise nodes.SkipNode if fname is None: self.body.append(self.encode(code)) else: if alt is None: alt = node.get('alt', self.encode(code).strip()) imgcss = imgcls and 'class="%s"' % imgcls or '' if format == 'svg': svgtag = '''<object data="%s" type="image/svg+xml"> <p class="warning">%s</p></object>\n''' % (fname, alt) self.body.append(svgtag) else: if 'align' in node: self.body.append('<div align="%s" class="align-%s">' % (node['align'], node['align'])) self.body.append('<img src="%s" alt="%s" %s/>\n' % (fname, alt, imgcss)) if 'align' in node: self.body.append('</div>\n') raise nodes.SkipNode
def html_visit_symbolator(self, node): # type: (nodes.NodeVisitor, symbolator) -> None render_symbol_html(self, node, node['code'], node['options'])
def render_symbol_latex(self, node, code, options, prefix='symbol'): # type: (nodes.NodeVisitor, symbolator, unicode, Dict, unicode) -> None try: fname, outfn = render_symbol(self, code, options, 'pdf', prefix) except SymbolatorError as exc: logger.warning('symbolator code %r: ' % code + str(exc)) raise nodes.SkipNode is_inline = self.is_inline(node) if is_inline: para_separator = '' else: para_separator = '\n' if fname is not None: post = None # type: unicode if not is_inline and 'align' in node: if node['align'] == 'left': self.body.append('{') post = '\\hspace*{\\fill}}' elif node['align'] == 'right': self.body.append('{\\hspace*{\\fill}') post = '}' self.body.append('%s\\includegraphics{%s}%s' % (para_separator, fname, para_separator)) if post: self.body.append(post) raise nodes.SkipNode
def latex_visit_symbolator(self, node): # type: (nodes.NodeVisitor, symbolator) -> None render_symbol_latex(self, node, node['code'], node['options'])
def texinfo_visit_symbolator(self, node): # type: (nodes.NodeVisitor, symbolator) -> None render_symbol_texinfo(self, node, node['code'], node['options'])
def text_visit_symbolator(self, node): # type: (nodes.NodeVisitor, symbolator) -> None if 'alt' in node.attributes: self.add_text(_('[symbol: %s]') % node['alt']) else: self.add_text(_('[symbol]')) raise nodes.SkipNode
def man_visit_symbolator(self, node): # type: (nodes.NodeVisitor, symbolator) -> None if 'alt' in node.attributes: self.body.append(_('[symbol: %s]') % node['alt']) else: self.body.append(_('[symbol]')) raise nodes.SkipNode
def __init__(self, props, document, **options): nodes.NodeVisitor.__init__(self, document) self.props = props self.level = 0 self.lines = [''] self.line = 0 self.cursor = 0 self.col = 0 self.row = 0 self.nb_rows = 0 self.options = options def unicode_intersection(char, next): switch = { ('?', '?'): '?', ('?', '?'): '?', ('?', '?'): '?', ('?', '?'): '?', ('?', '?'): '?', ('?', '?'): '?', ('?', '?'): '?', (' ', '?'): '?', ('?', '?'): '?', ('?', '?'): '?', ('?', '?'): '?', ('?', '?'): '?', ('?', '?'): '?', ('?', '?'): '?', ('?', '?'): '?', ('?', '?'): '?', (' ', '?'): '?', ('?', '?'): '?', } return switch[(u(char), u(next))] if options.get('unicode', False): self.char_single_rule = '?' self.char_double_rule = '?' self.char_vertical_rule = '?' self.get_intersection = unicode_intersection self.top_left = '?' self.top_right = '?' self.bottom_left = '?' else: self.char_single_rule = '-' self.char_double_rule = '=' self.char_vertical_rule = '|' self.get_intersection = lambda *args: '+' self.top_left = self.bottom_left = self.top_right = '+'
def __init__(self, document): nodes.NodeVisitor.__init__(self, document) self.settings = settings = document.settings lcode = settings.language_code self.language = languages.get_language(lcode, document.reporter) self.meta = [self.generator % docutils.__version__] self.head_prefix = [] self.html_prolog = [] if settings.xml_declaration: self.head_prefix.append(self.xml_declaration % settings.output_encoding) # self.content_type = "" # encoding not interpolated: self.html_prolog.append(self.xml_declaration) self.head = self.meta[:] self.stylesheet = [self.stylesheet_call(path) for path in utils.get_stylesheet_list(settings)] self.body_prefix = ['</head>\n<body>\n'] # document title, subtitle display self.body_pre_docinfo = [] # author, date, etc. self.docinfo = [] self.body = [] self.fragment = [] self.body_suffix = ['</body>\n</html>\n'] self.section_level = 0 self.initial_header_level = int(settings.initial_header_level) self.math_output = settings.math_output.split() self.math_output_options = self.math_output[1:] self.math_output = self.math_output[0].lower() # A heterogenous stack used in conjunction with the tree traversal. # Make sure that the pops correspond to the pushes: self.context = [] self.topic_classes = [] # TODO: replace with self_in_contents self.colspecs = [] self.compact_p = True self.compact_simple = False self.compact_field_list = False self.in_docinfo = False self.in_sidebar = False self.in_footnote_list = False self.title = [] self.subtitle = [] self.header = [] self.footer = [] self.html_head = [self.content_type] # charset not interpolated self.html_title = [] self.html_subtitle = [] self.html_body = [] self.in_document_title = 0 # len(self.body) or 0 self.in_mailto = False self.author_in_authors = False # for html4css1 self.math_header = []
def __init__(self, document): nodes.NodeVisitor.__init__(self, document) self.settings = settings = document.settings lcode = settings.language_code self.language = languages.get_language(lcode, document.reporter) self.meta = [self.generator % docutils.__version__] self.head_prefix = [] self.html_prolog = [] if settings.xml_declaration: self.head_prefix.append(self.xml_declaration % settings.output_encoding) # self.content_type = "" # encoding not interpolated: self.html_prolog.append(self.xml_declaration) self.head = self.meta[:] self.stylesheet = [self.stylesheet_call(path) for path in utils.get_stylesheet_list(settings)] self.body_prefix = ['</head>\n<body>\n'] # document title, subtitle display self.body_pre_docinfo = [] # author, date, etc. self.docinfo = [] self.body = [] self.fragment = [] self.body_suffix = ['</body>\n</html>\n'] self.section_level = 0 self.initial_header_level = int(settings.initial_header_level) self.math_output = settings.math_output.split() self.math_output_options = self.math_output[1:] self.math_output = self.math_output[0].lower() self.context = [] """Heterogeneous stack. Used by visit_* and depart_* functions in conjunction with the tree traversal. Make sure that the pops correspond to the pushes.""" self.topic_classes = [] # TODO: replace with self_in_contents self.colspecs = [] self.compact_p = True self.compact_simple = False self.compact_field_list = False self.in_docinfo = False self.in_sidebar = False self.in_footnote_list = False self.title = [] self.subtitle = [] self.header = [] self.footer = [] self.html_head = [self.content_type] # charset not interpolated self.html_title = [] self.html_subtitle = [] self.html_body = [] self.in_document_title = 0 # len(self.body) or 0 self.in_mailto = False self.author_in_authors = False # for html4css1 self.math_header = []
def __init__(self, document): nodes.NodeVisitor.__init__(self, document) self.settings = settings = document.settings lcode = settings.language_code self.language = languages.get_language(lcode, document.reporter) self.meta = [self.generator % docutils.__version__] self.head_prefix = [] self.html_prolog = [] if settings.xml_declaration: self.head_prefix.append(self.xml_declaration % settings.output_encoding) # encoding not interpolated: self.html_prolog.append(self.xml_declaration) self.head = self.meta[:] self.stylesheet = [self.stylesheet_call(path) for path in utils.get_stylesheet_list(settings)] self.body_prefix = ['</head>\n<body>\n'] # document title, subtitle display self.body_pre_docinfo = [] # author, date, etc. self.docinfo = [] self.body = [] self.fragment = [] self.body_suffix = ['</body>\n</html>\n'] self.section_level = 0 self.initial_header_level = int(settings.initial_header_level) self.math_output = settings.math_output.split() self.math_output_options = self.math_output[1:] self.math_output = self.math_output[0].lower() # A heterogenous stack used in conjunction with the tree traversal. # Make sure that the pops correspond to the pushes: self.context = [] self.topic_classes = [] self.colspecs = [] self.compact_p = True self.compact_simple = False self.compact_field_list = False self.in_docinfo = False self.in_sidebar = False self.title = [] self.subtitle = [] self.header = [] self.footer = [] self.html_head = [self.content_type] # charset not interpolated self.html_title = [] self.html_subtitle = [] self.html_body = [] self.in_document_title = 0 # len(self.body) or 0 self.in_mailto = False self.author_in_authors = False self.math_header = []
def render_symbol(self, code, options, format, prefix='symbol'): # type: (nodes.NodeVisitor, unicode, Dict, unicode, unicode) -> Tuple[unicode, unicode] """Render symbolator code into a PNG or SVG output file.""" symbolator_cmd = options.get('symbolator_cmd', self.builder.config.symbolator_cmd) hashkey = (code + str(options) + str(symbolator_cmd) + str(self.builder.config.symbolator_cmd_args)).encode('utf-8') # Use name option if present otherwise fallback onto SHA-1 hash name = options.get('name', sha1(hashkey).hexdigest()) fname = '%s-%s.%s' % (prefix, name, format) relfn = posixpath.join(self.builder.imgpath, fname) outfn = path.join(self.builder.outdir, self.builder.imagedir, fname) if path.isfile(outfn): return relfn, outfn if (hasattr(self.builder, '_symbolator_warned_cmd') and self.builder._symbolator_warned_cmd.get(symbolator_cmd)): return None, None ensuredir(path.dirname(outfn)) # Symbolator expects UTF-8 by default if isinstance(code, text_type): code = code.encode('utf-8') cmd_args = [symbolator_cmd] cmd_args.extend(self.builder.config.symbolator_cmd_args) cmd_args.extend(['-i', '-', '-f', format, '-o', outfn]) try: p = Popen(cmd_args, stdout=PIPE, stdin=PIPE, stderr=PIPE) except OSError as err: if err.errno != ENOENT: # No such file or directory raise logger.warning('symbolator command %r cannot be run (needed for symbolator ' 'output), check the symbolator_cmd setting', symbolator_cmd) if not hasattr(self.builder, '_symbolator_warned_cmd'): self.builder._symbolator_warned_cmd = {} self.builder._symbolator_warned_cmd[symbolator_cmd] = True return None, None try: # Symbolator may close standard input when an error occurs, # resulting in a broken pipe on communicate() stdout, stderr = p.communicate(code) except (OSError, IOError) as err: if err.errno not in (EPIPE, EINVAL): raise # in this case, read the standard output and standard error streams # directly, to get the error message(s) stdout, stderr = p.stdout.read(), p.stderr.read() p.wait() if p.returncode != 0: raise SymbolatorError('symbolator exited with error:\n[stderr]\n%s\n' '[stdout]\n%s' % (stderr, stdout)) if not path.isfile(outfn): raise SymbolatorError('symbolator did not produce an output file:\n[stderr]\n%s\n' '[stdout]\n%s' % (stderr, stdout)) return relfn, outfn