Search code examples
parsingwikimediawiki-templates

Making a simpler parser function for a wiki


I need a template on a wiki about a game (Alphabetty Wiki) that gives a range of levels when given a particular level (e.g. "42" -> "1-50")

I could make one using many #ifexpr, but I am wondering now if there is a simpler solution. I tried with #switch but it seems bulkier.

I am asking this just to learn a bit more about parser functions, as the template I made already kinda works.

Template:

{{#ifexpr:{{{1|}}}<=50|1-50|<!--
-->{{#ifexpr:{{{1|}}}<=95|51-95|<!--
-->{{#ifexpr:{{{1|}}}<=140|96-140|<!--
-->{{#ifexpr:{{{1|}}}<=185|141-185|<!--
-->{{#ifexpr:{{{1|}}}<=230|186-230|<!--
-->{{#ifexpr:{{{1|}}}<=275|231-275|<!--
-->{{#ifexpr:{{{1|}}}<=320|276-320|<!--
-->{{#ifexpr:{{{1|}}}<=365|321-365|<!--
-->{{#ifexpr:{{{1|}}}<=410|366-410|<!--
-->{{#ifexpr:{{{1|}}}<=455|411-455|<!--
-->{{#ifexpr:{{{1|}}}<=500|456-500|<!--
-->{{#ifexpr:{{{1|}}}<=545|501-545|<!--
-->{{#ifexpr:{{{1|}}}<=590|546-590|<!--
-->{{#ifexpr:{{{1|}}}<=620|591-620|<!--
-->{{#ifexpr:{{{1|}}}<=650|621-650|<!--
-->{{#ifexpr:{{{1|}}}<=680|651-680|<!--
-->{{#ifexpr:{{{1|}}}<=710|681-710|<!--
-->{{#ifexpr:{{{1|}}}<=740|711-740|<!--
-->{{#ifexpr:{{{1|}}}<=770|741-770|<!--
-->{{#ifexpr:{{{1|}}}<=800|771-800|<!--
-->{{#ifexpr:{{{1|}}}<=830|801-830|<!--
-->{{#ifexpr:{{{1|}}}<=860|831-860|<!--
-->{{#ifexpr:{{{1|}}}<=890|861-890|891-920}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}

You can make tests directly on the wiki if you know a bit how wikicode works (you don't need an account to make edits, switch to "Source editor"): https://alphabettysaga.fandom.com/fr/wiki/Mod%C3%A8le:Chapitre?action=edit

Thank you for your time.


Solution

  • Use some arithmetics with {{{#expr:}}:

    <includeonly>{{#ifexpr:{{{1|}}}<=50|1-50|{{#ifexpr:{{{1|}}}<=590|<!--
    
    range length is 45
    
    -->{{#expr: 51 + trunc ( ( {{{1|}}} - 50 ) / 45 ) * 45 }}-{{#expr: 95 + trunc ( ( {{{1|}}} - 50 ) / 45 ) * 45 }}|{{#ifexpr:{{{1|}}} <= 890|<!--
    
    range length is 30
    
    -->{{#expr: 591 + trunc ( ( {{{1|}}} - 590 ) / 30 ) * 30 }}-{{#expr: 620 + trunc ( ( {{{1|}}} - 590 ) / 30 ) * 30 }}|
    
    891-920
    }} }} }}</includeonly><noinclude>
    {| class="wikitable"
    ! Level !! Range
    |-
    | 5 || {{Range|5}}
    |-
    | 45 || {{Range|45}}
    |-
    | 55 || {{Range|55}}
    |-
    | 100 || {{Range|100}}
    |-
    | 585 || {{Range|585}}
    |-
    | 595 || {{Range|595}}
    |-
    | 885 || {{Range|885}}
    |-
    | 895 || {{Range|895}}
    |}</noinclude>