Exemplo n.º 1
0
def _transform_section_urls(section, prefix, content_id):
    """
    Since paths defined in assets/menus/<version>.json are defined relative to the folder structure of the content
    directories, we will need to append the URL path prefix so our URL router knows how to resolve the URLs.
    """
    # Make a copy that we shall mutate.
    new_transformed_sections = []
    for subsection in section['sections']:
        new_subsection = {}

        for key, value in subsection.items():
            if key == 'link':
                new_subsection['link'] = {}

                for lang, lang_link in subsection['link'].items():
                    if content_id in ['book']:
                        lang_link = os.path.join(
                            os.path.dirname(lang_link),
                            'index.%shtml' % ('' if lang == 'en' else 'cn.'))

                    new_subsection['link'][lang] = url_helper.get_url_path(
                        prefix, lang_link)

            elif key == 'sections':
                new_subsection['sections'] = _transform_section_urls(
                    subsection, prefix, content_id)

            else:
                new_subsection[key] = value

        new_transformed_sections.append(new_subsection)

    return new_transformed_sections
Exemplo n.º 2
0
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)
Exemplo n.º 3
0
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']
Exemplo n.º 4
0
            path = _get_first_link_in_contents(navigation, lang)
        else:
            path = 'README.cn.html' if lang == 'zh' else 'README.html'

        # Because READMEs get replaced by index.htmls, so we have to undo that.
        if content_id in ['book']:
            path = os.path.join(
                os.path.dirname(path),
                'index.%shtml' % ('' if lang == 'en' else 'cn.'))

        if not path:
            msg = 'Cannot perform reverse lookup on link: %s' % path
            raise Exception(msg)

        return redirect(
            url_helper.get_url_path(url_prefix, path) +
            ('?raw=1' if is_raw else ''))

    except Exception as e:
        print e.message
        return redirect('/')


def _generate_content(source_dir, destination_dir, content_id, lang, version):
    # If this content has been generated yet, try generating it.
    if not os.path.exists(destination_dir):

        # Generate the directory.
        os.makedirs(destination_dir)

    transform(source_dir, destination_dir, content_id, version, lang)
Exemplo n.º 5
0
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)