SEESenv/scripts/rst2docbook.py
author amit@thunder
Sat, 13 Feb 2010 13:32:38 +0530
changeset 5 c9c9819cd5e5
parent 4 231572ecfb59
child 6 1ce9b33fb6ff
permissions -rw-r--r--
tags added
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
     1
#!/usr/bin/python
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
     2
"""
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
     3
Just a hack to convert rst to xml and then docbook . 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
     4
May not containt all the required elements of a docbook .
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
     5
Just done to make it run for the specific rst for our 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
     6
sees documentation.
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
     7
"""
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
     8
import xml.etree.ElementTree as ET
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
     9
import os 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    10
import re
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    11
import subprocess
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    12
import os
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    13
import pkg_resources
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    14
import glob, os, re, sys
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    15
names = glob.glob('ch*.xml')
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    16
"""
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    17
chapterno=0
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    18
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    19
def convert2xml(file):
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    20
#    print folder,subfolder,file
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    21
    global chapterno
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    22
    name=file.split('/')[-1]
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    23
    name=str(chapterno)+name.split('.')[0]
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    24
#    full_file=os.path.join(folder,file)    
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    25
#    if file.endswith('.rst'):    
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    26
    print file	    
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    27
    xml_file=name+'.xml'        
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    28
    command="rst2xml.py %s > %s" %(file , xml_file)
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    29
    print command        
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    30
    a=subprocess.Popen(command , shell=True)
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    31
	   	
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    32
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    33
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    34
def walk(repo):
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    35
    global chapterno
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    36
    mainfolder='/home/amit/sttp_latest/'    
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    37
    for readline in open('index.config','r').readlines():
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    38
        chapterno+=1		
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    39
        filename=mainfolder+readline
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    40
        convert2xml(filename)
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    41
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    42
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    43
"""
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    44
def convert2docbook(xml_string):
4
231572ecfb59 Some more tags added
amit@thunder
parents: 2
diff changeset
    45
		
231572ecfb59 Some more tags added
amit@thunder
parents: 2
diff changeset
    46
	xml_string=re.sub('<strong>','<emphasis role="strong">', xml_string)   
231572ecfb59 Some more tags added
amit@thunder
parents: 2
diff changeset
    47
	xml_string=re.sub('<literal_block xml:space="preserve">','<programlisting>',xml_string)
231572ecfb59 Some more tags added
amit@thunder
parents: 2
diff changeset
    48
	xml_string=re.sub('</literal_block>','</programlisting>',xml_string)
231572ecfb59 Some more tags added
amit@thunder
parents: 2
diff changeset
    49
	xml_string=re.sub('<paragraph>' ,'<para>',xml_string)
231572ecfb59 Some more tags added
amit@thunder
parents: 2
diff changeset
    50
	xml_string=re.sub('</paragraph>' ,'</para>',xml_string)  
231572ecfb59 Some more tags added
amit@thunder
parents: 2
diff changeset
    51
	xml_string=re.sub('</strong>' ,'</emphasis>',xml_string)
231572ecfb59 Some more tags added
amit@thunder
parents: 2
diff changeset
    52
	xml_string=re.sub('<bullet_list bullet="[-*+]">','<itemizedlist mark="*">',xml_string,)
231572ecfb59 Some more tags added
amit@thunder
parents: 2
diff changeset
    53
	xml_string=re.sub('</bullet_list>','</itemizedlist>',xml_string)
231572ecfb59 Some more tags added
amit@thunder
parents: 2
diff changeset
    54
	xml_string=re.sub('<list_item>','<listitem>',xml_string)	
231572ecfb59 Some more tags added
amit@thunder
parents: 2
diff changeset
    55
	xml_string=re.sub('</list_item>','</listitem>',xml_string)	
231572ecfb59 Some more tags added
amit@thunder
parents: 2
diff changeset
    56
	xml_string=re.sub('<enumerated_list enumtype="arabic" prefix="" suffix=".">', '<orderedlist numeration="arabic">',xml_string)
231572ecfb59 Some more tags added
amit@thunder
parents: 2
diff changeset
    57
 	xml_string=re.sub('</enumerated_list>', '</orderedlist>',xml_string)
231572ecfb59 Some more tags added
amit@thunder
parents: 2
diff changeset
    58
	xml_string=re.sub('<entry>', '',xml_string)	
231572ecfb59 Some more tags added
amit@thunder
parents: 2
diff changeset
    59
	xml_string=re.sub('</entry>', '',xml_string) 	
231572ecfb59 Some more tags added
amit@thunder
parents: 2
diff changeset
    60
	xml_string=re.sub('<row>', '',xml_string)	
231572ecfb59 Some more tags added
amit@thunder
parents: 2
diff changeset
    61
	xml_string=re.sub('</row>', '',xml_string)	
231572ecfb59 Some more tags added
amit@thunder
parents: 2
diff changeset
    62
	xml_string=re.sub('<tbody>', '',xml_string)	
231572ecfb59 Some more tags added
amit@thunder
parents: 2
diff changeset
    63
	xml_string=re.sub('</tbody>', '',xml_string)	
231572ecfb59 Some more tags added
amit@thunder
parents: 2
diff changeset
    64
	xml_string=re.sub('<table>', '',xml_string)			
231572ecfb59 Some more tags added
amit@thunder
parents: 2
diff changeset
    65
	xml_string=re.sub('</table>', '',xml_string)	
231572ecfb59 Some more tags added
amit@thunder
parents: 2
diff changeset
    66
	xml_string=re.sub('<thead>', '',xml_string)	
231572ecfb59 Some more tags added
amit@thunder
parents: 2
diff changeset
    67
	xml_string=re.sub('</thead>', '',xml_string)	
231572ecfb59 Some more tags added
amit@thunder
parents: 2
diff changeset
    68
#	xml_string=re.sub('<tgroup cols="[1-90]*"><colspec colwidth="[1-90]*"/><colspec colwidth="[1-90]*"/><colspec colwidth="[1-90]"/>', '',xml_string)
231572ecfb59 Some more tags added
amit@thunder
parents: 2
diff changeset
    69
#	xml_string=re.sub('</tgroup', '',xml_string)	
231572ecfb59 Some more tags added
amit@thunder
parents: 2
diff changeset
    70
	chapter= ET.Element("chapter")
231572ecfb59 Some more tags added
amit@thunder
parents: 2
diff changeset
    71
	article=ET.SubElement(chapter,"article")
231572ecfb59 Some more tags added
amit@thunder
parents: 2
diff changeset
    72
	articleinfo=ET.SubElement(article,"articleinfo")
231572ecfb59 Some more tags added
amit@thunder
parents: 2
diff changeset
    73
		
231572ecfb59 Some more tags added
amit@thunder
parents: 2
diff changeset
    74
	try:	
231572ecfb59 Some more tags added
amit@thunder
parents: 2
diff changeset
    75
		tree = ET.fromstring(xml_string)
231572ecfb59 Some more tags added
amit@thunder
parents: 2
diff changeset
    76
	except:
231572ecfb59 Some more tags added
amit@thunder
parents: 2
diff changeset
    77
		print "xml_string problem"	
231572ecfb59 Some more tags added
amit@thunder
parents: 2
diff changeset
    78
	#	print "fromstring"	
231572ecfb59 Some more tags added
amit@thunder
parents: 2
diff changeset
    79
	try:    
231572ecfb59 Some more tags added
amit@thunder
parents: 2
diff changeset
    80
	        title= ET.SubElement(articleinfo,"title") 
231572ecfb59 Some more tags added
amit@thunder
parents: 2
diff changeset
    81
		title.text=tree.items()[1][1]
231572ecfb59 Some more tags added
amit@thunder
parents: 2
diff changeset
    82
	except:
231572ecfb59 Some more tags added
amit@thunder
parents: 2
diff changeset
    83
	        pass    
231572ecfb59 Some more tags added
amit@thunder
parents: 2
diff changeset
    84
	article.insert(1,tree)
231572ecfb59 Some more tags added
amit@thunder
parents: 2
diff changeset
    85
	xml_string=ET.tostring(chapter)
231572ecfb59 Some more tags added
amit@thunder
parents: 2
diff changeset
    86
	xml_string=re.sub('<document [A-Za-z=/_."]*>' ,'',xml_string)
231572ecfb59 Some more tags added
amit@thunder
parents: 2
diff changeset
    87
	xml_string=re.sub('</document>' ,'',xml_string)
231572ecfb59 Some more tags added
amit@thunder
parents: 2
diff changeset
    88
	return xml_string
231572ecfb59 Some more tags added
amit@thunder
parents: 2
diff changeset
    89
231572ecfb59 Some more tags added
amit@thunder
parents: 2
diff changeset
    90
    
0
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    91
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    92
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    93
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    94
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    95
if __name__=='__main__':
4
231572ecfb59 Some more tags added
amit@thunder
parents: 2
diff changeset
    96
	for name in names:
231572ecfb59 Some more tags added
amit@thunder
parents: 2
diff changeset
    97
		#print name        
5
c9c9819cd5e5 tags added
amit@thunder
parents: 4
diff changeset
    98
		xml_string=open(name,'r').read()
c9c9819cd5e5 tags added
amit@thunder
parents: 4
diff changeset
    99
		#xml_string=open('ch9Using_Linux_Tools.xml','r').read()
4
231572ecfb59 Some more tags added
amit@thunder
parents: 2
diff changeset
   100
		xml_string=convert2docbook(xml_string)
231572ecfb59 Some more tags added
amit@thunder
parents: 2
diff changeset
   101
		docbook_file=name.split('.')[0]+'.docbook'
231572ecfb59 Some more tags added
amit@thunder
parents: 2
diff changeset
   102
		f=open(docbook_file,'w')
231572ecfb59 Some more tags added
amit@thunder
parents: 2
diff changeset
   103
		try:        
231572ecfb59 Some more tags added
amit@thunder
parents: 2
diff changeset
   104
		    f.write(xml_string)
231572ecfb59 Some more tags added
amit@thunder
parents: 2
diff changeset
   105
		except:
231572ecfb59 Some more tags added
amit@thunder
parents: 2
diff changeset
   106
		        pass
0
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   107
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   108
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   109
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   110
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   111
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   112
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   113
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   114
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   115
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   116
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   117
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   118
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   119