| #! /usr/bin/env python | |
| # Copyright 2026 Free Software Foundation, Inc. | |
| # | |
| # This program is free software; you can redistribute it and/or modify | |
| # it under the terms of the GNU General Public License as published by | |
| # the Free Software Foundation; either version 3 of the License, | |
| # or (at your option) any later version. | |
| # | |
| # This program is distributed in the hope that it will be useful, | |
| # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| # GNU General Public License for more details. | |
| # | |
| # You should have received a copy of the GNU General Public License | |
| # along with this program. If not, see <https://www.gnu.org/licenses/>. | |
| import os | |
| import sys | |
| import argparse | |
| import re | |
| import Texinfo | |
| import xml.etree.ElementTree as ET | |
| parser = argparse.ArgumentParser('read XML represention of Texinfo tree and create a parsed document') | |
| parser.add_argument('input_file') | |
| args = parser.parse_args() | |
| def unprotect_text(text): | |
| # form feed is not possible in XML | |
| result = re.sub(r'\\\\', "\x1F", text); | |
| result = re.sub(r'\\f', "\f", result); | |
| result = re.sub('\x1F', r'\\', result); | |
| return result | |
| def set_text_attribute(document, element, txi_element): | |
| for key, value in element.items(): | |
| # we do not exclude XML attributes not associated to Texinfo attributes | |
| # such as cmdname or type, we rely on get_attribute_type returning | |
| attribute_type = Texinfo.get_attribute_type(key) | |
| if attribute_type == Texinfo.att_type_integer: | |
| Texinfo.set_element_attribute_integer(txi_element, key, int(value)) | |
| def set_attributes(document, element, txi_element): | |
| for key, value in element.items(): | |
| # we do not exclude XML attributes not associated to Texinfo attributes | |
| # such as cmdname, type or source*, we rely on get_attribute_type | |
| # returning att_type_none | |
| attribute_type = Texinfo.get_attribute_type(key) | |
| if attribute_type == Texinfo.att_type_integer: | |
| Texinfo.set_element_attribute_integer(txi_element, key, int(value)) | |
| elif attribute_type == Texinfo.att_type_string: | |
| Texinfo.set_element_attribute_string(txi_element, key, | |
| unprotect_text(value)) | |
| elif attribute_type == Texinfo.att_type_integer_list: | |
| int_list = Texinfo.new_integer_list() | |
| for int_value in value.split(','): | |
| Texinfo.add_to_integer_list(int_list, int(int_value)) | |
| Texinfo.set_element_attribute_integer_list(txi_element, key, int_list); | |
| elif attribute_type == Texinfo.att_type_string_list: | |
| text_list = Texinfo.new_string_list() | |
| element_cmdname = element.get("cmdname") | |
| # there can be commas in set second argument | |
| if element_cmdname == 'set': | |
| values = value.split(',', maxsplit=1) | |
| else: | |
| values = value.split(',') | |
| for text_value in values: | |
| Texinfo.add_string(text_list, unprotect_text(text_value)) | |
| Texinfo.set_element_attribute_string_list(txi_element, key, text_list); | |
| elif attribute_type == Texinfo.att_type_index_entry: | |
| index_name, entry_number = value.split(','); | |
| Texinfo.element_add_index_entry(document, txi_element, index_name, | |
| int(entry_number)); | |
| def set_source_info(document, element, txi_element): | |
| source_line = element.get("sourcelinenumber") | |
| source_file_name = element.get("sourcefilename") | |
| source_macro = element.get("sourcemacro") | |
| if source_line is not None or source_file_name is not None or source_macro is not None: | |
| if (source_file_name, source_macro) in source_infos: | |
| source_info = source_infos[(source_file_name, source_macro)] | |
| else: | |
| source_info = Texinfo.new_source_info(source_file_name, | |
| source_macro, document) | |
| source_infos[(source_file_name, source_macro)] = source_info | |
| if source_line is None: | |
| source_info.line_nr = int(0) | |
| else: | |
| source_info.line_nr = int(source_line) | |
| Texinfo.set_element_source_info(txi_element, source_info) | |
| def string_list_to_list(list_input): | |
| return [Texinfo.string_list_string_by_index(list_input, i) \ | |
| for i in range(0, Texinfo.string_list_strings_number(list_input))] | |
| def associate_title_command_anchor(current_node_relations, current, | |
| section_relations, heading_relations): | |
| if current_node_relations.associated_title_command is None: | |
| current_node_relations.associated_title_command = current | |
| if section_relations is not None: | |
| section_relations.associated_anchor_command = current_node_relations | |
| else: | |
| heading_relations.associated_anchor_command = current_node_relations | |
| # code in utils.c | |
| def empty_spaces_argument(txi_element): | |
| contents_nr = Texinfo.element_children_number(txi_element) | |
| if contents_nr == 0: | |
| return True | |
| for i in range(0, contents_nr): | |
| content = Texinfo.element_get_child(txi_element, i) | |
| if (Texinfo.element_text(content) is not None | |
| and Texinfo.element_type(content) != "spaces_before_argument"): | |
| return False | |
| elif Texinfo.element_cmdname(content) in ('c', 'comment'): | |
| pass | |
| else: | |
| return False | |
| return True | |
| # add information not already in the tree, in indices, global info, | |
| # global commands, structuring commands lists... | |
| def start_element_set_document_info(document, txi_element, current_node, | |
| current_section, current_part, current_encoding): | |
| cmdname = Texinfo.element_cmdname(txi_element) | |
| # code in end_line_misc_line | |
| if Texinfo.element_command_is_root(txi_element): | |
| if cmdname != 'node': | |
| section_relations = Texinfo.place_document_section_in_list(document, txi_element) | |
| if section_relations is not None: | |
| if cmdname != "part": | |
| if current_node is not None: | |
| associate_title_command_anchor (current_node, txi_element, | |
| section_relations, None) | |
| if current_node.associated_section is None: | |
| current_node.associated_section = section_relations | |
| section_relations.associated_node = current_node | |
| if current_part: | |
| section_relations.associated_part = current_part | |
| current_part.part_associated_section = section_relations | |
| current_part = None | |
| current_section = section_relations | |
| else: | |
| current_part = section_relations | |
| else: | |
| Texinfo.document_register_target_label(document, txi_element) | |
| node_relations = Texinfo.place_document_node_in_list(document, txi_element) | |
| if node_relations is not None: | |
| current_node = node_relations | |
| if current_part and current_part.part_associated_section is None: | |
| node_relations.node_preceding_part = current_part | |
| current_part.part_following_node = node_relations | |
| elif Texinfo.element_command_is_sectioning_heading(txi_element) \ | |
| or cmdname == "xrefname": | |
| heading_relations = Texinfo.place_document_heading_in_list(document, txi_element) | |
| if current_node: | |
| associate_title_command_anchor (current_node, txi_element, | |
| None, heading_relations) | |
| elif cmdname == "float": | |
| Texinfo.document_register_target_label(document, txi_element) | |
| float_type = Texinfo.element_attribute_string(txi_element, 'float_type') | |
| if float_type is not None: | |
| # the C char is used, not the float_type string, as the python string | |
| # will be reused when it goes out of scope | |
| Texinfo.add_to_document_float_record_list(document, | |
| txi_element, current_section) | |
| elif cmdname == "anchor" or cmdname == "namedanchor": | |
| Texinfo.document_register_target_label(document, txi_element) | |
| elif cmdname == 'menu': | |
| if current_node is not None: | |
| parent = Texinfo.element_parent(txi_element) | |
| if (Texinfo.element_cmdname(parent) is None | |
| or Texinfo.element_command_is_root(parent)): | |
| if current_node.menus is None: | |
| current_node.menus = Texinfo.new_const_element_list() | |
| Texinfo.add_to_const_element_list(current_node.menus, txi_element) | |
| elif cmdname == 'nodedescription': | |
| if current_node is not None: | |
| if current_node.node_descriptions is None: | |
| current_node.node_descriptions = Texinfo.new_const_element_list() | |
| Texinfo.add_to_const_element_list(current_node.node_descriptions, | |
| txi_element) | |
| elif cmdname == 'nodedescriptionblock': | |
| if current_node is not None: | |
| if current_node.node_long_descriptions is None: | |
| current_node.node_long_descriptions = Texinfo.new_const_element_list() | |
| Texinfo.add_to_const_element_list(current_node.node_long_descriptions, | |
| txi_element) | |
| elif (cmdname is None | |
| and Texinfo.element_type(txi_element) == "menu_entry_node"): | |
| do_register = Texinfo.element_attribute_integer_list(txi_element, | |
| 'node_content') | |
| if do_register is None: | |
| do_register = Texinfo.element_attribute_integer_list(txi_element, | |
| 'manual_content') | |
| if do_register is not None: | |
| Texinfo.document_register_internal_reference(document, txi_element) | |
| elif cmdname == 'synindex' or cmdname == 'syncodeindex': | |
| txi_misc_args = Texinfo.element_attribute_string_list(txi_element, 'misc_args') | |
| if txi_misc_args is not None: | |
| misc_args = string_list_to_list(txi_misc_args) | |
| Texinfo.document_set_merged_index(document, misc_args[0], misc_args[1]) | |
| if cmdname == 'syncodeindex': | |
| Texinfo.document_set_index_in_code(document, misc_args[0], 1) | |
| elif cmdname == 'defindex' or cmdname == 'defcodeindex': | |
| txi_misc_args = Texinfo.element_attribute_string_list(txi_element, 'misc_args') | |
| if txi_misc_args is not None: | |
| misc_args = string_list_to_list(txi_misc_args) | |
| if cmdname == 'defindex': | |
| Texinfo.document_set_index_in_code(document, misc_args[0], 0) | |
| else: | |
| Texinfo.document_set_index_in_code(document, misc_args[0], 1) | |
| elif cmdname == 'documentencoding': | |
| encoding_name = Texinfo.element_attribute_string(txi_element, 'encoding_name') | |
| if encoding_name is not None: | |
| current_encoding = encoding_name | |
| if Texinfo.element_command_is_global(txi_element): | |
| Texinfo.document_add_global_multi_command(document, txi_element) | |
| elif Texinfo.element_command_is_global_unique(txi_element): | |
| Texinfo.document_set_global_uniq_command(document, txi_element) | |
| return current_node, current_section, current_part, current_encoding | |
| # for code that requires contents elements to be set | |
| def end_element_set_document_info(document, txi_element, current_node, | |
| current_section, current_part, current_encoding): | |
| cmdname = Texinfo.element_cmdname(txi_element) | |
| if Texinfo.element_command_is_ref(txi_element): | |
| # code in handle_close_brace | |
| contents_nr = Texinfo.element_children_number(txi_element) | |
| if contents_nr > 0: | |
| arg_nr = 0 | |
| link_or_inforef = (cmdname == "link" or cmdname == "inforef") | |
| do_register = None | |
| for i in range(0, contents_nr): | |
| content = Texinfo.element_get_child(txi_element, i) | |
| if Texinfo.element_type(content) == "brace_arg": | |
| arg_nr += 1 | |
| if arg_nr == 1: | |
| do_register = Texinfo.element_attribute_integer_list(content, | |
| 'node_content') | |
| if do_register is None: | |
| break | |
| elif (link_or_inforef and arg_nr == 2 | |
| and not empty_spaces_argument(content)): | |
| do_register = None | |
| break | |
| elif (not link_or_inforef and (arg_nr == 4 or arg_nr == 5) | |
| and not empty_spaces_argument(content)): | |
| do_register = None | |
| break | |
| if do_register is not None: | |
| Texinfo.document_register_internal_reference(document, txi_element) | |
| Texinfo.setup() | |
| input_file_path = args.input_file | |
| input_dir = os.path.dirname(input_file_path) | |
| input_file_name = os.path.basename(input_file_path) | |
| document = Texinfo.new_document(); | |
| parents = [] | |
| sourcemarks_stack = [] | |
| source_infos = {} | |
| current_node = None | |
| current_part = None | |
| current_section = None | |
| current_encoding = None | |
| for event, element in ET.iterparse(input_file_path, ["start", "end"]): | |
| if element.tag == "text": | |
| if event == "start": | |
| element_type = element.get("type") | |
| txi_element = Texinfo.new_element(element_type, None); | |
| set_text_attribute(document, element, txi_element) | |
| Texinfo.add_element_to_element_contents(parents[-1][1], txi_element) | |
| parents.append([element, txi_element]) | |
| else: | |
| parents.pop() | |
| parents[-1][0].remove(element) | |
| elif element.tag == 'elementstring': | |
| if event == "end": | |
| txi_element = parents[-1][1] | |
| element_text = element.text; | |
| # text can be empty in rawline_text on comment line | |
| if element_text is not None: | |
| element_text = unprotect_text(element_text) | |
| Texinfo.element_append_text(txi_element, element_text) | |
| parents[-1][0].remove(element) | |
| elif element.tag == "sourcemarks": | |
| if event == "start": | |
| sourcemarks_stack.append([element]) | |
| else: | |
| sourcemarks_stack.pop() | |
| parents[-1][0].remove(element) | |
| elif element.tag == "sourcemark": | |
| if event == "start": | |
| parents.append([element, None]) | |
| sourcemarks_stack[-1].append({}) | |
| else: | |
| parents.pop() | |
| source_mark_txi_element = None | |
| source_mark = sourcemarks_stack[-1].pop() | |
| if 'element' in source_mark: | |
| source_mark_txi_element = source_mark['element'] | |
| source_mark_line = None | |
| if 'string' in source_mark: | |
| source_mark_line = source_mark['string'] | |
| Texinfo.element_add_source_mark(parents[-1][1], element.get("type"), | |
| element.get("status"), int(element.get("position")), | |
| int(element.get("counter")), source_mark_line, | |
| source_mark_txi_element) | |
| sourcemarks_stack[-1][0].remove(element) | |
| elif element.tag == "sourcemarkstring": | |
| if event == "end": | |
| sourcemarks_stack[-1][1]['string'] = unprotect_text(element.text) | |
| parents[-1][0].remove(element) | |
| else: | |
| if event == "start": | |
| element_type = element.get("type") | |
| element_cmdname = element.get("cmdname") | |
| txi_element = Texinfo.new_element(element_type, element_cmdname); | |
| set_attributes(document, element, txi_element) | |
| set_source_info(document, element, txi_element) | |
| if len(parents) == 0: | |
| Texinfo.set_document_root(document, txi_element) | |
| elif parents[-1][1] is None: | |
| # directly in source mark | |
| sourcemarks_stack[-1][1]['element'] = txi_element | |
| else: | |
| Texinfo.add_element_to_element_contents(parents[-1][1], txi_element) | |
| current_node, current_section, current_part, current_encoding \ | |
| = start_element_set_document_info(document, | |
| txi_element, current_node, current_section, | |
| current_part, current_encoding) | |
| parents.append([element, txi_element]) | |
| else: | |
| txi_element = parents[-1][1] | |
| parents.pop() | |
| end_element_set_document_info(document, | |
| txi_element, current_node, current_section, | |
| current_part, current_encoding) | |
| if len(parents) > 0: | |
| parents[-1][0].remove(element) | |
| if event == "end": | |
| element.clear() | |
| Texinfo.document_resolve_indices_merged_in(document) | |
| Texinfo.document_float_list_to_listoffloats_list(document) | |
| Texinfo.document_set_labels_identifiers_target(document) | |
| Texinfo.document_set_global_information(document, input_file_name, input_dir, | |
| current_encoding, None) | |
| # select structure information | |
| Texinfo.complete_transform_document(document, | |
| Texinfo.STTF_nodes_tree | Texinfo.STTF_floats, 1, 0, 0); | |
| # select HTML transformations | |
| #Texinfo.complete_transform_document(document, | |
| # Texinfo.STTF_relate_index_entries_to_table_items | |
| # | Texinfo.STTF_move_index_entries_after_items | |
| # | Texinfo.STTF_no_warn_non_empty_parts | |
| # | Texinfo.STTF_nodes_tree | Texinfo.STTF_floats, 0, 1, 0); | |
| tree = Texinfo.document_tree(document) | |
| result = Texinfo.tree_print_details(tree) | |
| #result = '' | |
| float_text = Texinfo.print_document_listoffloats(document) | |
| if float_text is not None and float_text != '': | |
| result += "\nLISTOFFLOATS\n" + float_text | |
| indices_info_text = Texinfo.print_document_indices_information(document) | |
| if indices_info_text is not None and indices_info_text != '': | |
| result += "\nINDICES\n" + indices_info_text | |
| nodes_list_text = Texinfo.print_nodes_list(document) | |
| if nodes_list_text is not None and nodes_list_text != '': | |
| result += "\nNODES_LIST\n" + nodes_list_text | |
| sections_list_text = Texinfo.print_sections_list(document) | |
| if sections_list_text is not None and sections_list_text != '': | |
| result += "\nSECTIONS_LIST\n" + sections_list_text | |
| headings_list_text = Texinfo.print_headings_list(document) | |
| if headings_list_text is not None and headings_list_text != '': | |
| result += "\nHEADINGS_LIST\n" + headings_list_text | |
| sys.stderr.write(result) | |
| sys.stdout.write(Texinfo.convert_to_texinfo(tree)) | |
| Texinfo.destroy_document(document) | |
| for source_info in source_infos.values(): | |
| Texinfo.destroy_source_info(source_info) |