Module:HF

-- HF stands for High Frequency. -- This Module augments the built-in HF local HF = mw.InfoboxBuilderHF

-- Libraries of functions --

-- Parses invocation and template parameters, trims whitespace, and removes blanks. local getArgs = require('Dev:Arguments').getArgs --- -- Libraries of data -- ---

-- Local functions (used only in this Module) --

-- -- Public functions (called from a Template or article) -- -- -- eliminates the link portion wikitext of a File: call, leaving only the bare filename. function HF.stripFileWrapper(frame) local args = getArgs(frame) return HF._stripFileWrapper(args[1]) end

-- returns the first argument if a content namespace, else the second function HF.content(frame) local args = getArgs(frame) return mw.title.getCurrentTitle.isContentPage and (args[1] or '') or (args[2] or '') end

-- returns preload template indicator function HF.PreloadPage(frame) local out = { '', ('This page is a preload template. Simply click one of the template preloads from the menu in the editor and its contents will be placed on the page that you are editing. More info on preloads can be found at this %s.') :format(HF.Link('Template:PreloadPage/doc', 'documentation page')), HF.Category('Preload templates') } return (mw.title.getCurrentTitle:inNamespace( 10 )    and mw.title.getCurrentTitle.text:lower:match('preload$')) and table.concat(out) or nil end

-- returns ifincl function HF.match(frame) local args = getArgs(frame) --  local test = args[lower] and args[1]:lower or args[1] or '' local subject = args[lower] and args[2]:lower or args[2] or '' return subject:match(test) and (args[3] or ) or (args[4] or ) end

-- returns backreferenced argument, if it exists function HF.backrefargs(frame) local iargs = getArgs(frame, {   	trim = true,    	removeBlanks = true,    	frameOnly = true    }) local targs = getArgs(frame, {   	trim = true,    	removeBlanks = true,    	parentOnly = true    }) local baseargument, ordinal = assert(iargs[1], "No base argument given"), assert(iargs[2], "No argument number given") return HF._backrefargs(baseargument, ordinal, targs) end

-- checks |image -- checks |image1 – |image20 for valid file lines -- parses corresponding caption (|version1) if they exist -- detects gallery and tabber in those entries and strips the wrapper to get at the bare lines -- checks for multiple files in a single entry and creates gallery entries for each function HF.galleryLines(frame) end

- -- Internal functions (used in this and other Modules) -- - -- eliminates whitespace from the front and back of a string function HF.trim(s) if type(s) == 'string' then return (s:gsub("^%s*(.-)%s*$", '%1')) else return false end end -- This creates an external link. function HF.ExternalLink( target, label, plain ) local output = string.format('[%s %s]', target, label) if plain == true then output = string.format(' %s ', output) end return output end -- This creates a link to a category, as well as placing it in that category. -- `sortkey` and `label` are optional -- If there's no `label` given, it will only place it in the category, -- which is what HF.Category is for. function HF.CategoryLink( category, sortkey, label ) if not HF.isempty( label ) then return HF.LinkToCategory( category, label ) .. HF.Category( category, sortkey ) else return HF.Category( category, sortkey ) end end -- Adds a Category -- `sortkey` is optional function HF.Category( category, sortkey ) if sortkey == nil then sortkey = '' else sortkey = '|' .. sortkey end return string.format('%s'..'Category:%s%s]]', '[[', category, sortkey) end -- Adds a link to a Category function HF.LinkToCategory( category, label ) return string.format('%s'..'Category:%s|%s]]', '[[:', category, label or 'Category:' .. category ) end -- Adds an internal link -- `label` is optional function HF.Link( link, text ) if not HF.isempty( text ) then return string.format('%s'..'%s|%s]]', '[[', link, text) else return string.format('%s'..'%s]]', '[[', link) end end -- eliminates the link portion wikitext of a File: call, leaving only the bare filename. function HF._stripFileWrapper (s) if type(s) == 'string' then return (s   :gsub("%[*(.*)", '%1') -- leading brackets    :gsub("([%a]*)|+.*", '%1') -- arguments    :gsub("([%a]*)%]*", '%1') -- closing brackets    ) else return false end end

-- returns backreferenced argument, if it exists function HF._backrefargs(baseargument, argumentnumber, targs) argumentnumber = tonumber( argumentnumber ) and tonumber( argumentnumber ) or argumentnumber local synth_argument = type(argumentnumber) == 'number' and (baseargument or '')..tonumber(argumentnumber) or baseargument or nil if not targs then return synth_argument end local value = synth_argument and targs[synth_argument] or nil local backref_num = value and value:match('^%$(%d+)$') or nil local backref_argument = backref_num and (baseargument or '')..backref_num or nil local backref_value = backref_argument and targs[backref_argument] or nil return backref_value or value or nil end

- -- Output (send it back to whatever called it) -- - return HF