http://www.mediawiki.org/wiki/Special:Code/pywikipedia/9198
Revision: 9198
Author: jayvdb
Date: 2011-04-25 12:53:39 +0000 (Mon, 25 Apr 2011)
Log Message:
-----------
categoryFormat() and replaceCategoryLinks() support for
categories to be specified by strings consisting of either
* category name (without namespace)
* raw wikitext as provided by Page.aslink()
Modified Paths:
--------------
trunk/pywikipedia/pywikibot/textlib.py
Added Paths:
-----------
trunk/pywikipedia/tests/test_textlib.py
Modified: trunk/pywikipedia/pywikibot/textlib.py
===================================================================
--- trunk/pywikipedia/pywikibot/textlib.py 2011-04-25 08:57:30 UTC (rev 9197)
+++ trunk/pywikipedia/pywikibot/textlib.py 2011-04-25 12:53:39 UTC (rev 9198)
@@ -633,7 +633,8 @@
Replace the category links given in the wikitext given
in oldtext by the new links given in new.
- 'new' should be a list of Category objects.
+ 'new' should be a list of Category objects or strings
+ which can be either the raw name or [[Category:..]].
If addOnly is True, the old category won't be deleted and the
category(s) given will be added (and so they won't replace anything).
@@ -695,7 +696,8 @@
def categoryFormat(categories, insite = None):
"""Return a string containing links to all categories in a list.
- 'categories' should be a list of Category objects.
+ 'categories' should be a list of Category objects or strings
+ which can be either the raw name or [[Category:..]].
The string is formatted for inclusion in insite.
@@ -704,7 +706,15 @@
return ''
if insite is None:
insite = pywikibot.getSite()
- catLinks = [category.aslink(noInterwiki=True) for category in categories]
+
+ if isinstance(categories[0],basestring):
+ if categories[0][0] == '[':
+ catLinks = categories
+ else:
+ catLinks = ['[[Category:'+category+']]' for category in categories]
+ else:
+ catLinks = [category.aslink(noInterwiki=True) for category in categories]
+
if insite.category_on_one_line():
sep = ' '
else:
Added: trunk/pywikipedia/tests/test_textlib.py
===================================================================
--- trunk/pywikipedia/tests/test_textlib.py (rev 0)
+++ trunk/pywikipedia/tests/test_textlib.py 2011-04-25 12:53:39 UTC (rev 9198)
@@ -0,0 +1,40 @@
+#!/usr/bin/python
+# -*- coding: utf-8 -*-
+
+"""Unit tests for pywikibot/textlib.py"""
+__version__ = '$Id$'
+
+import unittest
+import tests.test_pywiki
+
+import wikipedia as pywikibot
+import pywikibot.textlib as textlib
+import catlib
+
+
+class PyWikiTextLibTestCase(tests.test_pywiki.PyWikiTestCase):
+
+ result1 = '[[Category:Cat1]]\r\n[[Category:Cat2]]\r\n'
+
+ def test_categoryFormat_raw(self):
+ self.assertEqual(self.result1,
+ textlib.categoryFormat(['[[Category:Cat1]]',
+ '[[Category:Cat2]]'],
+ self.site))
+
+ def test_categoryFormat_bare(self):
+ self.assertEqual(self.result1,
+ textlib.categoryFormat(['Cat1', 'Cat2'], self.site))
+
+ def test_categoryFormat_Category(self):
+ data = [catlib.Category(self.site, 'Category:Cat1'),
+ catlib.Category(self.site, 'Category:Cat2')]
+ self.assertEqual(self.result1, textlib.categoryFormat(data, self.site))
+
+ def test_categoryFormat_Page(self):
+ data = [pywikibot.Page(self.site, 'Category:Cat1'),
+ pywikibot.Page(self.site, 'Category:Cat2')]
+ self.assertEqual(self.result1, textlib.categoryFormat(data, self.site))
+
+if __name__ == "__main__":
+ unittest.main()
Property changes on: trunk/pywikipedia/tests/test_textlib.py
___________________________________________________________________
Added: svn:keywords
+ Author Date Id Revision
Added: svn:eol-style
+ native