From d4d3b81939013345a3f0c520e7616104c48f45cf Mon Sep 17 00:00:00 2001 From: Rikard Blixt Date: Mon, 16 Sep 2024 10:15:02 +0200 Subject: [PATCH] refactor(widget): improve building part 4 - `Html[]` into `string` (#4717) * refactor(widget): improve building part 4 - `Html[]` into `Html` * reorder * update usages * unused code * change from `Html` to `string` * fix annos * ensure we don't tostring nils --- components/infobox/commons/infobox.lua | 6 +---- .../prize_pool/commons/prize_pool_base.lua | 4 +-- components/squad/commons/squad.lua | 7 ++---- components/squad/commons/squad_utils.lua | 4 +-- components/squad/wikis/dota2/squad_custom.lua | 2 +- .../squad/wikis/overwatch/squad_custom.lua | 4 +-- components/squad/wikis/smash/squad_custom.lua | 2 +- .../squad/wikis/starcraft/squad_custom.lua | 2 +- .../squad/wikis/starcraft2/squad_custom.lua | 4 +-- .../squad/wikis/stormgate/squad_custom.lua | 4 +-- components/widget/widget.lua | 4 +-- components/widget/widget_breakdown.lua | 8 +++--- components/widget/widget_builder.lua | 9 +++---- components/widget/widget_cell.lua | 8 +++--- components/widget/widget_center.lua | 8 +++--- components/widget/widget_chronology.lua | 14 +++++------ components/widget/widget_factory.lua | 25 +++++++------------ components/widget/widget_header.lua | 8 ++++-- components/widget/widget_highlights.lua | 6 ++--- components/widget/widget_links.lua | 6 ++--- components/widget/widget_table.lua | 8 +++--- components/widget/widget_table_cell.lua | 4 +-- components/widget/widget_table_cell_new.lua | 4 +-- components/widget/widget_table_new.lua | 6 ++--- components/widget/widget_table_row.lua | 8 +++--- components/widget/widget_table_row_new.lua | 6 ++--- components/widget/widget_title.lua | 8 +++--- 27 files changed, 79 insertions(+), 100 deletions(-) diff --git a/components/infobox/commons/infobox.lua b/components/infobox/commons/infobox.lua index dfe0da3fb6d..95bfe45fc15 100644 --- a/components/infobox/commons/infobox.lua +++ b/components/infobox/commons/infobox.lua @@ -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) diff --git a/components/prize_pool/commons/prize_pool_base.lua b/components/prize_pool/commons/prize_pool_base.lua index c9e9643b7e0..7e930d1efe7 100644 --- a/components/prize_pool/commons/prize_pool_base.lua +++ b/components/prize_pool/commons/prize_pool_base.lua @@ -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 diff --git a/components/squad/commons/squad.lua b/components/squad/commons/squad.lua index c0229610aa3..a52215bc08d 100644 --- a/components/squad/commons/squad.lua +++ b/components/squad/commons/squad.lua @@ -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') @@ -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 diff --git a/components/squad/commons/squad_utils.lua b/components/squad/commons/squad_utils.lua index 2f18e6a085e..dd690f5012f 100644 --- a/components/squad/commons/squad_utils.lua +++ b/components/squad/commons/squad_utils.lua @@ -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 @@ -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 diff --git a/components/squad/wikis/dota2/squad_custom.lua b/components/squad/wikis/dota2/squad_custom.lua index c20db55a4d6..2cb2554f6bf 100644 --- a/components/squad/wikis/dota2/squad_custom.lua +++ b/components/squad/wikis/dota2/squad_custom.lua @@ -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 diff --git a/components/squad/wikis/overwatch/squad_custom.lua b/components/squad/wikis/overwatch/squad_custom.lua index 1ba4ba70881..2fea3de24c4 100644 --- a/components/squad/wikis/overwatch/squad_custom.lua +++ b/components/squad/wikis/overwatch/squad_custom.lua @@ -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() @@ -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 diff --git a/components/squad/wikis/smash/squad_custom.lua b/components/squad/wikis/smash/squad_custom.lua index 4c587b89fc8..552c59d4e7d 100644 --- a/components/squad/wikis/smash/squad_custom.lua +++ b/components/squad/wikis/smash/squad_custom.lua @@ -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() diff --git a/components/squad/wikis/starcraft/squad_custom.lua b/components/squad/wikis/starcraft/squad_custom.lua index 596488a7182..aa7d2e33e4b 100644 --- a/components/squad/wikis/starcraft/squad_custom.lua +++ b/components/squad/wikis/starcraft/squad_custom.lua @@ -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) diff --git a/components/squad/wikis/starcraft2/squad_custom.lua b/components/squad/wikis/starcraft2/squad_custom.lua index 7659410be8e..048db1bcab1 100644 --- a/components/squad/wikis/starcraft2/squad_custom.lua +++ b/components/squad/wikis/starcraft2/squad_custom.lua @@ -18,7 +18,7 @@ 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 @@ -26,7 +26,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, diff --git a/components/squad/wikis/stormgate/squad_custom.lua b/components/squad/wikis/stormgate/squad_custom.lua index e0de2461311..44337dfe8ef 100644 --- a/components/squad/wikis/stormgate/squad_custom.lua +++ b/components/squad/wikis/stormgate/squad_custom.lua @@ -17,7 +17,7 @@ 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 @@ -25,7 +25,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, diff --git a/components/widget/widget.lua b/components/widget/widget.lua index e6f35872d54..146f41315dc 100644 --- a/components/widget/widget.lua +++ b/components/widget/widget.lua @@ -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, diff --git a/components/widget/widget_breakdown.lua b/components/widget/widget_breakdown.lua index 15623203c0e..019ed3baff2 100644 --- a/components/widget/widget_breakdown.lua +++ b/components/widget/widget_breakdown.lua @@ -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 --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 @@ -54,7 +54,7 @@ function Breakdown:_breakdown(contents, classes, contentClasses) div:node(infoboxCustomCell) end - return div + return tostring(div) end return Breakdown diff --git a/components/widget/widget_builder.lua b/components/widget/widget_builder.lua index 767f3ff6212..0fb0f8d8fa2 100644 --- a/components/widget/widget_builder.lua +++ b/components/widget/widget_builder.lua @@ -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') @@ -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 diff --git a/components/widget/widget_cell.lua b/components/widget/widget_cell.lua index 36e7e6d651c..f598100bf79 100644 --- a/components/widget/widget_cell.lua +++ b/components/widget/widget_cell.lua @@ -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 diff --git a/components/widget/widget_center.lua b/components/widget/widget_center.lua index 3122a9a9f12..42551a99ffa 100644 --- a/components/widget/widget_center.lua +++ b/components/widget/widget_center.lua @@ -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 @@ -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 diff --git a/components/widget/widget_chronology.lua b/components/widget/widget_chronology.lua index e3d50e0cf0e..e7236007fd1 100644 --- a/components/widget/widget_chronology.lua +++ b/components/widget/widget_chronology.lua @@ -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 ----@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 diff --git a/components/widget/widget_factory.lua b/components/widget/widget_factory.lua index 18d4312debc..bf5074b64dc 100644 --- a/components/widget/widget_factory.lua +++ b/components/widget/widget_factory.lua @@ -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 diff --git a/components/widget/widget_header.lua b/components/widget/widget_header.lua index a232ea4f105..4a0dfb73432 100644 --- a/components/widget/widget_header.lua +++ b/components/widget/widget_header.lua @@ -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), @@ -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? diff --git a/components/widget/widget_highlights.lua b/components/widget/widget_highlights.lua index 3d431c56862..152b63e2f58 100644 --- a/components/widget/widget_highlights.lua +++ b/components/widget/widget_highlights.lua @@ -23,13 +23,13 @@ local Highlights = Class.new( ) ---@param injector WidgetInjector? ----@return {[1]: Html}? +---@return string? function Highlights:make(injector) return Highlights:_highlights(self.list) end ---@param list (string|number)[]? ----@return Html? +---@return string? function Highlights:_highlights(list) if list == nil or Table.size(list) == 0 then return nil @@ -44,7 +44,7 @@ function Highlights:_highlights(list) div:node(highlights) - return {div} + return tostring(div) end return Highlights diff --git a/components/widget/widget_links.lua b/components/widget/widget_links.lua index ba2a1157c9f..80cb8f14a93 100644 --- a/components/widget/widget_links.lua +++ b/components/widget/widget_links.lua @@ -28,7 +28,7 @@ local Links = Class.new( local PRIORITY_GROUPS = Lua.import('Module:Links/PriorityGroups', {loadData = true}) ---@param injector WidgetInjector? ----@return {[1]: Html} +---@return string? function Links:make(injector) local infoboxLinks = mw.html.create('div') infoboxLinks :addClass('infobox-center') @@ -56,9 +56,7 @@ function Links:make(injector) infoboxLinks:wikitext(' ' .. self:_makeLink(key, value)) end - return { - mw.html.create('div'):node(infoboxLinks) - } + return tostring(mw.html.create('div'):node(infoboxLinks)) end ---@param key string diff --git a/components/widget/widget_table.lua b/components/widget/widget_table.lua index d5c6dc3783c..832d2b50b2a 100644 --- a/components/widget/widget_table.lua +++ b/components/widget/widget_table.lua @@ -50,7 +50,7 @@ function Table:addClass(class) end ---@param injector WidgetInjector? ----@return {[1]: Html} +---@return string? function Table:make(injector) local displayTable = mw.html.create('div'):addClass('csstable-widget') displayTable:css{ @@ -64,12 +64,10 @@ function Table:make(injector) displayTable:css(self.css) for _, row in ipairs(self.rows) do - for _, node in ipairs(WidgetFactory.work(row, injector)) do - displayTable:node(node) - end + displayTable:node(WidgetFactory.work(row, injector)) end - return {displayTable} + return tostring(displayTable) end ---@return integer? diff --git a/components/widget/widget_table_cell.lua b/components/widget/widget_table_cell.lua index 1b81ffe0c60..dcec7ccbc9a 100644 --- a/components/widget/widget_table_cell.lua +++ b/components/widget/widget_table_cell.lua @@ -57,7 +57,7 @@ function TableCell:addCss(key, value) end ---@param injector WidgetInjector? ----@return {[1]: Html} +---@return string? function TableCell:make(injector) local cell = mw.html.create('div'):addClass('csstable-widget-cell') cell:css{ @@ -73,7 +73,7 @@ function TableCell:make(injector) cell:node(self:_concatContent()) - return {cell} + return tostring(cell) end ---@return string diff --git a/components/widget/widget_table_cell_new.lua b/components/widget/widget_table_cell_new.lua index 8ac97cb1ea5..809dbac43e1 100644 --- a/components/widget/widget_table_cell_new.lua +++ b/components/widget/widget_table_cell_new.lua @@ -43,7 +43,7 @@ local TableCell = Class.new( ) ---@param injector WidgetInjector? ----@return {[1]: Html} +---@return string? function TableCell:make(injector) local cell = mw.html.create(self.isHeader and 'th' or 'td') cell:attr('colspan', self.colSpan) @@ -55,7 +55,7 @@ function TableCell:make(injector) cell:node(self:_content()) - return {cell} + return tostring(cell) end ---@return string diff --git a/components/widget/widget_table_new.lua b/components/widget/widget_table_new.lua index 631643d0f4a..3e8bca8e6c8 100644 --- a/components/widget/widget_table_new.lua +++ b/components/widget/widget_table_new.lua @@ -34,7 +34,7 @@ local Table = Class.new( ) ---@param injector WidgetInjector? ----@return {[1]: Html} +---@return string? function Table:make(injector) local wrapper = mw.html.create('div'):addClass('table-responsive') local output = mw.html.create('table'):addClass('wikitable') @@ -44,11 +44,11 @@ function Table:make(injector) output:css(self.css) Array.forEach(self.children, function(child) - Array.forEach(WidgetFactory.work(child, injector), FnUtil.curry(output.node, output)) + output:node(WidgetFactory.work(child, injector)) end) wrapper:node(output) - return {wrapper} + return tostring(wrapper) end return Table diff --git a/components/widget/widget_table_row.lua b/components/widget/widget_table_row.lua index 24486f59bf1..fc194699d53 100644 --- a/components/widget/widget_table_row.lua +++ b/components/widget/widget_table_row.lua @@ -59,7 +59,7 @@ function TableRow:getCellCount() end ---@param injector WidgetInjector? ----@return {[1]: Html} +---@return string? function TableRow:make(injector) local row = mw.html.create('div'):addClass('csstable-widget-row') @@ -70,12 +70,10 @@ function TableRow:make(injector) row:css(self.css) for _, cell in ipairs(self.cells) do - for _, node in ipairs(WidgetFactory.work(cell, injector)) do - row:node(node) - end + row:node(WidgetFactory.work(cell, injector)) end - return {row} + return tostring(row) end return TableRow diff --git a/components/widget/widget_table_row_new.lua b/components/widget/widget_table_row_new.lua index ca022a80aa9..e8f015d9e38 100644 --- a/components/widget/widget_table_row_new.lua +++ b/components/widget/widget_table_row_new.lua @@ -34,7 +34,7 @@ local TableRow = Class.new( ) ---@param injector WidgetInjector? ----@return {[1]: Html} +---@return string? function TableRow:make(injector) local row = mw.html.create('tr') @@ -43,10 +43,10 @@ function TableRow:make(injector) row:css(self.css) Array.forEach(self.children, function(child) - Array.forEach(WidgetFactory.work(child, injector), FnUtil.curry(row.node, row)) + row:node(WidgetFactory.work(child, injector)) end) - return {row} + return tostring(row) end return TableRow diff --git a/components/widget/widget_title.lua b/components/widget/widget_title.lua index 06ca2ef6287..62b4c9320cd 100644 --- a/components/widget/widget_title.lua +++ b/components/widget/widget_title.lua @@ -22,20 +22,20 @@ local Title = Class.new( ) ---@param injector WidgetInjector? ----@return {[1]: Html} +---@return string? function Title:make(injector) - return {Title:_create(self.content)} + return Title:_create(self.content) end ---@param infoDescription string|number|nil ----@return Html +---@return string function Title:_create(infoDescription) local header = mw.html.create('div') header :addClass('infobox-header') :addClass('wiki-backgroundcolor-light') :addClass('infobox-header-2') :wikitext(infoDescription) - return mw.html.create('div'):node(header) + return tostring(mw.html.create('div'):node(header)) end return Title