1 # Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 # Use of this source code is governed by a BSD-style license that can be
3 # found in the LICENSE file.
5 from HTMLParser
import HTMLParser
6 from StringIO
import StringIO
8 class _ConverterHTMLParser(HTMLParser
):
9 def __init__(self
, io
):
10 HTMLParser
.__init
__(self
)
14 def handle_starttag(self
, tag
, attrs
):
15 attrs_dict
= dict(attrs
)
16 self
._tag
_stack
.append({'tag': tag
})
17 class_attr
= dict(attrs
).get('class', None)
18 if class_attr
is not None:
19 if class_attr
== 'doc-family extensions':
20 self
._io
.write('{{^is_apps}}\n')
21 self
._tag
_stack
[-1]['close'] = True
22 if class_attr
== 'doc-family apps':
23 self
._io
.write('{{?is_apps}}\n')
24 self
._tag
_stack
[-1]['close'] = True
25 self
._io
.write(self
.get_starttag_text())
27 def handle_startendtag(self
, tag
, attrs
):
28 self
._io
.write(self
.get_starttag_text())
30 def handle_endtag(self
, tag
):
31 self
._io
.write('</' + tag
+ '>')
32 if len(self
._tag
_stack
) == 0:
34 if self
._tag
_stack
[-1]['tag'] == tag
:
35 if self
._tag
_stack
[-1].get('close', False):
36 self
._io
.write('\n{{/is_apps}}')
39 def handle_data(self
, data
):
42 def handle_comment(self
, data
):
43 self
._io
.write('<!--' + data
+ '-->')
45 def handle_entityref(self
, name
):
46 self
._io
.write('&' + name
+ ';')
48 def handle_charref(self
, name
):
49 self
._io
.write('&#' + name
+ ';')
51 def handle_decl(self
, data
):
52 self
._io
.write('<!' + data
+ '>')
54 def HandleDocFamily(html
):
56 parser
= _ConverterHTMLParser(output
)
58 return output
.getvalue()