Skip to content

Commit

Permalink
refactor(teamcard): move qualifier text parsing from external module (#…
Browse files Browse the repository at this point in the history
…4170)

* refactor(teamcard): move qualifier text parsing from external module

* forgot to stage the tests
  • Loading branch information
Rathoz authored Apr 15, 2024
1 parent 5f83f60 commit e1301a3
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 3 deletions.
44 changes: 41 additions & 3 deletions components/team_card/team_card_storage.lua
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ local Table = require('Module:Table')
local Variables = require('Module:Variables')

local Custom = Lua.import('Module:TeamCard/Custom')
-- TODO: Once the Template calls are not needed (when RL has been moved to Module), deprecate Qualifier Module
local Qualifier = require('Module:TeamCard/Qualifier')

local OpponentLibrary = require('Module:OpponentLibraries')
local Opponent = OpponentLibrary.Opponent
Expand Down Expand Up @@ -89,7 +87,7 @@ function TeamCardStorage._addStandardLpdbFields(lpdbData, team, args, lpdbPrefix
lpdbData.date = args.date
or Variables.varDefault(lpdbData.objectName .. '_placementdate')
or endDate
lpdbData.qualifier, lpdbData.qualifierpage, lpdbData.qualifierurl = Qualifier.parseQualifier(args.qualifier)
lpdbData.qualifier, lpdbData.qualifierpage, lpdbData.qualifierurl = TeamCardStorage._parseQualifier(args.qualifier)

if team ~= 'TBD' then
lpdbData.image = args.image1
Expand Down Expand Up @@ -126,4 +124,44 @@ function TeamCardStorage._getLpdbObjectName(team, lpdbPrefix)
return storageName
end

--- Link internal and link external is mutually exclusive
---@param rawQualifier string?
---@return string? #link text
---@return string? #internal link
---@return string? #external link
function TeamCardStorage._parseQualifier(rawQualifier)
if not rawQualifier then
return nil, nil, nil
end

local cleanQualifier = rawQualifier:gsub('%[', ''):gsub('%]', '')
if cleanQualifier:find('|') then
-- Internal link
local qualifier = mw.text.split(cleanQualifier, '|', true)
local qualifierLink, qualifierText = qualifier[1], qualifier[2]

if qualifierLink:sub(1, 1) == '/' then
-- Relative link
qualifierLink = mw.title.getCurrentTitle().fullText .. qualifierLink
end
qualifierLink = qualifierLink:gsub(' ', '_')
return qualifierText, qualifierLink, nil

elseif rawQualifier:sub(1, 1) == '[' then
-- Not internal link, but a link -> must be external link
local qualifier = mw.text.split(cleanQualifier, ' ', true)
local qualifierLink = qualifier[1]

table.remove(qualifier, 1)
local qualifierText = table.concat(qualifier, ' ')

return qualifierText, nil, qualifierLink

else
-- Just text
return rawQualifier, nil, nil
end
end


return TeamCardStorage
50 changes: 50 additions & 0 deletions spec/team_card_storage_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,54 @@ describe('Team Card Storage', function()
assert.are_equal('participant_tbd_1', TCStorage._getLpdbObjectName('TBD'))
assert.are_equal('participant_tbd_2', TCStorage._getLpdbObjectName('TBD'))
end)

describe('qualifier parsing', function()
it('raw text', function()
local test = 'Foo Bar'
local text, internal, external = TCStorage._parseQualifier(test)
assert.are_equal('Foo Bar', text)
assert.is_nil(internal)
assert.is_nil(external)
end)

it('SimpleInternalLink', function()
local test = '[[Foo_Bar/2022|Foo Bar]]'
local text, internal, external = TCStorage._parseQualifier(test)
assert.are_equal('Foo Bar', text)
assert.are_equal('Foo_Bar/2022', internal)
assert.is_nil(external)
end)

it('RelativeInternalLink', function()
local test = '[[/2022|Foo Bar]]'
local text, internal, external = TCStorage._parseQualifier(test)
assert.are_equal('Foo Bar', text)
assert.are_equal('FakePage/2022', internal)
assert.is_nil(external)
end)

it('FixingSpaceInternalLink', function()
local test = '[[Foo Bar/2022|Foo Bar]]'
local text, internal, external = TCStorage._parseQualifier(test)
assert.are_equal('Foo Bar', text)
assert.are_equal('Foo_Bar/2022', internal)
assert.is_nil(external)
end)

it('ExternalLink', function()
local test = '[https://foo.bar Foo Bar]'
local text, internal, external = TCStorage._parseQualifier(test)
assert.are_equal('Foo Bar', text)
assert.is_nil(internal)
assert.are_equal('https://foo.bar', external)
end)

it('ExternalLinkNoSpace', function()
local test = '[https://foo.bar]'
local text, internal, external = TCStorage._parseQualifier(test)
assert.are_equal('', text)
assert.is_nil(internal)
assert.are_equal('https://foo.bar', external)
end)
end)
end)

0 comments on commit e1301a3

Please sign in to comment.