Module:Brief

From The Observatory

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

local p = {}

-- Helper function to create a form red link (Recipient)
local function create_formredlink(frame, form, page)
	local page_notsub = frame:callParserFunction(
		'#explode',
		page,
		'#',
		'0'
	)
    return frame:callParserFunction(
    	'#formredlink',
        'form=' .. form,
        'link text=' .. page_notsub,
        'existing page link text=' .. page_notsub,
        'target=' .. page
    )
end

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

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

    if formatted_items and type(formatted_items) == table then
        local last_item = table.remove(formatted_items) -- Remove the last item
        local 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 = mw.title.getCurrentTitle()
local root  = title.rootText
local display_title = frame:callParserFunction(
    '#show',
    root,
    '?Display title of',
    'default=' .. root
) or ''
local authors = frame:callParserFunction(
    '#show',
    root,
    '?Text author',
    'sep=;',
    'valuesep=;'
) or ''
frame:preprocess('{{DISPLAYTITLE:' .. display_title .. '}}')
local category = '[[Category:Briefs]]'
local note_text = '🔭 &nbsp; <i>This summary was human-edited with AI-assist</i>.'
local intro_start = 'Read the [[' .. root .. '|full article]] “[[' .. root .. '|' .. display_title .. ']]”'
local intro_end = ''
if authors ~= '' then
    intro_end = formredlinks(frame, 'Author', authors, ' by ')
end
local intro_text = intro_start .. intro_end

local area = mw.smw.ask {
	'[[' .. root .. ']]',
	'mainlabel=-',
	'?Area#-=area'
}

local linkarea = area[1].area or ''

local html = mw.html.create()

-- ARTICLE PATH

local articlepath = html:tag('div')
    :attr('id', 'article-path')
    :addClass( 'interface d-flex align-items-center justify-content-between' )
    :addClass( 'font-weight-bold text-uppercase d-print-none screenonly noprint' )

local lastsegment = ''
if linkarea ~= '' then
	lastsegment = '&raquo; [[' .. linkarea .. ']]'
end

articlepath:tag('span')
    :attr('id', 'observatory-path')
    :addClass( 'd-print-none noprint' )
    :wikitext('[[' .. 
          frame:preprocess('{{int:mainpage}}|{{SITENAME}}') ..
         ']] &raquo;&nbsp;' ..
         '[[All Areas|Area]] ' ..
         lastsegment
    )
    
articlepath:tag('span')
	:wikitext(frame:expandTemplate{ title = 'Article buttons', args = { 
		['Area'] = linkarea, 
	} })

local ul = html:tag('ul')
    :attr('id', 'article-tabs')
    :addClass('nav nav-tabs interface d-print-none noprint mb-4')

    ul:tag('li')
        :addClass('nav-item order-1')
        :wikitext(frame:callParserFunction('#widget',
            'a',
            'id=quickread-show',
            'class=nav-link interface active',
            'toggle=tab',
            'href=',
            'linktext=Quick Read'
        ))

    ul:tag('li')
        :addClass('nav-item order-0')
        :wikitext(frame:callParserFunction('#widget',
            'a',
            'id=longread-show',
            'class=nav-link interface',
            'toggle=',
            'href=' .. frame:preprocess('{{fullurl:' .. root .. '}}'),
            'linktext=Full Article'
        ))

local intro = html:tag('div')
    :attr('id', 'quick-read-intro')
    :addClass('alert rounded mb-4')
    :wikitext(intro_text)

local note = html:tag('div')
    :attr('id', 'quick-read-note')
    :addClass('alert rounded mb-4')
    :wikitext(note_text)

result = tostring(html) .. category

return result

end

function p.test()

local article = '10_Reasons_Why_Hydropower_Dams_Are_a_False_Climate_Solution'

local area = mw.smw.ask {
	'[[' .. article .. ']]',
	'mainlabel=-',
	'?Area#-=area'
}

return area[1].area

end

return p