📄 misc.py
字号:
else: error = state_machine.reporter.error( 'The "%s" directive is empty; content required.' % (name), line=lineno) return [error]replace.content = 1def unicode_directive(name, arguments, options, content, lineno, content_offset, block_text, state, state_machine): r""" Convert Unicode character codes (numbers) to characters. Codes may be decimal numbers, hexadecimal numbers (prefixed by ``0x``, ``x``, ``\x``, ``U+``, ``u``, or ``\u``; e.g. ``U+262E``), or XML-style numeric character entities (e.g. ``☮``). Text following ".." is a comment and is ignored. Spaces are ignored, and any other text remains as-is. """ if not isinstance(state, states.SubstitutionDef): error = state_machine.reporter.error( 'Invalid context: the "%s" directive can only be used within a ' 'substitution definition.' % (name), nodes.literal_block(block_text, block_text), line=lineno) return [error] substitution_definition = state_machine.node if options.has_key('trim'): substitution_definition.attributes['ltrim'] = 1 substitution_definition.attributes['rtrim'] = 1 if options.has_key('ltrim'): substitution_definition.attributes['ltrim'] = 1 if options.has_key('rtrim'): substitution_definition.attributes['rtrim'] = 1 codes = unicode_comment_pattern.split(arguments[0])[0].split() element = nodes.Element() for code in codes: try: decoded = directives.unicode_code(code) except ValueError, err: error = state_machine.reporter.error( 'Invalid character code: %s\n%s: %s' % (code, err.__class__.__name__, err), nodes.literal_block(block_text, block_text), line=lineno) return [error] element += nodes.Text(decoded) return element.childrenunicode_directive.arguments = (1, 0, 1)unicode_directive.options = {'trim': directives.flag, 'ltrim': directives.flag, 'rtrim': directives.flag}unicode_comment_pattern = re.compile(r'( |\n|^)\.\. ')def class_directive(name, arguments, options, content, lineno, content_offset, block_text, state, state_machine): """ Set a "class" attribute on the directive content or the next element. When applied to the next element, a "pending" element is inserted, and a transform does the work later. """ try: class_value = directives.class_option(arguments[0]) except ValueError: error = state_machine.reporter.error( 'Invalid class attribute value for "%s" directive: "%s".' % (name, arguments[0]), nodes.literal_block(block_text, block_text), line=lineno) return [error] node_list = [] if content: container = nodes.Element() state.nested_parse(content, content_offset, container) for node in container: node['classes'].extend(class_value) node_list.extend(container.children) else: pending = nodes.pending(misc.ClassAttribute, {'class': class_value, 'directive': name}, block_text) state_machine.document.note_pending(pending) node_list.append(pending) return node_listclass_directive.arguments = (1, 0, 1)class_directive.content = 1role_arg_pat = re.compile(r'(%s)\s*(\(\s*(%s)\s*\)\s*)?$' % ((states.Inliner.simplename,) * 2))def role(name, arguments, options, content, lineno, content_offset, block_text, state, state_machine): """Dynamically create and register a custom interpreted text role.""" if content_offset > lineno or not content: error = state_machine.reporter.error( '"%s" directive requires arguments on the first line.' % name, nodes.literal_block(block_text, block_text), line=lineno) return [error] args = content[0] match = role_arg_pat.match(args) if not match: error = state_machine.reporter.error( '"%s" directive arguments not valid role names: "%s".' % (name, args), nodes.literal_block(block_text, block_text), line=lineno) return [error] new_role_name = match.group(1) base_role_name = match.group(3) messages = [] if base_role_name: base_role, messages = roles.role( base_role_name, state_machine.language, lineno, state.reporter) if base_role is None: error = state.reporter.error( 'Unknown interpreted text role "%s".' % base_role_name, nodes.literal_block(block_text, block_text), line=lineno) return messages + [error] else: base_role = roles.generic_custom_role assert not hasattr(base_role, 'arguments'), ( 'Supplemental directive arguments for "%s" directive not supported' '(specified by "%r" role).' % (name, base_role)) try: (arguments, options, content, content_offset) = ( state.parse_directive_block(content[1:], content_offset, base_role, option_presets={})) except states.MarkupError, detail: error = state_machine.reporter.error( 'Error in "%s" directive:\n%s.' % (name, detail), nodes.literal_block(block_text, block_text), line=lineno) return messages + [error] if not options.has_key('class'): try: options['class'] = directives.class_option(new_role_name) except ValueError, detail: error = state_machine.reporter.error( 'Invalid argument for "%s" directive:\n%s.' % (name, detail), nodes.literal_block(block_text, block_text), line=lineno) return messages + [error] role = roles.CustomRole(new_role_name, base_role, options, content) roles.register_local_role(new_role_name, role) return messagesrole.content = 1def default_role(name, arguments, options, content, lineno, content_offset, block_text, state, state_machine): """Set the default interpreted text role.""" if not arguments: if roles._roles.has_key(''): # restore the "default" default role del roles._roles[''] return [] role_name = arguments[0] role, messages = roles.role( role_name, state_machine.language, lineno, state.reporter) if role is None: error = state.reporter.error( 'Unknown interpreted text role "%s".' % role_name, nodes.literal_block(block_text, block_text), line=lineno) return messages + [error] roles._roles[''] = role # @@@ should this be local to the document, not the parser? return messagesdefault_role.arguments = (0, 1, 0)def title(name, arguments, options, content, lineno, content_offset, block_text, state, state_machine): state_machine.document['title'] = arguments[0] return []title.arguments = (1, 0, 1)def date(name, arguments, options, content, lineno, content_offset, block_text, state, state_machine): if not isinstance(state, states.SubstitutionDef): error = state_machine.reporter.error( 'Invalid context: the "%s" directive can only be used within a ' 'substitution definition.' % (name), nodes.literal_block(block_text, block_text), line=lineno) return [error] format = '\n'.join(content) or '%Y-%m-%d' text = time.strftime(format) return [nodes.Text(text)]date.content = 1def directive_test_function(name, arguments, options, content, lineno, content_offset, block_text, state, state_machine): """This directive is useful only for testing purposes.""" if content: text = '\n'.join(content) info = state_machine.reporter.info( 'Directive processed. Type="%s", arguments=%r, options=%r, ' 'content:' % (name, arguments, options), nodes.literal_block(text, text), line=lineno) else: info = state_machine.reporter.info( 'Directive processed. Type="%s", arguments=%r, options=%r, ' 'content: None' % (name, arguments, options), line=lineno) return [info]directive_test_function.arguments = (0, 1, 1)directive_test_function.options = {'option': directives.unchanged_required}directive_test_function.content = 1
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -