Hoppa till innehållet

Modul:Rating

Från Wikipedia

Dokumentation [visa] [redigera] [historik] [rensa sidcachen]


local p = {}
-- Based on the wikicode in http://en.wikipedia.org/w/index.php?title=Template:Rating&oldid=486677637

-- Arguments
-- The first unnamed parameter is the number of the rating received.
-- The second unnamed parameter is the maximum possible for the rating system.

-- Optional arguments
-- "score" is used to change the alt text when mousing over the rating, in the case where "x/x" is not accurate or more information can be added.
-- "full", "half" and "empty" are used to change the images from the default stars to images you specify for each case.
-- rating= should be used to name the new rating symbol if you change the images.
-- rating-plural= accepts non-standard pluralization in the tooltip to be used instead of adding "s" in simple cases.
-- size= can be used to adjust the width at which the rating symbols display.

-- Testing with the Debug console:
-- frame = {} 
-- frame.args = {1,2}
-- =p.whole_and_half(frame)

function p.whole_and_half(frame)
    local arguments = frame.args -- the arguments passed BY the template, in the wikitext of the template itself
    local num = tonumber(arguments[1])
    local max = tonumber(arguments[2])
    if num<0 or max<1 or num>max then
        return "<span class=\"error\">Felaktiga värden till '''Modul:Rating'' (" .. num .. ", " .. max .. ")</span>"
    end 
    local full = arguments['full'] or 'Star_full.svg'
    local half = arguments['half'] or 'Star_half.svg'
    local empty = arguments['empty'] or 'Star_empty.svg'
    local score = arguments['score']
    local rating = arguments['rating'] or 'star'
    if num > 1 then
        rating = arguments['rating-plural'] or 'stars'
    end
    local size = arguments['size']
    if size == nil then
        if num>6 then
            size=7
        else
            size=11
        end
    end
    local text = "<span style=\"white-space:nowrap\" title=\"" .. num .. "/" .. max .. " " .. rating .. "\">"
    local symbol=""
    for loop = 1, math.floor(num) do
        text = text .. "[[File:" .. full .. "|" .. size .. "px|link=|alt=]]"
    end
    if num < max then
        if num < math.floor(num) + 0.25 then
            symbol = empty
        elseif num > math.floor(num) + 0.75 then
            symbol = full
        else
            symbol = half
        end
        text = text .. "[[File:" .. symbol .. "|" .. size .. "px|link=|alt=]]"

        for loop = math.floor(num) + 1, max - 1 do
            text = text .. "[[File:" .. empty .. "|" .. size .. "px|link=|alt=]]"
        end
    end
    return text .. "</span>"
end  
return p