| 1 |
# |
| 2 |
# Licensed to the Apache Software Foundation (ASF) under one |
| 3 |
# or more contributor license agreements. See the NOTICE file |
| 4 |
# distributed with this work for additional information |
| 5 |
# regarding copyright ownership. The ASF licenses this file |
| 6 |
# to you under the Apache License, Version 2.0 (the |
| 7 |
# "License"); you may not use this file except in compliance |
| 8 |
# with the License. You may obtain a copy of the License at |
| 9 |
# |
| 10 |
# http://www.apache.org/licenses/LICENSE-2.0 |
| 11 |
# |
| 12 |
# Unless required by applicable law or agreed to in writing, |
| 13 |
# software distributed under the License is distributed on an |
| 14 |
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 |
# KIND, either express or implied. See the License for the |
| 16 |
# specific language governing permissions and limitations |
| 17 |
# under the License. |
| 18 |
# |
| 19 |
|
| 20 |
""" |
| 21 |
Useful transforms for dom objects. |
| 22 |
""" |
| 23 |
from __future__ import absolute_import |
| 24 |
|
| 25 |
from . import dom |
| 26 |
try: |
| 27 |
from io import StringIO |
| 28 |
except ImportError: |
| 29 |
from cStringIO import StringIO |
| 30 |
|
| 31 |
|
| 32 |
class Visitor: |
| 33 |
|
| 34 |
def descend(self, node): |
| 35 |
for child in node.children: |
| 36 |
child.dispatch(self) |
| 37 |
|
| 38 |
def node(self, node): |
| 39 |
self.descend(node) |
| 40 |
|
| 41 |
def leaf(self, leaf): |
| 42 |
pass |
| 43 |
|
| 44 |
|
| 45 |
class Identity: |
| 46 |
|
| 47 |
def descend(self, node): |
| 48 |
result = [] |
| 49 |
for child in node.children: |
| 50 |
result.append(child.dispatch(self)) |
| 51 |
return result |
| 52 |
|
| 53 |
def default(self, tag): |
| 54 |
result = dom.Tag(tag.name, *tag.attrs) |
| 55 |
result.extend(self.descend(tag)) |
| 56 |
return result |
| 57 |
|
| 58 |
def tree(self, tree): |
| 59 |
result = dom.Tree() |
| 60 |
result.extend(self.descend(tree)) |
| 61 |
return result |
| 62 |
|
| 63 |
def tag(self, tag): |
| 64 |
return self.default(tag) |
| 65 |
|
| 66 |
def leaf(self, leaf): |
| 67 |
return leaf.__class__(leaf.data) |
| 68 |
|
| 69 |
|
| 70 |
class Sexp(Identity): |
| 71 |
|
| 72 |
def __init__(self): |
| 73 |
self.stack = [] |
| 74 |
self.level = 0 |
| 75 |
self.out = "" |
| 76 |
|
| 77 |
def open(self, s): |
| 78 |
self.out += "(%s" % s |
| 79 |
self.level += len(s) + 1 |
| 80 |
self.stack.append(s) |
| 81 |
|
| 82 |
def line(self, s=""): |
| 83 |
self.out = self.out.rstrip() |
| 84 |
self.out += "\n" + " " * self.level + s |
| 85 |
|
| 86 |
def close(self): |
| 87 |
s = self.stack.pop() |
| 88 |
self.level -= len(s) + 1 |
| 89 |
self.out = self.out.rstrip() |
| 90 |
self.out += ")" |
| 91 |
|
| 92 |
def tree(self, tree): |
| 93 |
self.open("+ ") |
| 94 |
for child in tree.children: |
| 95 |
self.line() |
| 96 |
child.dispatch(self) |
| 97 |
self.close() |
| 98 |
|
| 99 |
def tag(self, tag): |
| 100 |
self.open("Node(%s) " % tag.name) |
| 101 |
for child in tag.children: |
| 102 |
self.line() |
| 103 |
child.dispatch(self) |
| 104 |
self.close() |
| 105 |
|
| 106 |
def leaf(self, leaf): |
| 107 |
self.line("%s(%s)" % (leaf.__class__.__name__, leaf.data)) |
| 108 |
|
| 109 |
|
| 110 |
class Output: |
| 111 |
|
| 112 |
def descend(self, node): |
| 113 |
out = StringIO() |
| 114 |
for child in node.children: |
| 115 |
out.write(child.dispatch(self)) |
| 116 |
return out.getvalue() |
| 117 |
|
| 118 |
def default(self, tag): |
| 119 |
out = StringIO() |
| 120 |
out.write("<%s" % tag.name) |
| 121 |
for k, v in tag.attrs: |
| 122 |
out.write(' %s="%s"' % (k, v)) |
| 123 |
out.write(">") |
| 124 |
out.write(self.descend(tag)) |
| 125 |
if not tag.singleton: |
| 126 |
out.write("</%s>" % tag.name) |
| 127 |
return out.getvalue() |
| 128 |
|
| 129 |
def tree(self, tree): |
| 130 |
return self.descend(tree) |
| 131 |
|
| 132 |
def tag(self, tag): |
| 133 |
return self.default(tag) |
| 134 |
|
| 135 |
def data(self, leaf): |
| 136 |
return leaf.data |
| 137 |
|
| 138 |
def entity(self, leaf): |
| 139 |
return "&%s;" % leaf.data |
| 140 |
|
| 141 |
def character(self, leaf): |
| 142 |
raise Exception("TODO") |
| 143 |
|
| 144 |
def comment(self, leaf): |
| 145 |
return "<!-- %s -->" % leaf.data |
| 146 |
|
| 147 |
|
| 148 |
class Empty(Output): |
| 149 |
|
| 150 |
def tag(self, tag): |
| 151 |
return self.descend(tag) |
| 152 |
|
| 153 |
def data(self, leaf): |
| 154 |
return "" |
| 155 |
|
| 156 |
def entity(self, leaf): |
| 157 |
return "" |
| 158 |
|
| 159 |
def character(self, leaf): |
| 160 |
return "" |
| 161 |
|
| 162 |
def comment(self, leaf): |
| 163 |
return "" |
| 164 |
|
| 165 |
|
| 166 |
class Text(Empty): |
| 167 |
|
| 168 |
def data(self, leaf): |
| 169 |
return leaf.data |
| 170 |
|
| 171 |
def entity(self, leaf): |
| 172 |
return "&%s;" % leaf.data |
| 173 |
|
| 174 |
def character(self, leaf): |
| 175 |
# XXX: is this right? |
| 176 |
return "&#%s;" % leaf.data |