User:TalBot/demosthenes-endnotes.py


 * 1) ! /usr/bin/env python
 * 2) _*_ coding: utf8 _*_
 * 3) Integrate the endnotes of The Public Orations of Demosthenes into the
 * 4) text as footnotes (using '
 * 1) text as footnotes (using '


 * 1) Function to obtain a multiline text blurb

def get_blurb(text): wikipedia.output(u'(???) %s' % text) wikipedia.output(u'(???) Please finish with .') result = u'' while True: line = wikipedia.input(u'') if line == u'.': return result.rstrip result += line + u'\n'


 * 1) Start operation

site = wikipedia.getSite

while True: title = wikipedia.input(u'(???) Please enter the title of the page you want to process next:') try: page = wikipedia.Page(site, title) text = page.get except wikipedia.Error: wikipedia.output(u'(EEE) Unable to obtain text from %s' % title) continue wikipedia.output(u'(III) Processing %s' % page.title) processed = 0 newtext = u'' while True: wikipedia.output(u'(III) Searching next footnote') footnotepos = text.find(footnotemarker, processed) if footnotepos == -1: wikipedia.output(u'(III) There are no more footnotes') newtext += text[processed:] break versebeginpos = text.rfind(versebeginmarker, 0, footnotepos) if versebeginpos == -1: wikipedia.output(u'(WWW) Warning: footnote found at position %s with no previous verse marker, ignoring' % footnotepos) newtext += text[processed:footnotepos + len(footnotemarker)] processed = footnotepos + len(footnotemarker) continue verseendpos = text.find(verseendmarker, versebeginpos) if verseendpos == -1: wikipedia.output(u'(WWW) Warning: end of verse not found, assuming verse length of 30') verseendpos = versebeginpos + 30 wikipedia.output(u'(III) Current footnote marker matches %s in context "...%s"' % ( text[versebeginpos:verseendpos + len(verseendmarker)], text[footnotepos-30:footnotepos] )) choice = wikipedia.inputChoice(refquestion, refanswers[:], refhotkeys, refdefault) if choice in [ u'i', u'I' ]: wikipedia.output(u'(III) OK, ignoring this footnote marker') newtext += text[processed:footnotepos + len(footnotemarker)] processed = footnotepos + len(footnotemarker) continue refname = wikipedia.input(u'(???) Please enter reference name:') if choice in [ u'r', u'R' ]: wikipedia.output(u'(III) OK, reusing reference %s' % refname) newtext += text[processed:footnotepos] + (base_reusedref % refname) else: # default: create refcontent = get_blurb(u'Please enter reference content') newtext += text[processed:footnotepos] + (base_newref % ( refname, refcontent )) processed = footnotepos + len(footnotemarker) continue wikipedia.output(u'(III) Difference between texts:') wikipedia.showDiff(text, newtext) choice = wikipedia.inputChoice(u'(???) Would you like to commit the changes?', [ u'yes', u'No' ], [ u'y', u'N' ], u'N') if choice in [ u'y', u'Y' ]: wikipedia.output(u'(III) Committing changes.') try: page.put(newtext, summ, minorEdit = False) wikipedia.output(u'(!!!) Please add the generator manually if necessary') except wikipedia.Error: wikipedia.output(u'(EEE) Failed to commit changes.')