Beispiel #1
0
def render_pages(*, page_render_jobs: List[PageRenderJob], source_code_stats: List[SourceCodeStat], site_render_config: SiteRenderConfig) -> Dict[pathlib.Path, bytes]:
    """
    :returns: mapping from absolute paths to file contents
    """

    page_title_dict = _build_page_title_dict(page_render_jobs=page_render_jobs)
    source_code_stats_dict = {stat.path: stat for stat in source_code_stats}

    rendered_pages: Dict[pathlib.Path, bytes] = {}
    for job in page_render_jobs:
        documentation_of = job.front_matter.get(FrontMatterItem.documentation_of.value)

        front_matter = copy.deepcopy(job.front_matter)
        if front_matter.get(FrontMatterItem.layout.value) == 'toppage':
            data = _render_source_code_stats_for_top_page(source_code_stats=source_code_stats, page_title_dict=page_title_dict, basedir=site_render_config.basedir)
            front_matter[FrontMatterItem.data.value] = data

        elif documentation_of is not None:
            front_matter.setdefault(FrontMatterItem.layout.value, 'document')
            data = _render_source_code_stat_for_page(pathlib.Path(documentation_of), source_code_stats_dict=source_code_stats_dict, page_title_dict=page_title_dict, basedir=site_render_config.basedir)
            front_matter.setdefault(FrontMatterItem.data.value, data)

        path = site_render_config.destination_dir / job.path
        content = onlinejudge_verify.documentation.front_matter.merge_front_matter(front_matter, job.content)
        rendered_pages[path] = content

    return rendered_pages
def convert_to_page_render_jobs(
        *, source_code_stats: List[SourceCodeStat],
        markdown_paths: List[pathlib.Path],
        site_render_config: SiteRenderConfig) -> List[PageRenderJob]:
    basedir = site_render_config.basedir

    page_render_jobs: Dict[pathlib.Path, PageRenderJob] = {}

    # Markdown pages
    for markdown_path in markdown_paths:
        markdown_absolute_path = (basedir / markdown_path).resolve()
        markdown_relative_path = markdown_absolute_path.relative_to(basedir)

        with open(markdown_path, 'rb') as fh:
            content = fh.read()
        front_matter, content = onlinejudge_verify.documentation.front_matter.split_front_matter(
            content)

        # move the location if documentation_of field exists
        path = markdown_relative_path
        documentation_of = front_matter.get(
            FrontMatterItem.documentation_of.value)
        if documentation_of is not None:
            documentation_of_path = resolve_documentation_of(
                documentation_of, markdown_path=path, basedir=basedir)
            if documentation_of_path is None:
                logger.warning(
                    'the `documentation_of` path of %s is not found: %s',
                    str(path), documentation_of)
                del front_matter[FrontMatterItem.documentation_of.value]
                continue
            documentation_of_relative_path = documentation_of_path.resolve(
            ).relative_to(basedir)
            front_matter[FrontMatterItem.documentation_of.value] = str(
                documentation_of_relative_path)
            path = documentation_of_relative_path.parent / (
                documentation_of_path.name + '.md')

        job = PageRenderJob(
            path=path,
            front_matter=front_matter,
            content=content,
        )
        page_render_jobs[job.path] = job

    # API pages
    for stat in source_code_stats:
        path = stat.path.parent / (stat.path.name + '.md')
        if path in page_render_jobs:
            continue

        front_matter = {}
        front_matter[FrontMatterItem.documentation_of.value] = str(stat.path)

        # add redirects from old URLs
        old_directory = 'verify' if stat.is_verification_file else 'library'
        front_matter[FrontMatterItem.redirect_from.value] = [
            '/' + str(pathlib.Path(old_directory) / stat.path),
            '/' + str(
                pathlib.Path(old_directory) / stat.path.parent /
                (stat.path.name + '.html')),
        ]

        # add title specified as a attributes like @title or @brief
        front_matter[FrontMatterItem.title.value] = str(stat.path)
        if 'document_title' in stat.attributes:
            front_matter[FrontMatterItem.title.
                         value] = stat.attributes['document_title']

        # treat @docs path/to.md directives
        content = b''
        if '_deprecated_at_docs' in stat.attributes:
            at_docs_path = pathlib.Path(stat.attributes['_deprecated_at_docs'])
            try:
                with open(at_docs_path, 'rb') as fh:
                    content = fh.read()
            except FileNotFoundError as e:
                logger.exception(
                    'failed to read markdown file specified by @docs in %s: %s',
                    str(stat.path), e)

        job = PageRenderJob(
            path=path,
            front_matter=front_matter,
            content=content,
        )
        page_render_jobs[job.path] = job

    # top page
    if pathlib.Path('index.md') not in page_render_jobs:
        content = b''
        if site_render_config.index_md.exists():
            with site_render_config.index_md.open('rb') as fh:
                content = fh.read()
        job = PageRenderJob(
            path=pathlib.Path('index.md'),
            front_matter={
                'layout': 'toppage',
            },
            content=content,
        )
        page_render_jobs[job.path] = job

    return list(page_render_jobs.values())