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 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 _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)