From 9d9207c175e435f40ae90a8db463e15fb9e09f10 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?GOLDERWEB=20=E2=80=93=20Jonathan=20Golder?= Date: Thu, 10 Dec 2015 23:13:45 +0100 Subject: [PATCH 1/2] CountryList-module: Put linksearching algorithm in separate function for simple reuse for Titel value --- countrylist.py | 78 +++++++++++++++++++++++++++++++++++--------------- 1 file changed, 55 insertions(+), 23 deletions(-) diff --git a/countrylist.py b/countrylist.py index 38e5c9b..b94ac75 100644 --- a/countrylist.py +++ b/countrylist.py @@ -298,31 +298,10 @@ missing!" ) parts.append( word ) parts.append( " " ) - # If we have indexes with out links, search for links + # If we have indexes without links, search for links if indexes: - # Iterate over wikilinks of refpage and try to find related links - for wikilink in self.wikicode.ifilter_wikilinks(): - - # Iterate over interpret names - for index in indexes: - - # Check wether wikilink matches - if( parts[index] == wikilink.text or - parts[index] == wikilink.title ): - - # Overwrite name with complete wikilink - parts[index] = str( wikilink ) - - # Remove index from worklist - indexes.remove( index ) - - # Other indexes won't also match - break - - # If worklist is empty, stop iterating over wikilinks - if not indexes: - break + parts = self._search_links( parts, indexes ) # Join the collected links sep = " " @@ -343,6 +322,59 @@ missing!" ) raise CountryListEntryError( "Template Parameter 'Interpret' is \ missing!" ) + def _search_links( self, keywords, indexes=None ): + """ + Search matching wikilinks for keyword(s) in CountryList's wikicode + + @param keywords: One or more keywords to search for + @type keywords: str, list + @param indexes: List with numeric indexes for items of keywords to work + on only + @type indexes: list of ints + @return: List or String with replaced keywords + @return type: str, list + """ + + # Maybe convert keywords string to list + if( isinstance( keywords, str ) ): + keywords = [ keywords, ] + string = True + else: + string = False + + # If indexes worklist was not provided, work on all elements + if not indexes: + indexes = range( len( keywords ) - 1 ) + + # Iterate over wikilinks of refpage and try to find related links + for wikilink in self.wikicode.ifilter_wikilinks(): + + # Iterate over interpret names + for index in indexes: + + # Check wether wikilink matches + if( keywords[index] == wikilink.text or + keywords[index] == wikilink.title ): + + # Overwrite name with complete wikilink + keywords[index] = str( wikilink ) + + # Remove index from worklist + indexes.remove( index ) + + # Other indexes won't also match + break + + # If worklist is empty, stop iterating over wikilinks + if not indexes: + break + + # Choose wether return list or string based on input type + if not string: + return keywords + else: + return keywords[0] + class CountryListError( Exception ): """ From e409c7a02bc79eb4bb1df71a34df37abc373f740 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?GOLDERWEB=20=E2=80=93=20Jonathan=20Golder?= Date: Fri, 11 Dec 2015 00:03:53 +0100 Subject: [PATCH 2/2] CountryList-module: Also search for Links in Titel --- countrylist.py | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/countrylist.py b/countrylist.py index b94ac75..6b76b07 100644 --- a/countrylist.py +++ b/countrylist.py @@ -244,7 +244,11 @@ missing!" ) if not self._titel_raw: self.get_titel_value() - self.titel = self._titel_raw + # Try to find a wikilink for Titel on countrylist + if "[[" not in self._titel_raw: + self.titel = self._search_links( str(self._titel_raw) ) + else: + self.titel = self._titel_raw def get_titel_value( self ): """ @@ -344,7 +348,7 @@ missing!" ) # If indexes worklist was not provided, work on all elements if not indexes: - indexes = range( len( keywords ) - 1 ) + indexes = list(range( len( keywords ) )) # Iterate over wikilinks of refpage and try to find related links for wikilink in self.wikicode.ifilter_wikilinks(): @@ -365,15 +369,15 @@ missing!" ) # Other indexes won't also match break - # If worklist is empty, stop iterating over wikilinks - if not indexes: - break + # If worklist is empty, stop iterating over wikilinks + if not indexes: + break # Choose wether return list or string based on input type if not string: return keywords else: - return keywords[0] + return str(keywords[0]) class CountryListError( Exception ):