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