| 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 from HTMLParser import HTMLParser | 5 from HTMLParser import HTMLParser |
| 6 import logging | 6 import logging |
| 7 import os | 7 import os |
| 8 import re | 8 import re |
| 9 | 9 |
| 10 from docs_server_utils import FormatKey | 10 from docs_server_utils import FormatKey |
| 11 from file_system import FileNotFoundError | 11 from file_system import FileNotFoundError |
| 12 import compiled_file_system as compiled_fs | 12 import compiled_file_system as compiled_fs |
| 13 from third_party.handlebar import Handlebar | 13 from third_party.handlebar import Handlebar |
| 14 | 14 |
| 15 # TODO(kalman): rename this HTMLDataSource or other, then have separate intro | 15 # TODO(kalman): rename this HTMLDataSource or other, then have separate intro |
| 16 # article data sources created as instances of it. | 16 # article data sources created as instances of it. |
| 17 | 17 |
| 18 # Increment this if the data model changes for IntroDataSource. | |
| 19 _VERSION = 5 | |
| 20 | |
| 21 _H1_REGEX = re.compile('<h1[^>.]*?>.*?</h1>', flags=re.DOTALL) | 18 _H1_REGEX = re.compile('<h1[^>.]*?>.*?</h1>', flags=re.DOTALL) |
| 22 | 19 |
| 23 class _IntroParser(HTMLParser): | 20 class _IntroParser(HTMLParser): |
| 24 """ An HTML parser which will parse table of contents and page title info out | 21 """ An HTML parser which will parse table of contents and page title info out |
| 25 of an intro. | 22 of an intro. |
| 26 """ | 23 """ |
| 27 def __init__(self): | 24 def __init__(self): |
| 28 HTMLParser.__init__(self) | 25 HTMLParser.__init__(self) |
| 29 self.toc = [] | 26 self.toc = [] |
| 30 self.page_title = None | 27 self.page_title = None |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 62 elif self._recent_tag in ['h2', 'h3']: | 59 elif self._recent_tag in ['h2', 'h3']: |
| 63 self._current_heading['title'] += data | 60 self._current_heading['title'] += data |
| 64 | 61 |
| 65 class IntroDataSource(object): | 62 class IntroDataSource(object): |
| 66 """This class fetches the intros for a given API. From this intro, a table | 63 """This class fetches the intros for a given API. From this intro, a table |
| 67 of contents dictionary is created, which contains the headings in the intro. | 64 of contents dictionary is created, which contains the headings in the intro. |
| 68 """ | 65 """ |
| 69 class Factory(object): | 66 class Factory(object): |
| 70 def __init__(self, compiled_fs_factory, ref_resolver_factory, base_paths): | 67 def __init__(self, compiled_fs_factory, ref_resolver_factory, base_paths): |
| 71 self._cache = compiled_fs_factory.Create(self._MakeIntroDict, | 68 self._cache = compiled_fs_factory.Create(self._MakeIntroDict, |
| 72 IntroDataSource, | 69 IntroDataSource) |
| 73 version=_VERSION) | |
| 74 self._ref_resolver = ref_resolver_factory.Create() | 70 self._ref_resolver = ref_resolver_factory.Create() |
| 75 self._base_paths = base_paths | 71 self._base_paths = base_paths |
| 76 | 72 |
| 77 def _MakeIntroDict(self, intro_path, intro): | 73 def _MakeIntroDict(self, intro_path, intro): |
| 78 # Guess the name of the API from the path to the intro. | 74 # Guess the name of the API from the path to the intro. |
| 79 api_name = os.path.splitext(intro_path.split('/')[-1])[0] | 75 api_name = os.path.splitext(intro_path.split('/')[-1])[0] |
| 80 intro_with_links = self._ref_resolver.ResolveAllLinks(intro, | 76 intro_with_links = self._ref_resolver.ResolveAllLinks(intro, |
| 81 namespace=api_name) | 77 namespace=api_name) |
| 82 apps_parser = _IntroParser() | 78 apps_parser = _IntroParser() |
| 83 apps_parser.feed(Handlebar(intro_with_links).render( | 79 apps_parser.feed(Handlebar(intro_with_links).render( |
| (...skipping 30 matching lines...) Expand all Loading... |
| 114 return self._cache.GetFromFile('%s/%s' % (base_path, path)) | 110 return self._cache.GetFromFile('%s/%s' % (base_path, path)) |
| 115 for base_path in self._base_paths: | 111 for base_path in self._base_paths: |
| 116 try: | 112 try: |
| 117 return get_from_base_path(base_path) | 113 return get_from_base_path(base_path) |
| 118 except FileNotFoundError: | 114 except FileNotFoundError: |
| 119 continue | 115 continue |
| 120 # Not found. Do the first operation again so that we get a stack trace - we | 116 # Not found. Do the first operation again so that we get a stack trace - we |
| 121 # know that it'll fail. | 117 # know that it'll fail. |
| 122 get_from_base_path(self._base_paths[0]) | 118 get_from_base_path(self._base_paths[0]) |
| 123 raise AssertionError() | 119 raise AssertionError() |
| OLD | NEW |