יחידה:תבנית מידע: הבדלים בין גרסאות בדף
מאין תקציר עריכה |
מאין תקציר עריכה |
||
| שורה 1: | שורה 1: | ||
local propertyLink = require('Module:PropertyLink') | local propertyLink = require('Module:PropertyLink') | ||
local | local Infobox = {} | ||
local args={} | function Infobox:new() | ||
local o = { | |||
args={}, | |||
templateStructure={}, | |||
isSelfUse=false, | |||
autoDocumentation=false, | |||
wikidataCats= {}, | |||
root = mw.html.create('table') | |||
} | |||
setmetatable(o, self) | |||
self.__index = self | |||
return o | |||
end | |||
function Infobox:render() | |||
if self.isSelfUse then | |||
local templateDocumentation='' | |||
for k,v in pairs(self.templateParams) do | |||
if mw.ustring.match( k, '-ויקינתונים') then | |||
templateDocumentation = templateDocumentation .. '[[קטגוריה:תבניות שמשתמשות בוויקינתונים]]' | |||
break | |||
end | |||
end | |||
if self.autoDocumentation then | |||
templateDocumentation = templateDocumentation .. self:_generateDocumentation() | |||
end | |||
return self:renderInfobox() .. templateDocumentation | |||
end | |||
return self:renderInfobox() .. table.concat(self.wikidataCats, '') | |||
end | |||
--[[ | --[[ | ||
Generate auto documentation for template. | Generate auto documentation for template. | ||
]] | ]] | ||
function Infobox:_generateDocumentation() | |||
local labelDataPrefix = 'תווית-מידע' | local labelDataPrefix = 'תווית-מידע' | ||
local parameters = {} | local parameters = {} | ||
for k,v in pairs(templateParams) do | for k,v in pairs(self.templateParams) do | ||
if mw.ustring.match( tostring(k),'תווית%-מידע%d' ) then | if mw.ustring.match( tostring(k),'תווית%-מידע%d' ) then | ||
table.insert(parameters, '"'..string.gsub(v, '"', '\\"')..'":'..'{ "description": ""}') | table.insert(parameters, '"'..string.gsub(v, '"', '\\"')..'":'..'{ "description": ""}') | ||
| שורה 29: | שורה 58: | ||
Renders the infobox title | Renders the infobox title | ||
]] | ]] | ||
function Infobox:renderTitle() | |||
-- renders the title | -- renders the title | ||
local titleCaption = | local titleCaption = self.root | ||
:tag('caption') | :tag('caption') | ||
:addClass(args.titleclass) | :addClass(self.args.titleclass) | ||
:cssText(args.titlestyle) | :cssText(self.args.titlestyle) | ||
:wikitext(args.title) | :wikitext(self.args.title) | ||
if args.subtitle and args.subtitle~=nil and #(args.subtitle)>0 then | if self.args.subtitle and self.args.subtitle~=nil and #(self.args.subtitle)>0 then | ||
local row = root:tag('tr') | local row = self.root:tag('tr') | ||
:tag('th') | :tag('th') | ||
:attr('colspan', 2) | :attr('colspan', 2) | ||
:css('text-align','center') | :css('text-align','center') | ||
:cssText(args.subtitlestyle) | :cssText(self.args.subtitlestyle) | ||
:addClass(args.subtitleclass) | :addClass(self.args.subtitleclass) | ||
:wikitext(args.subtitle) | :wikitext(self.args.subtitle) | ||
end | end | ||
return titleCaption | return self.titleCaption | ||
end | end | ||
| שורה 52: | שורה 81: | ||
Renders the right/left images (possibly 1/2/none of them available) and the main image | Renders the right/left images (possibly 1/2/none of them available) and the main image | ||
]] | ]] | ||
function Infobox:renderImages() | |||
-- render right an left images | -- render right an left images | ||
args.rightImage = args.rightImage~=nil and #(args.rightImage)>0 and args.rightImage | self.args.rightImage = self.args.rightImage~=nil and #(self.args.rightImage)>0 and self.args.rightImage | ||
args.leftImage = args.leftImage~=nil and #(args.leftImage)>0 and args.leftImage | self.args.leftImage = self.args.leftImage~=nil and #(self.args.leftImage)>0 and self.args.leftImage | ||
if args.rightImage or args.leftImage then | if self.args.rightImage or self.args.leftImage then | ||
if args.rightImageDesc~=nil and #(args.rightImageDesc)>0 then | if self.args.rightImageDesc~=nil and #(self.args.rightImageDesc)>0 then | ||
self.args.rightImage = self.args.rightImage..'<div>' .. self.args.rightImageDesc .. '</div>' | |||
args.rightImage = args.rightImage..'<div>' .. args.rightImageDesc .. '</div>' | |||
end | end | ||
if args.leftImageDesc~=nil and #(args.leftImageDesc)>0 then | if self.args.leftImageDesc~=nil and #(self.args.leftImageDesc)>0 then | ||
args.leftImage = args.leftImage .. '<div>' .. args.leftImageDesc .. '</div>' | self.args.leftImage = self.args.leftImage .. '<div>' .. self.args.leftImageDesc .. '</div>' | ||
end | end | ||
local row = root:tag('tr') | local row = self.root:tag('tr') | ||
:tag('td') | :tag('td') | ||
:attr('colspan', 2) | :attr('colspan', 2) | ||
:css('text-align','center') | :css('text-align','center') | ||
if args.rightImage and args.leftImage then | if self.args.rightImage and self.args.leftImage then | ||
row:tag('table') | row:tag('table') | ||
:css('width','100%') | :css('width','100%') | ||
| שורה 77: | שורה 105: | ||
:css('vertical-align','middle') | :css('vertical-align','middle') | ||
:css('text-align','center') | :css('text-align','center') | ||
:wikitext(args.rightImage) | :wikitext(self.args.rightImage) | ||
:tag('td') | :tag('td') | ||
:css('width','50%') | :css('width','50%') | ||
:css('vertical-align','middle') | :css('vertical-align','middle') | ||
:css('text-align','center') | :css('text-align','center') | ||
:wikitext(args.leftImage) | :wikitext(self.args.leftImage) | ||
:done() | :done() | ||
else | else | ||
row:wikitext(args.leftImage or args.rightImage) | row:wikitext(self.args.leftImage or self.args.rightImage) | ||
end | end | ||
end | end | ||
-- render the main image | -- render the main image | ||
if args.image then | if self.args.image then | ||
root:tag('tr') | self.root:tag('tr') | ||
:tag('td') | :tag('td') | ||
:attr('colspan', 2) | :attr('colspan', 2) | ||
:css('text-align','center') | :css('text-align','center') | ||
:wikitext(args.image) | :wikitext(self.args.image) | ||
if args.imageDesc and #args.imageDesc>0 then | if self.args.imageDesc and #self.args.imageDesc>0 then | ||
root:tag('tr') | self.root:tag('tr') | ||
:tag('td') | :tag('td') | ||
:attr('colspan', 2) | :attr('colspan', 2) | ||
| שורה 102: | שורה 130: | ||
:addClass('borderless') | :addClass('borderless') | ||
:css('text-align','center') | :css('text-align','center') | ||
:wikitext(args.imageDesc) | :wikitext(self.args.imageDesc) | ||
end | end | ||
| שורה 111: | שורה 139: | ||
Adds edit links for easier editing of the template. If the template use data from wikidata it also adds edit link in Wikidata | Adds edit links for easier editing of the template. If the template use data from wikidata it also adds edit link in Wikidata | ||
]] | ]] | ||
function Infobox:renderEditLinks() | |||
local wbEdit='' -- link to wikidata | local wbEdit='' -- link to wikidata | ||
if args.usingWikidata then | if self.args.usingWikidata then | ||
local entity = mw.wikibase.getEntityObject() | local entity = mw.wikibase.getEntityObject() | ||
wbEdit = '[[File:Wikidata-logo S.svg|20px|link=d:'..entity.id..'|לעריכה בוויקינתונים שמשמש מקור לחלק מהמידע בתבנית]]' | wbEdit = '[[File:Wikidata-logo S.svg|20px|link=d:'..entity.id..'|לעריכה בוויקינתונים שמשמש מקור לחלק מהמידע בתבנית]]' | ||
end | end | ||
if #wbEdit > 0 then | if #wbEdit > 0 then | ||
root:tag('tr') | self.root:tag('tr') | ||
:tag('td') | :tag('td') | ||
:attr('colspan', 2) | :attr('colspan', 2) | ||
| שורה 129: | שורה 157: | ||
Adds a styled row to the table | Adds a styled row to the table | ||
]] | ]] | ||
function Infobox:addRow(rowArgs) | |||
-- Adds a row to the infobox, with either a header cell | -- Adds a row to the infobox, with either a header cell | ||
-- or a label/data cell combination. | -- or a label/data cell combination. | ||
if rowArgs.header then | if rowArgs.header then | ||
root | self.root | ||
:tag('tr') | :tag('tr') | ||
:addClass(rowArgs.rowclass) | :addClass(rowArgs.rowclass) | ||
:cssText(args.rowsstyle) | :cssText(self.args.rowsstyle) | ||
:cssText(rowArgs.rowstyle) | :cssText(rowArgs.rowstyle) | ||
:attr('id', rowArgs.rowid) | :attr('id', rowArgs.rowid) | ||
| שורה 143: | שורה 171: | ||
:attr('id', rowArgs.headerid) | :attr('id', rowArgs.headerid) | ||
:addClass(rowArgs.class) | :addClass(rowArgs.class) | ||
:addClass(args.headerclass) | :addClass(self.args.headerclass) | ||
:css('text-align', 'center') | :css('text-align', 'center') | ||
:cssText(args.headerstyle) | :cssText(self.args.headerstyle) | ||
:cssText(rowArgs.headerstyle) | :cssText(rowArgs.headerstyle) | ||
:wikitext(rowArgs.header) | :wikitext(rowArgs.header) | ||
elseif rowArgs.data and #(rowArgs.data) > 0 then | elseif rowArgs.data and #(rowArgs.data) > 0 then | ||
local row = root:tag('tr') | local row = self.root:tag('tr') | ||
row:addClass(rowArgs.rowclass) | row:addClass(rowArgs.rowclass) | ||
:cssText(args.rowsstyle) | :cssText(self.args.rowsstyle) | ||
:cssText(rowArgs.rowstyle) | :cssText(rowArgs.rowstyle) | ||
:attr('id', rowArgs.rowid) | :attr('id', rowArgs.rowid) | ||
| שורה 160: | שורה 188: | ||
:attr('id', rowArgs.labelid) | :attr('id', rowArgs.labelid) | ||
:css('text-align', 'right') | :css('text-align', 'right') | ||
:cssText(args.labelstyle) | :cssText(self.args.labelstyle) | ||
:cssText(rowArgs.labelstyle) | :cssText(rowArgs.labelstyle) | ||
:wikitext(rowArgs.label) | :wikitext(rowArgs.label) | ||
| שורה 177: | שורה 205: | ||
:attr('id', rowArgs.dataid) | :attr('id', rowArgs.dataid) | ||
:addClass(rowArgs.class) | :addClass(rowArgs.class) | ||
:cssText(args.datastyle) | :cssText(self.args.datastyle) | ||
:cssText(rowArgs.datastyle) | :cssText(rowArgs.datastyle) | ||
:newline() | :newline() | ||
| שורה 183: | שורה 211: | ||
end | end | ||
end | end | ||
--[[ | --[[ | ||
This function removes redundent keys from templateStructure: nil entries and header entries with no data | This function removes redundent keys from templateStructure: nil entries and header entries with no data | ||
]] | ]] | ||
function removeEmptyHeaders() | function Infobox:removeEmptyHeaders() | ||
local lastHeaderIndex=nil | local lastHeaderIndex=nil | ||
local removeFirstHeader = (not args.image or #args.image==0) -- remove the first header if there is no image | local removeFirstHeader = (not self.args.image or #self.args.image==0) -- remove the first header if there is no image | ||
local tempTemplateStructure = {} | local tempTemplateStructure = {} | ||
for i,v in pairs(templateStructure) do | for i,v in pairs(self.templateStructure) do | ||
if v~=nil then | if v~=nil then | ||
if v.header then | if v.header then | ||
| שורה 199: | שורה 228: | ||
if lastHeaderIndex then | if lastHeaderIndex then | ||
if not removeFirstHeader then | if not removeFirstHeader then | ||
table.insert(tempTemplateStructure, templateStructure[lastHeaderIndex]) | table.insert(tempTemplateStructure, self.templateStructure[lastHeaderIndex]) | ||
end | end | ||
lastHeaderIndex = nil | lastHeaderIndex = nil | ||
| שורה 208: | שורה 237: | ||
end | end | ||
end | end | ||
templateStructure=tempTemplateStructure | self.templateStructure=tempTemplateStructure | ||
end | end | ||
--[[ | --[[ | ||
This function builds the infobox table using structure templateStructure and args | This function builds the infobox table using structure templateStructure and args | ||
]] | ]] | ||
function | function Infobox:renderInfobox() | ||
removeEmptyHeaders( | self:removeEmptyHeaders() | ||
-- sub infobox | -- sub infobox | ||
if args.title == '-' then | if self.args.title == '-' then | ||
root:cssText(args.tablestyle):css('width', '100%') | self.root:cssText(self.args.tablestyle):css('width', '100%') | ||
else | else | ||
root:addClass('infobox'):css('width', '18em'):cssText(args.tablestyle) | self.root:addClass('infobox'):css('width', '18em'):cssText(self.args.tablestyle) | ||
end | end | ||
if args.tableclass then | if self.args.tableclass then | ||
root:addClass(args.tableclass) | self.root:addClass(self.args.tableclass) | ||
end | end | ||
if args.title ~='-' then | if self.args.title ~='-' then | ||
renderTitle() | self:renderTitle() | ||
end | end | ||
renderImages() | self:renderImages() | ||
local infobox_rows={} | local infobox_rows={} | ||
for k,v in pairs(templateStructure) do | for k,v in pairs(self.templateStructure) do | ||
addRow(v) | self:addRow(v) | ||
end | end | ||
if args.title ~= '-' then | if self.args.title ~= '-' then | ||
renderEditLinks() | self:renderEditLinks() | ||
end | end | ||
return tostring(root) | return tostring(self.root) | ||
end | end | ||
| שורה 279: | שורה 306: | ||
end | end | ||
function splitMaintainceCategory(value) | function splitMaintainceCategory(value) | ||
| שורה 331: | שורה 357: | ||
end | end | ||
function | |||
function Infobox:parseArgs(frame) | |||
local DEFAULT_IMAGE_PROPERTY = 'P18' | local DEFAULT_IMAGE_PROPERTY = 'P18' | ||
local templateParams = frame.args | local templateParams = frame.args | ||
local templateArgs = frame:getParent().args | local templateArgs = frame:getParent().args | ||
local args={} | |||
local templateStructure = {} | |||
args.title = frame.args['כותרת תבנית'] | args.title = frame.args['כותרת תבנית'] | ||
args.titlestyle = frame.args['כותרת תבנית-עיצוב'] | args.titlestyle = frame.args['כותרת תבנית-עיצוב'] | ||
| שורה 355: | שורה 386: | ||
args.tableclass = frame.args['טבלה-מחלקה'] | args.tableclass = frame.args['טבלה-מחלקה'] | ||
local isSelfUse = (mw.title.getCurrentTitle().namespace==10) | local isSelfUse = (mw.title.getCurrentTitle().namespace==10) | ||
self.autoDocumentation = frame.args['תיעוד-אוטומטי'] | |||
local labelPrefix = 'תווית' | local labelPrefix = 'תווית' | ||
| שורה 464: | שורה 495: | ||
end | end | ||
self.args = args | |||
self.templateStructure = templateStructure | |||
self.wikidataCats = wikidataCats | |||
self.isSelfUse = isSelfUse | |||
self.templateParams = templateParams | |||
end | |||
function infobox(frame) | |||
local infoObj = Infobox:new() | |||
infoObj:parseArgs(frame) | |||
return infoObj:render() | |||
end | end | ||
return { | return { | ||
['מידע']=infobox | ['מידע']=infobox | ||
} | } | ||