Browse Source

Rewrite heading_parser using mwparserfromhell to make it simpler

develop
Jonathan Golder 8 years ago
parent
commit
b81694c6d3
  1. 52
      redfam.py

52
redfam.py

@ -31,7 +31,7 @@ import re
from datetime import datetime
import mwparserfromhell as mwparser # noqa
import pywikibot
import pywikibot # noqa
from pywikibot.tools import deprecated # noqa
import jogobot
@ -205,36 +205,36 @@ class RedFamParser( RedFam ):
def heading_parser( self, heading ):
"""
Parses given red_fam_heading string and saves articles list
@param heading Heading of RedFam-Section
@type heading wikicode or mwparser-parseable
"""
# Predefine a pattern for wikilinks' destination
wikilink_pat = re.compile( r"\[\[([^\[\]\|]+)(?:\]\]|\|)" )
# Parse content of heading for generating section links later
match = type( self ).__sectionhead_pat.search( heading )
if match:
self._heading = match.group(2).strip()
else:
raise RedFamHeadingError( heading )
# We get the pages in first [0] element iterating over
# wikilink_pat.findall( line )
# Strip leading and trailing whitespace in Links to prevent wrong
# fam_hashes (when receiving redfam from db) since MySQL drops it
self._articlesList = [ link.strip() for link
in wikilink_pat.findall( self._heading ) ]
# Parse heading with mwparse if needed
if not isinstance( heading, mwparser.wikicode.Wikicode ):
heading = mwparser.parse( heading )
# Save heading as string
self._heading = str( heading )
# Save destinations of wikilinks in headings
self._articlesList = [ str( link.title ) for link
in heading.ifilter_wikilinks() ]
# Catch sections with more then 8 articles, print error
if len( self._articlesList ) > 8:
# For repression in output we need to know the fam hash
self.calc_fam_hash()
pywikibot.output( "\
{datetime} \03{{lightred}}[WARNING] \
Maximum number of articles in red_fam exceeded, maximum number is 8, \
{number:d} were given \n {repress}".format(
datetime=datetime.now().strftime( "%Y-%m-%d %H:%M:%S" ),
number=len( self._articlesList ), repress=repr( self ) ) )
jogobot.output(
( "\03{{lightred}}" +
"Maximum number of articles in red_fam exceeded, " +
"maximum number is 8, {number:d} were given \n {repress}"
).format( datetime=datetime.now().strftime(
"%Y-%m-%d %H:%M:%S" ), number=len( self._articlesList ),
repress=repr( self ) ),
"WARNING" )
# Only save the first 8 articles
self._articlesList = self._articlesList[:8]
def add_beginning( self, beginning ):

Loading…
Cancel
Save