1 #!/usr/bin/env python
\r
2 # -*- encoding: utf-8 -*-
\r
7 interesting_normal = re.compile('[&<]')
\r
8 interesting_cdata = re.compile(r'<(/|\Z)')
\r
9 incomplete = re.compile('&[a-zA-Z#]')
\r
11 entityref = re.compile('&([a-zA-Z][-.a-zA-Z0-9]*)[^a-zA-Z0-9]')
\r
12 charref = re.compile('&#(?:[0-9]+|[xX][0-9a-fA-F]+)[^0-9a-fA-F]')
\r
14 starttagopen = re.compile('<[a-zA-Z]')
\r
15 piclose = re.compile('>')
\r
16 commentclose = re.compile(r'--\s*>')
\r
17 tagfind = re.compile('[a-zA-Z][-.a-zA-Z0-9:_]*')
\r
18 attrfind = re.compile(
\r
19 r'\s*([a-zA-Z_][-.:a-zA-Z_0-9]*)(\s*=\s*'
\r
20 r'(\'[^\']*\'|"[^"]*"|[-a-zA-Z0-9./,:;+*%?!&$\(\)_#=~@]*))?')
\r
23 def _get_attribute(text):
\r
24 m = re.search(r'''([-_A-Za-z0-9]+?)=["']{0,1}(.*)["']{0,1}''', text)
\r
26 return (m.group(1), m.group(2))
\r
28 rex_attribute = re.compile(r'''([-A-Za-z0-9_]+)=["'](.*?)["']''')
\r
29 rex_attribute_keyonly = re.compile(r'^([-A-Za-z0-9_]+)$')
\r
30 rex_attribute_incomplete = re.compile(r'''([-A-Za-z0-9_]+)=["'](.*)''')
\r
32 def parse_attributes(tag):
\r
33 m = re.search(r'<[A-Za-z0-9]+\s+(.*?)\s*>', tag)
\r
35 attrs = rex_attribute.findall(m.group(1))
\r
39 def _build_attribute(key, value):
\r
41 return """%s='%s'""" % (key, value)
\r
43 return '''%s="%s"''' % (key, value)
\r
45 def build_tag(tagname, attributes):
\r
46 attrs = [_build_attribute(x, attributes[x]) for x in attributes]
\r
47 attrs.insert(0, tagname)
\r
48 return '<' + ' '.join(attrs) + '>'
\r
50 if __name__ == '__main__':
\r
51 test1 = '''<a href="/hoge" alt="hoge 'hoge">'''
\r
53 attrs = parse_attributes(test1)
\r
55 print build_tag('a', attrs)
\r