def get_content_navigation(request, content_id, language, version): """ Get the navigation menu for a particular content service. """ if content_id == 'visualdl': valid_navigation_items = settings.VISUALDL_SIDE_NAVIGATION else: valid_navigation_items = settings.SIDE_NAVIGATION if version >= '0.14.0' and language == 'zh': # if the version is '0.14.0', we only show # 'Documentation' and 'API'. Otherwise, show all # ['Documentation', 'API', 'Book', 'Models', 'Mobile'] valid_navigation_items = settings.SIDE_NAVIGATION[:2] navigation = {'sections': []} for index, side_navigation_item in enumerate(valid_navigation_items): content_id = side_navigation_item['path'][1:] try: transformed_menu = _transform_section_urls( get_menu(content_id, language, version)[0], url_helper.get_page_url_prefix(content_id, language, version), content_id) if index > 0: navigation['sections'].append({ 'title': side_navigation_item['title'], 'sections': transformed_menu }) else: navigation['sections'] = transformed_menu except: # Since we re-arrange the models, mobile folders. We now need to guard against them. navigation['sections'].append({ 'title': side_navigation_item['title'], 'link': { 'en': '/' + url_helper.get_page_url_prefix( content_id, language, version) + '/README.html', 'zh': '/' + url_helper.get_page_url_prefix( content_id, language, version) + '/README.html' } }) return navigation
def _conditionally_preprocess_document(document, soup, path, subpath, version): """ Takes a soup-ed document that is about to be written into final output. Any changes can be conditionally made to it. """ # Determine if this is an API path, and specifically, if this is a path to # Chinese API. if subpath.startswith('/api_cn/') and len(subpath.split('/')) == 3 and ( subpath.split('/')[-1] != 'index_cn.html'): # Determine the class name. current_class = sys.modules['.'.join(['paddle', document.find('h1').contents[0]])] print 'Finding/building source for: ' + current_class.__file__ for api_call in document.find_all(re.compile('^h(1|2|3)')): url = _get_repo_source_url_from_api(current_class, api_call, version) # Create an element that wraps the heading level class or function # name. title_wrapper = soup.new_tag('div') title_wrapper['class'] = 'api-wrapper' api_call.insert_before(title_wrapper) api_call.wrap(title_wrapper) # NOTE: This path might be not unique in the system. # Needs to be made tighter in future. url_path = path[path.rfind('documentation/docs/'):] content_id, lang, version = url_helper.get_parts_from_url_path(url_path) # Now add a link on the same DOM wrapper of the heading to include # a link to the expected English doc link. lang_source_link_wrapper = soup.new_tag('div') lang_source_link_wrapper['class'] = 'btn-group' # Add a link to the GitHub source. source_link = soup.new_tag('a', href=url) source_link['target'] = '_blank' source_link.string = 'Source' source_link['class'] = 'btn btn-outline-info' lang_source_link_wrapper.append(source_link) # Add a link to the English docs source. lang_link = soup.new_tag('a', href=( '/' + url_helper.get_page_url_prefix(content_id, 'en', version)) + ( # Take everything after the version, and replace '_cn' in it. '/' + '/'.join(url_path.split('/')[4:]).replace('_cn', '')) + ( # This is usually the anchor bit. api_call.find('a')['href'])) lang_link.string = 'English' lang_link['class'] = 'btn btn-outline-secondary' lang_source_link_wrapper.append(lang_link) title_wrapper.append(lang_source_link_wrapper) return document
def get_menu_path_cache(content_id, lang, version): menu_path = cache.get( '%s/%s' % ('menu_path', url_helper.get_page_url_prefix(content_id, lang, version)), None) if not menu_path: menu_path = get_menu(content_id, lang, version)[1] set_menu_path_cache(content_id, lang, version, menu_path) return menu_path
def prepare_internal_urls(soup, lang, version): """ Replaces references to files in other repos with the "correct" links. """ all_internal_links = soup.select('a[repo]') for link in all_internal_links: content_id = link['repo'].lower() if link['version']: version = link['version'] link['href'] = url_helper.get_url_path( url_helper.get_page_url_prefix(content_id, lang, version), link['href']) del link['repo']
def _conditionally_preprocess_document(self, document, soup, path, subpath): """ Takes a soup-ed document that is about to be written into final output. Any changes can be conditionally made to it. """ # Determine if this is an API path, and specifically, if this is a path to # Chinese API. if self.version >= '1.2' and len(subpath.split('/')) == 3: is_chinese_api = subpath.startswith('/api_cn/') is_english_api = subpath.startswith('/api/') if (is_chinese_api or is_english_api) and (subpath.split('/')[-1] not in [ 'index_cn.html', 'index.html' ]): if is_chinese_api: # Determine the class name. current_class = sys.modules['.'.join( ['paddle', document.find('h1').contents[0]])] print 'Finding/building source for: ' + current_class.__file__ for api_call in document.find_all(re.compile('^h(1|2|3)')): if is_chinese_api: url = self._get_repo_source_url_from_api( current_class, api_call) # Create an element that wraps the heading level class or function # name. title_wrapper = soup.new_tag('div') title_wrapper['class'] = 'api-wrapper' api_call.insert_before(title_wrapper) api_call.wrap(title_wrapper) # NOTE: This path might be not unique in the system. # Needs to be made tighter in future. url_path = path[path.rfind('documentation/docs/'):] content_id, lang, version = url_helper.get_parts_from_url_path( url_path) # Now add a link on the same DOM wrapper of the heading to include # a link to the expected English doc link. lang_source_link_wrapper = soup.new_tag('div') lang_source_link_wrapper['class'] = 'btn-group' if is_chinese_api: # Add a link to the GitHub source. source_link = soup.new_tag('a', href=url) source_link['target'] = '_blank' source_link.string = 'Source' source_link['class'] = 'btn btn-outline-info' lang_source_link_wrapper.append(source_link) # Toggle the URL based on which language it can change into. if is_chinese_api: url_path_parts = url_path.split('/') page_path = os.path.join( os.path.join(*url_path_parts[4:-1]).replace( 'api_cn', 'api'), url_path_parts[-1].replace('_cn', '')) else: url_path_split = os.path.splitext(url_path) page_path = '/'.join(url_path_split[0].replace( '/api/', '/api_cn/').split('/') [4:]) + '_cn' + url_path_split[1] # Add a link to the alternative language's docs source. lang_link = soup.new_tag( 'a', href=('/' + url_helper.get_page_url_prefix( content_id, 'en' if is_chinese_api else 'zh', version)) + ( # Take everything after the version, and replace '_cn' in it. '/' + page_path) + ( # This is usually the anchor bit. api_call.find('a')['href'])) lang_link.string = 'English' if is_chinese_api else 'Chinese' lang_link['class'] = 'btn btn-outline-secondary' lang_source_link_wrapper.append(lang_link) title_wrapper.append(lang_source_link_wrapper) return document
def set_menu_path_cache(content_id, lang, version, path): cache.set( '%s/%s' % ('menu_path', url_helper.get_page_url_prefix(content_id, lang, version)), path)
def get_content_navigation(request, content_id, language, version): """ Get the navigation menu for a particular content service. """ if content_id == 'visualdl': valid_navigation_items = settings.VISUALDL_SIDE_NAVIGATION else: valid_navigation_items = settings.SIDE_NAVIGATION if language == 'zh': # if the version is '0.14.0', we only show # 'Documentation' and 'API'. Otherwise, show all # ['Documentation', 'API', 'Book', 'Models', 'Mobile'] if version >= '0.14.0': valid_navigation_items = settings.SIDE_NAVIGATION[:2] if version >= '1.1': valid_navigation_items = settings.SIDE_NAVIGATION[:1] elif language == 'en' and version >= '1.1': valid_navigation_items = ( settings.SIDE_NAVIGATION[:1]) + settings.SIDE_NAVIGATION[2:] navigation = {'sections': []} for index, side_navigation_item in enumerate(valid_navigation_items): content_id = side_navigation_item['path'][1:] try: transformed_menu = _transform_section_urls( get_menu(content_id, language, version)[0], url_helper.get_page_url_prefix(content_id, language, version), content_id) if index > 0: navigation['sections'].append({ 'title': side_navigation_item['title'], 'sections': transformed_menu }) else: navigation['sections'] = transformed_menu except Exception, e: # When we are unable to local the menu.json, we want to provide a link for users to click and generate # the documentations (ex: api), if type(e) == IOError: navigation['sections'].append({ 'title': side_navigation_item['title'], 'link': { 'en': url_helper.get_content_root_path(content_id), 'zh': url_helper.get_content_root_path(content_id) } }) else: # Since we re-arrange the models, mobile folders. We now need to guard against them. navigation['sections'].append({ 'title': side_navigation_item['title'], 'link': { 'en': '/' + url_helper.get_page_url_prefix( content_id, language, version) + '/README.html', 'zh': '/' + url_helper.get_page_url_prefix( content_id, language, version) + '/README.html' } })