示例#1
0
        def include_subfun(mo):
            match = mo.groups()

            if wikiutil.AbsPageName(page, match[1]) == oldpagename:
                # If the link was relative:
                if not oldpagename in match[0]:
                    # If the new page will be a subpage of the
                    # source, retain relative link. Else, make
                    # an absolute link.
                    if (rel_newpagename.startswith('/') or
                        rel_newpagename.startswith('../')):
                        return match[0].replace(
                            wikiutil.RelPageName(page, oldpagename),
                            rel_newpagename)
                    else:
                        return match[0].replace(
                            wikiutil.RelPageName(page, oldpagename),
                            newpagename)

                # Else, change absolute link
                return match[0].replace(oldpagename, newpagename)
            # No match in this link -> move on
            else:
                return match[0]
示例#2
0
 def _replace(self, key):
     """ replace a item_name if it is in the renames dict
         key is either a 2-tuple ('PAGE', pagename)
         or a 3-tuple ('FILE', pagename, filename)
     """
     current_page = self.pagename
     item_type, page_name, file_name = (key + (None, ))[:3]
     abs_page_name = wikiutil.AbsPageName(current_page, page_name)
     if item_type == 'PAGE':
         key = (item_type, abs_page_name)
         new_name = self.renames.get(key)
         if new_name is None:
             # we don't have an entry in rename map - apply the same magic
             # to the page name as 1.5 did (" " -> "_") and try again:
             abs_magic_name = abs_page_name.replace(u' ', u'_')
             key = (item_type, abs_magic_name)
             new_name = self.renames.get(key)
             if new_name is None:
                 # we didn't find it under the magic name either -
                 # that means we do not rename it!
                 new_name = page_name
         if new_name != page_name and abs_page_name != page_name:
             # we have to fix the (absolute) new_name to be a relative name (as it was before)
             new_name = wikiutil.RelPageName(current_page, new_name)
     elif item_type == 'FILE':
         key = (item_type, abs_page_name, file_name)
         new_name = self.renames.get(key)
         if new_name is None:
             # we don't have an entry in rename map - apply the same magic
             # to the page name as 1.5 did (" " -> "_") and try again:
             abs_magic_name = abs_page_name.replace(u' ', u'_')
             key = (item_type, abs_magic_name, file_name)
             new_name = self.renames.get(key)
             if new_name is None:
                 # we didn't find it under the magic name either -
                 # that means we do not rename it!
                 new_name = file_name
     return new_name
示例#3
0
    def _inlink_rename(self, page, newpagename, oldpagename, comment):
        rel_newpagename = wikiutil.RelPageName(page, newpagename)

        # The following regexp match functions search for
        # occurrences of the target page name, determine
        # if they're absolute, relative or subpage
        # matches, and replace them accordingly
        def word_subfun(mo):
            match = mo.groups()

            if wikiutil.AbsPageName(page, match[1]) == oldpagename:
                # If the link was relative:
                if not oldpagename in match[1]:
                    # If the new page will be a subpage of the
                    # source, retain relative link. Else, make
                    # an absolute link.
                    if (rel_newpagename.startswith('/') or
                        rel_newpagename.startswith('../')):
                        return match[1].replace(
                            wikiutil.RelPageName(page, oldpagename),
                            rel_newpagename)
                    else:
                        return match[1].replace(
                            wikiutil.RelPageName(page, oldpagename),
                            newpagename)

                # Else, change absolute link
                return match[1].replace(oldpagename, newpagename)
            # No match in this link -> move on
            else:
                return match[1]

        def link_subfun(mo):
            match = mo.groups()

            if wikiutil.AbsPageName(page, match[1]) == oldpagename:
                # If the link was relative:
                if not oldpagename in match[0]:
                    # If the new page will be a subpage of the
                    # source, retain relative link. Else, make
                    # an absolute link.
                    if (rel_newpagename.startswith('/') or
                        rel_newpagename.startswith('../')):
                        return match[0].replace(
                            wikiutil.RelPageName(page, oldpagename),
                            rel_newpagename)
                    else:
                        return match[0].replace(
                            wikiutil.RelPageName(page, oldpagename),
                            newpagename)

                # Else, change absolute link
                return match[0].replace(oldpagename, newpagename)
            # No match in this link -> move on
            else:
                return match[0]

        def include_subfun(mo):
            match = mo.groups()

            if wikiutil.AbsPageName(page, match[1]) == oldpagename:
                # If the link was relative:
                if not oldpagename in match[0]:
                    # If the new page will be a subpage of the
                    # source, retain relative link. Else, make
                    # an absolute link.
                    if (rel_newpagename.startswith('/') or
                        rel_newpagename.startswith('../')):
                        return match[0].replace(
                            wikiutil.RelPageName(page, oldpagename),
                            rel_newpagename)
                    else:
                        return match[0].replace(
                            wikiutil.RelPageName(page, oldpagename),
                            newpagename)

                # Else, change absolute link
                return match[0].replace(oldpagename, newpagename)
            # No match in this link -> move on
            else:
                return match[0]

        self.page = PageEditor(self.request, page)
        savetext = self.page.get_raw_body()

        # Must replace both WikiWords and links, as
        # [[WikiWord]] is a link.
        word_re = re.compile(Parser.word_rule, re.VERBOSE)
        savetext = word_re.sub(word_subfun, savetext)
        link_re = re.compile(Parser.link_rule, re.VERBOSE)
        savetext = link_re.sub(link_subfun, savetext)

        # Also handle renaming (non-regexped) includes of the page
        savetext = include_re.sub(include_subfun, savetext)

        _ = self.request.getText

        success = True

        try:
            msg = self.page.saveText(savetext, 0, comment=comment,
                                     notify=False)
        except self.page.Unchanged:
            msg = _('Error changing links on page %s!') % (self.page.page_name)
            success = False

        return success, msg
示例#4
0
 def _check_rel_pagename(self, current_page, absolute_page, relative_page):
     assert relative_page == wikiutil.RelPageName(current_page, absolute_page)