Изменения

м
Нет описания правки
Строка 5: Строка 5:  
end
 
end
   −
-- For debug
+
 
function ctd.test( frame )
+
-- Convert local links [[link|text]] and URL [link text] to plain text.
local i, j = string.find(frame.args['userparam'],'@',1,true)
+
-- See original function in module PM.
local sub1 = string.sub( string.lower(frame.args['userparam']), 1, i-1 )
+
local function remove_links( wiki_str )
local sub2 = string.sub( string.lower(frame.args['userparam']), j+1 )
+
local str = wiki_str
 +
 
 +
-- remove local links
 +
i = mw.ustring.find(str,'[[',1,true)
 +
j = mw.ustring.find(str,']]',i,true)
 +
while i ~= nil do
 +
local link_text = mw.ustring.sub(str, i+2, j-1)
 +
local k = mw.ustring.find(link_text, '|', 1, true)  
 +
if k ~= nil then
 +
link_text = mw.ustring.sub(link_text, k+1)
 +
end
 +
str = mw.ustring.sub(str, 1, i-1) .. link_text .. mw.ustring.sub(str, j+2, -1)
 +
i = mw.ustring.find(str,'[[',1,true)
 +
j = mw.ustring.find(str,']]',i,true)
 +
end
 +
 +
-- remove URL
 +
i = mw.ustring.find(str,'[http',1,true)
 +
j = mw.ustring.find(str,']',i,true)
 +
while i ~= nil do
 +
local link_text = mw.ustring.sub(str, i+1, j-1)
 +
local k = mw.ustring.find(link_text, ' ', 1, true)
 +
if k ~= nil then
 +
link_text = mw.ustring.sub(link_text, k+1)
 +
end
 +
str = mw.ustring.sub(str, 1, i-1) .. link_text .. mw.ustring.sub(str, j+1, -1)
 +
i = mw.ustring.find(str,'[http',1,true)
 +
j = mw.ustring.find(str,']',i,true)
 +
end
 
 
return 'userparam='.. frame.args['userparam'] .. '; i=' .. i .. '; j=' .. '; sub1=' .. sub1 .. '; sub2=' .. sub2
+
return str
 
end
 
end
 +
    
-- Create short description
 
-- Create short description
Строка 28: Строка 57:  
result = mw.ustring.sub(result, 1, i-1)
 
result = mw.ustring.sub(result, 1, i-1)
 
end
 
end
-- convert local links [[link|text]] to plain text
+
-- convert all links to plain text
result = mw.text.unstrip(result)
+
result = remove_links(result)
i = mw.ustring.find(result,'[[',1,true)
  −
while i ~= nil do
  −
local j = mw.ustring.find(result,'|',i,true)
  −
local k = mw.ustring.find(result,']]',i,true) -- assume that there is no mistakes and brackets are always closed; otherwise we should consider k == nil
  −
local str = mw.ustring.sub(result, i+2, k-1)
  −
if j ~= nil then
  −
str = mw.ustring.sub(result, j+1, k-1)
  −
end
  −
result = mw.ustring.sub(result, 1, i-1) .. str .. mw.ustring.sub(result, k+2, -1)
  −
i = mw.ustring.find(result,'[[',1,true)
  −
end
  −
 
  −
-- convert external links [link text] to plain text
  −
 
   
-- take only first 100 characters; string.sub() uses bytes instead of chars
 
-- take only first 100 characters; string.sub() uses bytes instead of chars
 
if mw.ustring.len(result) > 100 then
 
if mw.ustring.len(result) > 100 then