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

Source Code for Module tdi.markup.soup.dtd

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