Changes

812 bytes added ,  13:47, 3 December 2023
Update package: OSW Core
Line 3: Line 3:  
p.file_type_mapping = {
 
p.file_type_mapping = {
 
["py"] = "python",
 
["py"] = "python",
["json"] = "json"
+
["json"] = "json",
 +
["md"] = "markdown",
 +
["yml"] = "yaml",
 +
["yaml"] = "yaml",
 
}
 
}
   Line 9: Line 12:  
local url = frame.args['url']
 
local url = frame.args['url']
 
local lang = frame.args['lang']
 
local lang = frame.args['lang']
 +
if (lang == "") then lang = nil end
 
local show_line_numbers = frame.args['show_line_numbers']
 
local show_line_numbers = frame.args['show_line_numbers']
if (show_line_numbers == "" or show_line_numbers == "0" or show_line_numbers == "false") then show_line_numbers = nil end
+
if (show_line_numbers == nil or show_line_numbers == "" or show_line_numbers == "0" or show_line_numbers == "false") then show_line_numbers = false
 +
else show_line_numbers = true end
 
local highlight = frame.args['highlight']
 
local highlight = frame.args['highlight']
 +
local render_markdown = frame.args['render_markdown']
 +
if (render_markdown == nil or render_markdown == "") then render_markdown = true end
 +
if (render_markdown == "0" or render_markdown == "false") then render_markdown = false end
    
local url_parts = p.splitString(url, "#")
 
local url_parts = p.splitString(url, "#")
Line 19: Line 27:  
local start_line = frame.args['start_line']
 
local start_line = frame.args['start_line']
 
local end_line = frame.args['end_line']
 
local end_line = frame.args['end_line']
if ((start_line == nil or start_line == "") and anchor ~= nil) then start_line =  p.splitString(p.splitString(anchor, "-")[1], "L")[1] end
+
if ((start_line == nil or start_line == "") and anchor ~= nil) then start_line =  p.splitString(p.splitString(p.splitString(anchor, "-")[1], "L")[1], "C")[1] end -- '#L65C1-L80C10' => 65
 
if ((end_line == nil or end_line == "") and anchor ~= nil) then  
 
if ((end_line == nil or end_line == "") and anchor ~= nil) then  
 
end_line =  p.splitString(anchor, "-")[2]
 
end_line =  p.splitString(anchor, "-")[2]
if (end_line ~= nil) then end_line =  p.splitString(end_line, "L")[1] end
+
if (end_line ~= nil) then end_line =  p.splitString(p.splitString(end_line, "L")[1], "C")[1] end -- '#L65C1-L80C10' => 10
 
end
 
end
 
mw.logObject(start_line)
 
mw.logObject(start_line)
Line 51: Line 59:  
mw.logObject(res["__text"])
 
mw.logObject(res["__text"])
 
 
lang = p.file_type_mapping[file_type]
+
if (lang == nil) then lang = p.file_type_mapping[file_type] end
 
if (lang == nil) then lang = "text" end
 
if (lang == nil) then lang = "text" end
 
 
local hl_params = {}
+
local wikitext = ""
hl_params["lang"] = lang
+
if (start_line ~= nil and start_line ~= "") then hl_params["start"] = start_line end
+
if (lang == "markdown" and render_markdown) then
if (show_line_numbers ~= nil and show_line_numbers ~= "") then hl_params["line"] = "1" end
+
-- https://www.mediawiki.org/wiki/Extension:WikiMarkdown
if (highlight ~= nil and highlight ~= "") then  
+
wikitext = frame:extensionTag{ name = "markdown" , content = res["__text"] }
if (start_line ~= nil and start_line ~= "") then
+
else
local highlight_with_offset =""
+
local hl_params = {}
local offset = tonumber(start_line) - 1
+
hl_params["lang"] = lang
d='(%D-)'
+
-- extract line numbers from url
for k,i,j in highlight:gmatch(d..'(%d+)'..d) do highlight_with_offset = highlight_with_offset .. k .. i-offset .. j end
+
if (start_line ~= nil and start_line ~= "") then hl_params["start"] = start_line end
hl_params["highlight"] = highlight_with_offset  
+
if (show_line_numbers) then hl_params["line"] = "1" end
else
+
if (highlight ~= nil and highlight ~= "") then  
hl_params["highlight"] = highlight
+
if (start_line ~= nil and start_line ~= "") then
 +
local highlight_with_offset =""
 +
local offset = tonumber(start_line) - 1
 +
d='(%D-)'
 +
for k,i,j in highlight:gmatch(d..'(%d+)'..d) do highlight_with_offset = highlight_with_offset .. k .. i-offset .. j end
 +
hl_params["highlight"] = highlight_with_offset  
 +
else
 +
hl_params["highlight"] = highlight
 +
end
 
end
 
end
 +
mw.logObject(hl_params)
 +
-- https://www.mediawiki.org/wiki/Extension:SyntaxHighlight
 +
wikitext = frame:extensionTag{ name = "syntaxhighlight" , content = res["__text"], args = hl_params }
 
end
 
end
mw.logObject(hl_params)
+
local wikitext = frame:extensionTag{ name = "syntaxhighlight" , content = res["__text"], args = hl_params }
   
mw.logObject(wikitext)
 
mw.logObject(wikitext)
 
return wikitext
 
return wikitext