ajr@google.com | 4301639 | 2009-02-10 16:59:11 +0000 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | # |
| 3 | # Copyright 2007 Google Inc. |
| 4 | # |
| 5 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | # you may not use this file except in compliance with the License. |
| 7 | # You may obtain a copy of the License at |
| 8 | # |
| 9 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | # |
| 11 | # Unless required by applicable law or agreed to in writing, software |
| 12 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | # See the License for the specific language governing permissions and |
| 15 | # limitations under the License. |
| 16 | # |
| 17 | |
| 18 | """Routines and a class for describing and manipulating data from a |
| 19 | TableOfContents wiki page. |
| 20 | """ |
| 21 | |
| 22 | nodes = {} |
| 23 | def lookup(wikiWord): |
| 24 | if nodes.has_key(wikiWord): |
| 25 | return nodes[wikiWord] |
| 26 | else: |
| 27 | return None |
| 28 | |
| 29 | class TOCNode: |
| 30 | def __init__(self, caption, wikiWord=None): |
| 31 | self.caption = caption |
| 32 | self.wikiWord = wikiWord |
| 33 | self.children = [] |
| 34 | |
| 35 | if(wikiWord): |
| 36 | nodes[wikiWord] = self |
| 37 | |
| 38 | def addChild(self, child): |
| 39 | self.children.append(child) |
| 40 | |
| 41 | def AsTree(self): |
| 42 | out = {"caption":self.caption, "wikiword":self.wikiWord} |
| 43 | |
| 44 | if(not self.children): |
| 45 | out["children"] = None |
| 46 | else: |
| 47 | out["children"] = map(TOCNode.AsTree, self.children) |
| 48 | |
| 49 | return out |
| 50 | |
| 51 | def __repr__(self): |
| 52 | return str(self) |
| 53 | |
| 54 | def __str__(self): |
| 55 | return "(((%s)))" % self.caption |