Modul:Quelle

Aus Steel Beasts Wiki
Version vom 8. September 2023, 22:18 Uhr von Dr.Thodt (Diskussion | Beiträge)
(Unterschied) ← Nächstältere Version | Aktuelle Version (Unterschied) | Nächstjüngere Version → (Unterschied)

Die Dokumentation für dieses Modul kann unter Modul:Quelle/Doku erstellt werden

local Quelle = {}

local metatable = {}
local methodtable = {}

metatable.__index = methodtable

local TNT = require( 'Module:Translate' ):new()

--[[

Generates references in the following format

"[url title]" vom date. _website_. Abgerufen am access_date.
"Comm-Link:12345 - Lorem Ipsum" vom 11.01.2020. _Roberts Space Industries_. Abgerufen am 12.01.2020.

"[url title]". _website_.
"Comm-Link:12345 - Lorem Ipsum". _Roberts Space Industries_.

"[url]". _website_.
"https://robertsspaceindustries.com/comm-links/12345-lorem-ipsum". _Roberts Space Industries_.

author. "[url title]" vom date. _website_. Abgerufen am access_date.
Dave Haddock. "Comm-Link:12345 - Lorem Ipsum" vom 11.01.2020. _Roberts Space Industries_. Abgerufen am 12.01.2020.

"[url title]" _([url_internal])_ vom date. _website_. Abgerufen am access_date.
"Comm-Link:12345 - Lorem Ipsum" _(Im Wiki)_ vom 11.01.2020. _Roberts Space Industries_. Abgerufen am 12.01.2020.

"[url title]" _([url_internal title_internal])_ vom date. _website_. Abgerufen am access_date.
"Comm-Link:12345 - Lorem Ipsum" _(Comm-Link:12345)_ vom 11.01.2020. _Roberts Space Industries_. Abgerufen am 12.01.2020.

"[url title]" vom date. _website_. Seite page.
"Broschüre der 300i" vom 11.01.2020. _Roberts Space Industries_. Seite 18, ff..

"[url title]" vom date. _website_. Abschnitt section. Abgerufen am access_date.
"10 for the Chairman: 24.12.2013" vom 11.01.2020. _YouTube_. Abschnitt 4:12 - 5:50. Abgerufen am 12.01.2020.

"[url]" vom date. _website_. Abschnitt ab section. Abgerufen am access_date.
"https://youtube.com/?v=th1Si54nUr7" vom 11.01.2020. _YouTube_. Abschnitt ab 5:50. Abgerufen am 12.01.2020.

]]

--- Helper function checking if a substring is in a string
---
--- @param needle string - Value to search for
--- @param haystack string - String to search in
---
--- @return bool - True if found
local function stringContains( needle, haystack )
    return mw.ustring.find( mw.ustring.lower( haystack ), needle, 1, true )
end


--- Returns wikitext linkparts for a link
---
--- @param link string -
---
--- @return table - Table containing 'linkStart', 'linkEnd', 'separator'
local function getWikiLinkParts( link )
    if stringContains( 'http', link ) then
        return {
            linkStart = '[',
            linkEnd = ']',
            separator = ' ',
        }
    else
        return {
            linkStart = '[[',
            linkEnd = ']]',
            separator = '|',
        }
    end
end

--- Builds a wikitext link
---
--- @param link string - The link
--- @param text string - The text to show instead of the link
--- @param quote bool - True to wrap link in '"'
---
--- @return string
local function makeLinkText( link, text, quote )
    local linkParts = getWikiLinkParts( link )

    if quote == nil then
        quote = false
    end

    if text == nil and quote == false then
        text = ''
        linkParts.separator = ''
    elseif quote == true then
        if text == nil then
            text = link
        end

        text = string.format( '"%s"', text )
    end

    return linkParts.linkStart .. link .. linkParts.separator .. text .. linkParts.linkEnd
end

---
--- Parts
---

--- Source URL Part
--- "[url title]"
--- Tries to guess the website part based on the url
--- Adds a icon for known urls
---
--- Template arguments used in this method:
--- @arg url | link
--- @arg archive_url
--- @arg title | link_text | 1
--- @arg extra_text - DEPRECATED
---
--- @return string - Usable wikitext
local function buildSourceUrlPart( instance )
    local websiteName
    local logoFileName
    local url = instance.args.url or instance.args.link
    local title = instance.args.title or instance.args.link_text or instance.args[ 1 ] or instance.args.text

    --- Add a maintenance category for sources without an url
    if url == nil then
        table.insert( instance.categories, '[[Kategorie:Seite mit fehlerhafter Quelle]]' )

        return ''
    end

    --- If the url does not contain 'http' also add a maintenance url
    if not stringContains( 'http', url ) then
        table.insert( instance.categories, '[[Kategorie:Seite mit fehlerhafter Quelle]]' )
    else
        mw.smw.set({
            Quelle = url
        })
    end

    if stringContains( 'youtube', url ) or stringContains( 'youtu.be', url ) then
        logoFileName = 'YouTube_Logo.svg'
        websiteName = 'YouTube'
    elseif stringContains( 'esimgames', url ) then
        logoFileName = 'ESimGames Logo.png'

        websiteName = 'eSim Games'

        if stringContains( '/comm-link', url ) then
            websiteName = websiteName .. ' Comm-Links'
        elseif stringContains( '/galactapedia', url ) then
            websiteName = websiteName .. ' Galactapedia'
        elseif stringContains( '/spectrum', url ) then
            websiteName = websiteName .. ' Spectrum'
        elseif stringContains( '/pledge', url ) then
            websiteName = websiteName .. ' Pledge Store'
        elseif stringContains( '/starmap', url ) then
            websiteName = websiteName .. ' Starmap'
        elseif stringContains( '/issue-council', url ) then
            websiteName = websiteName .. ' Issue Council'
        else
            websiteName = websiteName .. ' Webseite'
        end
    elseif stringContains( 'starcitizenbase', url ) then
        logoFileName = 'Star_Citizen_Base_Logo.svg'
        websiteName = 'Star Citizen Base'
    elseif stringContains( 'starcitizenblog', url ) then
        websiteName = 'Star Citizen Blog'
    elseif stringContains( 'star-citizen-news-radio', url ) then
        logoFileName = 'Star_Citizen_News_Radio_Logo.png'
        websiteName = 'Star Citizen News Radio'
    elseif stringContains( 'star-citizen.wiki', url ) then
        logoFileName = 'Star_Citizen_Wiki_Stern.png'
        websiteName = 'Star Citizen Wiki'
    end

    instance.website = websiteName

    if logoFileName then
        table.insert( instance.parts, 1, string.format( '[[Datei:%s|x15px|link=|class=noviewer]]', logoFileName ) )
    end

    if instance.args.archive_url ~= nil then
        return makeLinkText( instance.args.archive_url, title, true )
    end

    return makeLinkText( url, title, true )
end

--- Internal URL Part
--- ''([url_internal title_internal])''
---
--- Template arguments used in this method:
--- @arg url_internal
--- @arg title_internal
---
--- @return string - Usable wikitext
local function buildInternalUrlPart( instance )
    if instance.args.url_internal == nil then
        return ''
    end

    local linkText = instance.args.title_internal or TNT.format( 'Module:Quelle/i18n.json', 'txt_in_wiki' )

    return string.format( " ''(%s)''", makeLinkText( instance.args.url_internal, linkText ) )
end

--- Source Date Part, the date the source was published
--- vom date
---
--- Template arguments used in this method:
--- @arg date
---
--- @return string - Date text
local function buildSourceDatePart( instance )
    local date = instance.args.date

    if date == nil then
        return ''
    end

    --- TODO: Format date?
    return ' ' .. TNT.format( 'Module:Quelle/i18n.json', 'txt_from', date )
end

--- Author Part
--- author.
---
--- Template arguments used in this method:
--- @arg author
---
--- @return string - Author text
local function buildAuthorPart( instance )
    local author = instance.args.author

    if author == nil then
        return
    end

    table.insert( instance.parts, string.format( '%s.', author ) )
end

--- URL Part
--- "[url title]" ''([url_internal title_internal])'' vom date.
---
--- @return string - The url part
local function buildUrlPart( instance )
    local sourceUrl = buildSourceUrlPart( instance )
    local internalUrl = buildInternalUrlPart( instance )
    local sourceUrlDate = buildSourceDatePart( instance )

    table.insert( instance.parts, string.format( '%s%s%s.', sourceUrl, internalUrl, sourceUrlDate ) )
end

--- Archive URL Part
--- Adds an "Archiviert vom [Original] am [archive_date]." part
---
--- Template arguments used in this method:
--- @arg archive_url
--- @arg archive_date
---
--- @return string - Usable wikitext
local function buildArchiveUrlPart( instance )
    --- adds the original url to the [Original] part
    if instance.args.archive_url == nil or ( instance.args.archive_url ~= nil and (instance.args.url == nil or not stringContains( 'http', instance.args.url ))) then
        return ''
    end

    local date = ''

    if instance.args.archive_date ~= nil then
        date = ' ' .. TNT.format( 'Module:Quelle/i18n.json', 'txt_on', instance.args.archive_date )
    end

    table.insert( instance.parts, TNT.format( 'Module:Quelle/i18n.json', 'txt_archived', makeLinkText( instance.args.url, 'Original' ), date ) )
end

--- Website Part
--- ''website.''
---
--- Template arguments used in this method:
--- @arg website
---
--- @return string - The website part
local function buildWebsitePart( instance )
    local websiteName = instance.args.website or instance.website

    if websiteName ~= nil then
        table.insert( instance.parts, string.format( "''%s''.", websiteName ) )
    end
end

--- Page Part, allow to specify a specific page e.g. in a pdf
--- Seite page.
---
--- Template arguments used in this method:
--- @arg page
---
--- @return string - The page part
local function buildPagePart( instance )
    if instance.args.page == nil then
        return
    end

    table.insert( instance.parts, TNT.format( 'Module:Quelle/i18n.json', 'txt_page', instance.args.page ) )
end

--- Section Part, allow to specify a specific section somewhere
--- Abschnitt section.
---
--- Template arguments used in this method:
--- @arg section
---
--- @return string - The section part
local function buildSectionPart( instance )
    if instance.args.section == nil then
        return
    end

    table.insert( instance.parts, TNT.format( 'Module:Quelle/i18n.json', 'txt_section', instance.args.section ) )
end

--- Accessed Date Part, the date the source was accessed
--- Abgerufen am access_date.
---
--- Template arguments used in this method:
--- @arg access_date
---
--- @return string - The accessed date part
local function buildAccessedDatePart( instance )
    local date = instance.args.access_date or instance.args.accessdate

    if date == nil then
        return
    end
    
    date = mw.getCurrentFrame():preprocess( date )

    table.insert( instance.parts, TNT.format( 'Module:Quelle/i18n.json', 'txt_retrieved', date ) )
end

--- Wraps the text in <ref> tags
---
--- @param text string The whole inner ref output
---
--- Template arguments used in this method:
--- @arg ref_name string - The optional reference name
--- @arg ref_group - The optional reference group
---
--- @return string The wrapped text
local function wrapInRef( instance, text )
    local options = {}

    if instance.args.ref_name ~= nil then
        options.name = tostring( instance.args.ref_name )
    end

    if instance.args.ref_group ~= nil then
        options.group = tostring( instance.args.ref_group )
    end

    return mw.getCurrentFrame():extensionTag( 'ref', text, options )
end

--- Build all parts of the reference
--- This defines the order of the outputted parts
---
--- @return string Ref output
local function buildParts( instance )
    buildAuthorPart( instance )
    buildUrlPart( instance )
    buildArchiveUrlPart( instance )
    buildWebsitePart( instance )
    buildPagePart( instance )
    buildSectionPart( instance )
    buildAccessedDatePart( instance )

    local text = table.concat( instance.parts, ' ' )

    if instance.args.wrap or instance.args.ref then
        return wrapInRef( instance, text )
    end

    return text
end

--- Checks if a deprecated template argument was used, sets a maintenance category
--- We'll only check 'extra_text' for now
local function checkOldArgumentNames( instance )
    if instance.args.extra_text ~= nil then
        table.insert( instance.categories, '[[Kategorie:Seite mit veraltetem Vorlagenparameter]]')
    end
end

--- Set the current frame for this instance
--- Automatically parses args
--- @param frame table - The current frame
function methodtable.setFrame( self, frame )
    self.args = require( 'Module:Arguments' ).getArgs( frame )
end

--- Format the arguments to text
---
--- @param arguments table|nil If param is set, overwrites the frame args
--- @return string
function methodtable.format( instance, arguments )
    if type( arguments ) == 'table' then
        instance.args = arguments
    end

    local text = buildParts( instance )

    if #instance.categories > 1 then
        return table.concat( instance.categories )
    end

    checkOldArgumentNames( instance )

    return text .. table.concat( instance.categories )
end

--- DEPRECATED
function Quelle.setArgs()

end

--- Entry point for template calls
--- @return string
function Quelle.format( frame )
    if frame == nil then
        return '[[Kategorie:Seite mit veraltetem Vorlagenaufruf]]'
    end

    local instance = Quelle:new()
    instance:setFrame( frame )

    return instance:format()
end

--- New Instance
--- Library entrance
function Quelle.new( self )
    local instance = {
        args = nil,
        categories = {},
        parts = {},
        website = nil,
    }

    setmetatable( instance, metatable )

    return instance
end

return Quelle