Revision 5604
trunk/docs/scripts/mkbook.py | ||
---|---|---|
1 |
#!/usr/bin/python |
|
2 |
import os |
|
3 |
import sys |
|
4 |
import shutil |
|
5 |
|
|
6 |
def adjustConfigFile(dir, sxwname, configFilePath="/etc/ooo2dbk.xml"): |
|
7 |
f=file(configFilePath,"r") |
|
8 |
todo=f.read() |
|
9 |
f.close() |
|
10 |
newLine="imagesRelativeDirectory=\"images%s\"" %os.path.splitext(sxwname)[0] |
|
11 |
newLine=newLine.replace("_","") |
|
12 |
todo=todo.replace("imagesRelativeDirectory=\"images\"",newLine) |
|
13 |
configFileName="%s.xml" % os.path.join(dir,sxwname) |
|
14 |
f=file(configFileName,"w") |
|
15 |
f.write(todo) |
|
16 |
f.close() |
|
17 |
|
|
18 |
def sxw2docbook(sxwdir, sxwname, docbookdir): |
|
19 |
shutil.copyfile( |
|
20 |
os.path.join(sxwdir,sxwname), |
|
21 |
os.path.join(docbookdir,sxwname) |
|
22 |
) |
|
23 |
os.chdir(docbookdir) |
|
24 |
adjustConfigFile(docbookdir, sxwname) |
|
25 |
os.system("ooo2dbk -a -c%s %s" % ("%s.xml" % os.path.join(docbookdir,sxwname),os.path.join(docbookdir,sxwname))) |
|
26 |
os.unlink(sxwname) |
|
27 |
os.unlink("%s.xml" % sxwname) |
|
28 |
|
|
29 |
def adjustDocbook(docbookPathName, rootBookDir): |
|
30 |
f=file(docbookPathName,"r") |
|
31 |
todo=f.readlines() |
|
32 |
f.close() |
|
33 |
del todo[0] # Eliminamos <? xml ... |
|
34 |
del todo[0] # Eliminanos <ENTITY .... |
|
35 |
f=file(docbookPathName,"w") |
|
36 |
for line in todo: |
|
37 |
f.write(line) |
|
38 |
f.close() |
|
39 |
|
|
40 |
def translatePath(src,dst,path): |
|
41 |
# Si path esta en src, lo traslada a dst |
|
42 |
return os.path.join( |
|
43 |
dst, |
|
44 |
path[ len(src)+1: ] |
|
45 |
) |
|
46 |
|
|
47 |
|
|
48 |
def srcBook2html(rootBookDir, binRootBookDir, distRootBookDir): |
|
49 |
if distRootBookDir[-1] !="/": |
|
50 |
distRootBookDir="%s/"%distRootBookDir |
|
51 |
print "A?adiendo barra de directorio a distRootBookDir ", distRootBookDir |
|
52 |
|
|
53 |
for root, dirs, files in os.walk(rootBookDir): |
|
54 |
for dir in dirs: |
|
55 |
if dir !="CVS": |
|
56 |
try: |
|
57 |
os.makedirs( os.path.join(binRootBookDir, dir) ) |
|
58 |
os.makedirs( os.path.join(distRootBookDir, dir) ) |
|
59 |
except: |
|
60 |
pass |
|
61 |
dstPath = translatePath(rootBookDir,binRootBookDir,root) |
|
62 |
#saltar los directorio CVS |
|
63 |
if os.path.split(root)[1] !="CVS": |
|
64 |
for fname in files: |
|
65 |
extension = os.path.splitext(fname)[1] # " .xxx" con el punto |
|
66 |
if extension ==".sxw": |
|
67 |
sxw2docbook(root, fname, dstPath) |
|
68 |
adjustFile=os.path.join(dstPath,"%s%s" %( |
|
69 |
os.path.splitext(fname)[0], |
|
70 |
".docb.xml" |
|
71 |
) |
|
72 |
) |
|
73 |
adjustDocbook(adjustFile,rootBookDir) |
|
74 |
if extension ==".docbook": |
|
75 |
shutil.copyfile( |
|
76 |
os.path.join(root,fname), |
|
77 |
os.path.join( |
|
78 |
dstPath, |
|
79 |
fname |
|
80 |
) |
|
81 |
) |
|
82 |
|
|
83 |
xsltFile="/usr/share/xml/docbook/stylesheet/nwalsh/xhtml/chunk.xsl" |
|
84 |
sourceFileName=os.path.join( |
|
85 |
binRootBookDir, |
|
86 |
"principal.docbook" |
|
87 |
) |
|
88 |
|
|
89 |
for root, dirs, files in os.walk(binRootBookDir): |
|
90 |
currentDir=root.split("/")[-1] |
|
91 |
if currentDir.startswith("images"): |
|
92 |
dstPath=os.path.join( |
|
93 |
distRootBookDir, |
|
94 |
root.split("/")[-2], |
|
95 |
currentDir |
|
96 |
) |
|
97 |
try: |
|
98 |
os.makedirs(dstPath) |
|
99 |
except: |
|
100 |
pass |
|
101 |
for fname in files: |
|
102 |
shutil.copyfile( |
|
103 |
os.path.join(root,fname), |
|
104 |
os.path.join( |
|
105 |
dstPath, |
|
106 |
fname |
|
107 |
) |
|
108 |
) |
|
109 |
|
|
110 |
os.chdir(distRootBookDir) |
|
111 |
os.system("xsltproc -o distRootBookDir %s %s"%(xsltFile,sourceFileName)) |
|
112 |
adjustHTML(distRootBookDir) |
|
113 |
|
|
114 |
def adjustHTML(distRootBookDir): |
|
115 |
for root, dirs, files in os.walk(distRootBookDir): |
|
116 |
for fname in files: |
|
117 |
extension = os.path.splitext(fname)[1] |
|
118 |
if extension ==".html": |
|
119 |
f=file(os.path.join(root,fname),"r") |
|
120 |
todo=f.read() |
|
121 |
f.close() |
|
122 |
todo=todo.replace("<img src=\"..","<img src=\".") |
|
123 |
f=file(os.path.join(root,fname),"w") |
|
124 |
f.write(todo) |
|
125 |
f.close() |
|
126 |
|
|
127 |
|
|
128 |
def main(): |
|
129 |
print "mkbook\n rootBookDir %s\n binRootBookDir %s\n distRootBookDir %s"%( |
|
130 |
sys.argv[1], |
|
131 |
sys.argv[2], |
|
132 |
sys.argv[3] |
|
133 |
) |
|
134 |
|
|
135 |
srcBook2html( |
|
136 |
sys.argv[1], |
|
137 |
sys.argv[2], |
|
138 |
sys.argv[3] |
|
139 |
) |
|
140 |
|
|
141 |
|
|
142 |
|
|
143 |
main() |
|
0 | 144 |
Also available in: Unified diff