Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(widget): improve building part 4 - Html[] into string #4717

Merged
merged 7 commits into from
Sep 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 1 addition & 5 deletions components/infobox/commons/infobox.lua
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,7 @@ function Infobox:build(widgets)
error('Infobox:build can only accept Widgets')
end

local contentItems = WidgetFactory.work(widget, self.injector)

for _, node in ipairs(contentItems or {}) do
self.content:node(node)
end
self.content:node(WidgetFactory.work(widget, self.injector))
end

self.root:node(self.content)
Expand Down
4 changes: 1 addition & 3 deletions components/prize_pool/commons/prize_pool_base.lua
Original file line number Diff line number Diff line change
Expand Up @@ -608,9 +608,7 @@ function BasePrizePool:_buildTable(isAward)
end

local tableNode = mw.html.create('div'):css('overflow-x', 'auto')
for _, node in ipairs(WidgetFactory.work(tbl, self._widgetInjector)) do
tableNode:node(node)
end
tableNode:node(WidgetFactory.work(tbl, self._widgetInjector))

return tableNode
end
Expand Down
7 changes: 2 additions & 5 deletions components/squad/commons/squad.lua
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
local Arguments = require('Module:Arguments')
local Array = require('Module:Array')
local Class = require('Module:Class')
local FnUtil = require('Module:FnUtil')
local Logic = require('Module:Logic')
local Lua = require('Module:Lua')
local String = require('Module:StringUtils')
Expand Down Expand Up @@ -102,16 +101,14 @@ function Squad:row(row)
return self
end

---@return Html
---@return string
function Squad:create()
local dataTable = Widget.TableNew{
css = {['margin-bottom'] = '10px'},
classes = {'wikitable-striped', 'roster-card'},
children = self.rows,
}
local wrapper = mw.html.create()
Array.forEach(WidgetFactory.work(dataTable, self.injector), FnUtil.curry(wrapper.node, wrapper))
return wrapper
return WidgetFactory.work(dataTable, self.injector)
end

return Squad
4 changes: 2 additions & 2 deletions components/squad/commons/squad_utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ end
---@param squadClass Squad
---@param personFunction fun(player: table, squadType: integer):WidgetTableRowNew
---@param injector WidgetInjector?
---@return Html
---@return string
function SquadUtils.defaultRunManual(frame, squadClass, personFunction, injector)
local args = Arguments.getArgs(frame)
local injectorInstance = (injector and injector()) or
Expand Down Expand Up @@ -210,7 +210,7 @@ end
---@param customTitle string?
---@param injector? WidgetInjector
---@param personMapper? fun(person: table): table
---@return Html?
---@return string?
function SquadUtils.defaultRunAuto(players, squadType, squadClass, rowCreator, customTitle, injector, personMapper)
local args = {type = squadType, title = customTitle}
local injectorInstance = (injector and injector()) or
Expand Down
2 changes: 1 addition & 1 deletion components/squad/wikis/dota2/squad_custom.lua
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ function ExtendedSquadRow:activeteam()
end

---@param frame Frame
---@return Html
---@return string
function CustomSquad.run(frame)
return SquadUtils.defaultRunManual(frame, Squad, CustomSquad._playerRow, CustomInjector)
end
Expand Down
4 changes: 2 additions & 2 deletions components/squad/wikis/overwatch/squad_custom.lua
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ function ExtendedSquadRow:number()
end

---@param frame Frame
---@return Html
---@return string
function CustomSquad.run(frame)
local args = Arguments.getArgs(frame)
local squad = Squad(args, CustomInjector()):title()
Expand All @@ -69,7 +69,7 @@ end
---@param playerList table[]
---@param squadType integer
---@param customTitle string?
---@return Html?
---@return string?
function CustomSquad.runAuto(playerList, squadType, customTitle)
return SquadUtils.defaultRunAuto(playerList, squadType, Squad, SquadUtils.defaultRow(SquadRow), customTitle)
end
Expand Down
2 changes: 1 addition & 1 deletion components/squad/wikis/smash/squad_custom.lua
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ function ExtendedSquadRow:mains()
end

---@param frame Frame
---@return Html
---@return string
function CustomSquad.run(frame)
local args = Arguments.getArgs(frame)
local squad = Squad(args, CustomInjector()):title():header()
Expand Down
2 changes: 1 addition & 1 deletion components/squad/wikis/starcraft/squad_custom.lua
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ function ExtendedSquadRow:elo()
end

---@param frame Frame
---@return Html
---@return string
function CustomSquad.run(frame)
local args = Arguments.getArgs(frame)
local tlpd = Logic.readBool(args.tlpd)
Expand Down
4 changes: 2 additions & 2 deletions components/squad/wikis/starcraft2/squad_custom.lua
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@ local SquadUtils = Lua.import('Module:Squad/Utils')
local CustomSquad = {}

---@param frame Frame
---@return Html
---@return string
function CustomSquad.run(frame)
return SquadUtils.defaultRunManual(frame, Squad, CustomSquad._playerRow)
end

---@param playerList table[]
---@param squadType integer
---@param customTitle string?
---@return Html?
---@return string?
function CustomSquad.runAuto(playerList, squadType, customTitle)
return SquadUtils.defaultRunAuto(
playerList,
Expand Down
4 changes: 2 additions & 2 deletions components/squad/wikis/stormgate/squad_custom.lua
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@ local SquadUtils = Lua.import('Module:Squad/Utils')
local CustomSquad = {}

---@param frame Frame
---@return Html
---@return string
function CustomSquad.run(frame)
return SquadUtils.defaultRunManual(frame, Squad, CustomSquad._playerRow)
end

---@param playerList table[]
---@param squadType integer
---@param customTitle string?
---@return Html?
---@return string?
function CustomSquad.runAuto(playerList, squadType, customTitle)
return SquadUtils.defaultRunAuto(
playerList,
Expand Down
4 changes: 2 additions & 2 deletions components/widget/widget.lua
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@ function Widget:assertExistsAndCopy(value)
end

---@param injector WidgetInjector?
---@return Widget[]|Html[]|nil
---@return Widget[]|string|nil
function Widget:make(injector)
error('A Widget must override the make() function!')
end

---@param injector WidgetInjector?
---@return Widget[]|Html[]|nil
---@return Widget[]|string|nil
function Widget:tryMake(injector)
return Logic.tryOrElseLog(
function() return self:make(injector) end,
Expand Down
8 changes: 4 additions & 4 deletions components/widget/widget_breakdown.lua
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,15 @@ local Breakdown = Class.new(
)

---@param injector WidgetInjector?
---@return {[1]: Html?}
---@return string?
function Breakdown:make(injector)
return {Breakdown:_breakdown(self.contents, self.classes, self.contentClasses)}
return Breakdown:_breakdown(self.contents, self.classes, self.contentClasses)
end

---@param contents (string|number)[]
---@param classes string[]
---@param contentClasses table<integer, string[]> --can have gaps in the outer table
---@return Html?
---@return string?
function Breakdown:_breakdown(contents, classes, contentClasses)
if type(contents) ~= 'table' or contents == {} then
return nil
Expand All @@ -54,7 +54,7 @@ function Breakdown:_breakdown(contents, classes, contentClasses)
div:node(infoboxCustomCell)
end

return div
return tostring(div)
end

return Breakdown
9 changes: 4 additions & 5 deletions components/widget/widget_builder.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
-- Please see https://github.com/Liquipedia/Lua-Modules to contribute
--

local Array = require('Module:Array')
local Class = require('Module:Class')
local Lua = require('Module:Lua')

Expand All @@ -24,14 +23,14 @@ local Builder = Class.new(
)

---@param injector WidgetInjector?
---@return Widget[]
---@return string
function Builder:make(injector)
local children = self.builder()
local widgets = {}
local builtChildren = mw.html.create()
for _, child in ipairs(children or {}) do
Array.extendWith(widgets, WidgetFactory.work(child, injector))
builtChildren:node(WidgetFactory.work(child, injector))
end
return widgets
return tostring(builtChildren)
end

return Builder
8 changes: 3 additions & 5 deletions components/widget/widget_cell.lua
Original file line number Diff line number Diff line change
Expand Up @@ -90,21 +90,19 @@ function Cell:_content(...)
end

---@param injector WidgetInjector?
---@return {[1]: Html?}
---@return string?
function Cell:make(injector)
self:_new(self.name)
self:_class(unpack(self.classes or {}))
self:_content(unpack(self.content))

if self.contentDiv == nil then
return {}
return
end

self.root :node(self.description)
:node(self.contentDiv)
return {
self.root
}
return tostring(self.root)
end

return Cell
8 changes: 4 additions & 4 deletions components/widget/widget_center.lua
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,14 @@ local Center = Class.new(
)

---@param injector WidgetInjector?
---@return {[1]: Html?}
---@return string?
function Center:make(injector)
return {Center:_create(self.content, self.classes)}
return Center:_create(self.content, self.classes)
end

---@param content (string|number)[]
---@param classes string[]
---@return Html?
---@return string?
function Center:_create(content, classes)
if Table.isEmpty(content) then
return nil
Expand All @@ -47,7 +47,7 @@ function Center:_create(content, classes)
centered:wikitext(item)
end

return mw.html.create('div'):node(centered)
return tostring(mw.html.create('div'):node(centered))
end

return Center
14 changes: 7 additions & 7 deletions components/widget/widget_chronology.lua
Original file line number Diff line number Diff line change
Expand Up @@ -23,33 +23,33 @@ local Chronology = Class.new(
)

---@param injector WidgetInjector?
---@return Html[]
---@return string?
function Chronology:make(injector)
return Chronology:_chronology(self.links)
end

---@param links table<string, string|number|nil>
---@return Html[]
---@return string?
function Chronology:_chronology(links)
if links == nil or Table.size(links) == 0 then
return self
return
end

local chronologyContent = {}
chronologyContent[1] = self:_createChronologyRow(links['previous'], links['next'])
local chronologyContent = mw.html.create()
chronologyContent:node(self:_createChronologyRow(links['previous'], links['next']))

local index = 2
local previous = links['previous' .. index]
local next = links['next' .. index]
while (previous ~= nil or next ~= nil) do
chronologyContent[index] = self:_createChronologyRow(previous, next)
chronologyContent:node(self:_createChronologyRow(previous, next))

index = index + 1
previous = links['previous' .. index]
next = links['next' .. index]
end

return chronologyContent
return tostring(chronologyContent)
end

---@param previous string|number|nil
Expand Down
25 changes: 9 additions & 16 deletions components/widget/widget_factory.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,34 +8,27 @@

local Array = require('Module:Array')
local Class = require('Module:Class')
local Lua = require('Module:Lua')

local Widget = Lua.import('Module:Widget')

---@class WidgetFactory
local WidgetFactory = Class.new()

---@param widget Widget
---@param injector WidgetInjector?
---@return Html[]
---@return string
function WidgetFactory.work(widget, injector)
local convertedWidgets = {} ---@type Html[]
local children = widget:tryMake(injector)

if widget == nil then
return {}
if not children then
return ''
end

for _, child in ipairs(widget:tryMake(injector) or {}) do
if type(child) == 'table' and type(child['is_a']) == 'function' and child:is_a(Widget) then
---@cast child Widget
Array.extendWith(convertedWidgets, WidgetFactory.work(child, injector))
else
---@cast child Html
table.insert(convertedWidgets, child)
end
if type(children) == 'string' then
return children
end

return convertedWidgets
return table.concat(Array.map(children, function(child)
return WidgetFactory.work(child, injector)
end))
end

return WidgetFactory
8 changes: 6 additions & 2 deletions components/widget/widget_header.lua
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ local Header = Class.new(
)

---@param injector WidgetInjector?
---@return Html[]
---@return string
function Header:make(injector)
local header = {
Header:_name(self.name),
Expand All @@ -59,7 +59,11 @@ function Header:make(injector)
table.insert(header, 2, subHeader)
end

return header
local wrapper = mw.html.create()
for _, element in ipairs(header) do
wrapper:node(element)
end
return tostring(wrapper)
end

---@param name string?
Expand Down
Loading
Loading