| 1 | # Copyright (C) 2008 Adriano Monteiro Marques. |
|---|
| 2 | # |
|---|
| 3 | # Author: Luis A. Bastiao Silva <luis.kop@gmail.com> |
|---|
| 4 | # |
|---|
| 5 | # This program is free software; you can redistribute it and/or modify |
|---|
| 6 | # it under the terms of the GNU General Public License as published by |
|---|
| 7 | # the Free Software Foundation; either version 2 of the License, or |
|---|
| 8 | # (at your option) any later version. |
|---|
| 9 | # |
|---|
| 10 | # This program is distributed in the hope that it will be useful, |
|---|
| 11 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 12 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 13 | # GNU General Public License for more details. |
|---|
| 14 | # |
|---|
| 15 | # You should have received a copy of the GNU General Public License |
|---|
| 16 | # along with this program; if not, write to the Free Software |
|---|
| 17 | # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
|---|
| 18 | |
|---|
| 19 | |
|---|
| 20 | HEADER = "<html>" |
|---|
| 21 | HEADER_C = "<html>" |
|---|
| 22 | TITLE = "<title>" |
|---|
| 23 | TITLE_C = "</title>" |
|---|
| 24 | BL = "<br />" |
|---|
| 25 | |
|---|
| 26 | """ |
|---|
| 27 | Tree of HTML tags |
|---|
| 28 | <html> |
|---|
| 29 | childs |
|---|
| 30 | ... |
|---|
| 31 | ... |
|---|
| 32 | ... |
|---|
| 33 | ... |
|---|
| 34 | </html> |
|---|
| 35 | |
|---|
| 36 | """ |
|---|
| 37 | |
|---|
| 38 | |
|---|
| 39 | class Node(object): |
|---|
| 40 | def __init__(self, tag_name, attr=None, text = "" ): |
|---|
| 41 | """ |
|---|
| 42 | @param tag_name name of tag (could be None) |
|---|
| 43 | @param attr is a list that contains dicts [{name and value}, ...] |
|---|
| 44 | """ |
|---|
| 45 | |
|---|
| 46 | self.tag = tag_name |
|---|
| 47 | self.attr = attr |
|---|
| 48 | self.childs = [] |
|---|
| 49 | self.text = text |
|---|
| 50 | def add_child(self, node): |
|---|
| 51 | """ |
|---|
| 52 | Add Childs |
|---|
| 53 | """ |
|---|
| 54 | assert node != None |
|---|
| 55 | self.childs.append(node) |
|---|
| 56 | def get_childs(self): |
|---|
| 57 | return self.childs |
|---|
| 58 | |
|---|
| 59 | def get_html(self): |
|---|
| 60 | """ |
|---|
| 61 | -- recursive code |
|---|
| 62 | returns a html string |
|---|
| 63 | """ |
|---|
| 64 | if self.tag == None : |
|---|
| 65 | return self.text |
|---|
| 66 | |
|---|
| 67 | str = self.tag |
|---|
| 68 | |
|---|
| 69 | # Fill Attributes of tag |
|---|
| 70 | if self.attr!=None: |
|---|
| 71 | for attr in self.attr: |
|---|
| 72 | str = str + " " |
|---|
| 73 | str = str + attr['name'] + "=\"" + attr['value']+"\"" |
|---|
| 74 | |
|---|
| 75 | ch = "" |
|---|
| 76 | for child in self.childs: |
|---|
| 77 | s = child.get_html() |
|---|
| 78 | ch = ch + "\n" + s |
|---|
| 79 | |
|---|
| 80 | |
|---|
| 81 | open = "<%s>" % str |
|---|
| 82 | close = "</%s>" % self.tag |
|---|
| 83 | if self.text == "": |
|---|
| 84 | result = open + ch + "\n" + close |
|---|
| 85 | else: |
|---|
| 86 | result = open + ch + self.text + "\n" + close |
|---|
| 87 | return result |
|---|
| 88 | |
|---|
| 89 | # DELME? |
|---|
| 90 | class CodesHTML(object): |
|---|
| 91 | def get_title(title): |
|---|
| 92 | return TITLE+"%s"% title + TITLE_C |
|---|
| 93 | |
|---|
| 94 | title = staticmethod(get_title) |
|---|