Rewrite heading_parser using mwparserfromhell to make it simpler
This commit is contained in:
52
redfam.py
52
redfam.py
@@ -31,7 +31,7 @@ import re
|
|||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
|
|
||||||
import mwparserfromhell as mwparser # noqa
|
import mwparserfromhell as mwparser # noqa
|
||||||
import pywikibot
|
import pywikibot # noqa
|
||||||
from pywikibot.tools import deprecated # noqa
|
from pywikibot.tools import deprecated # noqa
|
||||||
|
|
||||||
import jogobot
|
import jogobot
|
||||||
@@ -205,36 +205,36 @@ class RedFamParser( RedFam ):
|
|||||||
def heading_parser( self, heading ):
|
def heading_parser( self, heading ):
|
||||||
"""
|
"""
|
||||||
Parses given red_fam_heading string and saves articles list
|
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
|
# Parse heading with mwparse if needed
|
||||||
wikilink_pat = re.compile( r"\[\[([^\[\]\|]+)(?:\]\]|\|)" )
|
if not isinstance( heading, mwparser.wikicode.Wikicode ):
|
||||||
|
heading = mwparser.parse( heading )
|
||||||
# Parse content of heading for generating section links later
|
|
||||||
match = type( self ).__sectionhead_pat.search( heading )
|
# Save heading as string
|
||||||
if match:
|
self._heading = str( heading )
|
||||||
self._heading = match.group(2).strip()
|
|
||||||
else:
|
# Save destinations of wikilinks in headings
|
||||||
raise RedFamHeadingError( heading )
|
self._articlesList = [ str( link.title ) for link
|
||||||
|
in heading.ifilter_wikilinks() ]
|
||||||
# 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 ) ]
|
|
||||||
|
|
||||||
# Catch sections with more then 8 articles, print error
|
# Catch sections with more then 8 articles, print error
|
||||||
if len( self._articlesList ) > 8:
|
if len( self._articlesList ) > 8:
|
||||||
# For repression in output we need to know the fam hash
|
# For repression in output we need to know the fam hash
|
||||||
self.calc_fam_hash()
|
self.calc_fam_hash()
|
||||||
pywikibot.output( "\
|
jogobot.output(
|
||||||
{datetime} – \03{{lightred}}[WARNING] – \
|
( "\03{{lightred}}" +
|
||||||
Maximum number of articles in red_fam exceeded, maximum number is 8, \
|
"Maximum number of articles in red_fam exceeded, " +
|
||||||
{number:d} were given \n {repress}".format(
|
"maximum number is 8, {number:d} were given \n {repress}"
|
||||||
datetime=datetime.now().strftime( "%Y-%m-%d %H:%M:%S" ),
|
).format( datetime=datetime.now().strftime(
|
||||||
number=len( self._articlesList ), repress=repr( self ) ) )
|
"%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]
|
self._articlesList = self._articlesList[:8]
|
||||||
|
|
||||||
def add_beginning( self, beginning ):
|
def add_beginning( self, beginning ):
|
||||||
|
|||||||
Reference in New Issue
Block a user