Changes

Jump to: navigation, search

Module:Wikitext

4,403 bytes added, 18:26, 1 June 2015
проверка на отсутствие параметров вообще
--[[
Служебные функции - конверсии викитекста

Синтаксис: функция со строчной буквы - - {{#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('</?%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
}
Anonymous user

Navigation menu