scripts/rst2docbook.py
changeset 0 8083d21c0020
equal deleted inserted replaced
-1:000000000000 0:8083d21c0020
       
     1 #!/usr/bin/python
       
     2 """
       
     3 Just a hack to convert rst to xml and then docbook . 
       
     4 May not containt all the required elements of a docbook .
       
     5 Just done to make it run for the specific rst for our 
       
     6 sees documentation.
       
     7 """
       
     8 import xml.etree.ElementTree as ET
       
     9 import os 
       
    10 import re
       
    11 import subprocess
       
    12 import os
       
    13 import pkg_resources
       
    14 import glob, os, re, sys
       
    15 names = glob.glob('ch*.xml')
       
    16 """
       
    17 chapterno=0
       
    18 
       
    19 def convert2xml(file):
       
    20 #    print folder,subfolder,file
       
    21     global chapterno
       
    22     name=file.split('/')[-1]
       
    23     name=str(chapterno)+name.split('.')[0]
       
    24 #    full_file=os.path.join(folder,file)    
       
    25 #    if file.endswith('.rst'):    
       
    26     print file	    
       
    27     xml_file=name+'.xml'        
       
    28     command="rst2xml.py %s > %s" %(file , xml_file)
       
    29     print command        
       
    30     a=subprocess.Popen(command , shell=True)
       
    31 	   	
       
    32 
       
    33 
       
    34 def walk(repo):
       
    35     global chapterno
       
    36     mainfolder='/home/amit/sttp_latest/'    
       
    37     for readline in open('index.config','r').readlines():
       
    38         chapterno+=1		
       
    39         filename=mainfolder+readline
       
    40         convert2xml(filename)
       
    41 
       
    42 
       
    43 """
       
    44 def convert2docbook(xml_string):
       
    45 #    xml_file_obj=open(file,'r')
       
    46 #    xml_string=xml_file_obj.read() 
       
    47 #    xml_file_obj.close()       
       
    48     try:
       
    49         xml_string=re.sub('<strong>','<emphasis role="strong">', xml_string)   
       
    50         xml_string=re.sub('<literal_block','<programlisting>',xml_string)
       
    51         xml_string=re.sub('</literal_block>','</programlisting>',xml_string)
       
    52         xml_string=re.sub('xml:space="preserve">','',xml_string)
       
    53         xml_string=re.sub('<paragraph>' ,'<para>',xml_string)
       
    54         xml_string=re.sub('</paragraph>' ,'</para>',xml_string)  
       
    55         xml_string=re.sub('</strong>' ,'</emphasis>',xml_string)
       
    56         chapter= ET.Element("chapter")
       
    57         article=ET.SubElement(chapter,"article")
       
    58         articleinfo=ET.SubElement(article,"articleinfo")
       
    59         tree = ET.fromstring(xml_string)
       
    60         title= ET.SubElement(articleinfo,"title")   
       
    61         try:    
       
    62             title.text=tree.items()[1][1]
       
    63         except:
       
    64                 pass    
       
    65         article.insert(1,tree)
       
    66         xml_string=ET.tostring(chapter)
       
    67         xml_string=re.sub('<document ids=.*">' ,'',xml_string)
       
    68         xml_string=re.sub('</document>' ,'',xml_string)
       
    69         return xml_string
       
    70 #    docbook_file=file.split('.')[0]+'.docbook'
       
    71 #    f=open(docbook_file,'w')
       
    72 #    f.write(xml_string)
       
    73 #    xml_string=None
       
    74     except :
       
    75         pass
       
    76 
       
    77 
       
    78 
       
    79 
       
    80 if __name__=='__main__':
       
    81 #    repo='/home/amit/sphinxvirt/review/'
       
    82 #    walk(repo)
       
    83 #    convert(1,2,3)
       
    84     for name in names:
       
    85         print name        
       
    86         xml_string=open(name,'r').read()
       
    87          
       
    88         xml_string=convert2docbook(xml_string)
       
    89         docbook_file=name.split('.')[0]+'.docbook'
       
    90         f=open(docbook_file,'w')
       
    91         try:        
       
    92             f.write(xml_string)
       
    93         except:
       
    94                 pass
       
    95 
       
    96 
       
    97 
       
    98 
       
    99 
       
   100 
       
   101 
       
   102 
       
   103 
       
   104 
       
   105 
       
   106 
       
   107 
       
   108 
       
   109 
       
   110 
       
   111 
       
   112 
       
   113 
       
   114 
       
   115 
       
   116 
       
   117 
       
   118 
       
   119 
       
   120 
       
   121 
       
   122 
       
   123 
       
   124 
       
   125 
       
   126