def change_version(request): """ Change current documentation version. """ # Look for a new version in the URL get params. version = request.GET.get('preferred_version', settings.DEFAULT_DOCS_VERSION) response = redirect('/') path = urlparse(request.META.get('HTTP_REFERER')).path try: if not path == '/': response = _find_matching_equivalent_page_for( path, request, None, version) except: print("Unable to switch version properly. redirect to home page") content_id, lang, version = url_helper.get_parts_from_url_path(path) if lang == None: lang = 'en' response = redirect('/documentation/' + lang) return response
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 _find_matching_equivalent_page_for(path, request, lang=None, version=None): content_id, old_lang, old_version = url_helper.get_parts_from_url_path( path) # Try to find the page in this content's navigation. menu_path = menu_helper.get_menu_path_cache(content_id, old_lang, old_version) if content_id in ['book']: path = os.path.join( os.path.dirname(path), 'README.%smd' % ('' if old_lang == 'en' else 'cn.')) matching_link = None if menu_path.endswith('.json'): with open(menu_path, 'r') as menu_file: menu = json.loads(menu_file.read()) path_to_seek = url_helper.get_raw_page_path_from_html(path) if lang: # We are switching to new language matching_link = menu_helper.find_all_languages_for_link( path_to_seek, old_lang, menu['sections'], lang) version = old_version else: # We are switching to new version new_menu_path = menu_helper.get_menu_path_cache( content_id, old_lang, version) with open(new_menu_path, 'r') as new_menu_file: new_menu = json.loads(new_menu_file.read()) # Try to find this link in the new menu path. # NOTE: We account for the first and last '/'. matching_link = menu_helper.find_link_in_sections( new_menu['sections'], path_to_seek) lang = old_lang if matching_link: content_path, url_prefix = url_helper.get_full_content_path( content_id, lang, version) # Because READMEs get replaced by index.htmls, so we have to undo that. if content_id in ['book'] and old_lang != lang: matching_link = os.path.join( os.path.dirname(matching_link), 'index.%shtml' % ('' if lang == 'en' else 'cn.')) return redirect(url_helper.get_url_path(url_prefix, matching_link)) # If no such page is found, redirect to first link in the content. else: return _redirect_first_link_in_contents(request, content_id, version, lang)
def get_menu(request): if not settings.DEBUG: return HttpResponseServerError( 'You need to be in a local development environment to show the raw menu' ) path = urlparse(request.META.get('HTTP_REFERER')).path content_id, lang, version = url_helper.get_parts_from_url_path(path) navigation, menu_path = menu_helper.get_menu(content_id, lang, version) return HttpResponse(json.dumps(navigation), content_type='application/json')
def save_menu(request): try: assert settings.DEBUG menu = json.loads(request.POST.get('menu'), None) except: return HttpResponseServerError('You didn\'t submit a valid menu') # Write the new menu to disk. path = urlparse(request.META.get('HTTP_REFERER')).path content_id, lang, version = url_helper.get_parts_from_url_path(path) menu_path = menu_helper.get_menu_path_cache(content_id, lang, version) with open(menu_path, 'w') as menu_file: menu_file.write(json.dumps(menu, indent=4)) return HttpResponse(status='200')
def reload_docs(request): try: path = urlparse(request.META.get('HTTP_REFERER')).path # Get all the params from the URL and settings to generate new content. content_id, lang, version = url_helper.get_parts_from_url_path(path) menu_path = menu_helper.get_menu_path_cache(content_id, lang, version) content_path, url_prefix = url_helper.get_full_content_path( content_id, lang, version) # Generate new content. _generate_content(os.path.dirname(menu_path), content_path, content_id, lang, version) return redirect(path) except Exception as e: return HttpResponseServerError("Cannot reload docs: %s" % e)
def base_context(request): path = urlparse(request.path).path content_id, lang, version = url_helper.get_parts_from_url_path(path) if not version: version = portal_helper.get_preferred_version(request) if lang in ['en', 'zh' ] and lang != portal_helper.get_preferred_language(request): portal_helper.set_preferred_language(request, None, lang) else: lang = portal_helper.get_preferred_language(request) return { 'CURRENT_DOCS_VERSION': version, 'settings': settings, 'url_helper': url_helper, 'lang': lang, 'content_id': content_id }
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 _find_matching_equivalent_page_for(path, request, lang=None, version=None): content_id, old_lang, old_version = url_helper.get_parts_from_url_path( path) # Try to find the page in this content's navigation. menu_path = menu_helper.get_menu_path_cache(content_id, old_lang, old_version) if content_id in ['book']: path = os.path.join( os.path.dirname(path), 'README.%smd' % ('' if old_lang == 'en' else 'cn.')) matching_link = None if menu_path.endswith('.json'): with open(menu_path, 'r') as menu_file: menu = json.loads(menu_file.read()) path_to_seek = url_helper.get_raw_page_path_from_html(path) # HACK: If this is an API lookup, forcefully adapt to the naming # convention of api_cn/name_cn (and vice versa) for the paths to seek. # This is a result of the Chinese API introduction in v1.2 if not old_version < '1.2' and path_to_seek[0].startswith( 'api/') or path_to_seek[0].startswith('api_') and lang: new_path_to_seek = [] for p2s in list(path_to_seek): extensionless_path, extension = os.path.splitext( p2s.replace('api/', 'api_cn/') if old_lang == 'en' else p2s.replace('api_cn/', 'api/')) new_path_to_seek.append(( (extensionless_path + '_cn' ) if old_lang == 'en' else extensionless_path[:-3]) + extension) path_to_seek = tuple(new_path_to_seek) if lang: # HACK: Since we failed to find a way make a merged menu.json. new_menu_path = menu_helper.get_menu_path_cache( content_id, lang, old_version) with open(new_menu_path, 'r') as new_menu_file: new_menu = json.loads(new_menu_file.read()) # We are switching to new language matching_link = menu_helper.find_all_languages_for_link( path_to_seek, old_lang, new_menu['sections'], lang) version = old_version else: # We are switching to new version new_menu_path = menu_helper.get_menu_path_cache( content_id, old_lang, version) with open(new_menu_path, 'r') as new_menu_file: new_menu = json.loads(new_menu_file.read()) # Try to find this link in the new menu path. # NOTE: We account for the first and last '/'. matching_link = menu_helper.find_link_in_sections( new_menu['sections'], path_to_seek) lang = old_lang if matching_link: content_path, url_prefix = url_helper.get_full_content_path( content_id, lang, version) # Because READMEs get replaced by index.htmls, so we have to undo that. if content_id in ['book'] and old_lang != lang: matching_link = os.path.join( os.path.dirname(matching_link), 'index.%shtml' % ('' if lang == 'en' else 'cn.')) return redirect(url_helper.get_url_path(url_prefix, matching_link)) # If no such page is found, redirect to first link in the content. else: return _redirect_first_link_in_contents(request, content_id, version, lang)