Jump to content
Toggle menu
  • 8 articles
  • 75 files
  • 7 users
  • 37.8K edits
Neodyland Wiki
Toggle preferences menu
Toggle personal menu
Not logged in
Your IP address will be publicly visible if you make any edits.

Module:FileName

From Neodyland Wiki
Revision as of 10:30, 4 December 2013 by commons>Rillke (with HTML-parser (that in turn has also issues sometimes))
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Module documentation[ create · purge ]
This documentation is transcluded from Module:FileName/doc.
-- Quick experiment with lua
-- Module for removing the extension from a file name (Should this or something like it be part of module:File?)
local p = {}
p.removeExtension = function(frame)
        local page = frame.args[1] or frame.args["file"] or frame.args["title"] or mw.title.getCurrentTitle().text
	local parts = mw.text.split( page , '.', true )
        local upTo = #parts - 1
        if (upTo) == 0 then
	        upTo = 1
        end
        return table.concat( parts, '.', 1, upTo )
end
-- I Probably shouldn't write this. This is a bad plan... I feel dirty...
-- Also probably unreliable, don't use it for anything important.
p.getYearFromInfo = function(frame)
       local pattern = 'fileinfotpl_date.*<time[^>]*datetime="([^-"]*)'
       local page = 'File:' .. (frame.args[1] or frame.args["file"] or frame.args["title"] or mw.title.getCurrentTitle().text)
       return string.match( frame:expandTemplate{ title = page, args = {}}, pattern )
end

-- This is possibly even more unstable, compared to the above
-- =p.getYearFromInfo2{ args={ file="Franziskanerkirche Salzburg panoramic view interior 39252px.jpg" } }
p.getYearFromInfo2 = function(frame)
	local page = 'File:' .. (frame.args[1] or frame.args["file"] or frame.args["title"] or mw.title.getCurrentTitle().text)
	local htmlparser = require("Module:HTMLParser")
	local root = htmlparser.parse(mw.getCurrentFrame():expandTemplate{ title = page, args = {}})
	local elements = root('#fileinfotpl_date')
	-- We queried an ID so there should be only one result
	for e in pairs(elements) do
		-- We need the next sibling, which doesn't seem to be directly supported by HTMLParser
		-- ... so ask him for the parent <tr> and find the <time> element in it
		local timeElem = e.parent('time')
		for x in pairs(timeElem) do
			return x.attributes['datetime']
		end
	end
end

return p
Cookies help us deliver our services. By using our services, you agree to our use of cookies.