2121from index_transform .common import IndexTransform
2222from xml_utils import xml_escape
2323from index_transform import *
24+ from lxml import etree
2425import io
2526
2627class Index2Devhelp (IndexTransform ):
2728
28- def __init__ (self , out_file ):
29+ def __init__ (self , functions_el ):
2930 super ().__init__ ()
30- self .out_file = out_file
31+ self .functions_el = functions_el
3132
3233 def get_mark (self , el ):
3334 if el .tag == 'const' : return 'macro'
@@ -44,32 +45,31 @@ def get_mark(self, el):
4445 return ''
4546
4647 def process_item_hook (self , el , full_name , full_link ):
47- self .out_file .write ('<keyword type="' + xml_escape (self .get_mark (el ))
48- + '" name="' + xml_escape (full_name )
49- + '" link="' + xml_escape (full_link ) + '"/>\n ' )
48+ keyword_el = etree .SubElement (self .functions_el , 'keyword' )
49+ keyword_el .set ('type' , self .get_mark (el ))
50+ keyword_el .set ('name' , full_name )
51+ keyword_el .set ('link' , full_link )
52+
5053 IndexTransform .process_item_hook (self , el , full_name , full_link )
5154
5255def transform_devhelp (book_title , book_name , book_base , rel_link , chapters_fn ,
5356 in_fn ):
54- out_f = io . StringIO ( )
55- out_f . write ( '<?xml version="1.0"?> \n '
56- + '<book title="' + xml_escape ( book_title )
57- + '" xmlns="http://www.devhelp.net/book'
58- + '" name="' + xml_escape ( book_name )
59- + '" base="' + xml_escape ( book_base )
60- + '" link="' + xml_escape ( rel_link )
61- + '" version="2" language=" c++"> \n ' )
57+ root_el = etree . Element ( 'book' )
58+ root_el . set ( 'xmlns' , 'http://www.devhelp.net/book' )
59+ root_el . set ( ' title' , book_title )
60+ root_el . set ( 'name' , book_name )
61+ root_el . set ( 'base' , book_base )
62+ root_el . set ( 'link' , rel_link )
63+ root_el . set ( 'version' , '2' )
64+ root_el . set ( ' language' , ' c++' )
6265
63- with open (chapters_fn , 'r' , encoding = 'utf-8' ) as chapters_f :
64- out_f . write ( chapters_f . read () + ' \n ' )
66+ chapters_tree = etree . parse (chapters_fn )
67+ root_el . append ( chapters_tree . getroot () )
6568
66- out_f . write ( '< functions> ' )
69+ functions_el = etree . SubElement ( root_el , ' functions' )
6770
68- tr = Index2Devhelp (out_f )
71+ tr = Index2Devhelp (functions_el )
6972 tr .transform_file (in_fn )
7073
71- out_f .write ('''
72- </functions>
73- </book>
74- ''' )
75- return out_f .getvalue ()
74+ return etree .tostring (root_el , pretty_print = True , xml_declaration = True ,
75+ encoding = 'utf-8' )
0 commit comments