cosmetic_changes
— Cosmetic Changes of Wikitext#
This module can do slight modifications to tidy a wiki page’s source code.
The changes are not supposed to change the look of the rendered wiki page.
If you wish to run this as an stand-alone script, use:
scripts/cosmetic_changes.py
For regular use, it is recommended to put this line into your user config:
cosmetic_changes = True
You may enable cosmetic changes for additional languages by adding the dictionary cosmetic_changes_enable to your user-config.py. It should contain a tuple of languages for each site where you wish to enable in addition to your own langlanguage if cosmetic_changes_mylang_only is True (see below). Please set your dictionary by adding such lines to your user config:
cosmetic_changes_enable['wikipedia'] = ('de', 'en', 'fr')
There is another config variable: You can set:
cosmetic_changes_mylang_only = False
if you’re running a bot on multiple sites and want to do cosmetic changes on all of them, but be careful if you do.
You may disable cosmetic changes by adding the all unwanted languages to
the dictionary cosmetic_changes_disable
in your user config file
(user-config.py
). It should contain a tuple of languages for each site
where you wish to disable cosmetic changes. You may use it with
cosmetic_changes_mylang_only
is False, but you can also disable your
own language. This also overrides the settings in the dictionary
cosmetic_changes_enable
. Please set this dictionary by adding such
lines to your user config file:
cosmetic_changes_disable['wikipedia'] = ('de', 'en', 'fr')
You may disable cosmetic changes for a given script by appending the all unwanted scripts to the list cosmetic_changes_deny_script in your user-config.py. By default it contains cosmetic_changes.py itself and touch.py. This overrides all other enabling settings for cosmetic changes. Please modify the given list by adding such lines to your user-config.py:
cosmetic_changes_deny_script.append('your_script_name_1')
or by adding a list to the given one:
cosmetic_changes_deny_script += ['your_script_name_1',
'your_script_name_2']
- class cosmetic_changes.CANCEL(value, names=None, *values, module=None, qualname=None, type=None, start=1, boundary=None)[source]#
Bases:
IntEnum
Cancel level to ignore exceptions.
If an error occurred and either skips the page or the method or a single match. ALL raises the exception.
Added in version 6.3.
- ALL = 0#
- MATCH = 3#
- METHOD = 2#
- PAGE = 1#
- class cosmetic_changes.CosmeticChangesToolkit(page, *, show_diff=False, ignore=CANCEL.ALL)[source]#
Bases:
object
Cosmetic changes toolkit.
Changed in version 7.0:
from_page()
method was removedChanged in version 5.2: instantiate the CosmeticChangesToolkit from a page object; only allow keyword arguments except for page parameter;
namespace
andpageTitle
parameters are deprecatedChanged in version 7.0:
namespace
andpageTitle
parameters were removed- Parameters:
page (pywikibot.page.BasePage) – the Page object containing the text to be modified
show_diff (bool) – show difference after replacements
ignore (IntEnum) – ignores if an error occurred and either skips the page or only that method. It can be set one of the CANCEL constants
- change(text)[source]#
Execute all clean up methods and catch errors if activated.
- Parameters:
text (str)
- Return type:
bool | str
- cleanUpLinks(text)[source]#
Tidy up wikilinks found in a string.
This function will:
Replace underscores with spaces
Move leading and trailing spaces out of the wikilink and into the surrounding text
Convert URL-encoded characters into Unicode-encoded characters
Move trailing characters out of the link and make the link without using a pipe, if possible
Capitalize the article title of the link, if appropriate
Changed in version 8.4: Convert URL-encoded characters if a link is an interwiki link or different from main namespace.
- Parameters:
text (str) – string to perform the clean-up on
- Returns:
text with tidied wikilinks
- Return type:
str
- cleanUpSectionHeaders(text)[source]#
Add a space between the equal signs and the section title.
Example:
==Section title==
becomes:
== Section title ==
Note
This space is recommended in the syntax help on the English and German Wikipedias. It is not wanted on Lojban and English Wiktionaries (T168399, T169064) and it might be that it is not wanted on other wikis. If there are any complaints, please file a bug report.
- Parameters:
text (str)
- Return type:
str
- commonsfiledesc(text)[source]#
Clean up file descriptions on Wikimedia Commons.
It works according to [1] and works only on pages in the file namespace on Wikimedia Commons.
[1]: https://commons.wikimedia.org/wiki/Commons:Tools/pywiki_file_description_cleanup
- Parameters:
text (str)
- Return type:
str
- fixArabicLetters(text)[source]#
Fix Arabic and Persian letters.
- Parameters:
text (str)
- Return type:
str
- fixHtml(text)[source]#
Replace html markups with wikitext markups.
- Parameters:
text (str)
- Return type:
str
- fixSelfInterwiki(text)[source]#
Interwiki links to the site itself are displayed like local links.
Remove their language code prefix.
- Parameters:
text (str)
- Return type:
str
- fixStyle(text)[source]#
Convert prettytable to wikitable class.
- Parameters:
text (str)
- Return type:
str
- fixSyntaxSave(text)[source]#
Convert weblinks to wikilink, fix link syntax.
- Parameters:
text (str)
- Return type:
str
- putSpacesInLists(text)[source]#
Add a space between the * or # and the text.
Note
This space is recommended in the syntax help on the English, German and French Wikipedias. It might be that it is not wanted on other wikis. If there are any complaints, please file a bug report.
- Parameters:
text (str)
- Return type:
str
- removeNonBreakingSpaceBeforePercent(text)[source]#
Remove a non-breaking space between number and percent sign.
Newer MediaWiki versions automatically place a non-breaking space in front of a percent sign, so it is no longer required to place it manually.
- Parameters:
text (str)
- Return type:
str
- removeUselessSpaces(text)[source]#
Cleanup multiple or trailing spaces.
- Parameters:
text (str)
- Return type:
str
- replaceDeprecatedTemplates(text)[source]#
Replace deprecated templates.
- Parameters:
text (str)
- Return type:
str
- resolveHtmlEntities(text)[source]#
Replace HTML entities with string.
- Parameters:
text (str)
- Return type:
str
- safe_execute(method, text)[source]#
Execute the method and catch exceptions if enabled.
- Parameters:
method (Callable[[str], str])
text (str)
- Return type:
str
Standardize page footer.
Makes sure that interwiki links and categories are put into the correct position and into the right order.
The page footer consists of the following parts in that sequence:
categories
additional information depending on the local site policy
interwiki
Changed in version 9.3: uses
main_sortkey
to determine the sort key for the main article within a category. If the main article has a sort key already, it will not be changed any longer.- Parameters:
text (str) – text to be modified
- Returns:
the modified text
- Raises:
ValueError – wrong value of sortkey in
main_sortkey
for the given site- Return type:
str
- cosmetic_changes.main_sortkey = {'_default': ' ', 'ar': '*'}#
Sort key to specify the main article within a category.
The sort key must be one of
' '
,'!'
,'*'
,'#'
and is used like a pipe link but sorts the page in front of the alphabetical order. This dict is used inCosmeticChangesToolkit.standardizePageFooter()
.Added in version 9.3.