1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/moinformat/utils/htmlparse/tree.py Sat Jul 01 00:43:48 2023 +0200
1.3 @@ -0,0 +1,102 @@
1.4 +#!/usr/bin/env python
1.5 +
1.6 +"""
1.7 +HTML document nodes.
1.8 +
1.9 +Copyright (C) 2023 Paul Boddie <paul@boddie.org.uk>
1.10 +
1.11 +This program is free software; you can redistribute it and/or modify it under
1.12 +the terms of the GNU General Public License as published by the Free Software
1.13 +Foundation; either version 3 of the License, or (at your option) any later
1.14 +version.
1.15 +
1.16 +This program is distributed in the hope that it will be useful, but WITHOUT
1.17 +ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
1.18 +FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
1.19 +details.
1.20 +
1.21 +You should have received a copy of the GNU General Public License along with
1.22 +this program. If not, see <http://www.gnu.org/licenses/>.
1.23 +"""
1.24 +
1.25 +# Element attributes.
1.26 +
1.27 +class Attribute:
1.28 + def __init__(self, name, value=None):
1.29 + self.name = name
1.30 + self.value = value
1.31 +
1.32 + def __repr__(self):
1.33 + return "%s(%r, %r)" % (self.__class__.__name__, self.name, self.value)
1.34 +
1.35 + def visit(self, visitor):
1.36 + visitor.attribute(self)
1.37 +
1.38 +
1.39 +
1.40 +# Nodes containing other nodes.
1.41 +
1.42 +class Fragment:
1.43 + def __init__(self, nodes=None):
1.44 + self.nodes = nodes or []
1.45 +
1.46 + def __repr__(self):
1.47 + return "%s(%r)" % (self.__class__.__name__, self.nodes)
1.48 +
1.49 + def visit(self, visitor):
1.50 + visitor.fragment(self)
1.51 +
1.52 +class Element(Fragment):
1.53 + def __init__(self, name, attributes=None, nodes=None):
1.54 + Fragment.__init__(self, nodes)
1.55 + self.name = name
1.56 + self.attributes = attributes or []
1.57 +
1.58 + def __repr__(self):
1.59 + return "%s(%r, %r, %r)" % (self.__class__.__name__, self.name, self.attributes, self.nodes)
1.60 +
1.61 + def visit(self, visitor):
1.62 + visitor.element(self)
1.63 +
1.64 +
1.65 +
1.66 +# Nodes having values.
1.67 +
1.68 +class Node:
1.69 + def __init__(self, value):
1.70 + self.value = value
1.71 +
1.72 + def __repr__(self):
1.73 + return "%s(%r)" % (self.__class__.__name__, self.value)
1.74 +
1.75 + def visit(self, visitor):
1.76 + visitor.node(self)
1.77 +
1.78 +class AttributeValue(Node):
1.79 + def __init__(self, value, quote):
1.80 + Node.__init__(self, value)
1.81 + self.quote = quote
1.82 +
1.83 + def __repr__(self):
1.84 + return "%s(%r, %r)" % (self.__class__.__name__, self.value, self.quote)
1.85 +
1.86 + def visit(self, visitor):
1.87 + visitor.attribute_value(self)
1.88 +
1.89 +class Comment(Node):
1.90 + def visit(self, visitor):
1.91 + visitor.comment(self)
1.92 +
1.93 +class Directive(Node):
1.94 + def visit(self, visitor):
1.95 + visitor.directive(self)
1.96 +
1.97 +class Inclusion(Node):
1.98 + def visit(self, visitor):
1.99 + visitor.inclusion(self)
1.100 +
1.101 +class Text(Node):
1.102 + def visit(self, visitor):
1.103 + visitor.text(self)
1.104 +
1.105 +# vim: tabstop=4 expandtab shiftwidth=4