Exemplo n.º 1
0
    def note_implicit_target(_wrapped, self, target, *args, **kwds):
        # use default behavior unless signal flag is set
        entry = self._modify_new_desc_section
        if not entry:
            return _wrapped(self, target, *args, **kwds)
        self._modify_new_desc_section = None

        # NOTE: target should be section() node we're modifying,
        #       as we just got called from RSTState.new_subsection(),
        #       which is what sets modify_new_desc_section flag.
        #       'entry' should be last item in memo.desc_stack
        #       (see below).

        # add our custom css classes for styling
        # NOTE: adding 'section-header' class to H<N> node,
        #       so that our css rules don't have to be duplicated for every H<N> value.
        target['classes'].append("desc-section")
        target['classes'].append("desc-section-%d" % entry['level'])
        target[0]['classes'].append("section-header")

        # for duration of call, modify settings.id_prefix to include
        # decription prefix in any auto-generated ids. this helpers
        # section names remaining unique even if used between classes
        # (e.g. common names such as 'Constructor Options')
        settings = self.settings
        orig = settings.id_prefix
        try:
            if entry['prefix']:
                if orig:
                    settings.id_prefix = u("%s-%s") % (settings.id_prefix, entry['prefix'])
                else:
                    settings.id_prefix = entry['prefix']
            return _wrapped(self, target, *args, **kwds)
        finally:
            settings.id_prefix = orig
    def note_implicit_target(_wrapped, self, target, *args, **kwds):
        # use default behavior unless signal flag is set
        entry = self._modify_new_desc_section
        if not entry:
            return _wrapped(self, target, *args, **kwds)
        self._modify_new_desc_section = None

        # NOTE: target should be section() node we're modifying,
        #       as we just got called from RSTState.new_subsection(),
        #       which is what sets modify_new_desc_section flag.
        #       'entry' should be last item in memo.desc_stack
        #       (see below).

        # add our custom css classes for styling
        # NOTE: adding 'section-header' class to H<N> node,
        #       so that our css rules don't have to be duplicated for every H<N> value.
        target['classes'].append("desc-section")
        target['classes'].append("desc-section-%d" % entry['level'])
        target[0]['classes'].append("section-header")

        # for duration of call, modify settings.id_prefix to include
        # decription prefix in any auto-generated ids. this helpers
        # section names remaining unique even if used between classes
        # (e.g. common names such as 'Constructor Options')
        settings = self.settings
        orig = settings.id_prefix
        try:
            if entry['prefix']:
                if orig:
                    settings.id_prefix = u("%s-%s") % (settings.id_prefix,
                                                       entry['prefix'])
                else:
                    settings.id_prefix = entry['prefix']
            return _wrapped(self, target, *args, **kwds)
        finally:
            settings.id_prefix = orig
Exemplo n.º 3
0
def emph_literal_role(typ,
                      rawtext,
                      text,
                      lineno,
                      inliner,
                      options={},
                      content=[]):
    """replacement for sphinx's ``:samp:`` role handler.
    this is a bit stricter in it's parsing, and allows escaping of literal
    ``{`` and ``}`` characters.
    """
    def make_error(pos, value):
        value = "%s at char %d of %s" % (value, pos, rawtext)
        msg = inliner.reporter.error(value, line=lineno)
        prb = inliner.problematic(rawtext, rawtext, msg)
        return [prb], [msg]

    text = utils.unescape(text)
    retnode = nodes.literal(role=typ.lower(), classes=[typ])
    buffer = u("")  # contains text being accumulated for next node
    in_escape = False  # True if next char is part of escape sequence
    in_var = False  # True if parsing variable section instead of plain text
    var_start = None  # marks start of var section if in_var is True
    i = 0
    for c in text:
        i += 1
        if in_escape:
            # parse escape sequence
            if c in ru("{}\\"):
                buffer += c
                in_escape = False
            else:
                return make_error(i - 2,
                                  "unknown samp-escape '\\\\%s'" % (c, ))
        elif c == ru("\\"):
            # begin escape sequence
            in_escape = True
            i += 1  # account for extra escape char in rawtext
        elif in_var:
            # parsing variable section
            if c == u("{"):
                return make_error(i, "unescaped '{'")
            elif c == u("}"):
                # finalize variable section, return to plaintext
                if not buffer:
                    return make_error(i - 1, "empty variable section")
                retnode += nodes.emphasis(buffer, buffer)
                buffer = u("")
                in_var = False
            else:
                buffer += c
        else:
            # parsing plaintext section
            if c == u("{"):
                # finalize plaintext section, start variable section
                if buffer:
                    retnode += nodes.Text(buffer, buffer)
                buffer = u("")
                in_var = True
                var_start = i
            elif c == u("}"):
                return make_error(i, "unescaped '}'")
            else:
                buffer += c
    if in_escape:
        return make_error(i, "unterminated samp-escape sequence")
    elif in_var:
        return make_error(var_start, "unterminated variable section")
    elif buffer:
        retnode += nodes.Text(buffer, buffer)
    return [retnode], []
Exemplo n.º 4
0
def emph_literal_role(typ, rawtext, text, lineno, inliner,
                      options={}, content=[]):
    """replacement for sphinx's ``:samp:`` role handler.
    this is a bit stricter in it's parsing, and allows escaping of literal
    ``{`` and ``}`` characters.
    """
    def make_error(pos, value):
        value = "%s at char %d of %s" % (value, pos, rawtext)
        msg = inliner.reporter.error(value, line=lineno)
        prb = inliner.problematic(rawtext, rawtext, msg)
        return [prb], [msg]
    text = utils.unescape(text)
    retnode = nodes.literal(role=typ.lower(), classes=[typ])
    buffer = u("") # contains text being accumulated for next node
    in_escape = False # True if next char is part of escape sequence
    in_var = False # True if parsing variable section instead of plain text
    var_start = None # marks start of var section if in_var is True
    i = 0
    for c in text:
        i += 1
        if in_escape:
            # parse escape sequence
            if c in ru("{}\\"):
                buffer += c
                in_escape = False
            else:
                return make_error(i-2, "unknown samp-escape '\\\\%s'" % (c,))
        elif c == ru("\\"):
            # begin escape sequence
            in_escape = True
            i += 1 # account for extra escape char in rawtext
        elif in_var:
            # parsing variable section
            if c == u("{"):
                return make_error(i, "unescaped '{'")
            elif c == u("}"):
                # finalize variable section, return to plaintext
                if not buffer:
                    return make_error(i-1, "empty variable section")
                retnode += nodes.emphasis(buffer, buffer)
                buffer = u("")
                in_var = False
            else:
                buffer += c
        else:
            # parsing plaintext section
            if c == u("{"):
                # finalize plaintext section, start variable section
                if buffer:
                    retnode += nodes.Text(buffer, buffer)
                buffer = u("")
                in_var = True
                var_start = i
            elif c == u("}"):
                return make_error(i, "unescaped '}'")
            else:
                buffer += c
    if in_escape:
        return make_error(i, "unterminated samp-escape sequence")
    elif in_var:
        return make_error(var_start, "unterminated variable section")
    elif buffer:
        retnode += nodes.Text(buffer, buffer)
    return [retnode], []