{{i}} Документація модуля[перегляд] [редагувати] [історія] [очистити кеш]
приклад отримання назви країни з виклику шаблону прапорця
Шаблон прапорця {{UKR}}
Результат Україна Україна
Отримати тільки текст {{#invoke:wikitext|plain|{{UKR}}}}
Результат  Україна
--[[
Службові функції для конверсії вікітексту

Синтаксис: функція з маленької літери - для {{#invoke:wikitext|fun|...}},
                   з великої          - для require(wikitext).Fun()
]]

specns={file='file',image='file',category='cat',
    ['к']='cat',['категорія']='cat',['файл']='file',['зображення']='file'}

linkprefs="http:// gopher:// https:// mailto: news:// ftp:// irc:// //"

local function Delink(text,repl,cat,file,href,colon)
    return ( --видалення вікі-посилань
        mw.ustring.gsub(text, "%[%[(%s*([^|%]]+)%s*)|?(.-)%]%](%a*)", function(a,l,c,e)
        l = l:gsub("%s+"," ");
        if specns[mw.ustring.lower(l):match("^(.*):")] then
            -- ссылка на специальное п. и.
            if specns[mw.ustring.lower(l):match("^(.*):")]=='cat' then--категория
                if cat then -- строчка замены, %1=catname, %2=key, %3=mode, %%="%"
                    return table.concat{ l:gsub("^.-:%s*",''), '|', c }:gsub( "^(.-)|([^|]*)|?(.-)$", cat ) .. e
                else 
                    return e -- удалять по умолчанию все категории
                end
            else--файл. Fixme:пока все поля описания файла не выдаются шаблонам
                if  file then -- строка замены, %1=filename, %2=parameters, %%="%"
                    return table.concat{ l:gsub("^.-:%s*",''), '|', c }:gsub( "^(.-)|(.*)", file ) .. e
                else return e -- вырезать фалы
                end
            end--if(файл или категория)
        end--if(спец. пространства)
        l=string.gsub(l,"^: *", colon or "%1");-- замена начального ":"
        if repl then
            return mw.ustring.gsub(l..'|'..(c=='' and a..e or c..e),"^(.-)|(.*)$",repl)
        else
            if c=='' then return a..e else return c..e end-- вырезаем 
        end
    end
):gsub(
    "(%[([%a%._\/~%%%-%+&#%?!=%(%)@]+)%s*(.-)%])", function(a,r,t)
        --вырезатор внешних ссылок
        local p,i=r:match('^(%a*:?//)(.*)')
        if not p then p,i=r:match('^(mailto:)(.*)') end
        if p then-- Это URL
            if href then
                return (p..' '..i..' '..t):gsub("^(.-) (.-) (.*)$",href) 
            else
                return t
            end
        else-- Это не URL
            return a
        end
    end--вырезатор
) )
end

local function Detag(t)
    if t == nil or t == '' then return '' end
    -- Вырезаем кавычки и > из литералов в тэгах, а потом сами литералы
    local text,j,i,k=t,0;
    repeat 
    text,i=text:gsub('(< /?%a[^>"\']*")[^"]*\\["\\]',"%1");--первая \", \\ из 1-го литерала
    text,k=text:gsub("(< /?%a[^>\"']*')[^']*\\['\\]","%1");--первая \', \\ из 1-го литерала
    if i+k==0 then--1-й литерал очищен
        text,j=text:gsub("(< /?%a[^>]*)(['\"]).-%2","%1")
    end
    until i+j+k==0
    -- и вырезать тэги, а также ''курсив'' и '''жирный'''
    return (text:gsub("'''?",""):gsub('<%s*%a[^>]*>',''))
end

local function Nodis(t) -- Убирает уточнения в скобках
    return (t:gsub('%s*%([^%)]+%)$',''))
end


return {
delink=function(f) -- Удаляет ссылки из викитекста
    local tf=f:getParent()
    return Delink(
        f.args[1] or tf.args[1],
        f.args['repl'] or tf.args['repl'],
        f.args['cat'] or tf.args['cat'],
        f.args['file'] or tf.args['file'],
        f.args['href'] or tf.args['href'],
        f.args[':'] or tf.args[':']
        )
end; Delink=Delink;

detag=function(f)
    return Detag(f.args[1] or f:getParent().args[1])
end; Detag=Detag;

plain=function(f)
    local tf=f:getParent()
    return Detag(Delink(
        f.args[1] or tf.args[1],
        f.args['repl'] or tf.args['repl'],
        f.args['cat'] or tf.args['cat'],
        f.args['file'] or tf.args['file'],
        f.args['href'] or tf.args['href'],
        f.args[':'] or tf.args[':']
        ));
end;

nodis=function(f)
    return Nodis(f.args[1] or f:getParent().args[1])
end;Nodis=Nodis
}