| 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 extensions_paths import INTROS_TEMPLATES, ARTICLES_TEMPLATES |
| 10 from docs_server_utils import FormatKey | 11 from docs_server_utils import FormatKey |
| 11 from file_system import FileNotFoundError | 12 from file_system import FileNotFoundError |
| 12 from third_party.handlebar import Handlebar | 13 from third_party.handlebar import Handlebar |
| 13 | 14 |
| 14 # TODO(kalman): rename this HTMLDataSource or other, then have separate intro | |
| 15 # article data sources created as instances of it. | |
| 16 | 15 |
| 17 _H1_REGEX = re.compile('<h1[^>.]*?>.*?</h1>', flags=re.DOTALL) | 16 _H1_REGEX = re.compile('<h1[^>.]*?>.*?</h1>', flags=re.DOTALL) |
| 18 | 17 |
| 18 |
| 19 # TODO(kalman): rename this HTMLDataSource or other, then have separate intro |
| 20 # article data sources created as instances of it. |
| 19 class _IntroParser(HTMLParser): | 21 class _IntroParser(HTMLParser): |
| 20 ''' An HTML parser which will parse table of contents and page title info out | 22 ''' An HTML parser which will parse table of contents and page title info out |
| 21 of an intro. | 23 of an intro. |
| 22 ''' | 24 ''' |
| 23 def __init__(self): | 25 def __init__(self): |
| 24 HTMLParser.__init__(self) | 26 HTMLParser.__init__(self) |
| 25 self.toc = [] | 27 self.toc = [] |
| 26 self.page_title = None | 28 self.page_title = None |
| 27 self._recent_tag = None | 29 self._recent_tag = None |
| 28 self._current_heading = {} | 30 self._current_heading = {} |
| (...skipping 27 matching lines...) Expand all Loading... |
| 56 else: | 58 else: |
| 57 self.page_title += data | 59 self.page_title += data |
| 58 elif self._recent_tag in ['h2', 'h3']: | 60 elif self._recent_tag in ['h2', 'h3']: |
| 59 self._current_heading['title'] += data | 61 self._current_heading['title'] += data |
| 60 | 62 |
| 61 class IntroDataSource(object): | 63 class IntroDataSource(object): |
| 62 '''This class fetches the intros for a given API. From this intro, a table | 64 '''This class fetches the intros for a given API. From this intro, a table |
| 63 of contents dictionary is created, which contains the headings in the intro. | 65 of contents dictionary is created, which contains the headings in the intro. |
| 64 ''' | 66 ''' |
| 65 class Factory(object): | 67 class Factory(object): |
| 66 def __init__(self, | 68 def __init__(self, compiled_fs_factory, file_system, ref_resolver_factory): |
| 67 compiled_fs_factory, | |
| 68 file_system, | |
| 69 ref_resolver_factory, | |
| 70 base_paths): | |
| 71 self._cache = compiled_fs_factory.Create(file_system, | 69 self._cache = compiled_fs_factory.Create(file_system, |
| 72 self._MakeIntroDict, | 70 self._MakeIntroDict, |
| 73 IntroDataSource) | 71 IntroDataSource) |
| 74 self._ref_resolver = ref_resolver_factory.Create() | 72 self._ref_resolver = ref_resolver_factory.Create() |
| 75 self._base_paths = base_paths | |
| 76 | 73 |
| 77 def _MakeIntroDict(self, intro_path, intro): | 74 def _MakeIntroDict(self, intro_path, intro): |
| 78 # Guess the name of the API from the path to the intro. | 75 # Guess the name of the API from the path to the intro. |
| 79 api_name = os.path.splitext(intro_path.split('/')[-1])[0] | 76 api_name = os.path.splitext(intro_path.split('/')[-1])[0] |
| 80 intro_with_links = self._ref_resolver.ResolveAllLinks(intro, | 77 intro_with_links = self._ref_resolver.ResolveAllLinks(intro, |
| 81 namespace=api_name) | 78 namespace=api_name) |
| 82 # TODO(kalman): Do $ref replacement after rendering the template, not | 79 # TODO(kalman): Do $ref replacement after rendering the template, not |
| 83 # before, so that (a) $ref links can contain template annotations, and (b) | 80 # before, so that (a) $ref links can contain template annotations, and (b) |
| 84 # we can use CompiledFileSystem.ForTemplates to create the templates and | 81 # we can use CompiledFileSystem.ForTemplates to create the templates and |
| 85 # save ourselves some effort. | 82 # save ourselves some effort. |
| (...skipping 13 matching lines...) Expand all Loading... |
| 99 # HTML content. | 96 # HTML content. |
| 100 intro_with_links = re.sub(_H1_REGEX, '', intro_with_links, count=1) | 97 intro_with_links = re.sub(_H1_REGEX, '', intro_with_links, count=1) |
| 101 return { | 98 return { |
| 102 'intro': Handlebar(intro_with_links), | 99 'intro': Handlebar(intro_with_links), |
| 103 'title': apps_parser.page_title, | 100 'title': apps_parser.page_title, |
| 104 'apps_toc': apps_parser.toc, | 101 'apps_toc': apps_parser.toc, |
| 105 'extensions_toc': extensions_parser.toc, | 102 'extensions_toc': extensions_parser.toc, |
| 106 } | 103 } |
| 107 | 104 |
| 108 def Create(self): | 105 def Create(self): |
| 109 return IntroDataSource(self._cache, self._base_paths) | 106 return IntroDataSource(self._cache) |
| 110 | 107 |
| 111 def __init__(self, cache, base_paths): | 108 def __init__(self, cache): |
| 112 self._cache = cache | 109 self._cache = cache |
| 113 self._base_paths = base_paths | |
| 114 | 110 |
| 115 def get(self, key): | 111 def get(self, key): |
| 116 path = FormatKey(key) | 112 path = FormatKey(key) |
| 117 def get_from_base_path(base_path): | 113 def get_from_base_path(base_path): |
| 118 return self._cache.GetFromFile('%s/%s' % (base_path, path)).Get() | 114 return self._cache.GetFromFile('%s/%s' % (base_path, path)).Get() |
| 119 for base_path in self._base_paths: | 115 base_paths = (INTROS_TEMPLATES, ARTICLES_TEMPLATES) |
| 116 for base_path in base_paths: |
| 120 try: | 117 try: |
| 121 return get_from_base_path(base_path) | 118 return get_from_base_path(base_path) |
| 122 except FileNotFoundError: | 119 except FileNotFoundError: |
| 123 continue | 120 continue |
| 124 # Not found. Do the first operation again so that we get a stack trace - we | 121 # Not found. Do the first operation again so that we get a stack trace - we |
| 125 # know that it'll fail. | 122 # know that it'll fail. |
| 126 get_from_base_path(self._base_paths[0]) | 123 get_from_base_path(base_paths[0]) |
| 127 raise AssertionError() | 124 raise AssertionError() |
| OLD | NEW |