Exemple #1
0
    def _url_repl(self, word):
        """Handle literal URLs including inline images."""
        scheme = word.split(":", 1)[0]

        attrs = {}

        # Handle literal URLs to local resources using a special 'file' scheme. This allows
        # to insert local images without using brackets and making them a link to themselfes
        # as a side effect.
        if scheme == 'file':
            # file:///image.gif -> image.gif
            word = word[7:].split('/', 1)[-1]
            text = wikiutil.url_unquote(os.path.basename(word))
        else:
            text = word
            if config.general.targetblank:
                attrs = dict(target='_blank')

        # CSS class split
        schsep = word.split('|')
        word = schsep[0]
        if len(schsep) > 1:
            scheme = '%s %s' % (scheme, ' '.join(schsep[1:]))

        if wikiutil.isPicture(word):
            # Get image name http://here.com/dir/image.gif -> image
            name = wikiutil.url_unquote(os.path.splitext(os.path.basename(word))[0])
            return self.formatter.image(src=word, alt=name, css=scheme)
        else:
            return (self.formatter.url(1, word, css=scheme, **attrs) +
                    self.formatter.text(text) +
                    self.formatter.url(0))
Exemple #2
0
    def _url_repl(self, word):
        """Handle literal URLs including inline images."""
        scheme = word.split(":", 1)[0]

        attrs = {}

        # Handle literal URLs to local resources using a special 'file' scheme. This allows
        # to insert local images without using brackets and making them a link to themselfes
        # as a side effect.
        if scheme == 'file':
            # file:///image.gif -> image.gif
            word = word[7:].split('/', 1)[-1]
            text = wikiutil.url_unquote(os.path.basename(word))
        else:
            text = word
            if config.general.targetblank:
                attrs = dict(target='_blank')

        # CSS class split
        schsep = word.split('|')
        word = schsep[0]
        if len(schsep) > 1:
            scheme = '%s %s' % (scheme, ' '.join(schsep[1:]))

        if wikiutil.isPicture(word):
            # Get image name http://here.com/dir/image.gif -> image
            name = wikiutil.url_unquote(
                os.path.splitext(os.path.basename(word))[0])
            return self.formatter.image(src=word, alt=name, css=scheme)
        else:
            return (self.formatter.url(1, word, css=scheme, **attrs) +
                    self.formatter.text(text) + self.formatter.url(0))
Exemple #3
0
    def _bracket_repl(self, word):
        """Handle bracketed URLs, WikiNames, etc."""

        # CSS class split
        schsep = word[1:-1].split('|')
        scheme = None
        if len(schsep) > 1:
            scheme = ' '.join(schsep[1:])

        # Traditional split on whitespace
        words = schsep[0].strip().split(None, 1)
        lenwords = len(words)
        if lenwords == 1:
            words = words * 2

        if words[0][0] == '#':
            # anchor link
            return (self.formatter.url(1, words[0]) +
                    self.formatter.text(words[1]) +
                    self.formatter.url(0))

        # Handle file:// URLs as local file names.
        href = []
        text = []
        for w in words:
            if w.startswith('file://'):
                href.append(w[7:].split('/', 1)[-1])
                text.append(os.path.basename(w))
            else:
                href.append(w)
                text.append(w)

        if re.match(self.url_rule, href[0]) and config.general.targetblank:
            attrs = dict(target='_blank')
        else:
            attrs = dict()

        if re.match(self.url_rule, words[1]) and wikiutil.isPicture(words[1]) and lenwords > 1:
            if re.match(self.url_rule, words[0]):
                return (self.formatter.url(1, href[0], do_escape=0, **attrs) +
                        self.formatter.image(title=text[0], alt=text[0], src=href[1], css=scheme) +
                        self.formatter.url(0))
            else:
                # This is similar to _word_repl() but creates an image link.
                parts = words[0].split('#', 1)
                anchor = ''
                if len(parts) == 2:
                    words[0], anchor = parts
                return (self.formatter.pagelink(1, words[0], anchor=anchor) +
                        self.formatter.image(title=words[0], alt=words[0], src=href[1], css=scheme) +
                        self.formatter.pagelink(0, words[0]))
        # Ben modif
        # elif re.match(self.url_rule, words[0]):
        elif re.match(self.url_rule, words[0]) and not re.match(self.pair_rule, words[0]):
            urlscheme = words[0].split(':', 1)[0]
            if scheme:
                scheme = '%s %s' % (urlscheme, scheme)
            else:
                scheme = urlscheme
            return (self.formatter.url(1, href[0], do_escape=0, css=scheme, **attrs) +
                    self.formatter.text(text[1]) +
                    self.formatter.url(0))
        else:
            return self._word_repl(words[0], text=text[1], css=scheme)
Exemple #4
0
    def _bracket_repl(self, word):
        """Handle bracketed URLs, WikiNames, etc."""

        # CSS class split
        schsep = word[1:-1].split('|')
        scheme = None
        if len(schsep) > 1:
            scheme = ' '.join(schsep[1:])

        # Traditional split on whitespace
        words = schsep[0].strip().split(None, 1)
        lenwords = len(words)
        if lenwords == 1:
            words = words * 2

        if words[0][0] == '#':
            # anchor link
            return (self.formatter.url(1, words[0]) +
                    self.formatter.text(words[1]) + self.formatter.url(0))

        # Handle file:// URLs as local file names.
        href = []
        text = []
        for w in words:
            if w.startswith('file://'):
                href.append(w[7:].split('/', 1)[-1])
                text.append(os.path.basename(w))
            else:
                href.append(w)
                text.append(w)

        if re.match(self.url_rule, href[0]) and config.general.targetblank:
            attrs = dict(target='_blank')
        else:
            attrs = dict()

        if re.match(self.url_rule, words[1]) and wikiutil.isPicture(
                words[1]) and lenwords > 1:
            if re.match(self.url_rule, words[0]):
                return (
                    self.formatter.url(1, href[0], do_escape=0, **attrs) +
                    self.formatter.image(
                        title=text[0], alt=text[0], src=href[1], css=scheme) +
                    self.formatter.url(0))
            else:
                # This is similar to _word_repl() but creates an image link.
                parts = words[0].split('#', 1)
                anchor = ''
                if len(parts) == 2:
                    words[0], anchor = parts
                return (
                    self.formatter.pagelink(1, words[0], anchor=anchor) +
                    self.formatter.image(
                        title=words[0], alt=words[0], src=href[1],
                        css=scheme) + self.formatter.pagelink(0, words[0]))
        elif re.match(self.url_rule, words[0]):
            urlscheme = words[0].split(':', 1)[0]
            if scheme:
                scheme = '%s %s' % (urlscheme, scheme)
            else:
                scheme = urlscheme
            return (self.formatter.url(
                1, href[0], do_escape=0, css=scheme, **attrs) +
                    self.formatter.text(text[1]) + self.formatter.url(0))
        else:
            return self._word_repl(words[0], text=text[1], css=scheme)