Module:SoyBooruPostID

From Soyjak Wiki, the free ensoyclopedia
Jump to navigationJump to search

This module supplies links to original SoyBooru posts for files using {{Booru}} with the post ID present in the file name.

Usage

Only intended for {{Booru}}.

{{#invoke:SoyBooruPostID|main}}

Auto detection

If no manual post ID is provided with {{Booru|11111}}, the module will attempt to extract one from the filename. To qualify:

  • Page has to be in file namespace (6),
  • Filename must contain soybooru, regardless of capitalization (subject to change)
  • a Post ID of at least three digits at the beginning of the filename (as is the case when downloaded from SoyBooru), followed by space or punctuation (such as - ).

Automatically generated links include a notice indicating that the post ID was detected by the module.

Manual ID

A post ID can be manually supplied in {{Booru}}, for example:

{{Booru|12345}}

Manual IDs must at least contain three digits. Supplying an explicitly blank parameter suppresses the post link.


local p = {}
local enabledNamespace = 6

local function category(name)
    return '[[Category:' .. name .. ']]'
end

local function makeLink(id)
    return 'See the original post [https://soybooru.com/post/view/' .. id .. '/ here].'
end

function p.main(frame)
    local parent = frame:getParent()
    local args = parent and parent.args or frame.args
    local manual = args[1]
    local title = mw.title.getCurrentTitle()
    local namespaceEnabled = title.namespace == enabledNamespace

    if manual ~= nil then
        manual = mw.text.trim(manual)

        if manual == '' then
            return ''
        end

        if not manual:match('^%d%d%d+$') then
            if namespaceEnabled then
                return category('SoyBooru files with invalid post IDs')
            end

            return ''
        end

        local result = makeLink(manual)

        if namespaceEnabled then
            result = result .. category('Files with detected SoyBooru post IDs')
        end

        return result
    end

    if not namespaceEnabled then
        return ''
    end

    local text = title.text

    if not mw.ustring.lower(text):find('soybooru', 1, true) then
        return category('SoyBooru files without post IDs')
    end

    local id = text:match('^(%d%d%d+)[%s%p]')

    if not id then
        return category('SoyBooru files without post IDs')
    end

    local notice = frame:expandTemplate {
        title = 'Hatnote',
        args = {
            '<small>Post ID automatically detected by [[Module:SoyBooruPostID]]</small>'
        }
    }

    return makeLink(id)
        .. notice
        .. category('Files with detected SoyBooru post IDs')
end

return p