diff --git a/bots/markpages.py b/bots/markpages.py index b08776c..7fae7c8 100644 --- a/bots/markpages.py +++ b/bots/markpages.py @@ -31,6 +31,8 @@ from datetime import datetime from pywikibot import pagegenerators from pywikibot.bot import CurrentPageBot +import mwparserfromhell as mwparser + import jogobot from lib.redfam import RedFamWorker @@ -116,7 +118,54 @@ class MarkPagesBot( CurrentPageBot ): # sets 'current_page' on each treat() def treat_page( self ): """ Handles work on current page + + We get a reference to related redfam in current_page.redfam """ - # Here is the place where to do what ever you want - print( self.current_page.title() ) + # First we need to have the current text of page + # and parse it as wikicode + self.current_wikicode = mwparser.parse( self.current_page.text ) + + # Add notice + self.add_disc_notice_template() + + # Convert wikicode back to string to save + self.new_text = str( self.current_wikicode ) + + # Save + self.put_current( self.new_text ) + + def add_disc_notice_template( self ): + """ + Will take self.current_wikicode and adds disc notice template after the + last template in leading section or as first element if there is no + other template in leading section + """ + # The notice to add + notice = self.current_page.redfam.generate_disc_notice_template() + + # Find the right place to insert notice template + # Therfore we need the first section (if there is one) + leadsec = self.current_wikicode.get_sections( + flat=False, include_lead=True )[0] + + # There is none on empty pages, so we need to check + if leadsec: + # Get the last template in leadsec + ltemplate = leadsec.filter_templates()[-1] + + # If there is one, add notice after this + if ltemplate: + self.current_wikicode.insert_after(ltemplate, notice ) + + # To have it in its own line we need to add a linbreak before + self.current_wikicode.insert_before(notice, "\n" ) + + # If there is no template, add before first element on page + else: + self.current_wikicode.insert( 0, notice ) + + # If there is no leadsec (and therefore no template in it, we will add + # before the first element + else: + self.current_wikicode.insert( 0, notice )