Module:Highlight

MyWikiBiz, Author Your Legacy — Saturday May 04, 2024
Jump to navigationJump to search

Template:Module rating

Usage

{{#invoke:Highlight|main|regex|page name|style}}

  • regex is a Lua pattern (a somewhat limited form of regular expression -- see mw:Extension:Scribunto/Lua_reference_manual#Patterns). Note that quotes around the regex are not needed and captures should have no effect (however, you can use them to break up blocks of [ or { characters that interfere with the processing of the #invoke block). Omitting the regex results in blank output.
  • page is the name of the page to highlight. However, it can be omitted, in which case the current page is used. However, if the page name ends in "/highlight", then that part is removed before the page is looked up.
  • style is an optional parameter. In addition to the default, it can presently be "yellow" for a classic yellow highlighter look, or "oval" for a bolder, oval-ish red outline. The span style="..." statements for accomplishing this are defined near the top of the module for your convenience in editing.
  • All of these parameters can be passed with names (regex = , page = , style =) if desired.
  • Therefore, a convenient way to use this function is to preview the text {{#invoke:Highlight|main|''regex''}} at (your-page-name)/highlight to view the text of (your-page-name) with markup added. Unless you'd like to share the marked-up text with others you shouldn't Save the page.



local p = {}

local MARKUP = {['default'] = '<span style="border:solid red 1px">%1</span>',
                ['yellow'] = '<span style="background:yellow">%1</span>',
                ['oval'] = '<span style="border:solid red 2px;border-radius:10px;padding:1px">%1</span>'}
                    
function p.main(frame)
	if frame then
        local parent = frame.getParent and frame:getParent()
        if parent and parent.args then
        	regex = parent.args[1] or parent.args['regex']
        	page = parent.args[2] or parent.args['page']
        	style = parent.args[3] or parent.args['style']
        end
        if frame.args then
        	regex = frame.args[1] or frame.args['regex']
        	page = frame.args[2] or frame.args['page']
        	style = frame.args[3] or frame.args['style']
        end
    else
    	return ''
    end
	if not page or mw.text.trim(page) == '' then
		page = frame:preprocess("{{FULLPAGENAME}}")
		if string.sub(page,-10,-1)  == '/highlight' then
			page = string.sub(page,1, -11)
		end
    end
    if style and mw.text.trim(style) ~= "" then
    else
    	style = "default"
    end
    local replace = MARKUP[style]
    -- OK, we now are searching for regex in page
    pageobject = mw.title.new(page)
    if not pageobject then return '' end
    text = pageobject:getContent()
    text = mw.ustring.gsub(text, "(" .. regex .. ")", replace)
    return frame:preprocess(text)
end

return p