1 #!/usr/bin/env python 2 3 """ 4 HTML document nodes. 5 6 Copyright (C) 2023 Paul Boddie <paul@boddie.org.uk> 7 8 This program is free software; you can redistribute it and/or modify it under 9 the terms of the GNU General Public License as published by the Free Software 10 Foundation; either version 3 of the License, or (at your option) any later 11 version. 12 13 This program is distributed in the hope that it will be useful, but WITHOUT 14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 15 FOR A PARTICULAR PURPOSE. See the GNU General Public License for more 16 details. 17 18 You should have received a copy of the GNU General Public License along with 19 this program. If not, see <http://www.gnu.org/licenses/>. 20 """ 21 22 # Element attributes. 23 24 class Attribute: 25 def __init__(self, name, value=None): 26 self.name = name 27 self.value = value 28 29 def __repr__(self): 30 return "%s(%r, %r)" % (self.__class__.__name__, self.name, self.value) 31 32 def visit(self, visitor): 33 visitor.attribute(self) 34 35 36 37 # Nodes containing other nodes. 38 39 class Fragment: 40 def __init__(self, nodes=None): 41 self.nodes = nodes or [] 42 43 def __repr__(self): 44 return "%s(%r)" % (self.__class__.__name__, self.nodes) 45 46 def visit(self, visitor): 47 visitor.fragment(self) 48 49 class Element(Fragment): 50 def __init__(self, name, attributes=None, nodes=None): 51 Fragment.__init__(self, nodes) 52 self.name = name 53 self.attributes = attributes or [] 54 55 def __repr__(self): 56 return "%s(%r, %r, %r)" % (self.__class__.__name__, self.name, self.attributes, self.nodes) 57 58 def visit(self, visitor): 59 visitor.element(self) 60 61 62 63 # Nodes having values. 64 65 class Node: 66 def __init__(self, value): 67 self.value = value 68 69 def __repr__(self): 70 return "%s(%r)" % (self.__class__.__name__, self.value) 71 72 def visit(self, visitor): 73 visitor.node(self) 74 75 class AttributeValue(Node): 76 def __init__(self, value, quote): 77 Node.__init__(self, value) 78 self.quote = quote 79 80 def __repr__(self): 81 return "%s(%r, %r)" % (self.__class__.__name__, self.value, self.quote) 82 83 def visit(self, visitor): 84 visitor.attribute_value(self) 85 86 class Comment(Node): 87 def visit(self, visitor): 88 visitor.comment(self) 89 90 class Directive(Node): 91 def visit(self, visitor): 92 visitor.directive(self) 93 94 class Inclusion(Node): 95 def visit(self, visitor): 96 visitor.inclusion(self) 97 98 class Text(Node): 99 def visit(self, visitor): 100 visitor.text(self) 101 102 # vim: tabstop=4 expandtab shiftwidth=4