User:TalBot/demosthenes-endnotes.py
Jump to navigation
Jump to search
#! /usr/bin/env python # _*_ coding: utf8 _*_ # # Integrate the endnotes of [[The Public Orations of Demosthenes]] into the # text as footnotes (using <ref> and <references />). # # This is an interactive script. # # Copyright © 2007, GrafZahl (en.wikisource.org user) # # Licence: GPLv2 # # run with standard args "-log -putthrottle:xx" # import wikipedia # Handle args args = wikipedia.handleArgs() for arg in args: wikipedia.output(u'(WWW) Ignoring unrecognised argument %s' % arg) # basic text tokens, etc. summ = u'[bot] interactive endnote integration' footnotemarker = u'[n]' versebeginmarker = u'{{verse' verseendmarker = u'}}' # Let's hope that's sufficient refquestion = u'(???) What would you like to do with this footnote marker?' refanswers = [ u'Create a new reference', u'Reuse old reference', u'Ignore footnote marker' ] refhotkeys = [ u'C', u'r', u'i' ] refdefault = u'C' base_reusedref = u'<ref name="%s" />' base_newref = u'<ref name="%s">%s</ref>' # Function to obtain a multiline text blurb def get_blurb(text): wikipedia.output(u'(???) %s' % text) wikipedia.output(u'(???) Please finish with <Return>.<Return>') result = u'' while True: line = wikipedia.input(u'') if line == u'.': return result.rstrip() result += line + u'\n' # 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 <references /> generator manually if necessary') except wikipedia.Error: wikipedia.output(u'(EEE) Failed to commit changes.')