SEESenv/scripts/rst2docbook.py
author amit@thunder
Fri, 12 Feb 2010 01:11:21 +0530
changeset 2 52d12eb31c30
parent 0 scripts/rst2docbook.py@8083d21c0020
child 4 231572ecfb59
permissions -rw-r--r--
Virtual enviroment for SEES-hacks 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):
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    45
#    xml_file_obj=open(file,'r')
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    46
#    xml_string=xml_file_obj.read() 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    47
#    xml_file_obj.close()       
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    48
    try:
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    49
        xml_string=re.sub('<strong>','<emphasis role="strong">', xml_string)   
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    50
        xml_string=re.sub('<literal_block','<programlisting>',xml_string)
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    51
        xml_string=re.sub('</literal_block>','</programlisting>',xml_string)
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    52
        xml_string=re.sub('xml:space="preserve">','',xml_string)
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    53
        xml_string=re.sub('<paragraph>' ,'<para>',xml_string)
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    54
        xml_string=re.sub('</paragraph>' ,'</para>',xml_string)  
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    55
        xml_string=re.sub('</strong>' ,'</emphasis>',xml_string)
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    56
        chapter= ET.Element("chapter")
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    57
        article=ET.SubElement(chapter,"article")
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    58
        articleinfo=ET.SubElement(article,"articleinfo")
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    59
        tree = ET.fromstring(xml_string)
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    60
        title= ET.SubElement(articleinfo,"title")   
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    61
        try:    
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    62
            title.text=tree.items()[1][1]
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    63
        except:
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    64
                pass    
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    65
        article.insert(1,tree)
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    66
        xml_string=ET.tostring(chapter)
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    67
        xml_string=re.sub('<document ids=.*">' ,'',xml_string)
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    68
        xml_string=re.sub('</document>' ,'',xml_string)
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    69
        return xml_string
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    70
#    docbook_file=file.split('.')[0]+'.docbook'
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    71
#    f=open(docbook_file,'w')
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    72
#    f.write(xml_string)
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    73
#    xml_string=None
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    74
    except :
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    75
        pass
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    76
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    77
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    78
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    79
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    80
if __name__=='__main__':
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    81
#    repo='/home/amit/sphinxvirt/review/'
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    82
#    walk(repo)
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    83
#    convert(1,2,3)
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    84
    for name in names:
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    85
        print name        
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    86
        xml_string=open(name,'r').read()
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    87
         
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    88
        xml_string=convert2docbook(xml_string)
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    89
        docbook_file=name.split('.')[0]+'.docbook'
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    90
        f=open(docbook_file,'w')
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    91
        try:        
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    92
            f.write(xml_string)
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    93
        except:
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    94
                pass
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    95
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    96
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    97
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    98
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    99
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   100
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   101
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   102
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   103
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   104
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   105
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   106
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
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   120
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   121
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   122
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   123
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   124
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   125
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   126