Commit abee413a8093fe8795d8135223bc2ce7165159e1

  • avatar
  • Krister Svanlund <adsum @BIG…SH.(none)>
  • Wed Jul 01 02:07:34 CEST 2009
MediaWiki: Added function for getting sections.
ChangeLog
(3 / 0)
  
112009-07-01 Krister Svanlund <krister.svanlund@gmail.com>
22
3 * kofoo_mediawiki.py (mw_get_section): Created function for getting an entire
4 section from a page.
5
36 * kofoo.py: Added BSD-license to entire project with some files not licensed.
47
582009-06-30 Krister Svanlund <krister.svanlund@gmail.com>
  
11# -*- coding: utf-8 -*-
2### A module using the MediaWiki API to add text to a wiki. ###
3### Python API: http://sourceforge.net/projects/mwclient/ ###
4### MediaWiki API: http://www.mediawiki.org/wiki/API ###
2### This file is part of KoFooBot and is licensed under BSD-license according to the ###
3### LICENSE file in the base directory. ###
4### Code in this file is contributed by: ###
5### Krister Svanlund <krister.svanlund gmail.com> ###
56
7### A module using the MediaWiki API to add text to a wiki. ###
8### MediaWiki API: http://www.mediawiki.org/wiki/API ###
9### Python API: http://sourceforge.net/projects/mwclient/ ###
10
611import mwclient
712import re
813
9292 table_name = m.group(2)
9393 page_name = m.group(3)
9494 mw_get_table(page_name, table_name)
95 return True
96 m = re.match(r'list ("?)(.*?)\1 on (\S+)', rest_arg)
97 if m:
98 list_name = m.group(2)
99 page_name = m.group(3)
100 mw_get_list(page_name, list_name)
101 return True
102 m = re.match(r'section ("?)(.*?)\1 on (\S+)', rest_arg)
103 if m:
104 section_name = m.group(2)
105 page_name = m.group(3)
106 mw_get_section(page_name, section_name)
107 return True
95108 elif base_arg == "on":
96109 m = re.match(r'(\S+) in ("?)([\S ]+)\2 add ("?)(.*)\4', rest_arg)
97110 if m:
376376 print " - Not enough arguments."
377377 return False
378378
379def mw_get_section(pagename, sectionname, text = None):
380 print "Get section '%s' from '%s'." % (sectionname, pagename)
381 if (pagename or text) and sectionname:
382 if not text:
383 page = wiki_site.Pages[pagename]
384 text = page.edit()
385 section = []
386 inside_section = False
387 for line in text.splitlines():
388 m = re.match(r'\s*(=+)\s*(.*?)\s*\1', line)
389 if m:
390 print " + Found match '%s'." % m.group(2)
391 if m.group(2) == sectionname:
392 inside_section = True
393 else:
394 inside_section = False
395 if inside_section:
396 print " + Add line '%s'." % line
397 section.append(line)
398 section = '\n'.join(section)
399 print section
400 return section
401 else:
402 print " - Not enough arguments."
403 return ""
404
379405def mw_get_list(pagename, listname, text = None):
380406 print "Get list '%s' from '%s'." % (listname, pagename)
381407 if (pagename or text) and listname: