File: html.py

package info (click to toggle)
tinyos-tools 1.4.2-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 3,140 kB
  • ctags: 1,626
  • sloc: cpp: 10,398; sh: 1,229; python: 1,200; ansic: 562; makefile: 305; xml: 24
file content (172 lines) | stat: -rw-r--r-- 4,410 bytes parent folder | download | duplicates (2)
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# Copyright (c) 2005 Intel Corporation
# All rights reserved.
#
# This file is distributed under the terms in the attached INTEL-LICENSE     
# file. If you do not find these files, copies can be found by writing to
# Intel Research Berkeley, 2150 Shattuck Avenue, Suite 1300, Berkeley, CA, 
# 94704.  Attention:  Intel License Inquiry.

# An HTML output class, with convenience routines for handling tags and
# indentation.

from string import *
from nesdoc.generators import *
from re import search

_doctags = {}

def register_doctag(name, handler):
  _doctags[name] = handler

class Html:
  # create a new HTML output file in filename
  def __init__(self, filename):
    self.f = file(filename, "w")
    self.tags = []
    self.ind = 0
    self.at0 = True
    self.pushln("html");
    self.pushln("head");
    # include stylesheet
    self.tag("LINK", "rel=\"stylesheet\"", "href=\"nesdoc.css\"", "type=\"text/css\"", "media=\"screen\"")

  # end of html generation. cleanup and close the underlying file.
  def close(self):
    self.popln()
    self.popln()
    assert self.ind == 0 and self.tags == []
    self.f.close()

  def indent(self):
    self.ind += 2

  def unindent(self, ):
    self.ind -= 2

  # print a string
  def p(self, s):
    if self.at0:
      self.f.write(" " * self.ind)
      self.at0 = False
    self.f.write(s)

  # print a string and end the line
  def pln(self, s):
    self.p(s)
    self.f.write("\n")
    self.at0 = True

  # print a string, quoting the characters with meaning in HTML
  def pq(self, s):
    s.replace("<", "&lt;")
    s.replace(">", "&gt;")
    s.replace("&", "&amp;")
    s.replace('"', "&quot;")
    self.p(s)

  # newline if not at the start of a line
  def startline(self):
    if not self.at0:
      self.pln("")

  # start a new tag
  def push(self, tag, *attrs):
    self.tag(tag, *attrs)
    self.tags.append(tag)
    self.indent()

  # start a new tag on a new line
  def pushln(self, tag, *attrs):
    self.startline();
    self.tag(tag, *attrs)
    self.pln("")
    self.tags.append(tag)
    self.indent()

  # print a tag, but don't save it on the tag stack
  def tag(self, tag, *attrs):
    if attrs == ():
      self.p("<%s>" % tag)
    else:
      self.p("<%s %s>" % (tag, join(attrs)))

  # print a tag on a new line, but don't save it on the tag stack
  def tagln(self, tag, *attrs):
    self.tag(tag, *attrs)
    self.pln("")
    
  # pop and print a terminator the most recent tag from the tag stack
  def pop(self):
    self.unindent()
    self.p("</%s>" % self.tags.pop())

  # pop and print (on a new line) a terminator the most recent tag from the
  # tag stack
  def popln(self):
    self.unindent()
    self.startline()
    self.pln("</%s>" % self.tags.pop())

  # print the HTML title
  def title(self, s):
    self.push("title");
    self.pq(s);
    self.pop();

  # start the body section
  def body(self):
    self.popln() # pop head
    self.pushln("body")

  # Highlevel methods

  # escape <> enclosed email addresses
  def escape_email(self, s):
    while True:
      email = search("<\S+@\S+>", s)
      if not email:
        break
      start = email.start()
      end = email.end()
      s = s[:start] + "&lt;" + s[start + 1 : end - 1] + "&gt;" + s[end:]
    return s

  # print a nesdoc string. @ entries go in a table
  def pdoc(self, docstr):
    (base, tags) = nd_docstring(docstr)
    self.pln(self.escape_email(base))
    if tags:
      self.tag("p")
      self.pushln("dl")
      lasttag = None
      for (tag, val) in tags:
        if _doctags.has_key(tag):
          (tag, val) = _doctags[tag](val)
        if tag != lasttag:
          self.tag("dt");
          self.push("b"); self.pq(capitalize(tag) + ":"); self.pop()
        self.pushln("dd");
        self.p(self.escape_email(val))
        self.popln() #dd
        lasttag = tag
      self.popln() #dl

  # print a nice fancy heading
  def heading(self, s):
    self.push("div", "id=heading")
    self.pq(s)
    self.pop();

  def func_sig_start(self) :
    self.push("div", "id=funcsig")

  def func_sig_stop(self) :
    self.pop();

  # print a function signature. namedecorator is called with the function
  # name as argument so that you can decorate the actual function name
  # (e.g., bold, a link)
  def pfnsig(self, fn, namedecorator):
    self.push("span", "id=funcnameshort")
    self.pln(function_signature_str(fn, namedecorator))
    self.pop()