Module:Navbox: تفاوت بین نسخه‌ها

از دانشنامه‌ی اسلامی
پرش به ناوبری پرش به جستجو
سطر ۱: سطر ۱:
 +
<includeonly><span dir="ltr">--
 +
-- This module implements {{Navbox}}
 
--
 
--
  -- This module will implement {{Navbox}}
+
 
  --
+
local p = {}
+
 
local p = {}
+
local navbar = require('Module:Navbar')._navbar
+
local getArgs -- lazily initialized
local HtmlBuilder = require('Module:HtmlBuilder')
+
 
local Navbar = require('Module:Navbar')
+
local args
+
local tableRowAdded = false
local args
+
local border
local frame
+
local listnums = {}
local tableRowAdded = false
+
 
local border
+
local function trim(s)
local listnums = {}
+
return (mw.ustring.gsub(s, "^%s*(.-)%s*$", "%1"))
+
end
function trim(s)
+
 
    return (mw.ustring.gsub(s, "^%s*(.-)%s*$", "%1"))
+
local function addNewline(s)
  end
+
if s:match('^[*:;#]') or s:match('^{|') then
+
return '\n' .. s ..'\n'
function addTableRow(tbl)
+
else
    -- If any other rows have already been added, then we add a 2px gutter row.
+
return s
    if tableRowAdded then
+
end
        tbl
+
end
            .tag('tr')
+
 
                .css('height', '2px')
+
local function addTableRow(tbl)
                .tag('td')
+
-- If any other rows have already been added, then we add a 2px gutter row.
  end
+
if tableRowAdded then
+
tbl
    tableRowAdded = true
+
:tag('tr')
+
:css('height', '2px')
    return tbl.tag('tr')
+
:tag('td')
end
+
:attr('colspan',2)
+
end
+
 
--
+
tableRowAdded = true
--  Title row
+
 
--
+
return tbl:tag('tr')
function renderTitleRow(tbl)
+
end
    if not args.title then return end
+
 
+
local function renderNavBar(titleCell)
    local titleRow = addTableRow(tbl)
+
-- Depending on the presence of the navbar and/or show/hide link, we may need to add a spacer div on the left
+
-- or right to keep the title centered.
    if args.titlegroup then
+
local spacerSide = nil
        titleRow
+
 
            .tag('th')
+
if args.navbar == 'off' then
                .attr('scope', 'row')
+
-- No navbar, and client wants no spacer, i.e. wants the title to be shifted to the left. If there's
                .addClass('navbox-group')
+
-- also no show/hide link, then we need a spacer on the right to achieve the left shift.
                .addClass(args.titlegroupclass)
+
if args.state == 'plain' then spacerSide = 'left' end
                .cssText(args.basestyle)
+
elseif args.navbar == 'plain' or (not args.name and mw.getCurrentFrame():getParent():getTitle() == 'الگو:Navbox' and (border == 'subgroup' or border == 'child' or border == 'none')) then
                .cssText(args.groupstyle)
+
-- No navbar. Need a spacer on the left to balance out the width of the show/hide link.
                .cssText(args.titlegroupstyle)
+
if args.state ~= 'plain' then spacerSide = 'right' end
                .wikitext(args.titlegroup)
+
else
    end
+
-- Will render navbar (or error message). If there's no show/hide link, need a spacer on the right
+
-- to balance out the width of the navbar.
    local titleCell = titleRow.tag('th').attr('scope', 'col')
+
if args.state == 'plain' then spacerSide = 'left' end
+
 
    if args.titlegroup then
+
titleCell:wikitext(navbar{
        titleCell
+
args.name,
            .css('border-left', '2px solid #fdfdfd')
+
mini = 1,
            .css('width', '100%')
+
fontstyle = (args.basestyle or '') .. ';' .. (args.titlestyle or '') ..  ';background:none transparent;border:none;'
    end
+
})
+
end
    local titleColspan = 2
+
 
    if args.imageleft then titleColspan = titleColspan + 1 end
+
-- Render the spacer div.
    if args.image then titleColspan = titleColspan + 1 end
+
if spacerSide then
    if args.titlegroup then titleColspan = titleColspan - 1 end
+
titleCell
+
:tag('span')
    titleCell
+
:css('float', spacerSide)
        .cssText(args.basestyle)
+
:css('width', '6em')
        .cssText(args.titlestyle)
+
:wikitext('&nbsp;')
        .addClass('navbox-title')
+
end
        .attr('colspan', titleColspan)
+
end
+
--   Title row
      renderNavBar(titleCell)
+
--
+
local function renderTitleRow(tbl)
      titleCell
+
if not args.title then return end
          .tag('div')
+
 
              .addClass(args.titleclass)
+
local titleRow = addTableRow(tbl)
              .css('font-size', '110%')
+
 
              .newline()
+
if args.titlegroup then
              .wikitext(args.title)
+
titleRow
end
+
:tag('th')
+
:attr('scope', 'row')
function renderNavBar(titleCell)
+
:addClass('navbox-group')
    -- Depending on the presence of the navbar and/or show/hide link, we may need to add a spacer div on the left
+
:addClass(args.titlegroupclass)
    -- or right to keep the title centered.
+
:cssText(args.basestyle)
    local spacerSide = nil
+
:cssText(args.groupstyle)
+
:cssText(args.titlegroupstyle)
    if args.navbar == 'off' then
+
:wikitext(args.titlegroup)
        -- No navbar, and client wants no spacer, i.e. wants the title to be shifted to the left. If there's
+
end
        -- also no show/hide link, then we need a spacer on the right to achieve the left shift.
+
 
        if args.state == 'plain' then spacerSide = 'right' end
+
local titleCell = titleRow:tag('th'):attr('scope', 'col')
    elseif args.navbar == 'plain' or args.navbar == 'off' or (not args.name and (border == 'subgroup' or border == 'child' or border == 'none')) then
+
 
        -- No navbar. Need a spacer on the left to balance out the width of the show/hide link.
+
if args.titlegroup then
        if args.state ~= 'plain' then spacerSide = 'left' end
+
titleCell
    else
+
:css('border-right', '2px solid #fdfdfd')
        -- Will render navbar (or error message). If there's no show/hide link, need a spacer on the right
+
:css('width', '100%')
        -- to balance out the width of the navbar.
+
end
        if args.state == 'plain' then spacerSide = 'right' end
+
 
+
local titleColspan = 2
        titleCell.wikitext(Navbar.navbar({
+
if args.imageright then titleColspan = titleColspan + 1 end
            args.name,
+
if args.image then titleColspan = titleColspan + 1 end
            mini = 1,
+
if args.titlegroup then titleColspan = titleColspan - 1 end
            fontstyle = (args.basestyle or '') .. ';' .. (args.titlestyle or '') ..  ';background:none transparent;border:none;'
+
 
        }))
+
titleCell
    end
+
:cssText(args.basestyle)
+
:cssText(args.titlestyle)
    -- Render the spacer div.
+
:addClass('navbox-title')
    --            .css('float', spacerSide)
+
:attr('colspan', titleColspan)
    if spacerSide then
+
 
        titleCell
+
renderNavBar(titleCell)
            .tag('span')
+
 
   
+
titleCell
                .css('float', 'right')
+
:tag('div')
                .css('width', '6em')
+
:addClass(args.titleclass)
                .wikitext('&nbsp;')
+
:css('font-size', '110%')
    end
+
:wikitext(addNewline(args.title))
end
+
end
+
--  Above/Below rows
+
--
--
+
 
--  Above/Below rows
+
local function getAboveBelowColspan()
--
+
local ret = 2
function renderAboveRow(tbl)
+
if args.imageright then ret = ret + 1 end
    if not args.above then return end
+
if args.image then ret = ret + 1 end
+
return ret
    addTableRow(tbl)
+
end
        .tag('td')
+
 
            .addClass('navbox-abovebelow')
+
local function renderAboveRow(tbl)
            .addClass(args.aboveclass)
+
if not args.above then return end
            .cssText(args.basestyle)
+
 
            .cssText(args.abovestyle)
+
addTableRow(tbl)
            .attr('colspan', getAboveBelowColspan())
+
:tag('td')
            .tag('div')
+
:addClass('navbox-abovebelow')
                .newline()
+
:addClass(args.aboveclass)
                .wikitext(args.above)
+
:cssText(args.basestyle)
end
+
:cssText(args.abovestyle)
+
:attr('colspan', getAboveBelowColspan())
function renderBelowRow(tbl)
+
:tag('div')
    if not args.below then return end
+
:wikitext(addNewline(args.above))
+
end
    addTableRow(tbl)
+
 
        .tag('td')
+
local function renderBelowRow(tbl)
            .addClass('navbox-abovebelow')
+
if not args.below then return end
            .addClass(args.belowclass)
+
 
            .cssText(args.basestyle)
+
addTableRow(tbl)
            .cssText(args.belowstyle)
+
:tag('td')
            .attr('colspan', getAboveBelowColspan())
+
:addClass('navbox-abovebelow')
            .tag('div')
+
:addClass(args.belowclass)
                .newline()
+
:cssText(args.basestyle)
                .wikitext(args.below)
+
:cssText(args.belowstyle)
end
+
:attr('colspan', getAboveBelowColspan())
+
:tag('div')
function getAboveBelowColspan()
+
:wikitext(addNewline(args.below))
    local ret = 2
+
end
    if args.imageleft then ret = ret + 1 end
+
--  List rows
    if args.image then ret = ret + 1 end
+
--
    return ret
+
local function renderListRow(tbl, listnum)
end
+
local row = addTableRow(tbl)
+
 
+
if listnum == 1 and args.imageright then
--
+
row
--  List rows
+
:tag('td')
--
+
:addClass('navbox-image')
function renderListRow(tbl, listnum)
+
:addClass(args.imageclass)
    local row = addTableRow(tbl)
+
:css('width', '0%')
+
:css('padding', '0px 2px 0px 0px')
    if listnum == 1 and args.imageleft then
+
:cssText(args.imagerightstyle)
        row
+
:attr('rowspan', 2 * #listnums - 1)
            .tag('td')
+
:tag('div')
                .addClass('navbox-image')
+
:wikitext(addNewline(args.imageright))
                .addClass(args.imageclass)
+
end
                .css('width', '0%')
+
 
                .css('padding', '0px 2px 0px 0px')
+
if args['group' .. listnum] then
                .cssText(args.imageleftstyle)
+
local groupCell = row:tag('th')
                .attr('rowspan', 2 * #listnums - 1)
+
 
                .tag('div')
+
groupCell
                    .newline()
+
:attr('scope', 'row')
                    .wikitext(args.imageleft)
+
:addClass('navbox-group')
end
+
:addClass(args.groupclass)
+
:cssText(args.basestyle)
    if args['group' .. listnum] then
+
 
        local groupCell = row.tag('th')
+
if args.groupwidth then
+
groupCell:css('width', args.groupwidth)
        groupCell
+
end
                .attr('scope', 'row')
+
 
                .addClass('navbox-group')
+
groupCell
                .addClass(args.groupclass)
+
:cssText(args.groupstyle)
                .cssText(args.basestyle)
+
:cssText(args['group' .. listnum .. 'style'])
+
:wikitext(args['group' .. listnum])
        if args.groupwidth then
+
end
            groupCell.css('width', args.groupwidth)
+
 
        end
+
local listCell = row:tag('td')
+
 
        groupCell
+
if args['group' .. listnum] then
            .cssText(args.groupstyle)
+
listCell
            .cssText(args['group' .. listnum .. 'style'])
+
:css('text-align', 'right')
            .wikitext(args['group' .. listnum])
+
:css('border-right-width', '2px')
    end
+
:css('border-right-style', 'solid')
+
else
    local listCell = row.tag('td')
+
listCell:attr('colspan', 2)
+
end
    if args['group' .. listnum] then
+
 
        listCell
+
if not args.groupwidth then
            .css('text-align', 'left')
+
listCell:css('width', '100%')
            .css('border-left-width', '2px')
+
end
            .css('border-left-style', 'solid')
+
 
    else
+
local isOdd = (listnum % 2) == 1
        listCell.attr('colspan', 2)
+
local rowstyle = args.evenstyle
    end
+
if isOdd then rowstyle = args.oddstyle end
+
 
    if not args.groupwidth then
+
local evenOdd
        listCell.css('width', '100%')
+
if args.evenodd == 'swap' then
    end
+
if isOdd then evenOdd = 'even' else evenOdd = 'odd' end
+
else
    local isOdd = (listnum % 2) == 1
+
if isOdd then evenOdd = args.evenodd or 'odd' else evenOdd = args.evenodd or 'even' end
    local rowstyle = args.evenstyle
+
end
    if isOdd then rowstyle = args.oddstyle end
+
 
+
listCell
    local evenOdd
+
:css('padding', '0px')
    if args.evenodd == 'swap' then
+
:cssText(args.liststyle)
        if isOdd then evenOdd = 'even' else evenOdd = 'odd' end
+
:cssText(rowstyle)
    else
+
:cssText(args['list' .. listnum .. 'style'])
        if isOdd then evenOdd = args.evenodd or 'odd' else evenOdd = args.evenodd or 'even' end
+
:addClass('navbox-list')
    end
+
:addClass('navbox-' .. evenOdd)
+
:addClass(args.listclass)
    listCell
+
:tag('div')
        .css('padding', '0px')
+
:css('padding', (listnum == 1 and args.list1padding) or args.listpadding or '0em 0.25em')
        .cssText(args.liststyle)
+
:wikitext(addNewline(args['list' .. listnum]))
        .cssText(rowstyle)
+
 
        .cssText(args['list' .. listnum .. 'style'])
+
if listnum == 1 and args.image then
        .addClass('navbox-list')
+
row
        .addClass('navbox-' .. evenOdd)
+
:tag('td')
        .addClass(args.listclass)
+
:addClass('navbox-image')
        .tag('div')
+
:addClass(args.imageclass)
            .css('padding', (listnum == 1 and args.list1padding) or args.listpadding or '0em 0.25em')
+
:css('width', '0%')
            .newline()
+
:css('padding', '0px 0px 0px 2px')
            .wikitext(args['list' .. listnum])
+
:cssText(args.imagestyle)
+
:attr('rowspan', 2 * #listnums - 1)
    if listnum == 1 and args.image then
+
:tag('div')
        row
+
:wikitext(addNewline(args.image))
            .tag('td')
+
end
                .addClass('navbox-image')
+
end
                .addClass(args.imageclass)
+
--  Tracking categories
                .css('width', '0%')
+
--
                .css('padding', '0px 0px 0px 2px')
+
 
                .cssText(args.imagestyle)
+
local function needsHorizontalLists()
                .attr('rowspan', 2 * #listnums - 1)
+
if border == 'child' or border == 'subgroup' or args.tracking == 'no' then return false end
                .tag('div')
+
 
                    .newline()
+
local listClasses = {'plainlist', 'hlist', 'hlist hnum', 'hlist hwrap', 'hlist vcard', 'vcard hlist', 'hlist vevent'}
                    .wikitext(args.image)
+
for _, cls in ipairs(listClasses) do
    end
+
if args.listclass == cls or args.bodyclass == cls then
end
+
return false
+
end
+
end
--
+
 
--  Tracking categories
+
return true
--
+
end
function renderTrackingCategories(builder)
+
 
    local frame = mw.getCurrentFrame()
+
local function hasBackgroundColors()
+
return mw.ustring.match(args.titlestyle or '','background') or mw.ustring.match(args.groupstyle or '','background') or mw.ustring.match(args.basestyle or '','background')
    if not frame then return end
+
end
+
 
    local s = frame:preprocess('{{#ifeq:{{NAMESPACE}}|{{ns:10}}|1|0}}{{SUBPAGENAME}}')
+
local function getTrackingCategories()
    if mw.ustring.sub(s, 1, 1) == '0' then return end -- not in template space
+
local cats = {}
    local subpage = mw.ustring.lower(mw.ustring.sub(s, 2))
+
if needsHorizontalLists() then table.insert(cats, 'ناوباکس‌های بدون فهرست افقی') end
    if subpage == 'doc' or subpage == 'sandbox' or subpage == 'testcases' then return end
+
if hasBackgroundColors() then table.insert(cats, 'جعبه‌های ناوبری با استفاده از رنگ‌های پس‌زمینه') end
+
return cats
    for i, cat in ipairs(getTrackingCategories()) do
+
end
        builder.wikitext('')
+
 
    end
+
local function renderTrackingCategories(builder)
end
+
local title = mw.title.getCurrentTitle()
+
if title.namespace ~= 10 then return end -- not in template space
function getTrackingCategories()
+
local subpage = title.subpageText
    local cats = {}
+
if subpage == 'توضیحات' or subpage == 'تمرین' or subpage == 'آزمایشی' then return end
    if needsHorizontalLists() then table.insert(cats, 'Navigational boxes without horizontal lists') end
+
 
    if hasBackgroundColors() then table.insert(cats, 'Navboxes using background colours') end
+
for _, cat in ipairs(getTrackingCategories()) do
    return cats
+
builder:wikitext('[[رده:' .. cat .. ']]')
end
+
end
+
end
function needsHorizontalLists()
+
--  Main navbox tables
    if border == 'child' or border == 'subgroup' or args.tracking == 'no' then return false end
+
--
+
local function renderMainTable()
    local listClasses = {'plainlist', 'hlist', 'hlist hnum', 'hlist hwrap', 'hlist vcard', 'vcard hlist', 'hlist vevent'}
+
local tbl = mw.html.create('table')
    for i, cls in ipairs(listClasses) do
+
:addClass('nowraplinks')
        if args.listclass == cls or args.bodyclass == cls then
+
:addClass(args.bodyclass)
            return false
+
 
        end
+
if args.title and (args.state ~= 'plain' and args.state ~= 'off') then
    end
+
tbl
+
:addClass('collapsible')
    return true
+
:addClass(args.state or 'autocollapse')
end
+
end
+
 
function hasBackgroundColors()
+
tbl:css('border-spacing', 0)
    return args.titlestyle or args.groupstyle
+
if border == 'subgroup' or border == 'child' or border == 'none' then
end
+
tbl
+
:addClass('navbox-subgroup')
+
:cssText(args.bodystyle)
--
+
:cssText(args.style)
--  Main navbox tables
+
else -- regular navobx - bodystyle and style will be applied to the wrapper table
--
+
tbl
function renderMainTable()
+
:addClass('navbox-inner')
    local tbl = HtmlBuilder.create('table')
+
:css('background', 'transparent')
        .attr('cellspacing', 0)
+
:css('color', 'inherit')
        .addClass('nowraplinks')
+
end
        .addClass(args.bodyclass)
+
tbl:cssText(args.innerstyle)
+
 
    if args.title and (args.state ~= 'plain' and args.state ~= 'off') then
+
renderTitleRow(tbl)
        tbl
+
renderAboveRow(tbl)
            .addClass('collapsible')
+
for _, listnum in ipairs(listnums) do
            .addClass(args.state or 'autocollapse')
+
renderListRow(tbl, listnum)  
    end
+
end
+
renderBelowRow(tbl)
    tbl.css('border-spacing', 0)
+
 
    if border == 'subgroup' or border == 'child' or border == 'none' then
+
return tbl
        tbl
+
end
            .addClass('navbox-subgroup')
+
 
            .cssText(args.bodystyle)
+
function p._navbox(navboxArgs)
            .cssText(args.style)
+
args = navboxArgs
    else -- regular navobx - bodystyle and style will be applied to the wrapper table
+
 
        tbl
+
for k, _ in pairs(args) do
            .addClass('navbox-inner')
+
local listnum = ('' .. k):match('^list(%d+)$')
            .css('background', 'transparent')
+
if listnum then table.insert(listnums, tonumber(listnum)) end
            .css('color', 'inherit')
+
end
    end
+
table.sort(listnums)
    tbl.cssText(args.innerstyle)
+
 
+
border = trim(args.border or args[1] or '')
    renderTitleRow(tbl)
+
 
    renderAboveRow(tbl)
+
-- render the main body of the navbox
    for i, listnum in ipairs(listnums) do
+
local tbl = renderMainTable()
        renderListRow(tbl, listnum)
+
 
    end
+
-- render the appropriate wrapper around the navbox, depending on the border param
    renderBelowRow(tbl)
+
local res = mw.html.create()
+
if border == 'none' then
    return tbl
+
res:node(tbl)
end
+
elseif border == 'subgroup' or border == 'child' then
+
-- We assume that this navbox is being rendered in a list cell of a parent navbox, and is
function p._navbox(navboxArgs)
+
-- therefore inside a div with padding:0em 0.25em. We start with a </div> to avoid the
    args = navboxArgs
+
-- padding being applied, and at the end add a <div> to balance out the parent's </div>
+
res
    for k, v in pairs(args) do
+
:wikitext('</div>') -- XXX: hack due to lack of unclosed support in mw.html.
        local listnum = ('' .. k):match('^list(%d+)$')
+
:node(tbl)
        if listnum then table.insert(listnums, tonumber(listnum)) end
+
:wikitext('<div>') -- XXX: hack due to lack of unclosed support in mw.html.
    end
+
else
    table.sort(listnums)
+
res
+
:tag('table')
    border = trim(args.border or args[1] or '')
+
:addClass('navbox')
+
:css('border-spacing', 0)
    -- render the main body of the navbox
+
:cssText(args.bodystyle)
    local tbl = renderMainTable()
+
:cssText(args.style)
+
:tag('tr')
    -- render the appropriate wrapper around the navbox, depending on the border param
+
:tag('td')
    local res = HtmlBuilder.create()
+
:css('padding', '2px')
    if border == 'none' then
+
:node(tbl)
        res.node(tbl)
+
end
    elseif border == 'subgroup' or border == 'child' then
+
 
-- We assume that this navbox is being rendered in a list cell of a parent navbox, and is
+
renderTrackingCategories(res)
        -- therefore inside a div with padding:0em 0.25em. We start with a </div> to avoid the
+
 
      res             .tag('/div', {unclosed = true})
+
return tostring(res)
                .done()
+
end
            .node(tbl)
+
 
            .tag('div', {unclosed = true})
+
function p.navbox(frame)
    else
+
if not getArgs then
        res
+
getArgs = require('Module:Arguments').getArgs
            .tag('table')
+
end
                .attr('cellspacing', 0)
+
args = getArgs(frame, {wrappers = 'الگو:Navbox'})
                .addClass('navbox')
+
 
                .css('border-spacing', 0)
+
-- Read the arguments in the order they'll be output in, to make references number in the right order.
                .cssText(args.bodystyle)
+
local _
                .cssText(args.style)
+
_ = args.title
                .tag('tr')
+
_ = args.above
                    .tag('td')
+
for i = 1, 20 do
                        .css('padding', '2px')
+
_ = args["group" .. tostring(i)]
                        .node(tbl)
+
_ = args["list" .. tostring(i)]
    end
+
end   
+
_ = args.below
    renderTrackingCategories(res)
+
 
+
return p._navbox(args)
    return tostring(res)
+
end
end
+
 
+
return p
function p.navbox(frame)
+
</span></includeonly>
    -- ParserFunctions considers the empty string to be false, so to preserve the previous
 
    -- behavior of {{navbox}}, change any empty arguments to nil, so Lua will consider
 
    -- them false too.
 
    local args = {}
 
    local parent_args = frame:getParent().args;
 
 
    -- Out of order parsing bug.
 
    local temp;
 
    temp = parent_args.title;
 
    temp = parent_args.above;
 
    for i = 1, 20 do
 
        temp = parent_args["group" .. tostring(i)];
 
        temp = parent_args["list" .. tostring(i)];
 
    end
 
    temp = parent_args.below;
 
 
    for k, v in pairs(parent_args) do
 
        if v ~= '' then
 
            args[k] = v
 
        end
 
    end
 
    return p._navbox(args)
 
end
 
 
return p
 

نسخهٔ ‏۱۲ سپتامبر ۲۰۱۵، ساعت ۱۲:۵۸

مسابقه از خطبه ۱۱۴ نهج البلاغه