Перейти к содержимому страницы
Переключить меню
Переключить меню настроек
Переключить персональное меню
Вы не представились системе
Ваш IP-адрес будет виден всем, если вы внесёте какие-либо изменения.
Сообщество «Повелителя Тайн»Telegram — новости, дунхуа, игры и мерчDiscord — обсуждение книг и теорий, вопросы по вики

Модуль:Helpers/WikiText

Материал из проекта «Повелитель Тайн Вики»
local p = {}

function p.sanitiseHtml(input)
	return input
	    :gsub("&", "&")    -- Replace & with &
	    :gsub("<", "&lt;")     -- Replace < with &lt;
	    :gsub(">", "&gt;")     -- Replace > with &gt;
	    :gsub('"', "&quot;")   -- Replace " with &quot;
	    :gsub("'", "&apos;")   -- Replace ' with &apos;
end

function p.refTag(content, name, group)
    return string.format(
    	"<ref %s %s>%s</ref>", 
    	
    	-- Dodgey ternary operation. condition AND value OR alt_value
    	group and string.format([[group="%s"]], group) or '',
    	name and string.format([[name="%s"]], name) or '',
    	content
    )
end

function p.desktopOnly(text)
	return string.format('<div class="nomobile">%s</div>', text)
end

function p.getElementByClassInvoker(frame)
	local html = frame.args[1]
	local className = frame.args[2]

    local pos = 1

    while pos <= #html do
        -- Find the next opening tag
        local tagStart, tagEnd, tag = html:find("<(%w+)([^>]*)>", pos)
        if not tagStart then break end

        local attrs = html:sub(tagEnd - #tag + 1, tagEnd) -- not used directly
        -- Get full tag attributes
        local _, attrEnd, tagName, attrStr = html:find("<(%w+)([^>]*)>", tagStart)

        -- Check if class attribute contains the target class
        local classAttr = attrStr:match('class%s*=%s*"([^"]*)"')
                       or attrStr:match("class%s*=%s*'([^']*)'")

        if classAttr then
            -- Check for exact class match (space-separated)
            for cls in classAttr:gmatch("%S+") do
                if cls == className then
                    -- Now find the matching closing tag
                    local depth = 1
                    local searchPos = attrEnd + 1
                    local fullTag = "<" .. tagName

                    while depth > 0 and searchPos <= #html do
                        local nextOpen = html:find("<" .. tagName .. "[%s>]", searchPos)
                        local nextClose = html:find("</" .. tagName .. ">", searchPos)

                        if not nextClose then break end

                        if nextOpen and nextOpen < nextClose then
                            depth = depth + 1
                            searchPos = nextOpen + #fullTag
                        else
                            depth = depth - 1
                            if depth == 0 then
                                local closeEnd = nextClose + #tagName + 2
                                return html:sub(tagStart, closeEnd)
                            end
                            searchPos = nextClose + #tagName + 3
                        end
                    end

                    break
                end
            end
        end

        pos = tagEnd + 1
    end

    return nil
end

return p