Пример #1
0
def intern_uri_anchor_value(docname, refuri):
    """
    determine the anchor value for an internal uri point

    This call helps determine the anchor value to use for a link to an anchor
    for an internal document. The anchor value will be parsed out of the
    provided URI checked to see if a target entry already exists (e.g. if a
    header with a preconfigured identifier can be used). If so, the target value
    will be provided. If not, the parsed/raw anchor value will be returned.

    Args:
        docname: the docname of the page to link to
        refuri: the uri

    Returns:
        the encoded text
    """

    anchor_value = None
    if '#' in refuri:
        anchor = refuri.split('#')[1]
        target_name = '{}#{}'.format(docname, anchor)

        # check if this target is reachable without an anchor; if so, use
        # the identifier value instead
        target = ConfluenceState.target(target_name)
        if target:
            anchor_value = target
        else:
            anchor_value = anchor

        anchor_value = encode_storage_format(anchor_value)

    return anchor_value
def build_intersphinx(builder):
    """
    build intersphinx information from the state of the builder

    Attempt to build a series of entries for an intersphinx inventory resource
    for Confluence builder generated content. This is only supported after
    processing a publishing event where page identifiers are cached to build URI
    entries.

    Args:
        builder: the builder
    """
    def escape(string):
        return re.sub("\\s+", ' ', string)

    if builder.cloud:
        pages_part = 'pages/{}/'
    else:
        pages_part = 'pages/viewpage.action?pageId={}'

    with open(path.join(builder.outdir, INVENTORY_FILENAME), 'wb') as f:
        # header
        f.write(('# Sphinx inventory version 2\n'
                 '# Project: %s\n'
                 '# Version: %s\n'
                 '# The remainder of this file is compressed using zlib.\n' %
                 (escape(builder.env.config.project),
                  escape(builder.env.config.version))).encode())

        # contents
        compressor = zlib.compressobj(9)

        for domainname, domain in sorted(builder.env.domains.items()):
            if domainname == 'std':
                for name, dispname, typ, docname, raw_anchor, prio in sorted(
                        domain.get_objects()):

                    page_id = ConfluenceState.uploadId(docname)
                    if not page_id:
                        continue

                    target_name = '{}#{}'.format(docname, raw_anchor)
                    target = ConfluenceState.target(target_name)

                    if raw_anchor and target:
                        title = ConfluenceState.title(docname)
                        anchor = 'id-' + title + '-' + target
                        anchor = anchor.replace(' ', '')

                        # confluence will convert quotes to right-quotes for
                        # anchor values; replace and encode the anchor value
                        anchor = anchor.replace('"', '”')
                        anchor = anchor.replace("'", '’')
                        anchor = requests.utils.quote(anchor)
                    else:
                        anchor = ''

                    uri = pages_part.format(page_id)
                    if anchor:
                        uri += '#' + anchor
                    if dispname == name:
                        dispname = '-'
                    entry = ('%s %s:%s %s %s %s\n' %
                             (name, domainname, typ, prio, uri, dispname))
                    ConfluenceLogger.verbose('(intersphinx) ' + entry.strip())
                    f.write(compressor.compress(entry.encode('utf-8')))

        f.write(compressor.flush())