| #! /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 | |
| # ./dump_texinfo_tree_xml.py ../../doc/texinfo.texi > texinfo-tree.xml | |
| parser = argparse.ArgumentParser('output Texinfo document tree representation as XML') | |
| parser.add_argument('input_file') | |
| args = parser.parse_args() | |
| def protect_text(text): | |
| text = re.sub('&', '&', text) | |
| text = re.sub('<', '<', text) | |
| text = re.sub('>', '>', text) | |
| text = re.sub('"', '"', text) | |
| text = re.sub('\n', ' ', text) | |
| text = re.sub('\t', '	', text) | |
| # needed to protect a literal \f. | |
| text = re.sub(r"\\", r'\\\\', text) | |
| # cannot happen at all in XML | |
| text = re.sub("\f", r'\\f', text) | |
| return text; | |
| 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 integer_list_to_list(list_input): | |
| return [Texinfo.integer_list_integer_by_index(list_input, i) \ | |
| for i in range(0, Texinfo.integer_list_integers_number(list_input))] | |
| def attributes_strings(element, document): | |
| attributes_string_list = Texinfo.enumerate_element_attributes(element) | |
| attributes = string_list_to_list(attributes_string_list) | |
| result = '' | |
| for attribute in attributes: | |
| attribute_type = Texinfo.get_attribute_type(attribute) | |
| if attribute_type == Texinfo.att_type_string: | |
| value = protect_text(Texinfo.element_attribute_string(element, attribute)) | |
| elif attribute_type == Texinfo.att_type_integer: | |
| (value, value_set) = Texinfo.element_attribute_integer(element, attribute) | |
| if value_set != 0: | |
| sys.stderr.write("BUG: {attr}: integer value not found".format(attr=attribute)+"\n") | |
| continue | |
| else: | |
| value = str(value) | |
| else: | |
| if attribute_type == Texinfo.att_type_index_entry: | |
| index_entry = Texinfo.element_index_entry(document, element) | |
| values = [index_entry.index_name, str(index_entry.number)] | |
| elif attribute_type == Texinfo.att_type_string_list: | |
| values = [protect_text(string_value) for string_value | |
| in string_list_to_list(Texinfo.element_attribute_string_list(element, attribute))] | |
| elif attribute_type == Texinfo.att_type_integer_list: | |
| values = [str(integer_value) for integer_value | |
| in integer_list_to_list(Texinfo.element_attribute_integer_list(element, attribute))] | |
| else: | |
| sys.stderr.write("BUG: unknown attribute type "+attribute+": "+str(attribute_type)+"\n") | |
| sys.exit(1) | |
| value = ','.join(values) | |
| result += " "+attribute+'="'+value+'"' | |
| Texinfo.destroy_string_list(attributes_string_list) | |
| return result | |
| def element_open(element, document): | |
| texinfo_type = Texinfo.element_type(element) | |
| cmdname = Texinfo.element_cmdname(element) | |
| if cmdname is not None: | |
| element_name = "command" | |
| else: | |
| element_name = "container" | |
| result = "<"+element_name+' type="'+texinfo_type+'"' | |
| if cmdname is not None: | |
| result += ' cmdname="'+protect_text(cmdname)+'"' | |
| result += attributes_strings(element, document) | |
| source_info = Texinfo.element_source_info(element) | |
| if source_info: | |
| if source_info.line_nr > 0: | |
| result += ' sourcelinenumber="'+str(source_info.line_nr)+'"' | |
| if source_info.file_name is not None: | |
| result += ' sourcefilename="'+source_info.file_name+'"' | |
| if source_info.macro is not None: | |
| result += ' sourcemacro="'+source_info.macro+'"' | |
| result += ">" | |
| return result | |
| def element_close(element): | |
| cmdname = Texinfo.element_cmdname(element) | |
| if cmdname is not None: | |
| return "</command>" | |
| else: | |
| return "</container>" | |
| def element_source_marks(element, document): | |
| source_marks_nr = Texinfo.element_source_marks_number(element) | |
| if source_marks_nr > 0: | |
| result = "<sourcemarks>\n" | |
| source_marks = [Texinfo.element_get_source_mark(element, i) | |
| for i in range(0, source_marks_nr)] | |
| for source_mark in source_marks: | |
| result += '<sourcemark type="' + Texinfo.source_mark_get_type(source_mark) \ | |
| + '" status="' + Texinfo.source_mark_get_status(source_mark) \ | |
| + '" position="' + str(source_mark.position) \ | |
| + '" counter="' + str(source_mark.counter) + '">\n' | |
| if source_mark.line: | |
| result += "<sourcemarkstring>"+protect_text(source_mark.line) \ | |
| +"</sourcemarkstring>\n" | |
| if source_mark.element: | |
| result += convert_tree(source_mark.element, document) | |
| result += '</sourcemark>\n' | |
| result += "</sourcemarks>" | |
| return result | |
| else: | |
| return '' | |
| def convert_tree(tree, document): | |
| reader = Texinfo.new_reader(tree, document) | |
| result = '' | |
| while True: | |
| next_token = Texinfo.reader_read(reader) | |
| if not next_token: | |
| break | |
| element = next_token.element | |
| token_category = next_token.category | |
| if (token_category == Texinfo.TXI_READ_ELEMENT_START | |
| or token_category == Texinfo.TXI_READ_EMPTY): | |
| result += element_open(element, document) | |
| result += element_source_marks(element, document) | |
| if token_category == Texinfo.TXI_READ_EMPTY: | |
| result += element_close(element) | |
| result += "\n" | |
| elif token_category == Texinfo.TXI_READ_ELEMENT_END: | |
| result += element_close(element)+"\n" | |
| elif (token_category == Texinfo.TXI_READ_TEXT | |
| or token_category == Texinfo.TXI_READ_IGNORABLE_TEXT): | |
| texinfo_type = Texinfo.element_type(element) | |
| text = Texinfo.element_text(element) | |
| result += '<text type="'+texinfo_type+'"' | |
| result += attributes_strings(element, document) | |
| result += ">" | |
| result += element_source_marks(element, document) | |
| result += '<elementstring>'+protect_text(text)+"</elementstring></text>\n" | |
| else: | |
| # cannot happen | |
| texinfo_type = Texinfo.element_type(element) | |
| sys.stderr.write("BUG: "+texinfo_type+": "+ \ | |
| str(token_category)+": unknown token category\n") | |
| return result | |
| Texinfo.setup() | |
| input_file_name = args.input_file | |
| input_dir = os.path.dirname(input_file_name) | |
| parser = Texinfo.parser() | |
| Texinfo.parser_conf_add_include_directory(parser, input_dir) | |
| Texinfo.parser_conf_clear_expanded_formats(parser) | |
| # TODO add --if* to select format? | |
| #for txi_format in ('info', 'plaintext', 'html', 'latex', 'docbook', 'xml'): | |
| # Texinfo.parser_conf_add_expanded_format(parser, txi_format) | |
| Texinfo.parser_conf_add_expanded_format(parser, 'html') | |
| document, status = Texinfo.parse_file(parser, input_file_name) | |
| Texinfo.destroy_parser(parser) | |
| Texinfo.output_parser_error_messages(document) | |
| tree = Texinfo.document_tree(document) | |
| # The strings returned through SWIG should be UTF-8 | |
| sys.stdout.write('<?xml version=\"1.0\"?>\n') | |
| result = convert_tree(tree, document) | |
| sys.stdout.write(result) | |
| Texinfo.destroy_document(document) |