Gebruiker:Milliped/WD/Lua indentatie fout voorbeeld
Indentatie poging 1
bewerkenIk probeer hier indentatie aan te bregen om de code van Module:Infobox station leesbaarder te maken. Ik volg het gegeven dat volgens de instructies hier je overal whitespace aan zou moeten kunnen brengen, maar dat gaat niet goed, ik krijg met de codewijziging waar ik spatiëring onder het kopje --Stationscode aan wil brengen de volgende fout:
Luafout op regel 94: ambiguous syntax (function call x new statement) near '('
De volledige modulecode met spatiëring ziet er dan als volgt uit:
local p = {}
--Function ter verificatie of een waarde leeg is (geen of lege string)
function is_empty(ie_val)
if(ie_val) then --if not nul, check for empty string
if(string.len(ie_val)>0 and ie_val~="-") then return false; --lengte van de waarde is niet nul en de waarde is geen streepje, derhalve niet leeg
elseif(ie_val=="-") then return true; --streepje, betekent leeg
else return true; end --lege string, betekent leeg
else return true; end --indien geen, betekent dat dat de waarde leeg is
end
--Function om een Wikidata item id te verkrijgen en daarmee een link naar het desbetreffende lemma te verkrijgen, of als er geen lemma voor deze tekst bestaat het wikidata label als tekst weer te geven
function title_for(item_id)
tf_item = mw.wikibase.getEntity(item_id);
if(tf_item.sitelinks and tf_item.sitelinks.nlwiki) then --er bestaat een sitelink naar nlwiki
page = tf_item.sitelinks.nlwiki.title;
return "[["..page.."|"..mw.text.split(page, " %(")[1].."]]";
elseif(tf_item.labels.nl) then return tf_item.labels.nl.value; --er bestaat op wikidata een label in het Nederlands
else return ""; end --als geen van beide bestaat, geef een lege string
end
--Main function
function p.main(frame)
parent = frame:getParent();
args = parent.args;
item = mw.wikibase.getEntity(mw.wikibase.getEntityIdForCurrentPage());
label = '\n|-\n| style="width:120px" | '; --begin van een rij
res = {'{| class="infobox" style="width:{{Infobox/breedte}}px; font-size:85%;"'}; --tabel waar het resultaat in zal worden opgeslagen
--Titel
if(is_empty(args["naam"])) then --if argument "naam" not set, it gets page name with text in brackets removed
args["naam"] = mw.text.split(tostring(mw.title.getCurrentTitle()), " %(")[1]; end
table.insert(res, '\n|-\n| align="center" colspan="2" bgcolor="#00aaFF" | '
.."<big>'''"..args["naam"].."'''</big>"); --insert argument "naam" into the result
--Afbeelding
if(is_empty(args["afbeelding"]) and item and item.claims["P18"]) then
args["afbeelding"] = item:getBestStatements('P18')[1].mainsnak.datavalue.value; end
if(is_empty(args["afbeeldingbreedte"])) then args["afbeeldingbreedte"] = "290px"; end
if(is_empty(args["beschrijving_afbeelding"]) and
item and item.claims["P18"] and
item.claims["P18"][1].qualifiers and
item.claims["P18"][1].qualifiers["P2096"]) then
desc_text = ""; nl_index = -1;
desc = item:getBestStatements('P18')[1].qualifiers["P2096"];
--iterate over P2096 qualifiers, untill there is one with dutch language
for i=1,#desc,1 do
if(desc[i].datavalue.value.language=="nl") then
nl_index = i; break; end
end
if(nl_index~=-1) then args["beschrijving_afbeelding"] = desc[nl_index].datavalue.value.text; end
end
--insert image and its description, if present, into the result table
if(not is_empty(args["afbeelding"])) then
table.insert(res, '\n|-\n| align="center" colspan="2" | [[Bestand:'..
args["afbeelding"].."|"..args["afbeeldingbreedte"].."|center]]"); end
if(not is_empty(args["beschrijving_afbeelding"])) then
table.insert(res, "{{center|1=''"..args["beschrijving_afbeelding"].."''}}"); end
--Land
if(is_empty(args["land"]) and item and item.claims["P17"]) then
args["land"] = title_for(item:getBestStatements('P17')[1].mainsnak.datavalue.value.id); end
if(is_empty(args["vlag"]) and item and item.claims["P17"]) then
--getting the item for country, then retrieving iso-3 code from it to use in Template:Vlagland
country_item = mw.wikibase.getEntity(item:getBestStatements('P17')[1].mainsnak.datavalue.value.id);
if(country_item and country_item.claims["P298"]) then
args["vlag"] = '{{Vlagland|code='..
country_item:getBestStatements('P298')[1].mainsnak.datavalue.value..'}} ';
else args["vlag"] = ""; end
elseif(is_empty(args["vlag"])) then args["vlag"] = ""; end --if there is no iso-3 code, there would be no flag
if(not is_empty(args["land"])) then
table.insert(res, label.."'''Land'''".." || "..args["vlag"]..args["land"]); end
--Plaats (voorbeeld enkel item)
if(is_empty(args["plaats"]) and item and item.claims["P131"]) then
args["plaats"] = title_for(item:getBestStatements('P131')[1].mainsnak.datavalue.value.id); end
if(not is_empty(args["plaats"])) then
table.insert(res, label.."'''Plaats'''".." || "..args["plaats"]); end
--Status(enkel item) In gebruik of niet, P5817
if(is_empty(args["gebruiksstatus"]) and item and item.claims["P5817"]) then
args["gebruiksstatus"] = title_for(item:getBestStatements('P5817')[1].mainsnak.datavalue.value.id); end
if(not is_empty(args["gebruiksstatus"])) then
table.insert(res, label.."'''Status'''".." || "..args["gebruiksstatus"]); end
--Stationscode (enkel item) Voor deze infobox de Franse stationscode, P8181
if
(
is_empty
(
args["stationscode_fr"]
)
and
item
and
item.claims["P8181"]
)
then
args["stationscode_fr"] = item:getBestStatements('P8181')[1].mainsnak.datavalue.value;
end
if
(
not is_empty
(
args["stationscode_fr"]
)
)
then
table.insert(res, label.."'''Stationscode'''".." || "..args["stationscode_fr"]);
end
--Stationscode UIC (enkel item) UIC stationscode, P722
if(is_empty(args["stationscode_uic"]) and item and item.claims["P722"]) then
args["stationscode_uic"] = item:getBestStatements('P722')[1].mainsnak.datavalue.value; end
if(not is_empty(args["stationscode_uic"])) then
table.insert(res, label.."'''UIC identificatie'''".." || "..args["stationscode_uic"]); end
--Openingsdatum (NB dit voorbeeld stript maand en dag, moet opgeruimd worden)
if(is_empty(args["opening"]) and item and item.claims["P1619"]) then
--date in Wikidata is stored in format +YYYY-MM-DD... , only year is needed
date_string = item:getBestStatements('P1619')[1].mainsnak.datavalue.value.time;
date_string = mw.text.split(date_string, "+")[2];
date_string = mw.text.split(date_string, "-")[1];
args["opening"] = "[["..date_string.."]]"; end
if(not is_empty(args["opening"])) then
table.insert(res, label.."'''Opening'''".." || "..args["opening"]); end
--Sluitingsdatum (NB dit voorbeeld stript maand en dag, moet opgeruimd worden)
if(is_empty(args["sluiting"]) and item and item.claims["P3999"]) then
--date in Wikidata is stored in format +YYYY-MM-DD... , only year is needed
date_string = item:getBestStatements('P3999')[1].mainsnak.datavalue.value.time;
date_string = mw.text.split(date_string, "+")[2];
date_string = mw.text.split(date_string, "-")[1];
args["sluiting"] = "[["..date_string.."]]"; end
if(not is_empty(args["sluiting"])) then
table.insert(res, label.."'''Sluiting'''".." || "..args["sluiting"]); end
--Perronsporen
if(is_empty(args["perronsporen"]) and item and item.claims["P1103"]) then
args["perronsporen"] = tonumber(item:getBestStatements('P1103')[1].mainsnak.datavalue.value.amount); end
if(not is_empty(args["perronsporen"])) then
table.insert(res, label.."'''Perronsporen'''".." || "..args["perronsporen"]); end
--Spoorlijnen (voorbeeld van mogelijk meerdere items)
if(is_empty(args["lijnen"]) and item and item.claims["P81"]) then
lijnen_lijst = {};
--there can be multiple values of P81, adding each of them to the table
for i = 1,#item.claims["P81"],1 do
table.insert(lijnen_lijst, title_for(item.claims["P81"][i].mainsnak.datavalue.value.id)); end
args["lijnen"] = table.concat(lijnen_lijst, ", <br/>"); --result is a table concatenated with separator being a comma and newline
end
if(not is_empty(args["lijnen"])) then
table.insert(res, label.."'''Lijnen'''".." || "..args["lijnen"]); end
--Aansluitende diensten
if(is_empty(args["aansluitende_dienst"]) and item and item.claims["P1192"]) then
aansluitende_dienst = {};
--there can be multiple values of P1192, adding each of them to the table
for i = 1,#item.claims["P1192"],1 do
table.insert(aansluitende_dienst, title_for(item.claims["P1192"][i].mainsnak.datavalue.value.id)); end
args["aansluitende_dienst"] = table.concat(aansluitende_dienst, ", <br/>"); --result is a table concatenated with separator being a comma and newline
end
if(not is_empty(args["aansluitende_dienst"])) then
table.insert(res, label.."'''Aansluiting'''".." || "..args["aansluitende_dienst"]); end
--Exploitant (voorbeeld van mogelijk meerdere items)
if(is_empty(args["exploitant"]) and item and item.claims["P137"]) then
exploitant_lijst = {};
--there can be multiple values of P137, adding each of them to the table
for i = 1,#item.claims["P137"],1 do
table.insert(exploitant_lijst, title_for(item.claims["P137"][i].mainsnak.datavalue.value.id)); end
args["exploitant"] = table.concat(exploitant_lijst, ", <br/>"); --result is a table concatenated with separator being a comma and newline
end
if(not is_empty(args["exploitant"])) then
table.insert(res, label.."'''Exploitant'''".." || "..args["exploitant"]); end
--Architect (mogelijk meerdere items)
if(is_empty(args["architect"]) and item and item.claims["P84"]) then
architect_lijst = {};
--there can be multiple values of P84, adding each of them to the table
for i = 1,#item.claims["P84"],1 do
table.insert(exploitant_lijst, title_for(item.claims["P84"][i].mainsnak.datavalue.value.id)); end
args["architect"] = table.concat(architect_lijst, ", <br/>"); --result is a table concatenated with separator being a comma and newline
end
if(not is_empty(args["architect"])) then
table.insert(res, label.."'''Architect'''".." || "..args["architect"]); end
--Links
if(is_empty(args["beschreven_op"]) and item and item.claims["P973"]) then
url_list = {};
--there can be multiple values of P973, adding each of them to the table
for i = 1,#item.claims["P973"],1 do
link = item.claims["P973"][i].mainsnak.datavalue.value;
--link text will be a part of url between http:// and first /
link_text = mw.text.split(mw.text.split(link, "//")[2], "/")[1];
table.insert(url_list, "["..link.." "..link_text.."]"); end
args["beschreven_op"] = table.concat(url_list, "<br/>");
end
if(not is_empty(args["beschreven_op"])) then
table.insert(res, label.."'''Beschreven op'''"..
" || <small>"..args["beschreven_op"].."</small>"); end
--Commonscategorie, P373
if(is_empty(args["commonscategorie"]) and item and item.claims["P373"]) then
args["commonscategorie"] = item:getBestStatements('P373')[1].mainsnak.datavalue.value; end
if(not is_empty(args["commonscategorie"])) then
table.insert(res, label.."'''Commonscategorie'''".." || [[Commons:Category:"..args["commonscategorie"].."|"..args["commonscategorie"].."]]"); end
--Coordinates and map
if((is_empty(args["breedtegraad"]) or is_empty(args["lengtegraad"])) and
item and item.claims["P625"]) then
--if arguments "breedtegraad" and "lengtegraad" not set, take value from Wikidata
coord = item:getBestStatements('P625')[1].mainsnak.datavalue.value;
args["breedtegraad"] = coord.latitude;
args["lengtegraad"] = coord.longitude;
end
--if both latitude and longtitude are set, add an OSM-map
if( (not is_empty(args["breedtegraad"])) and (not is_empty(args["lengtegraad"])) ) then
if(is_empty(args["mapframe_breedte"])) then
args["mapframe_breedte"] = mw.text.split(args["afbeeldingbreedte"], "px")[1];
else args["mapframe_breedte"] = mw.text.split(args["mapframe_breedte"], "px")[1]; end
table.insert(res, '\n|-\n| align="center" colspan="2" | '..
'<mapframe width='..args["mapframe_breedte"]..' height='..args["mapframe_breedte"]..
' latitude='..args["breedtegraad"]..' longitude='..args["lengtegraad"]..
' zoom=11 frameless align="center">'..
'{"type": "Feature","geometry": { "type": "Point", "coordinates": ['..
args["lengtegraad"]..','..args["breedtegraad"]..
'] },"properties": {"title": "'..args["naam"]..'"'..
',"marker-symbol": "rail","marker-size": "medium","marker-color": "C0C"'..
'}}</mapframe>');
end
return frame:preprocess(table.concat(res)..'\n|}'); --the final result id the concatenated result table
end
return p;