Module:Gutenberg

From Zoophilia Wiki
Revision as of 13:41, 4 October 2015 by meta>GreenC
Jump to navigationJump to search

Documentation for this module may be created at Module:Gutenberg/doc

local p = {}
 
function p.author(frame)
  
  local pframe = frame:getParent()
  local args = pframe.args
 
  local tname = "Gutenberg author" -- name of calling template. Change if template is renamed.
 
  local id       = nil -- author name, or author number. The later will go direct to the author page, the former to a search results page.
  local name     = nil -- display name on Wikipedia (default: article title)
  local url      = nil
  local tagline  = "at [[Project Gutenberg]]"
  local urlheadname  = "//www.gutenberg.org/author/"
  local urlheadnumb  = "//www.gutenberg.org/ebooks/author/"
  local urlhead  = nil

  if args.id == "" or args.id == nil then
    -- return "Error in {{" .. tname .. "}}, id missing. Please see documentation."
    return error("Parameter id is missing")
  else
    id = mw.text.trim(args.id)
    if tonumber(id) ~= nil then -- it's a number
      urlhead = urlheadnumb
    else
      urlhead = urlheadname
    end
  end 

  if args.name == "" or args.name == nil then
    name = mw.title.getCurrentTitle().basePageTitle
  else
    name = mw.text.trim(args.name)
  end

  url = "[" .. urlhead .. id .. " Works by " .. name .. "] " .. tagline

  return url

end

--[[
replace 

Source: https://en.wikipedia.org/wiki/Module:String#replace

This function allows one to replace a target string or pattern within another string.

Usage:
{{#invoke:String|replace|source_str|pattern_string|replace_string|replacement_count|plain_flag}}
OR
{{#invoke:String|replace|source=source_string|pattern=pattern_string|replace=replace_string|
   count=replacement_count|plain=plain_flag}}

Parameters
    source: The string to search
    pattern: The string or pattern to find within source
    replace: The replacement text
    count: The number of occurences to replace, defaults to all.
    plain: Boolean flag indicating that pattern should be understood as plain
        text and not as a Lua style regular expression, defaults to true 
]]
function replace( frame )
    local new_args = str._getParameters( frame.args, {'source', 'pattern', 'replace', 'count', 'plain' } ); 
    local source_str = new_args['source'] or '';
    local pattern = new_args['pattern'] or '';
    local replace = new_args['replace'] or '';
    local count = tonumber( new_args['count'] );
    local plain = new_args['plain'] or true;
        
    if source_str == '' or pattern == '' then
        return source_str;
    end    
    plain = str._getBoolean( plain );

    if plain then
        pattern = str._escapePattern( pattern );
        replace = mw.ustring.gsub( replace, "%%", "%%%%" ); --Only need to escape replacement sequences.
    end
    
    local result;

    if count ~= nil then
        result = mw.ustring.gsub( source_str, pattern, replace, count );
    else
        result = mw.ustring.gsub( source_str, pattern, replace );
    end        

    return result;
end


return p