Wikipedia talk:WikiProject User scripts/Scripts/Formatter

From Wikipedia, the free encyclopedia

The Formatter script automatically performs a number of common formatting operations.

To install, simply include this line in your monobook.js:

importScript('Wikipedia:WikiProject_User_scripts/Scripts/Formatter');

Optionally, for an automatic edit summary, also include this line:

importScript('Wikipedia:WikiProject_User_scripts/Scripts/Formatter/Auto Edit Summary');

Make sure you refresh (Ctrl+F5) after saving to complete the installation.

When using an edit box, you should have a tab called format. Click this to use Formatter. Make sure you preview all the changes before saving!

[edit] Functions

  • Standardises and removes excess whitespace (tabs, spaces, carriage returns etc.)
    • Does not break special case of using a space at the start of the line for preformatted data
  • Converts dashes, squared2 and degree° symbols to Unicode characters, e.g. –, – or – to – (U+2013)
    • Makes sure not to break items which should not be converted to Unicode
  • Fixes some common non-standard headings:
    • Standard headings have whitespace and capitalisation standardised, e.g. "== eXterNAL LiNKs ==" would become "==External links=="
    • "see also", "related topics", "related articles", "internal links" and "also see" sections become "See also"
    • "external links", "outside links", "web links" and "exterior links" sections become "External links"
    • Convert singular case to plural, e.g. "external link" becomes "External links"
    • "references", "reference", "sources" and "source" sections have whitespace, capitalisation, and pluralisation standardised as above
  • Standardises appearances of categories, e.g. [[category : Test]] to [[Category:Test]]
  • Fixes common syntax errors, e.g. converts <b>text</b> to '''text'''
  • Removes external images and removes code from internal images
  • Simplifies some links, e.g. [[Dog|dog]] to [[dog]] and [[Dog|dogs]] to [[dog]]s
  • Loads as a tab on edit pages


Contents


[edit] Talk

Hi. I added this script to my monobook.js, but I'm not sure if it works (I don't see the difference). How is it supposed to work? Thanks. IronChris | (talk) 18:22, 14 May 2006 (UTC)

Ok I've found the "format" tab... But it doesn't work very well! It modifies some links so that they no longer send to the right article, etc. (see for example the effect on "[[United States]]" here). Am I the only one to have problems with it? IronChris | (talk) 18:31, 14 May 2006 (UTC)
It doesnt do that for me at all. Martin 19:38, 14 May 2006 (UTC)
I removed this :
//entities that should never be unicoded
 function noUnicodify(str) {
  str = str.replace(" & ", " & ");
  str = str.replace("&", "&").replace("<", "&lt;").replace(">", "&gt;").replace(""", "&quot;").replace("&apos;", "&apos;");
  str = str.replace("−", "−").replace("×", "×");
  str = str.replace(" ", " ").replace(" ", " ").replace("­", "­");
  str = str.replace("′", "′");
  str = str.replace(/&(#0?9[13];)/, "&$1");
  str = str.replace(/&(#0?12[345];)/, "&$1");

  return str;
 }
from my monobook.js, that seems to have solved the problems I was having. Not sure what that function was for anyway. IronChris | (talk) 20:55, 14 May 2006 (UTC)
This happened because somebody put a nonbreaking space in between the works "United" and "States" to stop it being split by line wrap. IIRC, the web standard calls for nonbreaking spaces to be used via a specific code, &nbsp; for precisely this sort of reason. The MediaWiki software by default converts all spaces in links to underscores, so this violation remained undiscovered until Formatter revealed it.
If you wanted the space in the link to be nonbreaking, the correct way would be to use the format [[United States|United&nbsp;States]], like so: United States. In the absence of being able to read users' minds, perhaps nonbreaking spaces in links should be converted to regular spaces? GrimRevenant 04:26, 22 February 2007 (UTC)

[edit] Documentation

Can we add a description of what this scipt does to the main page? It may be a good idea for those using it to have similar edit summary advertising this script, like [[Wikipedia:WikiProject User scripts/Scripts/Formatter|format]].-- Piotr Konieczny aka Prokonsul Piotrus  talk  22:34, 25 July 2006 (UTC)

yes, that's a good idea. thanks Martin 08:26, 26 July 2006 (UTC)
I hope we do so, then - I am not familar enough with specific of what this script does to write a description.-- Piotr Konieczny aka Prokonsul Piotrus | talk  19:51, 31 July 2006 (UTC)
Ahem. I've added a doc page at Wikipedia:WikiProject_User_scripts/Scripts/Formatter/doc and transcluded it at the start of the page, in a similar manner to the way template documentation works. This will let us keep discussion away from the documentation, while making the doc available. Hope it's useful like this! GrimRevenant 03:52, 22 February 2007 (UTC)

[edit] Problem with single brackets being wrongly doubled

I keep noticing a problem with this script: when there is a string enclosed in single brackets, like [foo], Formatter assumes from the lack of http: that it is supposed to be an internal link, and doubles the brackets. But http:// is not the only kind of resource; ftp links are valid as well as irc: or mail: links. In addition, even if there is a quote and one inserts an eluicidating comment like this: <nowiki>[they really meant this]</nowiki>, the brackets will be doubled regardless. There doesn't seem to be an easy way to make that regexp smart enough to deal with all these cases, so perhaps it should just be removed. --Gwern (contribs) 16:25, 20 October 2006 (UTC)

Agreed. Usualy a single bracket is intentional; what the formater should fix is a single bracket/two bracket error (when one bracket is missing from an ilink). -- Piotr Konieczny aka Prokonsul Piotrus | talk  21:26, 5 December 2006 (UTC)

[edit] Fix level 1 headings

Other things to easily fix with the script: if the article has level 1 headings (=heading=), the formatter should add '=' to each heading. A slightly more complex: if a new user does not know how to make headings, they often create bolded text in a line, thus the formatter should convert lines containing only bold text into headings. Last but not least, another common newbie mistake is to start paragraphs without an extra line wiki needs; thus if the formatter could detect such 'unwikified' paras and instert an extra line, it would be another useful feature.-- Piotr Konieczny aka Prokonsul Piotrus | talk  21:26, 5 December 2006 (UTC)

[edit] Recent change broken?

Did Ilmari's recent change break Formatter (and one's monobook.js in general) for anyone else? --Gwern (contribs) 19:45 14 January 2007 (GMT)

Yes, and for obvious reasons too. Sorry for the inconvenience. —Ilmari Karonen (talk) 21:13, 14 January 2007 (UTC)

[edit] Dash spacing

At present, this script incorrectly adds spaces around the dash in numerical ranges, e.g. changes "12–34" to "12 – 34". This goes against the Manual of Style.

All that needs to be done to correct this is make sure that the dash is not in a numerical range, i.e. include [^1-9] (or equivalent) at the start and end of the regexp. If I knew more about JavaScript I'd do it myself – actually, might test it in my own userscript if I get a chance and see how I go. GrimRevenant 00:52, 22 February 2007 (UTC)

To clarify, the issue was with en-dashes, and appears to be fixed with this change (links to my personal copy). I'm not sure what the procedure with updating the main script here is, but I'd certainly appreciate some feedback before I go ahead and change anything... cheers. GrimRevenant 01:30, 22 February 2007 (UTC)
It doesn't seem to be particularly maintained, so I'd go ahead if it compiles, runs, and seems to work. --Gwern (contribs) 03:52 22 February 2007 (GMT)
Cheers. Will do – hope it works as well for others as it does for me. GrimRevenant 04:04, 22 February 2007 (UTC)

[edit] Auto summary

Where to put this: document.editform.wpSummary.value = "Formatted using the [[Wikipedia talk:WikiProject User scripts/Scripts/Formatter|WikiProject Formatter]]"? -- Darkest Hour 21:04, 2 March 2007 (UTC)

    • Also, is it possible that it find <br> and replace it with <br />? -- Darkest Hour 21:25, 2 March 2007 (UTC)
I have to say, I rather dislike the autosummary. It seems to blindly wipe out all text already in there, which can be a serious annoyance: it obliterates my existing autosummary JS, and can overwrite manual summaries. This is a very bad change unless it can be smarter and only insert a summary if one is already there. --Gwern (contribs) 07:13 4 March 2007 (GMT)
I,ll do something about it, Darkest Hour 21:03, 5 March 2007 (UTC)
Done Darkest Hour 21:16, 5 March 2007 (UTC)

[edit] IPA

Can the scripts be made to ignore IPA symbols. I had a complaint the other day about my formatting IPA brackets [ɫ ɮ ʏ] = [[ɫ ɮ ʏ]]. I would like not to have another complaint like that one. Thanks for your thoughts on this matter, -- Darkest Hour 00:32, 8 March 2007 (UTC)

This is a generalization of my earlier complaint bout it blindly doubling single brackets. --Gwern (contribs) 02:09 8 March 2007 (GMT)

[edit] Useless edits?

I posted this on User talk:Darkest Hour, but he doesn't know why the script made a useless edit:

I'm curious about why you made this edit. First off, it's generally a bad idea to make edits that have no effect on the page, since they cause server load. Second, I wasn't aware that there was anything wrong with not placing a space between the asterisk and the bracket. Can you please point me to the guideline that supports your edit? Thank you. --NE2 23:17, 9 March 2007 (UTC)

Is this normal practice for the script? --NE2 23:37, 9 March 2007 (UTC)

The List guideline uses spaces in its format description. As regards normal practice for the script, I try not to make edits simply for minor markup changes; however, if I am editing the article for another reason, I will often run the Formatter and check what it picks up. Perhaps we should add an automatic "Show Changes" just to make sure people know what they are changing? (I've seen this in other scripts, and it is useful.) GrimRevenant 11:07, 10 March 2007 (UTC)
It doesn't say that you need to use a space though. Also note that its whitespace in the heading disagrees with what this script does:
Standard headings have whitespace and capitalisation standardised, e.g. "== eXterNAL LiNKs ==" would become "==External links=="
--NE2 15:34, 10 March 2007 (UTC)
No, it doesn't say specifically that spaces are necessary. However, it does include them in the example we are meant to be following for lists. As for section headers, the examples in Help:Section use no spaces, which I would guess is the reason why the script does the same. Basically, it's formatting them according to the best authority we have on formatting minutiae. Again, this kind of stuff is definitely not worth solely editing an article over, but it doesn't hurt to do everything to standard if you're there anyway. GrimRevenant 07:13, 13 March 2007 (UTC)
Just found Wikipedia:Manual of Style (headings), which says:

Spaces above and below headings are completely optional. Spaces between the == and the heading text are also optional (==Heading== vs == Heading ==). With the current Mediawiki software, these extra spaces will not affect the appearance of the heading in any way; the rendered page is identical. Multiple blank lines will add white space, however. It is much like the issue of whether to use one or two spaces after a full stop. It only affects the appearance of the text in the edit box, so use whichever approach best facilitates editing for you, but don't add or remove these spaces just as an edit style preference as it can cause large differences to appear in the edit history.

So really, the section heading part of the script needs either adjustment or removal, as it currently operates counter to the guideline. GrimRevenant 07:35, 13 March 2007 (UTC)

[edit] Done

The adding of [[single]] to [single] is taken out until we can come up with a "smart" version that will know what to and what not to double bracket. Regards, -- Darkest Hour 17:06, 26 March 2007 (UTC)

[edit] "External link"

As per WP:EL (The header should be "External links" (plural) even if there is only a single link listed.) I think the following change should be made.

str = str.replace(/(== ?)(external link:?|web ?link:?|exterior link:?)( ?==)/gi, "$1External link$3");

str = str.replace(/(== ?)(external link:?|web ?link:?|exterior link:?)( ?==)/gi, "$1External links$3");

MrSomeone ( tlkcntrb ) 02:56, 11 May 2007 (UTC)

I've worked the change in by merging with the previous regexp, like so:
 str = str.replace(/(== ?)(external links?:?|outside links?|web ?links?:?|exterior links?:?)( ?==)/gi, "$1External links$3");
In addition, I've made all References or Sources sections convert to the plural. I've tested on a private copy of the formatter script and it appears to be working fine. I'll commit the change and hopefully nothing will break. :) GrimRevenant 16:27, 11 May 2007 (UTC)
Thanks, I'm not bold enough to make changes like this on pages being used by so many users ;) —MrSomeone ( tlkcntrb ) 21:10, 11 May 2007 (UTC)

[edit] Bug

Note for clarity: User:Gerbrant copied this section from their talk page GrimRevenant 09:43, 18 May 2007 (UTC)

See here (diff). Now I did use the formatter (diff) but the search and replace is what did this mess. Please fix thu bug. -- Darkest Hour 21:45, 7 March 2007 (UTC)

[edit] another example

diff —The preceding unsigned comment was added by Darkest Hour (talkcontribs) 21:52, 7 March 2007 (UTC).

Hm. My own formatter (while far from perfect) doesn't seem to cause quite as much mess. I'll take this to the wikiproject to ask whether this problem still exists and where people will have some familiarity with the code-base. Shinobu 19:52, 15 May 2007 (UTC)
Note: I'd fix it myself, but lately RL keeps me kind of busy. Shinobu 19:54, 15 May 2007 (UTC)
First: is this being caused by Formatter or by a search & replace tool? Your comment isn't quite clear to me. Second: seems like over-enthusiastic conversion of numerical codes to characters without checking that they're actually codes first (i.e. escaped by &;). I'll take a look and see what I can come up with. GrimRevenant 07:45, 18 May 2007 (UTC)
While I realise this is some time later, I've run the formatter on the exact versions of both pages you reported problems with, and I can't duplicate any of these issues. If you hit this kind of issue, it can be worthwhile doing a force refresh (Ctrl+F5 in most browsers) to make sure you're using the latest version of any scripts you're using, before you cry "bug". Cheers. GrimRevenant 07:58, 18 May 2007 (UTC)
Just checked the post dates against the edit history and realised that you had copied this here from your talk page. While there's nothing wrong with that, it would be better to have included some sort of notice, because it appears User:Darkest Hour's problem was with a different tool. GrimRevenant 09:43, 18 May 2007 (UTC)
Actually, no. Darkest Hour used the Wikiproject Formatter, not my one, as you can see by looking at his edit summaries. So that's why I brought the issue here, because this is (was?) a bug in the formatter here, not in mine. And because I thought you would be more familiar with the code base than I am (after all I only use mine), I thought you could fix the problem (if any) faster than I could. Shinobu 14:55, 29 May 2007 (UTC)
From Darkest Hour's original post on your talk page (emphasis added): "Now I did use the formatter (diff) but the search and replace is what did this mess. Please fix thu bug." FWIW, I ran this formatter on the exact same page revisions Darkest Hour had problems with, and it worked flawlessly. I suspect we won't get to the bottom of this without more input from the original poster, especially regarding what exactly was searched and replaced... it's very easy to break something with a regex tool if it's used incorrectly, which I suspect may have been the case. Cheers. GrimRevenant 04:36, 30 May 2007 (UTC)
@it's very easy to break something with a regex tool if it's used incorrectly: True. By the way, the only regex related tool I can find in his monobook.js is wikEd. And there is of course the possibility that he was using an old verions of any of these tools; in other words that the bug has already been fixed but that his cache needs purging. Let's wait for more details. Cheers, Shinobu 01:41, 4 June 2007 (UTC)

[edit] Tab location

I'm thinking about the location of the tab. Currently, it goes just before the edit tab, which seems strange. Perhaps it should go after the edit tab (and the "+" tab, if there is one)? One could do this by changing the getElementById statement near the end. Anyone else think this should be moved? — Tuvok[T@lk/Improve me] 21:28, 29 May 2007 (UTC)

[edit] Stub placement

It's a minor issue, but I just wanted to point out that this script removes the extra blank line between the stub template and the rest of the content. According to this, there should be two blank lines. I have no opinion which should be practiced (one or two blank lines), however this script and the guide should agree. -Rocket000 22:01, 2 September 2007 (UTC)

I generally remove superfluous newlines in all cases. But in the case of stubs the whole issue is pretty much moot, since a stub templates is by its nature a temporary notice. Shinobu 05:02, 4 November 2007 (UTC)

[edit] Formatting

When I click the format tab, it autosaves without giving a chance to preview. Is anyone else having this problem? Help! --Skylights76 (talk) 22:36, 12 April 2008 (UTC)

No problem here. --Gwern (contribs) 22:18 13 April 2008 (GMT)

[edit] Request to remove disruptive code

{{editprotected}} The script currently facilitates changing the headings "Related topics" and "Related articles" to "See also". The Wikipedia Manual of Style explicitly permits these as legitimate headings, and this script's removal of them is disruptive. Please stop this before it gets out of hand (it has to be manually reverted) by removing the following from the section of the page entitled "//Fix ==See also== and similar section common errors.":

"|related topics:?|related articles:?"

Many thanks, Skomorokh 00:55, 1 May 2008 (UTC)

Where in the MoS? I couldn't find it on a 30-second perusal. Happymelon 17:42, 1 May 2008 (UTC)
Sorry, I thought it would be a question of AGF; the specific reference is in WP:SEEALSO (a style guideline subpage of the MOS). It currently reads "A less common practice is to name the section "Related topics". Regards, Skomorokh 17:55, 1 May 2008 (UTC)
I do indeed assume that you would not be deliberately pushing a POV, but that doesn't mean that claims of support from the MoS shouldn't be unverified; honest mistakes and misinterpretations are just as common - my own knowledge of the MoS has just been found somewhat lacking in one instance. As it turns out, the phrase you referred to seems to have been removed by Dank55, so it seems that the issue is controversial. Per that, and meta:The Wrong Version, N Not done Happymelon 18:46, 4 May 2008 (UTC)
If you had read the talkpage, you would see that Dan neither Dan nor any other recent editors of the Layout support this "standardisation" of the wrong version. Someone made a mistake, no problem, bit why on earth exacerbate it? I came here because User:Iridescent, through no fault of her own, was performing unrelated cleanup tasks using AWB and inadvertently altered dozens of articles with "Related topics" sections to "See also". "See also" addresses the reader, in violation of encyclopedian tone. "Related articles" and "related topics" do not. I am restoring the edit protected request so that an administrator whose knowledge of the MoS is not lacking may address it. Thank you for your time, Skomorokh 21:01, 4 May 2008 (UTC)
I've reviewed the WP:SEEALSO section and it seems like the "Related topics" was removed as an acceptable alternative. In addition, I couldn't find "Related articles" in this MOS's recent history as being an alternative. I ran a database scan and found only 144 uses of "Related topics" (many of which were since converted to "See also"), and I noticed that you personally added a significant number of those. Also, only 72 articles used "Related articles". It seems to me that since it is now out of the MOS, this should be discussed further on that talk page before I go change the code on this script. So, I'm keeping the N Not done decision of Happy Melon, and opting for more discussion and consensus before making such a change. --CapitalR (talk) 01:06, 5 May 2008 (UTC)