-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPage.py
151 lines (128 loc) · 4.44 KB
/
Page.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
""" Helper class to help generate webpages """
class Page:
""" Page class
This class is meant to be an abstract class to help generate webpage in
cherrypy (or other frameworks), by helping creating:
- header filled at instantiation
- body filling helpers
- block indentation
- default indentation is 4 spaces
Example Usage
-------------
import Page
p = Page() # instantiate
p.opn("<div>") # open tag: insert and indent following insertions
p.add("content") # add content
p.cls("</div>") # close tag: deindent and insert closing tag
p.get() # close body and html tags
"""
# header
_doctype = "html"
_lang = "en"
_title = "title"
_content = "text/html"
_charset = "utf-8"
_keywords = []
_description = { 'en': "No description" }
_css = "/index.css"
_additionnal_headers = []
def __init__(self, lang='en', indent=4):
# content of the page
self._line = []
# indent status and count per indent
self._indent = ""
self._indent_count = indent
# set instance language
self._lang = lang
def indent(self):
"""indent text that will be inserted with the insertion methods"""
self._line.append(self._indent_count)
def deindent(self):
"""Remove a level of indent to insertion methods"""
self._line.append(-self._indent_count)
def add(self, line, newline=True):
"""Add an indented line with current indentation
@param line The line to be added
@param newline Defines if newline is to be inserted
"""
if newline:
self._line.append(line + "\n")
else:
self._line.append(line)
def app(self, line):
"""Append a string to the current line, mostly useful after add and
newline=False use, to finish a given line.
"""
self._line[-1] = self._line[-1] + line
def opn(self, line, newline=True):
"""Used to open an html tag, adding a line containing the tag, and
indenting the following text so it reflect the opened tag in the
indentation.
"""
self.add(line, newline)
self.indent()
def cls(self, line, newline=True):
"""Used to close a html tag, deindenting the text, then adding a line
normally containing the closing tag.
"""
self.deindent()
self.add(line, newline)
def head(self):
""" Prepare header for the page """
self.add("<!DOCTYPE %s>" % (self._doctype))
self.opn('<html lang="%s">' % (self._lang))
self.opn("<head>")
self.add('')
self.add("<title>%s</title>" % (self._title))
self.add('')
self.add('<meta http-equiv="Content-Type" content="%s; charset=%s"/>' %
(self._content, self._charset))
# adding keywords
self.add('<meta name="keywords" content="', newline=False)
start = True
for k in self._keywords:
if start == False:
self.app(', ')
else:
start = False
self.app('%s' % (k))
self.app('" />\n')
# add description
self.add('<meta name="description" content="%s" />' %
(self._description[self._lang]))
# stylesheet
if self._css != None:
self.add('<link rel="stylesheet" type="text/css" href="%s" />' %
(self._css))
# additionnal headers
for header in self._additionnal_headers:
self.add("%s" % (header))
self.add('')
self.cls("</head>")
self.add('')
self.opn("<body>")
def get(self):
"""Return the current page text"""
page = ""
tmp = self._line
self._line = []
self.head()
self._line = self._line + tmp
self.cls("</body>")
self.cls("</html>")
indent = ""
# now we have to generate text from lines
for line in self._line:
if isinstance(line, int):
if line > 0:
i = 0
while i < line:
indent = indent + " "
i = i + 1
else:
indent = indent[:line]
else:
page = page + indent
page = page + line
self._line = []
return page.strip()