Esempio n. 1
0
def image_wrap(parser, token, image_node):
    cont = token.split_contents()
    nodes = []
    alt = src = None
    for val in cont[1:]:
        val = utils.unquote_string(val)
        if val == '|':
            nodes.append(ConstNode("<br>"))
        elif not alt and not src:
            alt = val
        elif alt and not src:
            src = val
            nodes.append(image_node(alt=alt, src=src))
            alt = src = None
    return ImageBlockNode(*nodes)
Esempio n. 2
0
    def _read(self, file):
        """Parse a sectioned setup file.

        The sections in setup file contains a title line at the top,
        indicated by a name in square brackets (`[]'), plus key/value
        options lines, indicated by `name: value' format lines.
        Continuations are represented by an embedded newline then
        leading whitespace.  Blank lines, lines beginning with a '#',
        and just about everything else are ignored.
        """
        commentPrefix = self.m_commentPrefix
        commentPrefixLen = 0 if commentPrefix is None else len(commentPrefix)
        keyValueSeperator = self.m_keyValueSeperator
        #print('commentPrefix='+str(commentPrefix))
        #print('keyValueSeperator='+str(keyValueSeperator))

        cursect = None  # None, or a dictionary
        optname = None
        lineno = 0
        e = None  # None, or an exception
        for line in file:
            # comment or blank line?
            if line.strip() == '':  # or line[0] in '#;':
                if cursect is None:
                    cursect = IniSection(self, None, lineno)
                    self.m_sections.append(cursect)
                cursect.appendRaw(lineno, line.rstrip('\n'), None, None, None,
                                  False)
                lineno = lineno + 1
                optname = None
                continue
            # continuation line?
            if False and line[0].isspace() and cursect is not None and optname:
                value = line.strip()
                if value:
                    inival = cursect._getLast(optname)
                    inival.value = "%s\n%s" % (inival.value, value)
            # a section header or option header?
            else:
                line = line.rstrip('\n')
                # is it a section header?
                mo = self.SECTCRE.match(line)
                if mo:
                    sectname = mo.group('header')
                    cursect = self._getSection(sectname)
                    if cursect is None:
                        cursect = IniSection(self, sectname, lineno, line)
                        self.m_sections.append(cursect)
                    # So sections can't start with a continuation line
                    optname = None
                else:
                    if cursect is None:
                        cursect = IniSection(self, None, lineno)
                        self.m_sections.append(cursect)
                    mo = self.OPTCRE.match(line)
                    if mo:
                        try:
                            disabled = mo.group('disabled')
                            if len(disabled) > 0:
                                disabled = True
                            else:
                                disabled = False
                        except IndexError:
                            disabled = False
                        optname, vi, optval = mo.group('option', 'vi', 'value')

                        if keyValueSeperator is None:
                            keyValueSeperator = vi
                            #print('got keyValueSeperator : \'' + keyValueSeperator + '\'')
                            gotValue = True
                        elif vi == keyValueSeperator:
                            gotValue = True
                        else:
                            #print('keyValueSeperator changed: \'' + keyValueSeperator + '\' to \'' + vi + '\'')
                            #print('line: ' + line)
                            gotValue = False
                    else:
                        gotValue = False

                    if gotValue:
                        if commentPrefix is None:
                            # no comment char has yet been determined or specified,
                            # so we should try to automatically detect the comment
                            # char, which can either be a semi-colon or a hash char.
                            # the comment char must be preceeded by a space char
                            pos = optval.find(';')
                            if pos != -1 and optval[pos - 1].isspace():
                                commentPrefix = ';'
                            else:
                                pos = optval.find('#')
                                if pos != -1 and optval[pos - 1].isspace():
                                    commentPrefix = '#'

                        if commentPrefix is not None:
                            # ';' or '#' is a comment delimiter only if it follows
                            # a spacing character
                            pos = optval.find(commentPrefix)
                            if pos != -1 and optval[pos - 1].isspace():
                                optval = optval[:pos]
                                optcomment = optval[pos + 1:]
                            else:
                                optcomment = None
                        else:
                            optcomment = None
                        #print('commentPrefix=' + str(commentPrefix) + ' optcomment=' + str(optcomment))

                        optval = optval.strip()
                        if self.m_autoQuoteStrings:
                            optval = unquote_string(optval)
                        else:
                            # allow empty values
                            if optval == '""':
                                optval = ''
                        if self.m_autoEscapeStrings:
                            optval = unescape_string_from_c(optval)
                        optname = optname.rstrip()

                        cursect.appendRaw(lineno, line, optname, optval,
                                          optcomment, disabled)
                    else:
                        #print('try to match comment for line: ' + line)
                        mo = self.COMMENTRE.match(line)
                        if mo:
                            (commentchar,
                             comment) = mo.group('commentchar', 'comment')
                            if commentPrefix is None:
                                commentPrefix = commentchar
                            #print('found comment line: ' + comment)
                            cursect.appendRaw(lineno, line, None, None,
                                              comment, False)
                        else:
                            cursect.appendRaw(lineno, line, None, None, None,
                                              False)
                            #print 'unrecognized line:' + line
            lineno = lineno + 1
        if commentPrefix is not None:
            self.m_commentPrefix = commentPrefix
        else:
            self.m_commentPrefix = ';'
        if keyValueSeperator is not None:
            self.m_keyValueSeperator = keyValueSeperator
        else:
            self.m_keyValueSeperator = '='
Esempio n. 3
0
 def tag_func(parser, token):
     tag, contents = token.contents.split(' ', 1)
     contents = utils.unquote_string(contents)
     return Node(func, contents)