CoCalc Shared Fileswww / cgi-bin / mfd / SGML.pyOpen in CoCalc with one click!
Author: William A. Stein
1
__version__ = "$Revision: 1.1 $"[11:-4]
2
3
from sys import stdout
4
from string import lower, join, replace
5
6
class Markup:
7
8
content_model = 1
9
10
def __init__(self, name, *content):
11
self.name = name
12
self.dict = {}
13
self.content = list(content)
14
15
start_tag_string = "<!%s "
16
17
def append(self, *items): map(self.content.append, items)
18
19
def start_tag(self): return self.start_tag_string % self.name
20
21
def end_tag(self): return ">\n"
22
23
def writeto(self, fp=stdout, indent=0, perlevel=0):
24
fp.write(self.start_tag())
25
for c in self.content:
26
if hasattr(c, 'writeto'):
27
getattr(c, 'writeto')(fp, indent+perlevel, perlevel)
28
else:
29
fp.write(str(c))
30
fp.write(self.end_tag())
31
32
def __str__(self, indent=0, perlevel=0):
33
# we don't actually indent here, it's for later.
34
c = map(str, self.content)
35
return join([self.start_tag()]+c+[self.end_tag()],'')
36
37
38
def Comment(*comment): return apply(Markup, ('--',)+comment+(' --',))
39
40