Модуль:Не переведено: различия между версиями
Перейти к навигации
Перейти к поиску
(а надо) |
(поддержка Викиданных, небольшая реструктуризация) |
||
Строка 7: | Строка 7: | ||
local function wikilink(lang, title, text) | local function wikilink(lang, title, text) | ||
− | if is_empty(text) then | + | if is_empty(text) then |
text = title | text = title | ||
end | end | ||
− | if lang ~= 'ru' then | + | if lang ~= 'ru' then |
title = string.format(':%s:%s', lang, title) | title = string.format(':%s:%s', lang, title) | ||
end | end | ||
Строка 29: | Строка 29: | ||
local languages = mw.loadData('Module:Languages/data') | local languages = mw.loadData('Module:Languages/data') | ||
local error = require('Module:Error').error | local error = require('Module:Error').error | ||
− | + | ||
− | |||
− | |||
− | |||
− | |||
− | |||
local leave = yesno(args['l']) | local leave = yesno(args['l']) | ||
Строка 47: | Строка 42: | ||
if is_empty(args[1]) and not args['есть']:match('^:[a-z-]+:') then | if is_empty(args[1]) and not args['есть']:match('^:[a-z-]+:') then | ||
− | if allow_cat then | + | if allow_cat then |
categories = categories .. orphan_categories['error'] | categories = categories .. orphan_categories['error'] | ||
end | end | ||
Строка 55: | Строка 50: | ||
if not is_empty(args['есть']) or args[1]:match('^:[a-z-]+:') then | if not is_empty(args['есть']) or args[1]:match('^:[a-z-]+:') then | ||
local temp | local temp | ||
− | if is_empty(args['есть']) then | + | if is_empty(args['есть']) then |
temp = args[1] | temp = args[1] | ||
else | else | ||
Строка 64: | Строка 59: | ||
args[3] = mw.ustring.sub(temp, 2, mw.ustring.find(temp, ':', 2, true) - 1) | args[3] = mw.ustring.sub(temp, 2, mw.ustring.find(temp, ':', 2, true) - 1) | ||
args[4] = mw.ustring.sub(temp, mw.ustring.find(temp, ':', 2, true) + 1) | args[4] = mw.ustring.sub(temp, mw.ustring.find(temp, ':', 2, true) + 1) | ||
− | if is_empty(args[1]) then | + | if is_empty(args[1]) then |
args[1] = args[4] | args[1] = args[4] | ||
end | end | ||
end | end | ||
− | title = args[1] | + | local title = args[1] |
− | text = args[2] | + | local text = args[2] |
local exists = mw.title.new(title).exists and not leave | local exists = mw.title.new(title).exists and not leave | ||
local ru_link = wikilink('ru', title, text) | local ru_link = wikilink('ru', title, text) | ||
Строка 80: | Строка 75: | ||
) | ) | ||
end | end | ||
− | + | if leave and mw.title.new(title).isRedirect and allow_cat then | |
− | local | + | categories = categories .. orphan_categories['redirect'] |
+ | end | ||
+ | |||
+ | local post_text_list = {} | ||
+ | local addition = args['a'] | ||
+ | local show_original = yesno(args['o']) or addition ~= nil | ||
+ | local wikidata_link = '' | ||
local i, j = 4, 1 | local i, j = 4, 1 | ||
while args[i] or args[i - 1] or i <= 4 do | while args[i] or args[i - 1] or i <= 4 do | ||
+ | |||
local lang = args[i - 1] | local lang = args[i - 1] | ||
local iw_title = args[i] | local iw_title = args[i] | ||
local iw_text = args[i + 1] | local iw_text = args[i + 1] | ||
+ | if is_empty(lang) then | ||
+ | lang = 'en' | ||
+ | end | ||
if is_empty(iw_title) then | if is_empty(iw_title) then | ||
iw_title = title | iw_title = title | ||
Строка 93: | Строка 98: | ||
iw_text = iw_title | iw_text = iw_title | ||
end | end | ||
− | if | + | |
− | + | local lang_text | |
− | + | local lang_title | |
− | + | local lang_link | |
− | if allow_cat then | + | if lang == 'd' then |
+ | iw_text = 'd' | ||
+ | elseif languages[lang] == nil then | ||
+ | if allow_cat then | ||
categories = categories .. orphan_categories['error'] | categories = categories .. orphan_categories['error'] | ||
end | end | ||
− | return error{'некорректный ISO-код | + | return error{'некорректный ISO-код «' .. lang .. '»'} .. categories |
+ | else | ||
+ | lang_text = languages[lang][1] | ||
+ | lang_title = languages[lang][2] | ||
+ | lang_link = wikilink('ru', lang_title, lang_text) | ||
end | end | ||
+ | |||
local iw_link | local iw_link | ||
− | if show_original then | + | if show_original or lang == 'd' then |
iw_link = wikilink(lang, iw_title, iw_text) | iw_link = wikilink(lang, iw_title, iw_text) | ||
else | else | ||
− | iw_link = wikilink(lang, iw_title, | + | iw_link = wikilink(lang, iw_title, lang_text) |
end | end | ||
− | + | ||
− | + | if lang == 'd' then | |
− | [' | + | if not ( mw.isSubsting() or exists ) then |
− | [ | + | wikidata_link = iw_link |
− | + | end | |
− | if show_original then | + | elseif mw.isSubsting() or exists then |
+ | if show_original then | ||
+ | post_text_list[j] = string.format('%s %s', lang_link, italic(iw_text)) | ||
+ | end | ||
+ | elseif show_original then | ||
+ | post_text_list[j] = string.format('%s %s', lang_link, italic(iw_link)) | ||
+ | else | ||
+ | post_text_list[j] = iw_link | ||
+ | end | ||
+ | |||
+ | if show_original and lang ~= 'd' then | ||
i = i + 3 | i = i + 3 | ||
else | else | ||
i = i + 2 | i = i + 2 | ||
end | end | ||
− | j = j + 1 | + | if lang ~= 'd' then |
+ | j = j + 1 | ||
+ | end | ||
end | end | ||
− | + | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
local post_text = '' | local post_text = '' | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
if not is_empty(addition) then | if not is_empty(addition) then | ||
addition = '; ' .. addition | addition = '; ' .. addition | ||
Строка 152: | Строка 152: | ||
addition = '' | addition = '' | ||
end | end | ||
− | if next(post_text_list) ~= nil then | + | if not is_empty(wikidata_link) and next(post_text_list) ~= nil then |
− | post_text = ' (' | + | wikidata_link = wikidata_link .. '; ' |
+ | end | ||
+ | if next(post_text_list) ~= nil or not is_empty(wikidata_link) then | ||
+ | post_text = string.format(' (%s%s%s)', wikidata_link, table.concat(post_text_list, ', '), addition) | ||
if not show_original then | if not show_original then | ||
post_text = tostring( | post_text = tostring( | ||
Строка 166: | Строка 169: | ||
local exist_message = '' | local exist_message = '' | ||
if exists then | if exists then | ||
+ | if allow_cat then | ||
+ | categories = categories .. orphan_categories['outdated'] | ||
+ | end | ||
exist_message = tostring( | exist_message = tostring( | ||
mw.html.create('sup') | mw.html.create('sup') |
Версия от 19:39, 12 декабря 2017
Для документации этого модуля может быть создана страница Модуль:Не переведено/doc
local getArgs = require('Module:Arguments').getArgs
local p = {}
local function is_empty(param)
return param == nil or param == ''
end
local function wikilink(lang, title, text)
if is_empty(text) then
text = title
end
if lang ~= 'ru' then
title = string.format(':%s:%s', lang, title)
end
return string.format('[[%s|%s]]', title, text)
end
local function italic(s)
return tostring(
mw.html.create('span')
:css('font-style', 'italic')
:wikitext(s)
)
end
function p.main(frame)
local yesno = require('Module:Yesno')
local args = getArgs(frame)
local languages = mw.loadData('Module:Languages/data')
local error = require('Module:Error').error
local leave = yesno(args['l'])
local categories = ''
local nocat = yesno(args['nocat'])
local allow_cat = mw.title.getCurrentTitle().namespace == 0 and not nocat
local orphan_categories = {
['error'] = '[[Категория:Википедия:Статьи с некорректно заполненным шаблоном Не переведено]]',
['outdated'] = '[[Категория:Википедия:Статьи с неактуальным шаблоном Не переведено]]',
['redirect'] = '[[Категория:Википедия:Запросы на замену перенаправлений переводами]]'
}
if is_empty(args[1]) and not args['есть']:match('^:[a-z-]+:') then
if allow_cat then
categories = categories .. orphan_categories['error']
end
return error{'не указано название статьи'} .. categories
end
if not is_empty(args['есть']) or args[1]:match('^:[a-z-]+:') then
local temp
if is_empty(args['есть']) then
temp = args[1]
else
temp = args['есть']
end
args[1] = args['надо'] or args['нужно'] or args[2] or ''
args[2] = args['текст'] or args[3]
args[3] = mw.ustring.sub(temp, 2, mw.ustring.find(temp, ':', 2, true) - 1)
args[4] = mw.ustring.sub(temp, mw.ustring.find(temp, ':', 2, true) + 1)
if is_empty(args[1]) then
args[1] = args[4]
end
end
local title = args[1]
local text = args[2]
local exists = mw.title.new(title).exists and not leave
local ru_link = wikilink('ru', title, text)
if exists and not mw.isSubsting() then
ru_link = tostring(
mw.html.create('span')
:css('background', '#FFFF00')
:wikitext(ru_link)
)
end
if leave and mw.title.new(title).isRedirect and allow_cat then
categories = categories .. orphan_categories['redirect']
end
local post_text_list = {}
local addition = args['a']
local show_original = yesno(args['o']) or addition ~= nil
local wikidata_link = ''
local i, j = 4, 1
while args[i] or args[i - 1] or i <= 4 do
local lang = args[i - 1]
local iw_title = args[i]
local iw_text = args[i + 1]
if is_empty(lang) then
lang = 'en'
end
if is_empty(iw_title) then
iw_title = title
end
if is_empty(iw_text) then
iw_text = iw_title
end
local lang_text
local lang_title
local lang_link
if lang == 'd' then
iw_text = 'd'
elseif languages[lang] == nil then
if allow_cat then
categories = categories .. orphan_categories['error']
end
return error{'некорректный ISO-код «' .. lang .. '»'} .. categories
else
lang_text = languages[lang][1]
lang_title = languages[lang][2]
lang_link = wikilink('ru', lang_title, lang_text)
end
local iw_link
if show_original or lang == 'd' then
iw_link = wikilink(lang, iw_title, iw_text)
else
iw_link = wikilink(lang, iw_title, lang_text)
end
if lang == 'd' then
if not ( mw.isSubsting() or exists ) then
wikidata_link = iw_link
end
elseif mw.isSubsting() or exists then
if show_original then
post_text_list[j] = string.format('%s %s', lang_link, italic(iw_text))
end
elseif show_original then
post_text_list[j] = string.format('%s %s', lang_link, italic(iw_link))
else
post_text_list[j] = iw_link
end
if show_original and lang ~= 'd' then
i = i + 3
else
i = i + 2
end
if lang ~= 'd' then
j = j + 1
end
end
local post_text = ''
if not is_empty(addition) then
addition = '; ' .. addition
else
addition = ''
end
if not is_empty(wikidata_link) and next(post_text_list) ~= nil then
wikidata_link = wikidata_link .. '; '
end
if next(post_text_list) ~= nil or not is_empty(wikidata_link) then
post_text = string.format(' (%s%s%s)', wikidata_link, table.concat(post_text_list, ', '), addition)
if not show_original then
post_text = tostring(
mw.html.create('span')
:addClass('noprint')
:css('font-size', '85%')
:wikitext(post_text)
)
end
end
local exist_message = ''
if exists then
if allow_cat then
categories = categories .. orphan_categories['outdated']
end
exist_message = tostring(
mw.html.create('sup')
:addClass('noprint')
:wikitext('[' .. italic('[[Шаблон:Не переведено#Действия после появления перевода|убрать шаблон]]') .. ']')
)
end
local result
result = ru_link .. post_text
if not mw.isSubsting() then
result = result .. exist_message .. categories
end
return result
end
return p