| 1 |
#! /usr/bin/env python3 |
| 2 |
# -*- coding: UTF-8 -*- |
| 3 |
|
| 4 |
from zzipdoc.match import Match |
| 5 |
|
| 6 |
class DocbookDocument: |
| 7 |
""" binds some xml content page with additional markup - in this |
| 8 |
variant we set the rootnode container to 'reference' and the DTD |
| 9 |
to the Docbook 4.1.2 version. Modify as you like.""" |
| 10 |
has_title_child = [ "book", "chapter", "section", "reference" ] |
| 11 |
docbook_dtd = ( |
| 12 |
' PUBLIC "-//OASIS//DTD DocBook XML V4.1.2//EN"'+"\n"+ |
| 13 |
' "http://www.oasis-open.org/docbook/xml/4.1.2/docbookx.dtd"') |
| 14 |
def __init__(self, o, filename = None): |
| 15 |
self.o = o |
| 16 |
self.rootnode = "reference" |
| 17 |
self.filename = filename |
| 18 |
self.title = "" |
| 19 |
self.text = [] |
| 20 |
def add(self, text): |
| 21 |
""" add some content """ |
| 22 |
self.text += [ text ] |
| 23 |
return self |
| 24 |
def get_title(self): |
| 25 |
if self.title: return title |
| 26 |
try: return self.text[0].get_title() |
| 27 |
except Exception as e: pass |
| 28 |
return self.title |
| 29 |
def _xml_doctype(self, rootnode): |
| 30 |
return "<!DOCTYPE "+rootnode+self.docbook_dtd+">" |
| 31 |
def _xml_text(self, xml): |
| 32 |
""" accepts adapter objects with .xml_text() """ |
| 33 |
try: return xml.xml_text() |
| 34 |
except Exception as e: print("DocbookDocument/text " + e); pass |
| 35 |
return str(xml) |
| 36 |
def _fetch_rootnode(self, text): |
| 37 |
fetch = Match(r"^[^<>]*<(\w+)\b") |
| 38 |
if text & fetch: return fetch[1] |
| 39 |
return self.rootnode |
| 40 |
def _filename(self, filename): |
| 41 |
if filename is not None: |
| 42 |
self.filename = filename |
| 43 |
filename = self.filename |
| 44 |
if not filename & Match(r"\.\w+$"): |
| 45 |
ext = self.o.docbook |
| 46 |
if not ext: ext = "docbook" |
| 47 |
filename += "."+ext |
| 48 |
return filename |
| 49 |
def save(self, filename = None): |
| 50 |
filename = self._filename(filename) |
| 51 |
print("writing '"+filename+"'") |
| 52 |
if len(self.text) > 1: |
| 53 |
self.save_all(filename) |
| 54 |
else: |
| 55 |
self.save_text(filename, self.text[0]) |
| 56 |
def save_text(self, filename, text): |
| 57 |
try: |
| 58 |
fd = open(filename, "w") |
| 59 |
xml_text = self._xml_text(text) |
| 60 |
rootnode = self._fetch_rootnode(xml_text) |
| 61 |
doctype = self._xml_doctype(rootnode) |
| 62 |
print(doctype, file=fd) |
| 63 |
print(xml_text, file=fd) |
| 64 |
fd.close() |
| 65 |
return True |
| 66 |
except IOError as e: |
| 67 |
print("could not open '"+filename+"'file" + str(e)) |
| 68 |
return False |
| 69 |
def save_all(self, filename): |
| 70 |
assert len(self.text) > 1 |
| 71 |
try: |
| 72 |
fd = open(filename, "w") |
| 73 |
xml_text = self._xml_text(self.text[0]) |
| 74 |
rootnode = self._fetch_rootnode(xml_text) |
| 75 |
if rootnode == self.rootnode: |
| 76 |
rootnode = "book" |
| 77 |
else: |
| 78 |
rootnode = self.rootnode |
| 79 |
doctype = self._xml_doctype(rootnode) |
| 80 |
print(doctype, file=fd) |
| 81 |
title = self.get_title() |
| 82 |
if title and self.rootnode in self.has_title_child: |
| 83 |
print("<"+self.rootnode+'><title>'+title+'</title>', file=fd) |
| 84 |
elif title: |
| 85 |
print("<"+self.rootnode+' id="'+title+'">', file=fd) |
| 86 |
else: |
| 87 |
print("<"+self.rootnode+'>', file=fd) |
| 88 |
for text in self.text: |
| 89 |
text = self._xml_text(text) |
| 90 |
print(text) |
| 91 |
print("</"+self.rootnode+">", file=fd) |
| 92 |
fd.close() |
| 93 |
return True |
| 94 |
except IOError as e: |
| 95 |
print("could not open '"+filename+"'file" + str(e)) |
| 96 |
return False |