aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRobin Krahl <me@robin-krahl.de>2014-11-01 15:25:00 +0100
committerRobin Krahl <me@robin-krahl.de>2014-11-01 15:25:00 +0100
commit0d2d08472aa09dd001b532698e04b0f02e0a44c6 (patch)
treece45fada86488a0b28f7afdb413162513daee8bf
parent9b31972e6bba8a9665a02cad050d0807a8b9c133 (diff)
downloadLrMediaWiki-0d2d08472aa09dd001b532698e04b0f02e0a44c6.tar.gz
LrMediaWiki-0d2d08472aa09dd001b532698e04b0f02e0a44c6.tar.bz2
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).
-rwxr-xr-xLICENSE.txt2
-rwxr-xr-xmediawiki.lrdevplugin/MediaWikiInterface.lua32
-rw-r--r--mediawiki.lrdevplugin/MediaWikiUtils.lua11
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] <https://raw.githubusercontent.com/LrMediaWiki/LrMediaWiki/master/CREDITS.txt>
+[0] <https://raw.githubusercontent.com/ireas/LrMediaWiki/master/CREDITS.txt>
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] <https://raw.githubusercontent.com/LrMediaWiki/LrMediaWiki/master/LICENSE.txt>
+-- [0] <https://raw.githubusercontent.com/ireas/LrMediaWiki/master/LICENSE.txt>
-- [1] <https://commons.wikimedia.org/wiki/Commons:LrMediaWiki>
--- [2] <https://raw.githubusercontent.com/LrMediaWiki/LrMediaWiki/master/CREDITS.txt>
+-- [2] <https://raw.githubusercontent.com/ireas/LrMediaWiki/master/CREDITS.txt>
-- 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] <https://raw.githubusercontent.com/LrMediaWiki/LrMediaWiki/master/LICENSE.txt>
+-- [0] <https://raw.githubusercontent.com/ireas/LrMediaWiki/master/LICENSE.txt>
-- [1] <https://commons.wikimedia.org/wiki/Commons:LrMediaWiki>
--- [2] <https://raw.githubusercontent.com/LrMediaWiki/LrMediaWiki/master/CREDITS.txt>
+-- [2] <https://raw.githubusercontent.com/ireas/LrMediaWiki/master/CREDITS.txt>
-- 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