User:Happy-melon/Convert

From mediawiki.org

This is a rough manual for the new {{#convert: ... }}

{{convert}} on enwiki is a behemoth of a construction that just about manages to do this sort of conversion, taking {{convert|5|mi|km}} and outputting "5 miles (8 km)", etc. To port this to another wiki requires copying over three and a half thousand subtemplates. The additional load produced by including numerous copies of this template is measurable on large pages on enwiki, and it eats voraciously into the template limits.

This revision introduces {{#convert: 5 mi | km }}, outputting "8 kilometres". We take advantage of the availability of regular expressions to allow the unit to be included in the input string, which should improve usability. There are options for the unit to be linked, and/or abbreviated:

It is also possible to specify that the output unit should be the same as the input unit, but maybe linked and/or abbreviated:

  • {{#convert: 5 mi | #sourceunit=#targetunit | #linkunit }} → "5 miles"

Between these options, it should be very easy to reconstruct the functionality of {{convert}} as a simple wrapper template: indeed something like this is a crude but effective start:

{{#convert: {{{1}}} {{{2}}} 
  | #sourceunit=#targetunit 
  | {{#switch: {{{lk}}} |in|both = #linkunit }}
  | {{#switch: {{{abbr}}} |in|on = #abbreviate }}
}}{{#switch: {{{disp}}}
  | or =  or&nbs;
  | comma = , 
  | #default=  (
}}{{#convert: {{{1}}} {{{2}}}
  | #targetunit = {{{3}}}
  | {{#switch: {{{lk}}} |out|both = #linkunit }}
  | {{#switch: {{{abbr}}} |out|on = #abbreviate }}
}}{{#switch: {{{disp}}} |or|comma=|#default = ) }}