Package tdi :: Package markup :: Package soup :: Module dtd
[frames] | no frames]

Source Code for Module tdi.markup.soup.dtd

  1  # -*- coding: ascii -*- 
  2  # 
  3  # Copyright 2006 - 2013 
  4  # Andr\xe9 Malo or his licensors, as applicable 
  5  # 
  6  # Licensed under the Apache License, Version 2.0 (the "License"); 
  7  # you may not use this file except in compliance with the License. 
  8  # 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, software 
 13  # distributed under the License is distributed on an "AS IS" BASIS, 
 14  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
 15  # See the License for the specific language governing permissions and 
 16  # limitations under the License. 
 17  """ 
 18  ================ 
 19   DTD Collection 
 20  ================ 
 21   
 22  This module provides a collection of DTD query classes to be used with 
 23  the soup parser. 
 24  """ 
 25  __author__ = u"Andr\xe9 Malo" 
 26  __docformat__ = "restructuredtext en" 
 27   
 28  from tdi import interfaces as _interfaces 
 29   
 30   
31 -class HTMLDTD(object):
32 """ 33 Query class for the HTML DTD 34 35 :IVariables: 36 `_cdata` : ``dict`` 37 Dict of CDATA elements (for speed lookup) 38 39 `_optional` : ``dict`` 40 Dict of optional elements (for speed lookup) 41 42 `_empty` : ``dict`` 43 Dict of empty elements (for speed lookup) 44 """ 45 __implements__ = [_interfaces.DTDInterface] 46 47 #: List of known empty elements 48 #: 49 #: :Type: ``tuple`` 50 _EMPTY = ('app', 'area', 'base', 'basefont', 'bgsound', 'br', 'col', 51 'command', 'embed', 'frame', 'hr', 'img', 'input', 'isindex', 52 'keygen', 'link', 'meta', 'nextid', 'param', 'sound', 'source', 53 'spacer', 'track', 'wbr') 54 55 #: List of CDATA elements 56 #: 57 #: plaintext has been specified differently over time. Sometimes it's 58 #: finishable, sometimes not. I let it be finishable here. You shouldn't 59 #: use it anyway. 60 #: 61 #: :Type: ``tuple`` 62 _CDATA = ('listing', 'plaintext', 'script', 'style', 'textarea', 'xmp') 63 64 # helper 65 _intable = ('caption', 'col', 'colgroup', 'tbody', 'thead', 'tfoot') 66 67 #: List of elements with optional end tag. This list consists 68 #: of (name, forbidden-list) pairs. 69 #: 70 #: :Type: ``tuple`` 71 _OPTIONAL = tuple({ 72 'html': ('html',), 73 'head': ('html', 'body', 'head',), 74 'body': ('html', 'body', 'head',), 75 'li': ('li',), 76 'dt': ('dt', 'dd',), 77 'dd': ('dt', 'dd',), 78 'p': ('address', 'article', 'aside', 'blockquote', 'body', 79 'dd', 'dir', 'div', 'dl', 'dt', 'fieldset', 'footer', 80 'form', 'frame', 'frameset', 'h1', 'h2', 'h3', 'h4', 81 'h5', 'h6', 'head', 'header', 'hgroup', 'hr', 'html', 82 'isindex', 'layer', 'li', 'listing', 'map', 'marquee', 83 'menu', 'multicol', 'nav', 'noframes', 'ol', 'p', 84 'plaintext', 'pre', 'section', 'table', 'td', 'th', 85 'tr', 'ul', 'xmp') + _intable, 86 'rt': ('rt', 'rp',), 87 'rp': ('rt', 'rp',), 88 'optgroup': ('optgroup',), 89 'option': ('option', 'optgroup',), 90 'colgroup': _intable + ('td', 'th', 'tr',), 91 'caption': _intable + ('td', 'th', 'tr',), 92 'thead': _intable, 93 'tbody': _intable, 94 'tfoot': _intable, 95 'tr': _intable + ('tr',), 96 'td': _intable + ('td', 'th', 'tr',), 97 'th': _intable + ('td', 'th', 'tr',), 98 }.items()) 99 del _intable 100
101 - def __init__(self):
102 """ Initialization """ 103 dict_ = dict 104 optional = dict_([ 105 (name, dict_([ 106 (item, None) for item in forbidden 107 ]).__contains__) 108 for name, forbidden in self._OPTIONAL 109 ]).get 110 if self.__class__ == HTMLDTD: 111 import operator as _operator 112 113 self.cdata = dict_([ 114 (item, None) for item in self._CDATA 115 ]).__contains__ 116 self.empty = empty = dict_([ 117 (item, None) for item in self._EMPTY 118 ]).__contains__ 119 def nestable(outer, inner): 120 """ :See: `tdi.interfaces.DTDInterface.nestable` """ 121 opt = optional(outer) 122 if opt is not None: 123 return not(opt(inner)) 124 elif empty(outer): 125 return False 126 return True
127 self.nestable = nestable 128 else: 129 self._empty = dict_([ 130 (item, None) for item in self._EMPTY 131 ]).__contains__ 132 self._cdata = dict_([ 133 (item, None) for item in self._CDATA 134 ]).__contains__ 135 self._optional = optional
136
137 - def cdata(self, name): # pylint: disable = E0202
138 """ :See: `tdi.interfaces.DTDInterface.cdata` """ 139 return self._cdata(name) 140
141 - def nestable(self, outer, inner): # pylint: disable = E0202
142 """ :See: `tdi.interfaces.DTDInterface.nestable` """ 143 opt = self._optional(outer) 144 if opt is not None: 145 return not(opt(inner)) 146 elif self._empty(outer): 147 return False 148 149 return True 150
151 - def empty(self, name): # pylint: disable = E0202
152 """ :See: `tdi.interfaces.DTDInterface.empty` """ 153 return self._empty(name) 154 155
156 -class XMLDTD(object):
157 """ 158 XML DTD query class 159 160 This class effectively defines every wellformed XML valid. 161 """ 162 __implements__ = [_interfaces.DTDInterface] 163
164 - def cdata(self, name):
165 """ :See: `DTDInterface` """ 166 # pylint: disable = W0613 167 return False
168
169 - def nestable(self, outer, inner):
170 """ :See: `DTDInterface` """ 171 # pylint: disable = W0613 172 return True
173
174 - def empty(self, name):
175 """ :See: `DTDInterface` """ 176 # pylint: disable = W0613 177 return False
178