OSDN Git Service

add wp_imgswap2.py for new OSDN Magazine
[otptools/otptools.git] / htmltaglib.py
1 #!/usr/bin/env python\r
2 # -*- encoding: utf-8 -*-\r
3 #\r
4 \r
5 import re\r
6 \r
7 interesting_normal = re.compile('[&<]')\r
8 interesting_cdata = re.compile(r'<(/|\Z)')\r
9 incomplete = re.compile('&[a-zA-Z#]')\r
10 \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
13 \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
21 \r
22 \r
23 def _get_attribute(text):\r
24     m = re.search(r'''([-_A-Za-z0-9]+?)=["']{0,1}(.*)["']{0,1}''', text)\r
25     if m:\r
26         return (m.group(1), m.group(2))\r
27 \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
31 \r
32 def parse_attributes(tag):\r
33     m = re.search(r'<[A-Za-z0-9]+\s+(.*?)\s*>', tag)\r
34     if m:\r
35         attrs = rex_attribute.findall(m.group(1))\r
36         return dict(attrs)\r
37     return {}\r
38 \r
39 def _build_attribute(key, value):\r
40     if '"' in value:\r
41         return """%s='%s'""" % (key, value)\r
42     else:\r
43         return '''%s="%s"''' % (key, value)\r
44 \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
49 \r
50 if __name__ == '__main__':\r
51     test1 = '''<a href="/hoge" alt="hoge 'hoge">'''\r
52     print test1\r
53     attrs = parse_attributes(test1)\r
54     print attrs\r
55     print build_tag('a', attrs)\r
56 \r