#! /usr/bin/env python # Copyright 2024 Tomas Brabec # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. try: import locale locale.setlocale(locale.LC_ALL, '') except: pass import docutils.frontend import docutils.utils import docutils.parsers def is_note(node): return node is not None and isinstance(node, docutils.nodes.note) def my_condition(node): return node is not None and isinstance(node, docutils.nodes.list_item) and node.astext() == 'a' settings = docutils.frontend.OptionParser().get_default_values() parser_class = docutils.parsers.get_parser_class('restructuredtext') parser = parser_class() option_parser = docutils.frontend.OptionParser( components=(parser,), read_config_files=True, description='') settings = option_parser.parse_args() document = docutils.utils.new_document('', settings) text = ''' My title ======== .. note:: 1st paragraph 2nd paragraph 3rd paragraph .. note:: 4th paragraph - a - b ''' parser.parse(text, document) print(document.pformat()) print(20 * '=') # in-place replacement of nodes for node in document.traverse(condition=is_note): parent = node.parent; childs = node.children; parent.replace(node,childs) # note: the above is equivalent to ``node.replace_self(node.children)`` # deletion of nodes for node in document.traverse(condition=my_condition): node.parent.remove(node) print(document.pformat())