Importer, Bürokraten, Moderatoren (CommentStreams), Strukturierte-Diskussionen-Bots, Oberflächenadministratoren, Push-Abonnementverwalter, Oversighter, Administratoren, Kampagnenbearbeiter (Hochladeassistent)
855
Bearbeitungen
Keine Bearbeitungszusammenfassung |
K (51 Versionen von wikivoyage:Modul:Citation/utilities importiert) |
||
| (41 dazwischenliegende Versionen von einem anderen Benutzer werden nicht angezeigt) | |||
| Zeile 1: | Zeile 1: | ||
local | -- documentation | ||
local citationUtilities = { | |||
suite = 'Citation', | |||
sub = 'utilities', | |||
serial = '2022-10-21' | |||
} | |||
-- module variable and administration | |||
local cu = { | |||
moduleInterface = citationUtilities | |||
} | |||
-- module import | |||
-- require( 'strict' ) | |||
local ci = require( 'Module:Citation/i18n' ) | |||
-- global variable | |||
local errorMsgs = {} | local errorMsgs = {} | ||
| Zeile 12: | Zeile 27: | ||
local mult = 10^( decimalPlaces or 0 ) | local mult = 10^( decimalPlaces or 0 ) | ||
return math.floor( num * mult + 0.5 ) / mult | return math.floor( num * mult + 0.5 ) / mult | ||
end | |||
-- converts string to number | |||
function cu.getNumber( s ) | |||
if not cu.isSet( s ) then | |||
return 0 | |||
end | |||
if s:find( '[sic!]', 1, true ) then | |||
return 0 | |||
end | |||
local kb = s:gsub( ci.texts.decimalPoint, '%.' ) | |||
return tonumber( kb ) or 0 | |||
end | end | ||
| Zeile 21: | Zeile 48: | ||
-- make complete message from message array | -- make complete message from message array | ||
function cu.getErrorMsgs() | function cu.getErrorMsgs() | ||
-- remove duplicates | -- remove duplicates | ||
for i = #errorMsgs, 1, -1 do | for i = #errorMsgs, 1, -1 do | ||
| Zeile 32: | Zeile 58: | ||
end | end | ||
result = table.concat( errorMsgs, ' ' ) | local result = table.concat( errorMsgs, ' ' ) | ||
if result ~= '' then | if result ~= '' then | ||
result = result .. ' ' | result = result .. ' ' | ||
| Zeile 54: | Zeile 80: | ||
end | end | ||
for index, value in ipairs( tab ) do | for index, value in ipairs( tab ) do | ||
if value == val then | if value == val then | ||
| Zeile 66: | Zeile 91: | ||
-- convert values t from list if translated | -- convert values t from list if translated | ||
function cu.getKey( t, list ) | function cu.getKey( t, list ) | ||
local result = '' | local result = '' | ||
for key, tab in pairs( list ) do | for key, tab in pairs( list ) do | ||
if cu.inArray( tab, t ) then | if cu.inArray( tab, t ) then | ||
| Zeile 78: | Zeile 103: | ||
-- returns a single value from frame argument table | -- returns a single value from frame argument table | ||
function cu.getArgValue( list, param, args ) | function cu.getArgValue( list, param, args ) | ||
value = '' | local value = '' | ||
if list[ param ] then | if list[ param ] then | ||
for k, v in ipairs( list[ param ] ) do | for k, v in ipairs( list[ param ] ) do | ||
| Zeile 91: | Zeile 116: | ||
-- string cleanup | -- string cleanup | ||
function cu. | function cu.parameterCleanup( s ) | ||
if not cu.isSet( s ) then | if not cu.isSet( s ) then | ||
return s | return s -- nil or '' should be kept | ||
end | end | ||
local orig = s | |||
-- replace control characters | |||
s = s:gsub( '[\009\010\013]', ' ' ) -- horizontal tab, line feed, carriage return | s = s:gsub( '[\009\010\013]', ' ' ) -- horizontal tab, line feed, carriage return | ||
s = s:gsub( '[%z%c]', '' ) -- control characters | s = s:gsub( '[%z%c]', '' ) -- control characters | ||
s = | |||
s = | -- remove tags | ||
s = mw.ustring.gsub( s, '[ | s = mw.ustring.gsub( s, '</*br[^/>]*/*>', '' ) -- <br> tag | ||
s = mw.ustring.gsub( s, '</*p[^/>]*/*>', '' ) -- <p> tag | |||
s = mw.ustring.gsub( s, '</*div[^/>]*/*>', '' ) -- <div> tag | |||
if orig ~= s then | |||
cu.addErrorMsg( ci.texts.wrongChars ) | |||
end | |||
-- replace character references and entities | |||
s = mw.text.decode( s, true ) | |||
-- replace characters | |||
s = mw.ustring.gsub( s, '%.%.%.', '…' ) | s = mw.ustring.gsub( s, '%.%.%.', '…' ) | ||
s = mw.ustring.gsub( s, '%.%.', '‥' ) | s = mw.ustring.gsub( s, '%.%.', '‥' ) | ||
return s:gsub( '%s%s+', ' ' ) -- multiple spaces | return s:gsub( '%s%s+', ' ' ) -- multiple spaces | ||
end | end | ||
-- remove illegal chars from | -- remove illegal chars from pages parameters | ||
function cu.cleanupPageNumbers( pages ) | function cu.cleanupPageNumbers( pages ) | ||
if not cu.isSet( pages ) then | if not cu.isSet( pages ) then | ||
return '' | return '' | ||
end | end | ||
-- replace dashes with hyphens | |||
return mw.ustring.gsub( '' .. pages, '[–‒—]', '-' ); | |||
end | |||
-- string cleanup before COinS creation | |||
function cu.coinsCleanup( s ) | |||
if not cu.isSet( s ) then | |||
return '' | |||
end | |||
-- replace characters | |||
s = s:gsub( '\226\128\138', ' ' ); -- hair space | |||
s = mw.ustring.gsub( s, '[\226\128\141\226\128\139\194\173]', '' ); | |||
-- zero-width joiner, zero-width space, soft hyphen | |||
-- remove characters: soft hyphen, LTR mark, RTL mark | |||
for i, value in ipairs( { '', '', '' } ) do | |||
s = mw.ustring.gsub( s, value, '' ) | |||
end | |||
-- replace Wiki syntax | |||
s = s:gsub( "''+", '' ) -- multiple apostrophes | |||
s = mw.ustring.gsub( s, '</*span[^/>]*/*>', '' ) -- span tags | |||
s = mw.ustring.gsub( s, '%[%[[^%[%]]*|([^%[%]]*)%]%]', '%1' ) -- MediaWiki links | |||
s = mw.ustring.gsub( s, '%[%[([^%[%]]*)%]%]', '%1' ) | |||
s = mw.ustring.gsub( s, '%[%a*:?//[^ ]+%s+([^%]]+)%]', '%1' ) -- web links | |||
s = mw.ustring.gsub( s, '%[mailto:[^ ]+%s+([^%]]+)%]', '%1' ) | |||
s = mw.ustring.gsub( s, '%[%a*:?//([^%]]+)%]', '%1' ) | |||
s = mw.ustring.gsub( s, '%[mailto:([^%]]+)%]', '%1' ) | |||
return s:gsub( '%s%s+', ' ' ) -- multiple spaces | |||
end | |||
-- remove adjoining punctuation marks etc. | |||
function cu.finalCleanup( s ) | |||
s = s:gsub( '%.+%.', '.' ):gsub( '%s%s+', ' ' ):gsub( '([,;:])(%s%.+)', '.' ) | |||
for _, replacement in ipairs( ci.replacements ) do | |||
s = mw.ustring.gsub( s, replacement.s, replacement.r ) | |||
end | |||
s = s:gsub( '#b#', '.' ) -- restore bibcode | |||
return s | |||
end | end | ||
function cu. | function cu.makeLink( url, text ) | ||
return mw.ustring.format( '[%s %s]', url, text ) | |||
end | end | ||
| Zeile 138: | Zeile 199: | ||
local function formatDate( aDate, aFormat ) | local function formatDate( aDate, aFormat ) | ||
return mw.getContentLanguage():formatDate( aFormat, aDate, true ) | return mw.getContentLanguage():formatDate( aFormat, aDate, true ) | ||
end | |||
if aFormat == 'R' then | |||
return aDate | |||
end | end | ||
if aDate ~='' then | if aDate ~='' then | ||
local | local success, t = pcall( formatDate, aDate, aFormat ) | ||
if success then | if success then | ||
return t | return t | ||
| Zeile 160: | Zeile 224: | ||
return url:match( ext .. '$' ) or url:match( ext .. '[%?#]' ) or | return url:match( ext .. '$' ) or url:match( ext .. '[%?#]' ) or | ||
url:match( ext .. '#' ); -- # is # | url:match( ext .. '#' ); -- # is # | ||
end | end | ||
| Zeile 192: | Zeile 249: | ||
end | end | ||
end | end | ||
end | |||
function cu.formatItem( s, formatStr ) | |||
if cu.isSet( s ) then | |||
if formatStr then | |||
return mw.ustring.format( formatStr, s ) | |||
else | |||
return s | |||
end | |||
else | |||
return '' | |||
end | |||
end | |||
function cu.formatItem2( s1, s2, formatStr ) | |||
if cu.isSet( s1 ) and cu.isSet( s2 ) then | |||
if formatStr then | |||
return mw.ustring.format( formatStr, s1, s2 ) | |||
else | |||
return s1 .. ' ' .. s2 | |||
end | |||
else | |||
return '' | |||
end | |||
end | |||
function cu.templateStyles( frame ) | |||
return frame:extensionTag( 'templatestyles', '', { src = ci.styleSrc } ); | |||
end | |||
-- Check digit estimation for countries at, ch, de, and fi | |||
-- See: https://github.com/bohnelang/URN-Pruefziffer | |||
-- Description of the algorithm: http://www.pruefziffernberechnung.de/U/URN.shtml | |||
function cu.getNbnCheckDigit( urn ) | |||
-- two-digits codes for ascii characters starting from - == '-' | |||
local code='3947450102030405060708094117############1814191516212223242542262713282931123233113435363738########43' | |||
local sum = 0 | |||
local pos = 1 | |||
local digit1, digit2, x | |||
urn = urn:upper():sub( 1, -2 ) -- remove last character | |||
for i = 1, urn:len() do | |||
x = 2 * ( urn:byte( i ) - 45 ); -- - == '-' | |||
digit1 = tonumber( code:sub( x + 1, x + 1 ) ); | |||
digit2 = tonumber( code:sub( x + 2, x + 2 ) ); | |||
if digit1 == 0 then | |||
sum = sum + digit2 * pos | |||
pos = pos + 1 | |||
else | |||
sum = sum + digit1 * pos + digit2 * ( pos + 1 ) | |||
pos = pos + 2 | |||
end | |||
end | |||
return tostring( math.floor( sum / digit2 ) % 10 ); | |||
end | |||
function cu.check_UrnNbn( urn ) | |||
urn = urn:gsub( '/fragment/.+$', '' ) -- remove fragment | |||
return urn:sub( -1 ) == cu.getNbnCheckDigit( urn ) | |||
end | end | ||
return cu | return cu | ||