Module:Locations

Documentation for this module may be created at Module:Locations/doc

local p = {}

-- Helper function to format a list of items as formredlinks
local function makelinks(items, prefix)
    
    local formatted_items = {}

    for item in items:gmatch("[^,]+") do
        item = mw.text.trim(item) -- Trim spaces around the name
        local formatted_item = '[[' .. item .. ']]'
        table.insert(formatted_items, formatted_item)
    end

    if #formatted_items > 1 then
        local last_item = table.remove(formatted_items) -- Remove the last item
        formatted_list = table.concat(formatted_items, ", ") .. " and " .. last_item
    else
        formatted_list = formatted_items[1] or "" -- Return the single item or an empty string
    end

    -- Add prefix if provided
    if prefix and prefix ~= "" then
        formatted_list = prefix .. formatted_list
    end

    return formatted_list
    
end
function p.main(frame)
    local title = 'Geo Tags'
    local pageid = frame:getParent().args['page']
    local pagedata = mw.smw.ask('[[PAGEID::' .. pageid .. ']]|mainlabel=Page|?Area|?Source|?Text author|?Teaser') or {}

    local area, source, text_author, page_name, page_name_plain, teaser

    if next(pagedata) then
        for page, properties in pairs(pagedata) do
            for property, value in pairs(properties) do
                if type(value) == "table" then
                    value = table.concat(value, ", ")
                end
                if property == "Page" then
                    page_name = tostring(value)
                    page_name_plain = tostring(value):match("%[%[:.-|(.+)%]%]") or tostring(value)
                elseif property == "Area" then
                    area = tostring(value)
                elseif property == "Source" then
                    source = tostring(value)
                elseif property == "Text author" then
                    text_author = tostring(value)
                elseif property == "Teaser" then
                    teaser = tostring(value)
                end
            end
        end
    else
        page_name = ''
        area = ''
        source = ''
        text_author = ''
    end

    -- Example of using the variables
    local output = mw.html.create()

    local mapbox = output:tag('div')
        :attr('id', 'featured-box')
        :addClass( 'rounded bg-white shadow border-0 h-100 mt-4 mb-5' )
        :css({
            ['border-radius'] = '5px !important',
            ['margin'] = '0 12px'
        })

    local mapbox_header = mapbox:tag('div')
        :addClass('card-header d-flex flex-row justify-content-between h3 p-0')

    local mapbox_header_left = mapbox_header:tag('div')
        :addClass('py-3 pr-3 pl-4 bg-dark border-darker text-white mx-0 w-50')
        :css({
            ['border-top-left-radius'] = '5px',
            ['margin'] = '0 12px'
        })
        :wikitext(title)

    local mapbox_header_right = mapbox_header:tag('div')
        :addClass('py-3 pr-3 pl-4 d-flex align-items-center w-50 font-weight-normal interface')
        :css({
            ['font-size'] = '1.2rem !important',
            ['border-left'] = '8px solid var(--primary)',
            ['border-top-right-radius'] = '5px',
            ['background-color'] = '#fff'
        })
        :wikitext('[[' .. area .. ']]')

    local map = mapbox:tag('div')
        :css({
            ['overflow'] = 'hidden'
        })
        :wikitext(frame:preprocess(
             '{{#ask:[[-Has subobject::' .. 
              page_name_plain .. 
             ']]|?Has coordinates|?Location|format=leaflet|width=100%}}'
        ))

    local body = mapbox:tag('div')
        :addClass('card-body')

    local text = body:tag('div')
        :addClass('px-0 px-md-4')

    local title = text:tag('div')
        :addClass('card-body-title interface h4 my-3 mt-md-0 mt-lg-3 mt-xl-0')
        :wikitext(page_name)

    local content = text:tag('div')
        :addClass('card-body-content mb-3')

    content:tag('div')
        :addClass('interface mb-3')
        :wikitext( makelinks(text_author, 'By ') .. ' | ' .. makelinks(source, 'From ') )

    content:tag('div')
        :wikitext(teaser)

    return tostring(output)
end

return p