minetestbot-modules/devwiki.py

81 lines
2.3 KiB
Python
Raw Normal View History

2013-06-18 13:23:09 -07:00
#!/usr/bin/env python
"""
2014-07-20 07:13:59 -07:00
wiki.py - Phenny Wiki Module
Copyright 2014, sfan5
Licensed under GNU General Public License v2.0
2013-06-18 13:23:09 -07:00
"""
2014-07-20 07:13:59 -07:00
import re
2013-06-18 13:23:09 -07:00
import web
2014-07-20 07:13:59 -07:00
import urllib.parse
wikiuri_g = 'http://dev.minetest.net/index.php?title=%s&printable=yes'
2014-07-20 11:46:13 -07:00
wikiuri_r = 'http://dev.minetest.net/%s'
2014-07-20 07:13:59 -07:00
r_content = re.compile(r'(?i)<div[^>]+class=.mw-content-ltr.>')
r_paragraph = re.compile(r'(?ims)<p>(.+?)</p>')
2014-09-13 10:47:51 -07:00
r_headline = re.compile(r'(?i)<span class="mw-headline" id="[^"]+">(.+?)</span>')
2014-07-20 11:46:13 -07:00
r_sentenceend = re.compile(r'\.[ A-Z]')
2014-07-20 07:13:59 -07:00
transforms = [
2014-09-13 10:47:51 -07:00
(re.compile(r'(?i)<a [^>]+>(.+?)</a>'), "\x0302\g<1>\x0f"),
2014-07-20 11:46:13 -07:00
(re.compile(r'(?i)<b>(.+?)</b>'), "\x02\g<1>\x02"),
(re.compile(r'(?i)<i>(.+?)</i>'), "\x1d\g<1>\x1d"),
(re.compile(r'(?i)<u>(.+?)</u>'), "\x1f\g<1>\x1f"),
2014-07-20 12:00:46 -07:00
(re.compile(r'(?i)<code>(.+?)</code>'), "\x0315\g<1>\x0f"),
2014-07-20 11:46:13 -07:00
(re.compile(r'(?i)<br\s*/?>'), ""),
]
nottext = [
2014-09-13 10:47:51 -07:00
re.compile(r'(?i)^<br\s*/?>$'),
re.compile('(?i)^' + re.escape('<font color="#800000"><b>This article is incomplete.</b></font>') + '$'),
re.compile('(?i)^' + re.escape('<b>This article is missing examples, feel free to add them.</b>') + '$'),
2014-07-20 07:13:59 -07:00
]
2014-07-20 11:46:13 -07:00
def devwiki(phenny, input):
2014-07-20 07:13:59 -07:00
term = input.group(2)
if not term:
return
log.log("event", "%s queried Developer Wiki for '%s'" % (log.fmt_user(input), term), phenny)
2014-07-20 11:46:13 -07:00
term = term.replace(" ", "_")
2014-07-20 07:13:59 -07:00
term = web.urlencode(term)
data, scode = web.get(wikiuri_g % term)
if scode == 404:
return phenny.say("No such page.")
data = str(data, "utf-8")
m = re.search(r_content, data)
if not m:
2014-09-13 10:47:51 -07:00
return phenny.say("Sorry, did not find any text to display. Here's the link: %s" % (wikiuri_r % term,))
2014-07-20 07:13:59 -07:00
data = data[m.span()[1]:]
2014-07-20 11:46:13 -07:00
mi = re.finditer(r_paragraph, data)
text = ""
for m in mi:
abort = False
for e in nottext:
if re.search(e, m.group(1)):
abort = True
break
if abort:
continue
text = m.group(1)
break
if not text:
2014-09-13 10:47:51 -07:00
m = re.search(r_headline, data)
if m:
text = "<b>" + m.group(1) + "</b>"
else:
return phenny.say("Sorry, did not find any text to display. Here's the link: %s" % (wikiuri_r % term,))
2014-07-20 11:46:13 -07:00
for tf in transforms:
text = re.sub(tf[0], tf[1], text)
m = re.search(r_sentenceend, text)
2014-07-20 07:13:59 -07:00
if m:
2014-07-20 11:46:13 -07:00
text = text[:m.span()[1]-1]
2014-09-13 10:47:51 -07:00
phenny.say('"%s" - %s' % (web.decode(text), wikiuri_r % term))
2014-07-20 07:13:59 -07:00
2014-07-20 11:46:13 -07:00
devwiki.commands = ['dev', 'devwiki']
2013-06-18 13:23:09 -07:00
if __name__ == '__main__':
2014-07-20 07:13:59 -07:00
print(__doc__.strip())