MTG Wiki
Register
No edit summary
(More, better error messages)
Line 13: Line 13:
   
 
}
 
}
  +
  +
local ERR_MALFORMED = "Malformed URL"
  +
local ERR_UNKNOWN = "Unknown tumblr (use [[Template:TumblrRef]] or add to [[Module:EzTumblr]]?)"
  +
local ERR_NOTITLE = "No title in template or URL (missing slug?)"
   
 
-- Match the bit between the double-slash and the first period in the given URL
 
-- Match the bit between the double-slash and the first period in the given URL
Line 18: Line 22:
 
local tumblrAddress = frame.args[1]
 
local tumblrAddress = frame.args[1]
 
local subdomain = tumblrAddress:match("^%w+://([^%.]+)")
 
local subdomain = tumblrAddress:match("^%w+://([^%.]+)")
  +
assert(Ez.tumblrs[subdomain], "Unknown tumblr!")
 
  +
assert(subdomain, ERR_MALFORMED)
 
assert(Ez.tumblrs[subdomain], ERR_UNKNOWN)
  +
 
return Ez.tumblrs[subdomain].author
 
return Ez.tumblrs[subdomain].author
 
end
 
end
Line 25: Line 32:
 
local tumblrAddress = frame.args[1]
 
local tumblrAddress = frame.args[1]
 
local subdomain = tumblrAddress:match("^%w+://([^%.]+)")
 
local subdomain = tumblrAddress:match("^%w+://([^%.]+)")
  +
assert(Ez.tumblrs[subdomain], "Unknown tumblr!")
 
  +
assert(subdomain, ERR_MALFORMED)
 
assert(Ez.tumblrs[subdomain], ERR_UNKNOWN)
  +
 
return Ez.tumblrs[subdomain].title
 
return Ez.tumblrs[subdomain].title
 
end
 
end
   
-- Generates a title for the post if not provided and the editor left the human-readable tail on the URL
+
-- Generates a title for the post if not provided and the editor left the human-readable slug on the URL
 
function Ez.createPostTitle(frame)
 
function Ez.createPostTitle(frame)
 
local tumblrAddress = frame.args[1]
 
local tumblrAddress = frame.args[1]
 
local slug = tumblrAddress:match("/([%w%-]+)$")
if not tumblrAddress:find("%w$") then return mw.html.create("span"):addClass("scribunto-error"):wikitext("No title in template or URL") end
 
  +
 
  +
assert(slug, ERR_NOTITLE)
local tail = tumblrAddress:match("/([%w%-]+)$")
 
  +
tail = tail:gsub("^%l", string.upper)
 
tail = tail:gsub("%-", " ") .. "..."
+
slug = slug:gsub("^%l", string.upper)
  +
slug = slug:gsub("%-", " ") .. "..."
return tail
+
return slug
 
end
 
end
   

Revision as of 00:39, 29 June 2017

This module stores information about commonly referenced Tumblr accounts, and retrieves that information when provided a URL from a known Tumblr account.

You should not need to use this module directly. {{EzTumblr}} uses this module to simplify {{TumblrRef}}.

If you find that you are often referencing a Tumblr not known by this module, feel free to edit the table at the top. Please be careful to match the syntax used.


local Ez = {}

-- Editors: make sure to carefully copy syntax, including the comma at the end of any new entry
Ez.tumblrs = {
  -- WotC staff
  ["markrosewater"] = { author = "Mark Rosewater", title = "Blogatog" },
  ["dougbeyermtg"] = { author = "Doug Beyer", title = "A Voice for Vorthos" },
  ["tabakrules"] = { author = "Matt Tabak", title = "Snarkham Asylum" },
  ["wizardsmagic"] = { author = "WotCStaff", title = "The Official Magic: The Gathering Tumblr" },

  -- Other notable MtG tumblrs
  ["vorthosjay"] = { author = "Jay Annelli", title = "Archive Trap Mini" },

}

local ERR_MALFORMED = "Malformed URL"
local ERR_UNKNOWN = "Unknown tumblr (use [[Template:TumblrRef]] or add to [[Module:EzTumblr]]?)"
local ERR_NOTITLE = "No title in template or URL (missing slug?)"

-- Match the bit between the double-slash and the first period in the given URL
function Ez.getAuthor(frame)
  local tumblrAddress = frame.args[1]
  local subdomain = tumblrAddress:match("^%w+://([^%.]+)")

  assert(subdomain, ERR_MALFORMED)
  assert(Ez.tumblrs[subdomain], ERR_UNKNOWN)

  return Ez.tumblrs[subdomain].author
end

function Ez.getTitle(frame)
  local tumblrAddress = frame.args[1]
  local subdomain = tumblrAddress:match("^%w+://([^%.]+)")

  assert(subdomain, ERR_MALFORMED)
  assert(Ez.tumblrs[subdomain], ERR_UNKNOWN)

  return Ez.tumblrs[subdomain].title
end

-- Generates a title for the post if not provided and the editor left the human-readable slug on the URL
function Ez.createPostTitle(frame)
  local tumblrAddress = frame.args[1]
  local slug = tumblrAddress:match("/([%w%-]+)$")

  assert(slug, ERR_NOTITLE)

  slug = slug:gsub("^%l", string.upper)
  slug = slug:gsub("%-", " ") .. "..."
  return slug
end

return Ez