Rosetta Code:Village Pump/Lang-tag bot/Source: Difference between revisions

no edit summary
m (Minor updates.)
No edit summary
 
(One intermediate revision by one other user not shown)
Line 1:
<langsyntaxhighlight lang="perl">use warnings;
use strict;
use feature 'say';
Line 7:
use MediaWiki::API;
 
use constant DELAY_BETWEEN_EDITS => 10 * 60; # In seconds.
 
my $username = 'UnderBot';
my $password = 'secret';
 
my $kill_switch_page = 'User talk:UnderBot';
my $kill_switch_trigger = qr/stopediting/;
 
my $tasks_path = '/home/hippo/Temporary/tasks.yml';
 
my %langtags = ('4d' => '4d',
'actionscript' => 'actionscript',
'4d' => '4d',
'ada' => 'ada',
'actionscript' => 'actionscript',
'agda2' => 'agda2',
'ada' => 'ada',
'algol 60' => 'algol60',
'agda2' => 'agda2',
'algol 6068' => 'algol60algol68',
'amigae' => 'amigae',
'algol 68' => 'algol68',
'apl' => 'apl',
'amigae' => 'amigae',
'applescript' => 'applescript',
'apl' => 'apl',
'assembly' => 'asm',
'applescript' => 'applescript',
'autohotkey' => 'autohotkey',
'assembly' => 'asm',
'awk' => 'awk',
'autohotkey' => 'autohotkey',
'bc' => 'bc',
'awk' => 'awk',
'befunge' => 'befunge',
'bc' => 'bc',
'brainf***' => 'bf',
'befunge' => 'befunge',
'caml' => 'caml',
'brainf***' => 'bf',
'c' => 'c',
'caml' => 'caml',
'c++' => 'cpp',
'c' => 'c',
'clean' => 'clean',
'c++' => 'cpp',
'clojure' => 'lisp',
'clean' => 'clean',
'cobol' => 'cobol',
'clojure' => 'lisp',
'coldfusion' => 'cfm',
'cobol' => 'cobol',
'common lisp' => 'lisp',
'coldfusion' => 'cfm',
'component pascal' => 'pascal',
'common lisp' => 'lisp',
'coq' => 'coq',
'component pascal' => 'pascal',
'c sharp|c#' => 'csharp',
'coq' => 'coq',
'c sharp|c#' => 'csharp',
'dc' => 'dc',
'c sharp' => 'csharp',
'd' => 'd',
'dc' => 'dc',
'delphi' => 'delphi',
'd' => 'd',
'dos batch file' => 'dos',
'delphi' => 'delphi',
'ec' => 'ec',
'dos batch file' => 'dos',
'e' => 'e',
'ec' => 'ec',
'eiffel' => 'eiffel',
'e' => 'e',
'ella' => 'ella',
'eiffel' => 'eiffel',
'emacs lisp' => 'lisp',
'ella' => 'ella',
'erlang' => 'erlang',
'emacs lisp' => 'lisp',
'esql' => 'sql',
'erlang' => 'erlang',
'factor' => 'factor',
'esql' => 'sql',
'false' => 'false',
'factor' => 'factor',
'fan' => 'fan',
'false' => 'false',
'f' => 'f',
'fan' => 'fan',
'forth' => 'forth',
'f' => 'f',
'fortran' => 'fortran',
'forth' => 'forth',
'fp' => 'fp',
'fortran' => 'fortran',
'f sharp|f#' => 'fsharp',
'fp' => 'fp',
'f sharp 'f_sharp|f#' => 'fsharp',
'gap' => 'gap',
'f_sharp|f#' => 'fsharp',
'genyris' => 'genyris',
'gap' => 'gap',
'gnuplot' => 'gnuplot',
'genyris' => 'genyris',
'go' => 'go',
'gnuplot' => 'gnuplot',
'groovy' => 'groovy',
'go' => 'go',
'haskell' => 'haskell',
'groovy' => 'groovy',
'haxe' => 'haxe',
'haskell' => 'haskell',
'hq9+' => 'hq9p',
'haxe' => 'haxe',
'html' => 'html4strict',
'hq9+' => 'hq9p',
'icon' => 'icon',
'html' => 'html4strict',
'idl' => 'idl',
'icon' => 'icon',
'io' => 'io',
'idl' => 'idl',
'javafx script' => 'javafx',
'io' => 'io',
'java' => 'java',
'javafx script' => 'javafx',
'javascript' => 'javascript',
'java' => 'java',
'j' => 'j',
'javascript' => 'javascript',
'jocaml' => 'jocaml',
'j' => 'j',
'joy' => 'joy',
'jocaml' => 'jocaml',
'jscript.net' => 'jscript.net',
'joy' => 'joy',
'json' => 'json',
'jscript.net' => 'jscript.net',
'judoscript' => 'judoscript',
'json' => 'json',
'korn shell' => 'korn',
'judoscript' => 'judoscript',
'labview' => 'labview',
'korn shell' => 'korn',
'latex' => 'latex',
'labview' => 'labview',
'lisaac' => 'lisaac',
'latex' => 'latex',
'lisp' => 'lisp',
'lisaac' => 'lisaac',
'logo' => 'logo',
'lisp' => 'lisp',
'logtalk' => 'logtalk',
'logo' => 'logo',
'lotusscript' => 'lotusscript',
'logtalk' => 'logtalk',
'lse64' => 'lse64',
'lotusscript' => 'lotusscript',
'lua' => 'lua',
'lse64' => 'lse64',
'lucid' => 'lucid',
'lua' => 'lua',
'm4' => 'm4',
'lucid' => 'lucid',
'make' => 'make',
'm4' => 'm4',
'maple' => 'maple',
'make' => 'make',
'mathematica' => 'mathematica',
'maple' => 'maple',
'matlab' => 'matlab',
'mathematica' => 'mathematica',
'maxima' => 'maxima',
'matlab' => 'matlab',
'maxscript' => 'maxscript',
'maxima' => 'maxima',
'metafont' => 'metafont',
'maxscript' => 'maxscript',
'mirc scripting language' => 'mirc',
'metafont' => 'metafont',
'mmix' => 'mmix',
'mirc scripting language' => 'mirc',
'modula-2' => 'modula2',
'mmix' => 'mmix',
'modula-23' => 'modula2modula3',
'moo' => 'moo',
'modula-3' => 'modula3',
'mpif90' => 'mpif90',
'moo' => 'moo',
'ms sql' => 'sql',
'mpif90' => 'mpif90',
'ms sql 'mysql' => 'sql',
'newlisp' => 'lisp',
'mysql' => 'sql',
'nial' => 'nial',
'newlisp' => 'lisp',
'oberon-2' => 'oberon2',
'nial' => 'nial',
'objective-c' => 'objc',
'oberon-2' => 'oberon2',
'object pascal' => 'objectpascal',
'objective-c' => 'objc',
'ocaml' => 'ocaml',
'object pascal' => 'objectpascal',
'octave' => 'octave',
'ocaml' => 'ocaml',
'omega' => 'omega',
'octave' => 'octave',
'openedge/progress' => 'openedge',
'omega' => 'omega',
'oz' => 'oz',
'openedge/progress' => 'openedge',
'pari/gp' => 'parigp',
'oz' => 'oz',
'pascal' => 'pascal',
'pari/gp' => 'parigp',
'perl 6' => 'perl6',
'pascal' => 'pascal',
'perl 6' => 'perl6perl',
'php' => 'php',
'perl' => 'perl',
'pike' => 'pike',
'php' => 'php',
'plaintex' => 'tex',
'pike' => 'pike',
'pl/i' => 'pli',
'plaintex' => 'tex',
'pl/ipgsql' => 'pliplpgsql',
'pl/pgsqlsql' => 'plpgsqlplsql',
'pop11' => 'pop11',
'pl/sql' => 'plsql',
'postgresql' => 'sql',
'pop11' => 'pop11',
'postscript' => 'postscript',
'postgresql' => 'sql',
'powerbasic' => 'powerbasic',
'postscript' => 'postscript',
'powershell' => 'powershell',
'powerbasic' => 'powerbasic',
'prolog' => 'prolog',
'powershell' => 'powershell',
'pure' => 'pure',
'prolog' => 'prolog',
'python' => 'python',
'pure' => 'pure',
'q' => 'q',
'python' => 'python',
'rapidq' => 'rapidq',
'q' => 'q',
'raven' => 'raven',
'rapidq' => 'rapidq',
'rexx' => 'rexx',
'raven' => 'raven',
'rhope' => 'rhope',
'rexx' => 'rexx',
'r' => 'r',
'rhope' => 'rhope',
'ruby' => 'ruby',
'r' => 'r',
'sas' => 'sas',
'ruby' => 'ruby',
'scala' => 'scala',
'sas' => 'sas',
'scheme' => 'scheme',
'scala' => 'scala',
'script3d' => 'script3d',
'scheme' => 'scheme',
'seed7' => 'seed7',
'script3d' => 'script3d',
'self' => 'self',
'seed7' => 'seed7',
'setl' => 'setl',
'self' => 'self',
'slate' => 'slate',
'setl' => 'setl',
'smalltalk' => 'smalltalk',
'slate' => 'slate',
'smeql' => 'smeql',
'smalltalk' => 'smalltalk',
'snusp' => 'snusp',
'smeql' => 'smeql',
'sql' => 'sql',
'snusp' => 'snusp',
'standard ml' => 'sml',
'sql' => 'sql',
'supercollider' => 'supercollider',
'standard ml' => 'sml',
'svg' => 'xml',
'supercollider' => 'supercollider',
'tcl' => 'tcl',
'svg' => 'xml',
'ti-83 basic' => 'ti83b',
'tcl' => 'tcl',
'ti-8389 basic' => 'ti83bti89b',
'toka' => 'toka',
'ti-89 basic' => 'ti89b',
'transact-sql' => 'sql',
'toka' => 'toka',
'tr' => 'tr',
'transact-sql' => 'sql',
'twelf' => 'twelf',
'tr' => 'tr',
'unixpipes' => 'bash',
'twelf' => 'twelf',
'unixpipesunix shell' => 'bash',
'unlambda' => 'unlambda',
'unix shell' => 'bash',
'ursala' => 'ursala',
'unlambda' => 'unlambda',
'vbscript' => 'vbscript',
'ursala' => 'ursala',
'vedit macro language' => 'vedit',
'vbscript' => 'vbscript',
'visual basic .net' => 'vbnet',
'vedit macro language' => 'vedit',
'visual basic .net' => 'vbnetvb',
'visual basicobjects' => 'vbvisobj',
'vorpal' => 'vorpal',
'visual objects' => 'visobj',
'v' => 'v',
'vorpal' => 'vorpal',
'wrapl' => 'wrapl',
'v' => 'v',
'xquery' => 'xquery',
'wrapl' => 'wrapl',
'xslt' => 'xml',
'xquery' => 'xquery',
'xtalk' => 'xtalk',);
'xslt' => 'xml',
'xtalk' => 'xtalk',
);
 
my $h = qr/(?:\t| )*/;
 
# Vaguely like Perl 6's \h.
# Vaguely like Perl 6's \h.
my $lwsl = qr/(?:\t| )+\S[^\n]*/;
 
# Leading WhiteSpace Line.
# Leading WhiteSpace Line.
 
# ------------------------------------------------------------
Line 196:
our (%tasks, @done, @todo);
local *tasks = LoadFile $tasks_path;
local *done = $tasks{done};
local *todo = $tasks{todo};
 
my $mw = new MediaWiki::API({api_url => 'http://rosettacode.org/mw/api.php'});
$mw->login({lgname => $username, lgpassword => $password})
or die q{Couldn't log in. (}, $mw->{error}->{code}, ': ',
$mw->{error}->{details}, ')';
 
while (@todo) {
{ my $pagetitle = shift @todo;
say "TITLE: $pagetitle";
 
Line 213:
 
$p = $mw->get_page({title => $pagetitle}) || die;
my $timestamp = $p->{timestamp}; # To prevent edit conflicts.
my $text = $p->{'*'};
 
$text =~ s/ (.+? \n) (== \s* {{) /$2/xs or die;
my $newtext = $1;
 
# So $newtext just contains the task description so far.
# So $newtext just contains the task description so far.
while ($text =~ s! \A
while (
$text =~ s! \A
( == $h {{ $h header $h \| $h ([^}]+?) $h }} $h == $h \n )
 
Line 226 ⟶ 228:
( \z | == $h {{ )
 
!$4!xs)
) {
{my ($header, $langname, $body) = ($1, $2, $3);
my ($header, $langname, $body) = ($1, $2, $3);
s/\bC #/C#/ foreach $header, $langname;
# Why some people put a space there, I have no idea.
my $tag = $langtags{lc $langname} ||
$langname =~ /assembl/i && 'asm' ||
## BASIC dialect-guessing is commented out because
## on some pages, programs for more than one dialect
## appear under "BASIC". Really we ought to treat
## each dialect as its own language.
#$langname =~ /basic/i &&
# ($body =~ /q(uick)?basic/i && 'qbasic' ||
# $body =~ /f(ree)?basic/i && 'freebasic' ||
# $body =~ /t(hin)?basic/i && 'thinbasic') ||
undef;
 
# Why some people put a space there, I have no idea.
if ($tag)
my {$tag = "<lang $tag>";
if ($bodylangtags{lc =~ /<lang/)$langname}
|| $langname =~ /assembl/i && {# Use the correct identifier.'asm'
||
## BASIC dialect-guessing is commented out because
## on some pages, programs for more than one dialect
## appear under "BASIC". Really we ought to treat
## each dialect as its own language.
#$langname =~ /basic/i &&
# ($body =~ /q(uick)?basic/i && 'qbasic' ||
# $body =~ /f(ree)?basic/i && 'freebasic' ||
# $body =~ /t(hin)?basic/i && 'thinbasic') ||
undef;
 
if ($tag) {
$tag = "<lang $tag>";
if ($body =~ /<lang/) { # Use the correct identifier.
$body =~ s {$h (<lang [^>]* >)}
{my $s = $1;
Line 251 ⟶ 256:
? $s # Don't replace "Mathematica" with "mathematica" or "java5" with "java"
: $tag}gxe;
 
# Get rid of any indenting spaces left behind when
# someone else added the lang tags.
Line 262 ⟶ 268:
{my $space = minstr($b =~ /^( +)\S/gm);
$b =~ s/^$space//gm;}
"$t$b\x3c/lang>"}xges;}
}
elsif ($body =~ /<pre/)
 
# Just assume they should all be lang tags.
{$body =~ s
$body =~ { <pre [^>]* > \s*
{ <pre [^>]* > \s*
(.+?)
\s* </pre> }
{decode_entities "$tag$1\x3c/lang>"}xseg;}
}
# HTML entities don't work in lang tags.
 
# But they aren't necessary, either.
# HTML entities don't work in lang tags.
# But they aren't necessary, either.
else
 
# Turn indented passages into lang-tagged passages.
{$body =~ s
$body =~ s
{ ( ^ $lwsl \n
(?: (?: $lwsl \n | $h \n )*
Line 282 ⟶ 295:
$t =~ s/^$space//gm;
$t =~ s/\s+\z//;
decode_entities("$tag$t\x3c/lang>\n");}mgex;}}
}
}
 
$body =~ s
{(<lang [^>]*>) <nowiki> \s* (.+?) \s* </nowiki> \x3c/lang>}
{$1$2\x3c/lang>}gsx;
$newtext .= $header . $body;}
}
 
$newtext .= $text;
$newtext =~ s/\s*\z/\n/;
 
my $success = $mw->edit(
( {action => 'edit',
action title => $pagetitle'edit',
title basetimestamp => $timestamppagetitle,
textbasetimestamp => $newtexttimestamp,
text minor => 1$newtext,
minor # All we're doing,=> ultimately1, is formatting.
 
bot => 1,
# All we're doing, nocreateultimately, =>is 1,formatting.
bot #=> If the page was deleted while we were regexing1,
nocreate => 1,
# we probably shouldn't resurrect it!
 
summary => 'Fixed lang tags.'},
# If the page was deleted while we were regexing,
# we probably shouldn't resurrect it!
summary => 'Fixed lang tags.'
},
{skip_encoding => 1});
# Without the skip_encoding option, non-ASCII characters
# will get corrupted.
 
# Without the skip_encoding option, non-ASCII characters
if ($success)
# will get corrupted.
{say(exists $success->{edit}->{nochange}
 
? 'Unchanged.'
if ($success) {
: 'Committed!');
push @done,say(exists $pagetitle;success->{edit}->{nochange}
? 'Unchanged.'
else
# Probably an edit conflict. : 'Committed!');
{saypush "Couldn't@done, commit; I'll try again later."$pagetitle;
}
push @todo, $pagetitle;}
else
DumpFile $tasks_path, \%tasks;
 
# Probably an edit conflict.
{
say "Couldn't commit; I'll try again later.";
push @todo, $pagetitle;
}
DumpFile $tasks_path, \%tasks;
 
sleep DELAY_BETWEEN_EDITS;}</lang>
}
</syntaxhighlight>
 
[[Category:Perl]]