en:Module:Wd 2017年3月19日 (日) 09:32(UTC)より
template>K-iczn (en:Module:Wd 2017年3月18日 (土) 20:02(UTC)より) |
template>K-iczn (en:Module:Wd 2017年3月19日 (日) 09:32(UTC)より) |
||
112行目: | 112行目: | ||
stt.conf = cfg | stt.conf = cfg | ||
stt. | stt.results = {} | ||
stt.parsedFormat = {} | stt.parsedFormat = {} | ||
517行目: | 515行目: | ||
end | end | ||
function sortOnRank(claims) | function sortOnRank(claims) | ||
local rankPos | local rankPos | ||
local ranks = {{}, {}, {}} -- preferred, normal, deprecated | local ranks = {{}, {}, {}, {}} -- preferred, normal, deprecated, (default) | ||
local sorted = {} | local sorted = {} | ||
1,185行目: | 1,179行目: | ||
return matches, rankPos | return matches, rankPos | ||
end | end | ||
function State:out() | function State:out() | ||
local result -- collection of arrays with value objects | |||
local valuesArray -- array with value objects | |||
local sep = "" | |||
local out = {} -- array with value objects | local out = {} -- array with value objects | ||
1,265行目: | 1,213行目: | ||
end | end | ||
-- iterate through the results from back to front, so that we know when to add separators | |||
for i = #self.results, 1, -1 do | |||
result = self.results[i] | |||
-- if there is already some output, then add the separators | |||
if #out > 0 then | |||
sep = self.separator[1] -- fixed separator | |||
result[parameters.separator] = { {self.movSeparator[1]} } -- movable separator | |||
else | |||
sep = "" | |||
result[parameters.separator] = { {self.puncMark[1]} } -- optional punctuation mark | |||
end | |||
valuesArray = walk(self.parsedFormat, result) | |||
if #valuesArray > 0 then | |||
valuesArray[#valuesArray + 1] = {sep} | |||
out = mergeTables(valuesArray, out) | |||
end | end | ||
end | end | ||
-- reset state before next iteration | -- reset state before next iteration | ||
self. | self.results = {} | ||
return out | return out | ||
1,494行目: | 1,430行目: | ||
matchHook = matchHook or alwaysTrue | matchHook = matchHook or alwaysTrue | ||
local matches = false | local matches = false | ||
local rankPos = nil | local rankPos = nil | ||
local result | local result, gotRequired | ||
for i, v in ipairs(statements) do | for i, v in ipairs(statements) do | ||
-- rankPos will be nil for non-claim statements (e.g. qualifiers, references, etc.) | -- rankPos will be nil for non-claim statements (e.g. qualifiers, references, etc.) | ||
matches, rankPos = matchHook(self, v) | matches, rankPos = matchHook(self, v) | ||
if matches then | if matches then | ||
result = {count = 0} -- collection of arrays with value objects | result = {count = 0} -- collection of arrays with value objects | ||
local function walk(formatTable) | |||
local miss | |||
for i2, v2 in pairs(formatTable.req) do | |||
-- call a hook, adding its return value to the result | |||
miss = self:callHook(i2, hooks, v, result) | |||
if miss then | |||
-- we miss a required value for this level, so return false | |||
return false | |||
end | end | ||
for i2, v2 in ipairs(formatTable) do | if result.count == hooks.count then | ||
-- we're done if all hooks have been called; | |||
-- returning at this point breaks the loop | |||
return true | |||
end | |||
end | |||
for i2, v2 in ipairs(formatTable) do | |||
if result.count == hooks.count then | |||
-- we're done if all hooks have been called; | |||
-- returning at this point prevents further childs from being processed | |||
return true | |||
end | end | ||
if v2.child then | |||
walk(v2.child) | |||
end | |||
end | end | ||
-- only append the result if we got values for all required parameters on the root level | return true | ||
if | end | ||
gotRequired = walk(self.parsedFormat) | |||
-- only append the result if we got values for all required parameters on the root level | |||
if gotRequired then | |||
-- if we have a rankPos (only with matchHook() for complete claims), then update the foundRank | |||
if rankPos and self.conf.foundRank > rankPos then | |||
self.conf.foundRank = rankPos | |||
end | |||
-- append the result | |||
self.results[#self.results + 1] = result | |||
-- break if we only need a single value | |||
if self.singleValue then | |||
break | |||
end | end | ||
end | end | ||
1,776行目: | 1,700行目: | ||
if _.entity and _.entity.claims then claims = _.entity.claims[_.propertyID] end | if _.entity and _.entity.claims then claims = _.entity.claims[_.propertyID] end | ||
if claims then | if claims then | ||
-- first sort the claims on | -- first sort the claims on rank to pre-define the order of output (preferred first, then normal, then deprecated) | ||
claims = sortOnRank(claims) | claims = sortOnRank(claims) | ||