def process_heading_alias(tree, node):
            init(tree)

            # TODO: have a reasonable arg validation story
            assert('name' in node.kwargs)

            # by visiting this alias, some links may be come valid.
            # make sure they are visited later.
            for link in tree.processor_data['heading_link_nodes']:
                if link.kwargs['name'] == node.kwargs['name']:
                    tree.mark_node_dirty(link)

            # find the "next" node with a TOC entry in this document.
            document = find_ancestor(node, lambda n: n.name == 'Document')
            toc_node = None
            for maybe_h in preorder_traversal(document, node):
                if maybe_h.data.get('toc_entry', None):
                    toc_node = maybe_h
                    break

            # if we found one, copy its TOC entry.
            if toc_node:
                # replace self with relevant markup (i.e. nothing).
                # incidentally guarantees that this node will never be
                # dirty again.
                tree.replace_node(node, CWEmptyNode())
                name = node.kwargs['name']
                toc_entry = toc_node.data['toc_entry']
                tree.processor_data['heading_aliases'][name] = toc_entry
            else:
                # the TOC entry might not have been created yet, so store a
                # reference to this node so our h* visitors can re-dirty it
                # and try again.
                tree.processor_data['heading_alias_nodes'].add(node)
 def process_document(tree, node):
     _add_toc_data_if_not_exists(tree)
     node.data['toc_entries'] = []
     ref_ids = set()
     for _node in preorder_traversal(node):
         if 'toc_entry' in _node.data:
             entry = _node.data['toc_entry']
             if entry.ref_id in ref_ids:
                 continue
             ref_ids.add(entry.ref_id)
             node.data['toc_entries'].append(entry)
     tree.mark_node_dirty(node.get_parent())
Beispiel #3
0
def write_document(config, options, output_dir, library, tree, document_node):
    body = _get_subtree_html(config, options, library, tree, document_node)

    output_path = output_dir
    for directory in document_node.document_id[:-1]:
        output_path = output_path / directory
    output_path = (output_path / document_node.document_id[-1]).with_suffix(".html")
    output_path.touch()

    nav_html = (
        _get_nav_html_part(
            config, options, library, tree, document_node, is_prev=True) +
        _get_nav_html_part(
            config, options, library, tree, document_node, is_prev=False))


    ctx = {k: v for k, v in config.items()}
    if not options.single_page:
        relative_site_url = doc_to_href(
            options,
            document_node,
            tree.processor_data['toc'][0][0].heading_node.document_id)
        ctx['title_url'] = relative_site_url
    else:
        ctx['title_url'] = options.site_url

    ctx['page_title'] = ctx['site_subtitle']
    for node in preorder_traversal(document_node):
        if node.name == 'h1':
            ctx['page_title'] = tree.subtree_to_text(node)
            break

    ctx['site_title_with_version'] = (
        ctx['site_title'] if ctx['project_version'] is None else
        '{} <span class="project-version">{}</span>'.format(
            ctx['site_title'], ctx['project_version'] ))

    with SINGLE_PAGE_TEMPLATE_PATH.open('r') as template_stream:
        with output_path.open('w') as output_stream:
            output_stream.write(template_stream.read().format(
                stylesheet_tags="".join(options.stylesheet_tag_strings),
                body=body,
                nav_html=nav_html,
                html_options=options,
                **ctx))