Commit abee413a8093fe8795d8135223bc2ce7165159e1
- Diff rendering mode:
- inline
- side by side
ChangeLog
(3 / 0)
|   | |||
| 1 | 1 | 2009-07-01 Krister Svanlund <krister.svanlund@gmail.com> | |
| 2 | 2 | ||
| 3 | * kofoo_mediawiki.py (mw_get_section): Created function for getting an entire | ||
| 4 | section from a page. | ||
| 5 | |||
| 3 | 6 | * kofoo.py: Added BSD-license to entire project with some files not licensed. | |
| 4 | 7 | ||
| 5 | 8 | 2009-06-30 Krister Svanlund <krister.svanlund@gmail.com> |
kofoo_mediawiki.py
(47 / 3)
|   | |||
| 1 | 1 | # -*- 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> ### | ||
| 5 | 6 | ||
| 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 | |||
| 6 | 11 | import mwclient | |
| 7 | 12 | import re | |
| 8 | 13 | ||
| … | … | ||
| 92 | 92 | table_name = m.group(2) | |
| 93 | 93 | page_name = m.group(3) | |
| 94 | 94 | 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 | ||
| 95 | 108 | elif base_arg == "on": | |
| 96 | 109 | m = re.match(r'(\S+) in ("?)([\S ]+)\2 add ("?)(.*)\4', rest_arg) | |
| 97 | 110 | if m: | |
| … | … | ||
| 376 | 376 | print " - Not enough arguments." | |
| 377 | 377 | return False | |
| 378 | 378 | ||
| 379 | def 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 | |||
| 379 | 405 | def mw_get_list(pagename, listname, text = None): | |
| 380 | 406 | print "Get list '%s' from '%s'." % (listname, pagename) | |
| 381 | 407 | if (pagename or text) and listname: |

