Difference between revisions of "Template:List lookup helper"

From QBWiki
Jump to navigation Jump to search
m
Line 3: Line 3:
 
If page name is previously defined AND it equals arg1 then this invocation of the template can be skipped. This maintains the invariant of "loaded exactly once" if this is transcluded in another template that is itself transcluded multiple times.
 
If page name is previously defined AND it equals arg1 then this invocation of the template can be skipped. This maintains the invariant of "loaded exactly once" if this is transcluded in another template that is itself transcluded multiple times.
  
-->|{{Add|invocations|1}}{{#vardefine:page_name|{{{1}}}}}{{#vardefine:loaded_page|{{:{{#var:page_name}}}}}}{{#vardefine:cleaned_page|{{#explode:{{#var:loaded_page}}|==|0}}}}{{#vardefine:minindex|0}}{{#vardefine:maxindex|1}}<!--
+
-->|{{Add|invocations|1}}{{#vardefine:page_name|{{{1}}}}}{{#vardefine:loaded_page|{{:{{#var:page_name}}}}}}<!--
  
Find the upper bound of the size in steps of 2^n
+
At this point behavior branches between two cases:
 +
1) the default case acts directly on loaded_page BUT fails if its length is above 1000 characters
 +
2) the sublist case gets around this limit but relies on manual separation of the list into sections of less than 1000 characters
  
-->{{#while:
+
-->{{#if:{{#var:sublist_index}}
|{{#explode:{{#var:cleaned_page}}|*|{{#var:maxindex}}|1000}}
+
|{{List lookup helper/sublist case}}<!--
|{{#vardefine:minindex|{{#var:maxindex}}}}{{Multiply|maxindex|2}}
 
}}{{#vardefine:index|{{#expr:floor(({{#var:maxindex}}+{{#var:minindex}})/2)}}}}<!--
 
  
Perform a binary search between [2^n-1, 2^n] to find the last element of the list
+
In the default case: check whether the page is too large and embed a Warning if so - otherwise do work
  
-->{{#while:
+
-->|{{#iferror:{{#len:{{#var:loaded_page}}}}|{{Warning|This list is too large to be parsed with the default behavior - please [{{#var:page_name}}edit it] to make use of the [[Template:Sublist|Sublist]] template.}}|{{List lookup helper/default case}}}}<!--
|{{#ifexpr:{{#var:minindex}}+1<{{#var:maxindex}}|true}}
+
  -->}}<!--
|{{#if:{{#explode:{{#var:cleaned_page}}|*|{{#var:index}}|1000}}
+
-->}}</includeonly><noinclude>
<!--undershoot-->|{{#vardefine:minindex|{{#var:index}}}}
 
<!-- overshoot-->|{{#vardefine:maxindex|{{#var:index}}}}
 
}}
 
{{#vardefine:index|{{#expr:floor(({{#var:maxindex}}+{{#var:minindex}})/2)}}}}
 
}}{{Copy|index|length}}<!--
 
 
 
-->{{#loop:ii|1|{{#var:length}}|{{#vardefine:entry_{{#var:ii}}|{{#explode:{{#var:cleaned_page}}|*|{{#var:ii}}|1000}}}}}}}}</includeonly><noinclude>
 
 
{{Template doc|
 
{{Template doc|
 
Given a list page, loads it exactly once and sets up enough variables it will not need to be loaded again to retrieve any entry. If this template has been invoked before with the same page_name, no-op.
 
Given a list page, loads it exactly once and sets up enough variables it will not need to be loaded again to retrieve any entry. If this template has been invoked before with the same page_name, no-op.
 
* page_name - the page name provided as arg1
 
* page_name - the page name provided as arg1
 +
* length - the length of the list
 +
* entry_i - the entry in the list at position ''i'', where i is in <nowiki>[1, length-1]</nowiki> (0-indexed)
 +
 +
If using the default case (<1000 total characters), the following will be set:
 
* loaded_page - the text of page_name
 
* loaded_page - the text of page_name
 
* cleaned_page - the text of page_name before the first section header ("<nowiki>==</nowiki>")
 
* cleaned_page - the text of page_name before the first section header ("<nowiki>==</nowiki>")
* length - the length of the list
 
* entry_i - the entry in the list at position ''i'', where i is in <nowiki>[1, length-1]</nowiki> (0-indexed)
 
  
 
There is also the housekeeping variable "invocations", which equals the number of times this template was resolved and should thus always be 1.
 
There is also the housekeeping variable "invocations", which equals the number of times this template was resolved and should thus always be 1.
 +
 +
Implementation is split between [[Template:List lookup helper/default case|List lookup helper/default case]] and [[Template:List lookup helper/sublist case|List lookup helper/sublist case]].
  
 
<pre>
 
<pre>
Line 40: Line 37:
 
{{#var:length}}, {{#var:invocations}}
 
{{#var:length}}, {{#var:invocations}}
  
 +
{{Save|jj}}
 
{{#loop:jj|1|{{#var:length}}|{{#var:entry_{{#var:jj}}}}<br/>}}
 
{{#loop:jj|1|{{#var:length}}|{{#var:entry_{{#var:jj}}}}<br/>}}
 +
{{Load|jj}}
 
</pre>
 
</pre>
  
Line 48: Line 47:
 
{{#var:length}}, {{#var:invocations}}
 
{{#var:length}}, {{#var:invocations}}
  
 +
{{Save|jj}}
 
{{#loop:jj|1|{{#var:length}}|{{#var:entry_{{#var:jj}}}}<br/>}}
 
{{#loop:jj|1|{{#var:length}}|{{#var:entry_{{#var:jj}}}}<br/>}}
 +
{{Load|jj}}
 
}}</noinclude>
 
}}</noinclude>

Revision as of 17:18, 20 December 2025

Template documentation

Given a list page, loads it exactly once and sets up enough variables it will not need to be loaded again to retrieve any entry. If this template has been invoked before with the same page_name, no-op.

  • page_name - the page name provided as arg1
  • length - the length of the list
  • entry_i - the entry in the list at position i, where i is in [1, length-1] (0-indexed)

If using the default case (<1000 total characters), the following will be set:

  • loaded_page - the text of page_name
  • cleaned_page - the text of page_name before the first section header ("==")

There is also the housekeeping variable "invocations", which equals the number of times this template was resolved and should thus always be 1.

Implementation is split between List lookup helper/default case and List lookup helper/sublist case.

{{:QBWiki:Quick links/lingo}}
{{List lookup helper|QBWiki:Quick links/lingo}}
{{#var:cleaned_page}}
{{#var:length}}, {{#var:invocations}}

{{Save|jj}}
{{#loop:jj|1|{{#var:length}}|{{#var:entry_{{#var:jj}}}}<br/>}}
{{Load|jj}}


  • Wondering why people keep saying "m"?
  • Confused what a "vulcher" is?
  • Want to find out about "superpowers"?
  • Wondering just what makes something "important"?
  • Heard something about the "canon"?


  • Wondering why people keep saying "m"?
  • Confused what a "vulcher" is?
  • Want to find out about "superpowers"?
  • Wondering just what makes something "important"?
  • Heard something about the "canon"?

5, 1


Wondering why people keep saying "m"?
Confused what a "vulcher" is?
Want to find out about "superpowers"?
Wondering just what makes something "important"?
Heard something about the "canon"?