• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

Python textlib.replace_links函数代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了Python中pywikibot.textlib.replace_links函数的典型用法代码示例。如果您正苦于以下问题:Python replace_links函数的具体用法?Python replace_links怎么用?Python replace_links使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。



在下文中一共展示了replace_links函数的16个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Python代码示例。

示例1: test_label_diff_namespace

 def test_label_diff_namespace(self):
     """Test that it uses the old label when the new doesn't match."""
     # These tests require to get the actual part which is before the title
     # (interwiki and namespace prefixes) which could be then compared
     # case insensitive.
     self.assertEqual(
         textlib.replace_links('[[Image:Foobar]]', ('File:Foobar', 'File:Foo'), self.wp_site),
         '[[File:Foo|Image:Foobar]]')
     self.assertEqual(
         textlib.replace_links('[[en:File:Foobar]]', ('File:Foobar', 'File:Foo'), self.wp_site),
         '[[File:Foo|en:File:Foobar]]')
开发者ID:metakgp,项目名称:batman,代码行数:11,代码来源:textlib_tests.py


示例2: test_linktrails

 def test_linktrails(self):
     """Test that the linktrails are used or applied."""
     self.assertEqual(
         textlib.replace_links('[[Foobar]]', ('Foobar', 'Foo'), self.wp_site),
         '[[Foo]]bar')
     self.assertEqual(
         textlib.replace_links('[[Talk:test]]s', ('Talk:Test', 'Talk:Tests'), self.wp_site),
         '[[Talk:tests]]')
     self.assertEqual(
         textlib.replace_links('[[Talk:test]]s', ('Talk:Test', 'Project:Tests'), self.wp_site),
         '[[Project:Tests|Talk:tests]]')
开发者ID:metakgp,项目名称:batman,代码行数:11,代码来源:textlib_tests.py


示例3: test_replace_different_case

 def test_replace_different_case(self):
     """Test that it uses piped links when the case is different."""
     source_text = '[[Foo|Bar]] and [[Foo|bar]]'
     self.assertEqual(
         textlib.replace_links(source_text, ('Foo', 'bar'), self.get_site('wp')),
         '[[Bar]] and [[bar]]')
     self.assertEqual(
         textlib.replace_links(source_text, ('Foo', 'bar'), self.get_site('wt')),
         '[[bar|Bar]] and [[bar]]')
     self.assertEqual(
         textlib.replace_links(source_text, ('Foo', 'Bar'), self.get_site('wt')),
         '[[Bar]] and [[Bar|bar]]')
开发者ID:metakgp,项目名称:batman,代码行数:12,代码来源:textlib_tests.py


示例4: test_unlink_some

 def test_unlink_some(self):
     """Test unlinking only some links."""
     self.assertEqual(
         textlib.replace_links(self.text, ('World', False), self.wp_site),
         'Hello World, [[how|are]] [[you#section|you]]? Are [[you]] a '
         '[[bug:1337]]?')
     self.assertEqual(
         textlib.replace_links('[[User:Namespace|Label]]\n'
                               '[[User:Namespace#Section|Labelz]]\n'
                               '[[Nothing]]',
                               ('User:Namespace', False),
                               self.wp_site),
         'Label\nLabelz\n[[Nothing]]')
开发者ID:metakgp,项目名称:batman,代码行数:13,代码来源:textlib_tests.py


示例5: test_replace_neighbour

 def test_replace_neighbour(self):
     """Test that it replaces two neighbouring links."""
     self.assertEqual(
         textlib.replace_links('[[A]][[A]][[C]]',
                               ('A', 'B'),
                               self.wp_site),
         '[[B|A]][[B|A]][[C]]')
开发者ID:metakgp,项目名称:batman,代码行数:7,代码来源:textlib_tests.py


示例6: test_replace_file

 def test_replace_file(self):
     """Test that it respects the namespace."""
     self.assertEqual(
         textlib.replace_links(
             '[[File:Meh.png|thumb|Description of [[fancy]]]] [[Fancy]]...',
             ('File:Meh.png', 'File:Fancy.png'),
             self.wp_site),
         '[[File:Fancy.png|thumb|Description of [[fancy]]]] [[Fancy]]...')
开发者ID:metakgp,项目名称:batman,代码行数:8,代码来源:textlib_tests.py


示例7: test_replacements_simplify

 def test_replacements_simplify(self):
     """Test a tuple as a replacement removing the need for a piped link."""
     self.assertEqual(
         textlib.replace_links(self.text,
                               ('how', 'are'),
                               self.wp_site),
         'Hello [[World]], [[are]] [[you#section|you]]? Are [[you]] a '
         '[[bug:1337]]?')
开发者ID:metakgp,项目名称:batman,代码行数:8,代码来源:textlib_tests.py


示例8: test_unlink_all

 def test_unlink_all(self):
     """Test unlinking."""
     def callback(link, text, groups, rng):
         self.assertEqual(link.site, self.wp_site)
         return False
     self.assertEqual(
         textlib.replace_links(self.text, callback, self.wp_site),
         'Hello World, are you? Are you a [[bug:1337]]?')
开发者ID:metakgp,项目名称:batman,代码行数:8,代码来源:textlib_tests.py


示例9: test_unicode_callback

 def test_unicode_callback(self):
     """Test returning unicode in the callback."""
     def callback(link, text, groups, rng):
         self.assertEqual(link.site, self.wp_site)
         if link.title == 'World':
             # This must be a unicode instance not bytes
             return 'homewörlder'
     self.assertEqual(
         textlib.replace_links(self.text, callback, self.wp_site),
         'Hello homewörlder, [[how|are]] [[you#section|you]]? Are [[you]] a '
         '[[bug:1337]]?')
开发者ID:metakgp,项目名称:batman,代码行数:11,代码来源:textlib_tests.py


示例10: test_replacements_function

 def test_replacements_function(self):
     """Test a dynamic function as the replacements."""
     def callback(link, text, groups, rng):
         self.assertEqual(link.site, self.wp_site)
         if link.title == 'World':
             return pywikibot.Link('Homeworld', link.site)
         elif link.title.lower() == 'you':
             return False
     self.assertEqual(
         textlib.replace_links(self.text, callback, self.wp_site),
         'Hello [[Homeworld]], [[how|are]] you? Are you a [[bug:1337]]?')
开发者ID:metakgp,项目名称:batman,代码行数:11,代码来源:textlib_tests.py


示例11: treat_page

    def treat_page(self):
        """Iterate over the linked pages and replace redirects conditionally."""
        text = self.current_page.text
        for linked_page in self.current_page.linkedPages():
            try:
                target = linked_page.getRedirectTarget()
            except (pywikibot.Error, pywikibot.SectionError):
                continue
            # TODO: Work on all links at the same time (would mean that the user
            # doesn't get them ordered like in links but how they appear in the page)
            text = textlib.replace_links(text, self._create_callback(linked_page, target), self.current_page.site)

        if text != self.current_page.get():
            self.put_current(text)
开发者ID:happy5214,项目名称:pywikibot-core,代码行数:14,代码来源:disambredir.py


示例12: test_replacements_once

 def test_replacements_once(self):
     """Test dynamic replacement."""
     def callback(link, text, groups, rng):
         if link.title.lower() == 'you':
             self._count += 1
             if link.section:
                 return pywikibot.Link(
                     '{0}#{1}'.format(self._count, link.section), link.site)
             else:
                 return pywikibot.Link('{0}'.format(self._count), link.site)
     self._count = 0  # buffer number of found instances
     self.assertEqual(
         textlib.replace_links(self.text, callback, self.wp_site),
         'Hello [[World]], [[how|are]] [[1#section]]? Are [[2]] a '
         '[[bug:1337]]?')
     del self._count
开发者ID:metakgp,项目名称:batman,代码行数:16,代码来源:textlib_tests.py


示例13: unlink

    def unlink(self, target_page):
        """Unlink all links linking to the target page."""
        text = self.current_page.text
        while True:
            unlink_callback = self._create_callback()
            try:
                text = replace_links(text, unlink_callback, target_page.site)
            except EditReplacement:
                new_text = TextEditor().edit(
                    unlink_callback.current_text,
                    jumpIndex=unlink_callback.current_range[0])
                # if user didn't press Cancel
                if new_text:
                    text = new_text
                else:
                    text = unlink_callback.current_text
            else:
                break

        self.put_current(text)
开发者ID:happy5214,项目名称:pywikibot-core,代码行数:20,代码来源:unlink.py


示例14: test_replace_modes

 def test_replace_modes(self):
     """Test replacing with or without label and section."""
     source_text = '[[Foo#bar|baz]]'
     self.assertEqual(
         textlib.replace_links(source_text, ('Foo', 'Bar'), self.wp_site),
         '[[Bar#bar|baz]]')
     self.assertEqual(
         textlib.replace_links(source_text,
                               ('Foo', pywikibot.Page(self.wp_site, 'Bar')),
                               self.wp_site),
         '[[Bar#bar|baz]]')
     self.assertEqual(
         textlib.replace_links(source_text,
                               ('Foo', pywikibot.Link('Bar', self.wp_site)),
                               self.wp_site),
         '[[Bar]]')
     self.assertEqual(
         textlib.replace_links(source_text, ('Foo', 'Bar#snafu'), self.wp_site),
         '[[Bar#bar|baz]]')
     self.assertEqual(
         textlib.replace_links(source_text,
                               ('Foo', pywikibot.Page(self.wp_site, 'Bar#snafu')),
                               self.wp_site),
         '[[Bar#bar|baz]]')
     self.assertEqual(
         textlib.replace_links(source_text,
                               ('Foo', pywikibot.Link('Bar#snafu', self.wp_site)),
                               self.wp_site),
         '[[Bar#snafu]]')
     self.assertEqual(
         textlib.replace_links(source_text, ('Foo', 'Bar|foo'), self.wp_site),
         '[[Bar#bar|baz]]')
     self.assertEqual(
         textlib.replace_links(source_text,
                               ('Foo', pywikibot.Page(self.wp_site, 'Bar|foo')),
                               self.wp_site),
         '[[Bar#bar|baz]]')
     self.assertEqual(
         textlib.replace_links(source_text,
                               ('Foo', pywikibot.Link('Bar|foo', self.wp_site)),
                               self.wp_site),
         '[[Bar|foo]]')
     self.assertEqual(
         textlib.replace_links(source_text, ('Foo', 'Bar#snafu|foo'), self.wp_site),
         '[[Bar#bar|baz]]')
     self.assertEqual(
         textlib.replace_links(source_text,
                               ('Foo', pywikibot.Page(self.wp_site, 'Bar#snafu|foo')),
                               self.wp_site),
         '[[Bar#bar|baz]]')
     self.assertEqual(
         textlib.replace_links(source_text,
                               ('Foo', pywikibot.Link('Bar#snafu|foo', self.wp_site)),
                               self.wp_site),
         '[[Bar#snafu|foo]]')
开发者ID:metakgp,项目名称:batman,代码行数:55,代码来源:textlib_tests.py


示例15: test_replace_invalid_link_text

 def test_replace_invalid_link_text(self):
     """Test that it doesn't pipe a link when it's an invalid link."""
     self.assertEqual(
         textlib.replace_links('[[Target|Foo:]]', ('Target', 'Foo'), self.wp_site),
         '[[Foo|Foo:]]')
开发者ID:metakgp,项目名称:batman,代码行数:5,代码来源:textlib_tests.py


示例16: test_replace_strings

 def test_replace_strings(self):
     """Test if strings can be used."""
     self.assertEqual(
         textlib.replace_links(self.text, ('how', 'are'), self.wp_site),
         'Hello [[World]], [[are]] [[you#section|you]]? Are [[you]] a '
         '[[bug:1337]]?')
开发者ID:metakgp,项目名称:batman,代码行数:6,代码来源:textlib_tests.py



注:本文中的pywikibot.textlib.replace_links函数示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Python textlib.TimeStripper类代码示例发布时间:2022-05-26
下一篇:
Python textlib.replaceExcept函数代码示例发布时间:2022-05-26
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap