Dateianhang 'docbook2wiki.py'
Herunterladen 1 #!/usr/bin/python2.2
2
3 import xml.parsers.expat
4 import sys, string, re
5
6 class xml2wiki:
7 def __init__(self, infile=None, outfile=None):
8 self.infile = infile
9 self.outfile = outfile
10 self.out = None
11 self.p = xml.parsers.expat.ParserCreate()
12 self.p.StartElementHandler = self.start_element
13 self.p.EndElementHandler = self.end_element
14 self.p.DefaultHandler = self.default_handler
15 self.p.CharacterDataHandler = self.char_data
16 self.level = 0 # sections
17 self.listlevel = 0 # lists
18 self.ignore = 0
19 self.ignorelist = ['articleinfo', 'indexterm', 'application']
20 self.lastchars = ""
21 self.vt = 0 # verbatim
22
23 def feed(self, data):
24 self.p.Parse(data, 0)
25
26 def close(self):
27 self.p.Parse("", 1) # end of data
28 del self.p # get rid of circular references
29
30 # sections, lists, paragraphs
31 def start_article(self, attrs): self.level+=1
32 def end_article(self): self.level-=1
33 def start_sect1(self, attrs): self.level+=1
34 def end_sect1(self): self.level-=1
35 def start_sect2(self, attrs): self.level+=1
36 def end_sect2(self): self.level-=1
37 def start_variablelist(self, attrs): self.listlevel+=1
38 def end_variablelist(self): self.listlevel-=1
39 def start_varlistentry(self, attrs): self.write(" "*self.listlevel+"* ")
40 def end_varlistentry(self): pass
41 def start_listitem(self, attrs): self.write(" "*self.listlevel+" ")
42 def end_listitem(self): pass
43 def start_title(self, attrs): self.write("\n"+"="*self.level+" ")
44 def end_title(self): self.write(" "+"="*self.level+"\n")
45 def start_abstract(self, attrs): pass
46 def end_abstract(self): self.write("\n\n")
47 def start_para(self, attrs): pass
48 def end_para(self): self.write("\n\n")
49
50 # emphasis
51 def start_emphasis(self, attrs): self.write("''")
52 def end_emphasis(self): self.write("''")
53 def start_term(self, attrs): self.write("'''")
54 def end_term(self): self.write("'''")
55 def start_firstterm(self, attrs): self.write("'''")
56 def end_firstterm(self): self.write("'''")
57
58 # code
59 def start_programlisting(self, attrs): self.vt+=1; self.write("\n{{{")
60 def end_programlisting(self): self.vt-=1; self.write("}}}\n")
61 def start_screen(self, attrs): self.vt+=1; self.write("\n{{{")
62 def end_screen(self): self.vt-=1; self.write("}}}\n")
63 def start_markup(self, attrs): self.write("{{{")
64 def end_markup(self): self.write("}}}")
65 def start_command(self, attrs): self.write("{{{")
66 def end_command(self): self.write("}}}")
67 def start_option(self, attrs): self.write("{{{")
68 def end_option(self): self.write("}}}")
69 def start_filename(self, attrs): self.write("{{{")
70 def end_filename(self): self.write("}}}")
71
72 # links
73 def start_ulink(self, attrs): self.write("[%s " % attrs['url'])
74 def end_ulink(self): self.write("]")
75
76 def start_mediaobject(self, attrs): pass
77 def end_mediaobject(self): pass
78 def start_imageobject(self, attrs): pass
79 def end_imageobject(self): pass
80 def start_imagedata(self, attrs): self.write("\nattachment:%s\n" % attrs['fileref'])
81 def end_imagedata(self): self.write("\n")
82
83 # misc
84 def start_citetitle(self, attrs): pass
85 def end_citetitle(self): pass
86 def start_citerefentry(self, attrs): pass
87 def end_citerefentry(self): pass
88 def start_refentrytitle(self, attrs): self.write("'''")
89 def end_refentrytitle(self): self.write("'''")
90 def start_manvolnum(self, attrs): self.write("(")
91 def end_manvolnum(self): self.write(")")
92
93 def start_element(self, name, attrs):
94 if name in self.ignorelist: self.ignore+=1
95 if not self.ignore:
96 try:
97 return getattr(self, 'start_' + name)(attrs)
98 except:
99 self.write("<<<"+name+":")
100
101 def end_element(self, name):
102 if not self.ignore:
103 try:
104 return getattr(self, 'end_' + name)()
105 except:
106 self.write(":"+name+">>>")
107 if name in self.ignorelist: self.ignore-=1
108
109 def char_data(self, data):
110 if not self.ignore:
111 if data.startswith("\n\n") and self.lastchars.endswith("\n\n"):
112 data = data[2:]
113 elif data.startswith("\n") and self.lastchars.endswith("\n"):
114 data = data[1:]
115 if not self.vt:
116 data = ' '.join(data.strip().split())+" "
117 if len(data):
118 self.write(data)
119 if len(data)>1:
120 self.lastchars=data[-2]
121 else:
122 self.lastchars=data[-1]
123
124 def default_handler(self, data):
125 pass # self.write('Default:', repr(data))
126
127 def parse(self):
128 if self.infile:
129 f = open(self.infile)
130 else:
131 f = sys.stdin
132 if self.outfile:
133 self.out = open(self.outfile, "w")
134 else:
135 self.out = sys.stdout
136
137 text = f.read()
138 f.close()
139 text = re.sub(r'>\s*<',r'><',text) # kill whitespace between tags
140 self.feed(text)
141 self.close()
142
143 def write(self, what):
144 self.out.write(what)
145
146 wiki = "/org/org.linuxwiki/data/text/"
147 o = xml2wiki("DocBook-Demystification-HOWTO.xml", wiki+"DocBookDemystificationHowTo")
148 o.parse()
Gespeicherte Dateianhänge
Um Dateianhänge in eine Seite einzufügen sollte unbedingt eine Angabe wie attachment:dateiname benutzt werden, wie sie auch in der folgenden Liste der Dateien erscheint. Es sollte niemals die URL des Verweises ("laden") kopiert werden, da sich diese jederzeit ändern kann und damit der Verweis auf die Datei brechen würde.Sie dürfen keine Anhänge an diese Seite anhängen!