OLD | NEW |
---|---|
1 # Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 # Use of this source code is governed by a BSD-style license that can be |
3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
4 | 4 |
5 import logging | |
6 | |
7 from HTMLParser import HTMLParser | 5 from HTMLParser import HTMLParser |
6 import re | |
8 | 7 |
9 from docs_server_utils import FormatKey | 8 from docs_server_utils import FormatKey |
10 from third_party.handlebar import Handlebar | 9 from third_party.handlebar import Handlebar |
11 | 10 |
12 class _IntroParser(HTMLParser): | 11 class _IntroParser(HTMLParser): |
13 """ An HTML parser which will parse table of contents and page title info out | 12 """ An HTML parser which will parse table of contents and page title info out |
14 of an intro. | 13 of an intro. |
15 """ | 14 """ |
16 def __init__(self): | 15 def __init__(self): |
17 HTMLParser.__init__(self) | 16 HTMLParser.__init__(self) |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
55 """This class fetches the intros for a given API. From this intro, a table | 54 """This class fetches the intros for a given API. From this intro, a table |
56 of contents dictionary is created, which contains the headings in the intro. | 55 of contents dictionary is created, which contains the headings in the intro. |
57 """ | 56 """ |
58 def __init__(self, cache_builder, base_paths): | 57 def __init__(self, cache_builder, base_paths): |
59 self._cache = cache_builder.build(self._MakeIntroDict) | 58 self._cache = cache_builder.build(self._MakeIntroDict) |
60 self._base_paths = base_paths | 59 self._base_paths = base_paths |
61 | 60 |
62 def _MakeIntroDict(self, intro): | 61 def _MakeIntroDict(self, intro): |
63 parser = _IntroParser() | 62 parser = _IntroParser() |
64 parser.feed(intro) | 63 parser.feed(intro) |
64 intro = re.sub(re.compile('<h1[^>.]*?>.*?</h1>', flags=re.DOTALL), | |
cduvall
2012/08/02 00:54:06
Used re here because there's not an easy way to re
| |
65 '', | |
66 intro, | |
67 count=1) | |
65 return { | 68 return { |
66 'intro': Handlebar(intro), | 69 'intro': Handlebar(intro), |
67 'toc': parser.toc, | 70 'toc': parser.toc, |
68 'title': parser.page_title | 71 'title': parser.page_title |
69 } | 72 } |
70 | 73 |
71 def __getitem__(self, key): | 74 def __getitem__(self, key): |
72 return self.get(key) | 75 return self.get(key) |
73 | 76 |
74 def get(self, key): | 77 def get(self, key): |
75 real_path = FormatKey(key) | 78 real_path = FormatKey(key) |
76 for base_path in self._base_paths: | 79 for base_path in self._base_paths: |
77 try: | 80 try: |
78 return self._cache.GetFromFile(base_path + '/' + real_path) | 81 return self._cache.GetFromFile(base_path + '/' + real_path) |
79 except Exception: | 82 except Exception: |
80 pass | 83 pass |
81 return None | 84 return None |
OLD | NEW |