2 # -*- coding: UTF-8 -*-
3 from match
import Match
6 """ binds some xml content page with additional markup - in this
7 variant we set the rootnode container to 'reference' and the DTD
8 to the Docbook 4.1.2 version. Modify as you like."""
9 has_title_child
= [ "book", "chapter", "section", "reference" ]
11 ' PUBLIC "-//OASIS//DTD DocBook XML V4.1.2//EN"'+"\n"+
12 ' "http://www.oasis-open.org/docbook/xml/4.1.2/docbookx.dtd"')
13 def __init__(self
, o
, filename
= None):
15 self
.rootnode
= "reference"
16 self
.filename
= filename
20 """ add some content """
24 if self
.title
: return title
25 try: return self
.text
[0].get_title()
26 except Exception, e
: pass
28 def _xml_doctype(self
, rootnode
):
29 return "<!DOCTYPE "+rootnode
+self
.docbook_dtd
+">"
30 def _xml_text(self
, xml
):
31 """ accepts adapter objects with .xml_text() """
32 try: return xml
.xml_text()
33 except Exception, e
: print "DocbookDocument/text", e
; pass
35 def _fetch_rootnode(self
, text
):
36 fetch
= Match(r
"^[^<>]*<(\w+)\b")
37 if text
& fetch
: return fetch
[1]
39 def _filename(self
, filename
):
40 if filename
is not None:
41 self
.filename
= filename
42 filename
= self
.filename
43 if not filename
& Match(r
"\.\w+$"):
45 if not ext
: ext
= "docbook"
48 def save(self
, filename
= None):
49 filename
= self
._filename
(filename
)
50 print "writing '"+filename
+"'"
51 if len(self
.text
) > 1:
52 self
.save_all(filename
)
54 self
.save_text(filename
, self
.text
[0])
55 def save_text(self
, filename
, text
):
57 fd
= open(filename
, "w")
58 xml_text
= self
._xml
_text
(text
)
59 rootnode
= self
._fetch
_rootnode
(xml_text
)
60 doctype
= self
._xml
_doctype
(rootnode
)
66 print "could not open '"+filename
+"'file", e
68 def save_all(self
, filename
):
69 assert len(self
.text
) > 1
71 fd
= open(filename
, "w")
72 xml_text
= self
._xml
_text
(self
.text
[0])
73 rootnode
= self
._fetch
_rootnode
(xml_text
)
74 if rootnode
== self
.rootnode
:
77 rootnode
= self
.rootnode
78 doctype
= self
._xml
_doctype
(rootnode
)
80 title
= self
.get_title()
81 if title
and self
.rootnode
in self
.has_title_child
:
82 print >>fd
, "<"+self
.rootnode
+'><title>'+title
+'</title>'
84 print >>fd
, "<"+self
.rootnode
+' id="'+title
+'">'
86 print >>fd
, "<"+self
.rootnode
+'>'
87 for text
in self
.text
:
88 text
= self
._xml
_text
(text
)
90 print >>fd
, "</"+self
.rootnode
+">"
94 print "could not open '"+filename
+"'file", e