From 0d2d08472aa09dd001b532698e04b0f02e0a44c6 Mon Sep 17 00:00:00 2001 From: Robin Krahl Date: Sat, 1 Nov 2014 15:25:00 +0100 Subject: Use improved string formatting Use string formatting by name instead of formatting by position when creating the file description. - adds the method `formatString` to `MediaWikiUtils` - modifies `MediaWikiInterface` to use `MediaWikiUtils.formatString` See issue [#29: Move file description template into a file and improve the string formatting](https://github.com/ireas/LrMediaWiki/issues/29). --- LICENSE.txt | 2 +- mediawiki.lrdevplugin/MediaWikiInterface.lua | 32 ++++++++++++++++++---------- mediawiki.lrdevplugin/MediaWikiUtils.lua | 11 ++++++++-- 3 files changed, 31 insertions(+), 14 deletions(-) diff --git a/LICENSE.txt b/LICENSE.txt index de4af60..4d85da0 100755 --- a/LICENSE.txt +++ b/LICENSE.txt @@ -1,7 +1,7 @@ Copyright (c) 2014 by the LrMediaWiki team (see CREDITS.txt file in the project root directory or [0]) -[0] +[0] The X11 License diff --git a/mediawiki.lrdevplugin/MediaWikiInterface.lua b/mediawiki.lrdevplugin/MediaWikiInterface.lua index 8d79974..e59b3f4 100755 --- a/mediawiki.lrdevplugin/MediaWikiInterface.lua +++ b/mediawiki.lrdevplugin/MediaWikiInterface.lua @@ -5,9 +5,9 @@ -- Copyright (C) 2014 by the LrMediaWiki team (see CREDITS.txt file in the -- project root directory or [2]) -- --- [0] +-- [0] -- [1] --- [2] +-- [2] -- Code status: -- doc: missing @@ -29,18 +29,18 @@ local MediaWikiInterface = { loggedIn = false, fileDescriptionPattern = [=[== {{int:filedesc}} == {{Information -|Description=%s -|Source=%s -|Date=%s -|Author=%s +|Description=${description} +|Source=${source} +|Date=${timestamp} +|Author=${author} |Permission= |other_versions= -|other_fields=%s +|other_fields=${other_fields} }} -%s +${templates} == {{int:license-header}} == -%s -%s[[Category:Uploaded with LrMediaWiki]]]=], +${license} +${categories}[[Category:Uploaded with LrMediaWiki]]]=], } MediaWikiInterface.prepareUpload = function(username, password, apiPath) @@ -134,7 +134,17 @@ MediaWikiInterface.buildFileDescription = function(description, source, timestam categoriesString = categoriesString .. string.format('[[Category:%s]]\n', category) end end - return string.format(MediaWikiInterface.fileDescriptionPattern, description, source, timestamp, author, other, templates, license, categoriesString) + local arguments = { + description = description, + source = source, + timestamp = timestamp, + author = author, + other_fields = other, + templates = templates, + license = license, + categories = categoriesString, + } + return MediaWikiUtils.formatString(MediaWikiInterface.fileDescriptionPattern, arguments) end return MediaWikiInterface diff --git a/mediawiki.lrdevplugin/MediaWikiUtils.lua b/mediawiki.lrdevplugin/MediaWikiUtils.lua index 00dd46d..0eeb7a9 100644 --- a/mediawiki.lrdevplugin/MediaWikiUtils.lua +++ b/mediawiki.lrdevplugin/MediaWikiUtils.lua @@ -5,9 +5,9 @@ -- Copyright (C) 2014 by the LrMediaWiki team (see CREDITS.txt file in the -- project root directory or [2]) -- --- [0] +-- [0] -- [1] --- [2] +-- [2] -- Code status: -- doc: missing @@ -17,6 +17,13 @@ local Info = require 'Info' local MediaWikiUtils = {} +-- Allows formatting of strings like "${test} eins zwei drei ${test2}" +-- Based on a solution by http://lua-users.org/wiki/RiciLake shown here: +-- http://lua-users.org/wiki/StringInterpolation +MediaWikiUtils.formatString = function(str, arguments) + return (str:gsub('($%b{})', function(w) return arguments[w:sub(3, -2)] or w end)) +end + MediaWikiUtils.isStringEmpty = function(str) return str == nil or string.match(str, '^%s*$') ~= nil end -- cgit v1.2.1