def _MakeIntroDict(self, intro): intro_with_links = self._ref_resolver.ResolveAllLinks( intro, 'intro') apps_parser = _IntroParser() apps_parser.feed( Handlebar(intro_with_links).render({ 'is_apps': True }).text) extensions_parser = _IntroParser() extensions_parser.feed( Handlebar(intro_with_links).render({ 'is_apps': False }).text) # TODO(cduvall): Use the normal template rendering system, so we can check # errors. if extensions_parser.page_title != apps_parser.page_title: logging.error( 'Title differs for apps and extensions: Apps: %s, Extensions: %s.' % (extensions_parser.page_title, apps_parser.page_title)) # The templates will render the heading themselves, so remove it from the # HTML content. intro_with_links = re.sub(_H1_REGEX, '', intro_with_links, count=1) return { 'intro': Handlebar(intro_with_links), 'title': apps_parser.page_title, 'apps_toc': apps_parser.toc, 'extensions_toc': extensions_parser.toc, }
def _CompileContent(self, path, text): assert text is not None, path _, ext = posixpath.splitext(path) mimetype = _MIMETYPE_OVERRIDES.get(ext, mimetypes.guess_type(path)[0]) if ext == '.md': # See http://pythonhosted.org/Markdown/extensions # for details on "extensions=". content = markdown(ToUnicode(text), extensions=('extra', 'headerid', 'sane_lists')) if self._supports_templates: content = Handlebar(content, name=path) mimetype = 'text/html' elif mimetype is None: content = text mimetype = 'text/plain' elif mimetype == 'text/html': content = ToUnicode(text) if self._supports_templates: content = Handlebar(content, name=path) elif (mimetype.startswith('text/') or mimetype in ('application/javascript', 'application/json')): content = ToUnicode(text) else: content = text return ContentAndType(content, mimetype, self.file_system.Stat(path).version)
def _MakeIntroDict(self, intro_path, intro): # Guess the name of the API from the path to the intro. api_name = os.path.splitext(intro_path.split('/')[-1])[0] intro_with_links = self._ref_resolver.ResolveAllLinks( intro, namespace=api_name) # TODO(kalman): Do $ref replacement after rendering the template, not # before, so that (a) $ref links can contain template annotations, and (b) # we can use CompiledFileSystem.ForTemplates to create the templates and # save ourselves some effort. apps_parser = _IntroParser() apps_parser.feed( Handlebar(intro_with_links).render({ 'is_apps': True }).text) extensions_parser = _IntroParser() extensions_parser.feed( Handlebar(intro_with_links).render({ 'is_apps': False }).text) # TODO(cduvall): Use the normal template rendering system, so we can check # errors. if extensions_parser.page_title != apps_parser.page_title: logging.error( 'Title differs for apps and extensions: Apps: %s, Extensions: %s.' % (extensions_parser.page_title, apps_parser.page_title)) # The templates will render the heading themselves, so remove it from the # HTML content. intro_with_links = re.sub(_H1_REGEX, '', intro_with_links, count=1) return { 'intro': Handlebar(intro_with_links), 'title': apps_parser.page_title, 'apps_toc': apps_parser.toc, 'extensions_toc': extensions_parser.toc, }
def testSimple(self): self._base_path = os.path.join(self._base_path, 'simple') fetcher = LocalFileSystem(self._base_path) cache_factory = CompiledFileSystem.Factory(fetcher, self._object_store) t_data_source = self._CreateTemplateDataSource(cache_factory) template_a1 = Handlebar(self._ReadLocalFile('test1.html')) self.assertEqual(template_a1.render({}, {'templates': {}}).text, t_data_source.get('test1').render({}, {'templates': {}}).text) template_a2 = Handlebar(self._ReadLocalFile('test2.html')) self.assertEqual(template_a2.render({}, {'templates': {}}).text, t_data_source.get('test2').render({}, {'templates': {}}).text) self.assertEqual(None, t_data_source.get('junk.html'))
def testSimple(self): self._base_path = os.path.join(self._base_path, 'simple') fetcher = LocalFileSystem(self._base_path) cache_builder = FileSystemCache.Builder(fetcher) t_data_source = self._CreateTemplateDataSource( self._fake_api_data_source_factory, cache_builder) template_a1 = Handlebar(self._ReadLocalFile('test1.html')) self.assertEqual(template_a1.render({}, {'templates': {}}).text, t_data_source['test1'].render({}, {'templates': {}}).text) template_a2 = Handlebar(self._ReadLocalFile('test2.html')) self.assertEqual(template_a2.render({}, {'templates': {}}).text, t_data_source['test2'].render({}, {'templates': {}}).text) self.assertEqual(None, t_data_source['junk.html'])
def testSimple(self): self._base_path = os.path.join(self._base_path, "simple") fetcher = LocalFetcher(self._base_path) cache_builder = FetcherCache.Builder(fetcher, 0) t_data_source = self._CreateTemplateDataSource(self._fake_api_data_source, cache_builder) template_a1 = Handlebar(self._ReadLocalFile("test1.html")) self.assertEqual( template_a1.render({}, {"templates": {}}).text, t_data_source["test1"].render({}, {"templates": {}}).text ) template_a2 = Handlebar(self._ReadLocalFile("test2.html")) self.assertEqual( template_a2.render({}, {"templates": {}}).text, t_data_source["test2"].render({}, {"templates": {}}).text ) self.assertEqual(None, t_data_source["junk.html"])
def ForTemplates(self, file_system): '''Creates a CompiledFileSystem for parsing templates. ''' return self.Create( file_system, SingleFile(lambda path, text: Handlebar(ToUnicode(text), name=path)), CompiledFileSystem)
def testSimple(self): self._base_path = os.path.join(self._base_path, "simple") fetcher = LocalFileSystem(self._base_path) cache_factory = CompiledFileSystem.Factory(fetcher, self._object_store) t_data_source = self._CreateTemplateDataSource(self._fake_api_data_source_factory, cache_factory) template_a1 = Handlebar(self._ReadLocalFile("test1.html")) self.assertEqual( template_a1.render({}, {"templates": {}}).text, t_data_source["test1"].render({}, {"templates": {}}).text ) template_a2 = Handlebar(self._ReadLocalFile("test2.html")) self.assertEqual( template_a2.render({}, {"templates": {}}).text, t_data_source["test2"].render({}, {"templates": {}}).text ) self.assertEqual(None, t_data_source["junk.html"])
def testSimple(self): self._base_path = os.path.join(self._base_path, 'simple') fetcher = LocalFileSystem(self._base_path) compiled_fs_factory = CompiledFileSystem.Factory( fetcher, ObjectStoreCreator.ForTest()) t_data_source = self._CreateTemplateDataSource( compiled_fs_factory, ObjectStoreCreator.ForTest()) template_a1 = Handlebar(self._ReadLocalFile('test1.html')) self.assertEqual(template_a1.render({}, {'templates': {}}).text, t_data_source.get('test1').render({}, {'templates': {}}).text) template_a2 = Handlebar(self._ReadLocalFile('test2.html')) self.assertEqual(template_a2.render({}, {'templates': {}}).text, t_data_source.get('test2').render({}, {'templates': {}}).text)
def testSimple(self): self._base_path = os.path.join(self._base_path, 'simple') fetcher = LocalFetcher(self._base_path) cache_builder = FetcherCache.Builder(fetcher, 0) t_data_source = TemplateDataSource('fake_branch', self._fake_api_data_source, cache_builder, ['./']) template_a1 = Handlebar(self._ReadLocalFile('test1.html')) self.assertEqual(template_a1.render({}, {'templates': {}}).text, t_data_source['test1'].render({}, {'templates': {}}).text) template_a2 = Handlebar(self._ReadLocalFile('test2.html')) self.assertEqual(template_a2.render({}, {'templates': {}}).text, t_data_source['test2'].render({}, {'templates': {}}).text) self.assertEqual(None, t_data_source['junk.html'])
def _MakeIntroDict(self, intro): parser = _IntroParser() parser.feed(intro) intro = re.sub(_H1_REGEX, '', intro, count=1) return { 'intro': Handlebar(intro), 'toc': parser.toc, 'title': parser.page_title }
def _MakeIntroDict(self, intro): apps_parser = _IntroParser() apps_parser.feed(Handlebar(intro).render({ 'is_apps': True }).text) extensions_parser = _IntroParser() extensions_parser.feed(Handlebar(intro).render({ 'is_apps': False }).text) # TODO(cduvall): Use the normal template rendering system, so we can check # errors. if extensions_parser.page_title != apps_parser.page_title: logging.error( 'Title differs for apps and extensions: Apps: %s, Extensions: %s.' % (extensions_parser.page_title, apps_parser.page_title)) intro = re.sub(_H1_REGEX, '', intro, count=1) return { 'intro': Handlebar(intro), 'title': apps_parser.page_title, # TODO(cduvall): Take this out, this is so the old TOCs don't break. 'toc': extensions_parser.toc, 'apps_toc': apps_parser.toc, 'extensions_toc': extensions_parser.toc, }
def testSimple(self): self._base_path = os.path.join(self._base_path, 'simple') fetcher = LocalFileSystem(self._base_path) compiled_fs_factory = CompiledFileSystem.Factory( fetcher, ObjectStoreCreator.Factory()) t_data_source = self._CreateTemplateDataSource( compiled_fs_factory, ObjectStoreCreator.Factory()) template_a1 = Handlebar(self._ReadLocalFile('test1.html')) self.assertEqual( template_a1.render({}, { 'templates': {} }).text, t_data_source.get('test1').render({}, { 'templates': {} }).text) template_a2 = Handlebar(self._ReadLocalFile('test2.html')) self.assertEqual( template_a2.render({}, { 'templates': {} }).text, t_data_source.get('test2').render({}, { 'templates': {} }).text) self.assertEqual(None, t_data_source.get('junk.html'))
def _CompileContent(self, path, text): assert text is not None, path mimetype = mimetypes.guess_type(path)[0] if mimetype is None: content = text mimetype = 'text/plain' elif mimetype == 'text/html': content = ToUnicode(text) if self._supports_templates: content = Handlebar(content) elif (mimetype.startswith('text/') or mimetype in ('application/javascript', 'application/json')): content = ToUnicode(text) else: content = text return ContentAndType(content, mimetype)
def testSimple(self): test_data_source = _CreateTestDataSource('simple') template_a1 = Handlebar(_ReadFile('simple', 'test1.html')) context = [{}, {'templates': {}}] self.assertEqual( template_a1.Render(*context).text, test_data_source.get('test1').Render(*context).text) template_a2 = Handlebar(_ReadFile('simple', 'test2.html')) self.assertEqual( template_a2.Render(*context).text, test_data_source.get('test2').Render(*context).text)
def testSimple(self): self._base_path = os.path.join(self._base_path, 'simple') fetcher = LocalFetcher(self._base_path) cache_builder = FetcherCache.Builder(fetcher, 0) t_data_source = self._CreateTemplateDataSource(self._fake_api_data_source, cache_builder) template_a1 = Handlebar(self._ReadLocalFile('test1.html')) self.assertEqual(template_a1.render({}, {'templates': {}}).text, t_data_source['test1'].render({}, {'templates': {}}).text) template_a2 = Handlebar(self._ReadLocalFile('test2.html')) self.assertEqual(template_a2.render({}, {'templates': {}}).text, t_data_source['test2'].render({}, {'templates': {}}).text) self.assertEqual(None, t_data_source['junk.html'])
def testSimple(self): self._base_path = os.path.join(self._base_path, 'simple') fetcher = LocalFileSystem(self._base_path) cache_factory = CompiledFileSystem.Factory(fetcher, self._object_store) t_data_source = self._CreateTemplateDataSource( self._fake_api_data_source_factory, cache_factory) template_a1 = Handlebar(self._ReadLocalFile('test1.html')) self.assertEqual( template_a1.render({}, { 'templates': {} }).text, t_data_source['test1'].render({}, { 'templates': {} }).text) template_a2 = Handlebar(self._ReadLocalFile('test2.html')) self.assertEqual( template_a2.render({}, { 'templates': {} }).text, t_data_source['test2'].render({}, { 'templates': {} }).text) self.assertEqual(None, t_data_source['junk.html'])
def _RenderTest(self, name, data_source): template_name = name + '_tmpl.html' template = Handlebar(self._ReadLocalFile(template_name)) self.assertEquals( self._ReadLocalFile(name + '_expected.html'), data_source.Render(template_name))
def _MakeIntro(self, intro_path, intro): # Guess the name of the API from the path to the intro. api_name = os.path.splitext(intro_path.split('/')[-1])[0] return Handlebar(self._ref_resolver.ResolveAllLinks( intro, relative_to=self._request.path, namespace=api_name), name=intro_path)
def testSimpleWiring(self): template = Handlebar('hello {{?true}}{{strings.extension}}{{/}}') text, warnings = self._template_renderer.Render(template, None) self.assertEqual('hello extension', text) self.assertEqual([], warnings)
def _CreateTemplate(self, template_name, text): return Handlebar(self._ref_resolver.ResolveAllLinks(text))
def _LoadTemplate(self, template): return Handlebar(template)