Example #1
0
def _doc_link(rawtext, text, options={}, content=[]):
    """Handle the doc role."""
    # split link's text and post's slug in role content
    has_explicit_title, title, slug = split_explicit_title(text)
    # check if the slug given is part of our blog posts/pages
    twin_slugs = False
    post = None
    for p in doc_role.site.timeline:
        if p.meta('slug') == slug:
            if post is None:
                post = p
            else:
                twin_slugs = True
                break

    try:
        if post is None:
            raise ValueError("No post with matching slug found.")
    except ValueError:
        return False, False, None, None, slug

    if not has_explicit_title:
        # use post's title as link's text
        title = post.title()
    permalink = post.permalink()

    return True, twin_slugs, title, permalink, slug
Example #2
0
def _doc_link(rawtext, text, options={}, content=[]):
    """Handle the doc role."""
    # split link's text and post's slug in role content
    has_explicit_title, title, slug = split_explicit_title(text)
    if '#' in slug:
        slug, fragment = slug.split('#', 1)
    else:
        fragment = None

    # Look for the unslugified input first, then try to slugify (Issue #3450)
    post, twin_slugs = _find_post(slug)
    if post is None:
        slug = slugify(slug)
        post, twin_slugs = _find_post(slug)

    try:
        if post is None:
            raise ValueError("No post with matching slug found.")
    except ValueError:
        return False, False, None, None, slug

    if not has_explicit_title:
        # use post's title as link's text
        title = post.title()
    permalink = post.permalink()
    if fragment:
        permalink += '#' + fragment

    return True, twin_slugs, title, permalink, slug
Example #3
0
def _doc_link(rawtext, text, options={}, content=[]):
    """Handle the doc role."""
    # split link's text and post's slug in role content
    has_explicit_title, title, slug = split_explicit_title(text)
    # check if the slug given is part of our blog posts/pages
    twin_slugs = False
    post = None
    for p in doc_role.site.timeline:
        if p.meta('slug') == slug:
            if post is None:
                post = p
            else:
                twin_slugs = True
                break

    try:
        if post is None:
            raise ValueError("No post with matching slug found.")
    except ValueError:
        return False, False, None, None, slug

    if not has_explicit_title:
        # use post's title as link's text
        title = post.title()
    permalink = post.permalink()

    return True, twin_slugs, title, permalink, slug
Example #4
0
def doc_role(name, rawtext, text, lineno, inliner, options={}, content=[]):

    # split link's text and post's slug in role content
    has_explicit_title, title, slug = split_explicit_title(text)
    # check if the slug given is part of our blog posts/pages
    twin_slugs = False
    post = None
    for p in doc_role.site.timeline:
        if p.meta("slug") == slug:
            if post is None:
                post = p
            else:
                twin_slugs = True
                break

    try:
        if post is None:
            raise ValueError
    except ValueError:
        msg = inliner.reporter.error('"{0}" slug doesn\'t exist.'.format(slug), line=lineno)
        prb = inliner.problematic(rawtext, rawtext, msg)
        return [prb], [msg]

    if not has_explicit_title:
        # use post's title as link's text
        title = post.title()
    permalink = post.permalink()
    if twin_slugs:
        msg = inliner.reporter.warning('More than one post with the same slug. Using "{0}"'.format(permalink))

    node = make_link_node(rawtext, title, permalink, options)
    return [node], []
Example #5
0
def doc_role(name, rawtext, text, lineno, inliner, options={}, content=[]):

    # split link's text and post's slug in role content
    has_explicit_title, title, slug = split_explicit_title(text)
    # check if the slug given is part of our blog posts/pages
    twin_slugs = False
    post = None
    for p in doc_role.site.timeline:
        if p.meta('slug') == slug:
            if post is None:
                post = p
            else:
                twin_slugs = True
                break

    try:
        if post is None:
            raise ValueError
    except ValueError:
        msg = inliner.reporter.error('"{0}" slug doesn\'t exist.'.format(slug),
                                     line=lineno)
        prb = inliner.problematic(rawtext, rawtext, msg)
        return [prb], [msg]

    if not has_explicit_title:
        # use post's title as link's text
        title = post.title()
    permalink = post.permalink()
    if twin_slugs:
        msg = inliner.reporter.warning(
            'More than one post with the same slug. Using "{0}"'.format(
                permalink))

    node = make_link_node(rawtext, title, permalink, options)
    return [node], []
Example #6
0
def _class_link(name, rawtext, text):
    """Handle the class role."""
    if class_role.site.processing_targets:
        return True, True, None, None, None, None

    context_map = {'py': 'cython', 'mat': 'matlab', 'cti': 'cti'}
    context = name.split(':', 1)[0]
    if context == name:
        context = class_role.site.config['DEFAULT_CONTEXT']
        default_context = True
    else:
        default_context = False

    target = '{}_targets'.format(context_map[context])

    if class_role.site.cache.get(target) is not None:
        targets = class_role.site.cache.get(target).copy()
    else:
        targets = getattr(class_role.site, target).copy()

    has_explicit_title, title, label = split_explicit_title(text)

    if label not in targets and not default_context:
        LOGGER.error('The label {label} was not found in the context {context}'.format(
            label=label, context=context))
        return False, False, None, None, None, label
    elif label not in targets and default_context:
        c_map = context_map.copy()
        c_map.pop(context)
        found_label = False
        for context, t in c_map.items():
            target = '{}_targets'.format(t)
            if class_role.site.cache.get(target) is not None:
                targets = class_role.site.cache.get(target).copy()
            else:
                targets = getattr(class_role.site, target).copy()

            if label not in targets:
                continue
            else:
                found_label = True
                LOGGER.info('The label {} was found in the context {}. Consider explicitly '
                            'specifying the context for this link'.format(label, context))
                break

        if not found_label:
            LOGGER.error('The label {} could not be found in any context'.format(label))
            return False, False, None, None, None, label

    doc_file = targets[label][0]
    permalink = '/documentation/docs-{}/'.format(class_role.site.config['CANTERA_VERSION'])
    permalink += doc_file + '#' + targets[label][1]
    code_node = nodes.literal(rawtext, targets[label][2] + '()', classes=['code', 'xref', context])
    if not has_explicit_title:
        title = targets[label][1]

    return True, False, title, code_node, permalink, label
Example #7
0
def _ref_link(rawtext, text, options={}, content=[]):
    """Handle the ref role."""
    # If we're just processing the targets, ignore the role
    if ref_role.site.processing_targets:
        return True, True, None, None, None

    has_explicit_title, title, target = split_explicit_title(text)

    if ref_role.site.cache.get('ref_targets') is not None:
        ref_targets = ref_role.site.cache.get('ref_targets').copy()
    else:
        ref_targets = ref_role.site.ref_targets.copy()

    if ref_role.site.cache.get('anon_ref_targets') is not None:
        anon_ref_targets = ref_role.site.cache.get('anon_ref_targets').copy()
    else:
        anon_ref_targets = ref_role.site.anon_ref_targets.copy()

    if target not in ref_targets and (target in anon_ref_targets
                                      and not has_explicit_title):
        LOGGER.error(
            'Anonymous targets must have a link text: {}'.format(target))
        return False, False, None, None, target
    elif target in anon_ref_targets:
        permalink = anon_ref_targets[target][0]
        if permalink.endswith('/'):
            permalink += 'index.html'

        permalink += '#' + target

        return True, False, title, permalink, target
    else:
        LOGGER.error('Unknown reference target: {}'.format(target))
        # LOGGER.error('ref_targets is: {}'.format(ref_role.site.ref_targets))
        return False, False, None, None, target

    if target not in ref_targets:
        LOGGER.error('Unknown reference target: {}'.format(target))
        # LOGGER.error('ref_targets is: {}'.format(ref_role.site.ref_targets))
        return False, False, None, None, target

    permalink = ref_targets[target][0]
    if permalink.endswith('/'):
        permalink += 'index.html'
    if not has_explicit_title:
        title = ref_targets[target][2]

    permalink += '#' + target

    return True, False, title, permalink, target
Example #8
0
def _class_link(name, rawtext, text):
    """Handle the class role."""
    if class_role.site.processing_targets:
        return True, True, None, None, None, None

    context_map = {"py": "cython", "mat": "matlab", "cti": "cti"}
    context = name.split(":", 1)[0]
    if context == name:
        context = class_role.site.config["DEFAULT_CONTEXT"]
        default_context = True
    else:
        default_context = False

    target = "{}_targets".format(context_map[context])

    if class_role.site.cache.get(target) is not None:
        targets = class_role.site.cache.get(target).copy()
    else:
        targets = getattr(class_role.site, target).copy()

    has_explicit_title, title, label = split_explicit_title(text)

    if label not in targets and not default_context:
        LOGGER.error(
            "The label {label} was not found in the context {context}".format(
                label=label, context=context))
        return False, False, None, None, None, label
    elif label not in targets and default_context:
        c_map = context_map.copy()
        c_map.pop(context)
        found_label = False
        for context, t in c_map.items():
            target = "{}_targets".format(t)
            if class_role.site.cache.get(target) is not None:
                targets = class_role.site.cache.get(target).copy()
            else:
                targets = getattr(class_role.site, target).copy()

            if label not in targets:
                continue
            else:
                found_label = True
                LOGGER.info(
                    "The label {} was found in the context {}. Consider explicitly "
                    "specifying the context for this link".format(
                        label, context))
                break

        if not found_label:
            LOGGER.error(
                "The label {} could not be found in any context".format(label))
            return False, False, None, None, None, label

    doc_file = targets[label][0]
    permalink = "/documentation/docs-{}/".format(
        class_role.site.config["CANTERA_VERSION"])
    permalink += doc_file + "#" + targets[label][1]
    code_node = nodes.literal(rawtext,
                              targets[label][2] + "()",
                              classes=["code", "xref", context])
    if not has_explicit_title:
        title = targets[label][1]

    return True, False, title, code_node, permalink, label