r111776 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r111775‎ | r111776 | r111777 >
Date:19:12, 17 February 2012
Author:amire80
Status:ok
Tags:
Comment:
Removed "use warnings" and "use Data::Dumper" - needed only for debugging.
Cleaned up a lot of obsolete code.
Set links color to blue (<a> doesn't work in rsvg).
Modified paths:
  • /trunk/extensions/timeline/EasyTimeline.pl (modified) (history)

Diff [purge]

Index: trunk/extensions/timeline/EasyTimeline.pl
@@ -59,7 +59,7 @@
6060 # - dot in folder name in input path was misunderstood as start of file extension
6161 # - utf-8 chars within 160-255 range are translated to extended ascii
6262 # however internal font used by Ploticus has strange mapping so some are replaced
63 -# by undercore or unaccented version of character
 63+# by underscore or unaccented version of character
6464 # this is a make do solution until full unicode support with external fonts will be added
6565 #
6666 # 1.12 June 2009
@@ -70,8 +70,9 @@
7171 # - change svg encoding from iso-8859-1 -> UTF-8
7272 # - allow font to be specified using -f option as opposed to hardcoded FreeSans.
7373
 74+use 5.010;
 75+
7476 use strict;
75 -use warnings;
7677
7778 our $VERSION = '1.90';
7879
@@ -82,6 +83,8 @@
8384
8485 # Global variables.
8586 # Many of these should be refactored.
 87+my $SVG_ONLY = 0;
 88+
8689 my @PlotLines;
8790 my $CntErrors = 0;
8891 my @Errors;
@@ -290,6 +293,10 @@
291294 $articlepath = "http://en.wikipedia.org/wiki/\$1";
292295 }
293296
 297+ if (defined $options{"s"}) {
 298+ $SVG_ONLY = 1;
 299+ }
 300+
294301 if (!-e $file_in) {
295302 &Abort("Input file '" . $file_in . "' not found.");
296303 }
@@ -310,8 +317,6 @@
311318 $file_html = $file . ".html";
312319 $file_errors = $file . ".err";
313320
314 - # $file_pl_info = $file . ".inf" ;
315 - # $file_pl_err = $file . ".err" ;
316321 print "Output: Image files $file_bitmap & $file_vector\n";
317322
318323 if ($linkmap) {
@@ -332,10 +337,6 @@
333338 sub SetImageFormat {
334339 $env = "";
335340
336 - # $dir = cwd() ; # is there a better way to detect OS?
337 - # if ($dir =~ /\//) { $env = "Linux" ; $image_file_fmt = "png" ; $pathseparator = "/";}
338 - # if ($dir =~ /\\/) { $env = "Windows" ; $image_file_fmt = "gif" ; $pathseparator = "\\";}
339 - # cwd always to returns '/'s ? ->
340341 if ($OSNAME =~ /darwin/i) {
341342 $env = "Linux";
342343 $image_file_fmt = "png";
@@ -346,7 +347,11 @@
347348 $image_file_fmt = "gif";
348349 $pathseparator = "\\";
349350 }
350 - else { $env = "Linux"; $image_file_fmt = "png"; $pathseparator = "/"; }
 351+ else {
 352+ $env = "Linux";
 353+ $image_file_fmt = "png";
 354+ $pathseparator = "/";
 355+ }
351356
352357 if ($env ne "") {
353358 print
@@ -646,12 +651,12 @@
647652 if ( ($name ne "bar")
648653 && ($name ne "text")
649654 && ($name ne "link")
650 - && ($name ne "legend")) # && ($name ne "hint")
 655+ && ($name ne "legend"))
651656 {
652657 $value = lc($value);
653658 }
654659
655 - if ($name eq "link") # restore colon
 660+ if ($name eq "link") # restore colon
656661 {
657662 $value =~ s/'colon'/:/;
658663 }
@@ -678,7 +683,9 @@
679684 }
680685 }
681686 }
682 - if (($name ne "") && ($Attributes{"single"} ne "")) {
 687+ if ( (defined $name and $name ne "")
 688+ and (defined $Attributes{"single"} and $Attributes{"single"} ne ""))
 689+ {
683690 &Error( "Invalid attribute '"
684691 . $Attributes{"single"}
685692 . "' ignored.\nSpecify attributes as 'name:value' pairs.");
@@ -1386,7 +1393,6 @@
13871394 }
13881395 }
13891396 }
1390 -
13911397 elsif ($attribute =~ /BarIncrement/i) {
13921398 if (!&ValidAbs($attrvalue)) {
13931399 &Error( "ImageSize attribute '$attribute' invalid.\n"
@@ -1397,11 +1403,6 @@
13981404
13991405 $Attributes{"barinc"} = $attrvalue;
14001406 }
1401 -
1402 - # if ($attribute =~ /Width/i)
1403 - # { $Attributes{"width"} = $attrvalue ; }
1404 - # elsif ($attribute =~ /Height/i)
1405 - # { $Attributes{"height"} = $attrvalue ; }
14061407 }
14071408
14081409 if ( ($Attributes{"width"} =~ /auto/i)
@@ -1756,7 +1757,7 @@
17571758 while ((!$InputParsed) && (!$NoData)) {
17581759 if (!&ValidAttributes("PlotData")) { &GetData; next; }
17591760
1760 - $bar = ""; # $barset = "" ;
 1761+ $bar = "";
17611762 $at = "";
17621763 $from = "";
17631764 $till = "";
@@ -1780,7 +1781,6 @@
17811782
17821783 if (defined($PlotDefs{"bar"})) { $bar = $PlotDefs{"bar"}; }
17831784
1784 - # if (defined ($PlotDefs{"barset"})) { $barset = $PlotDefs{"barset"} ; }
17851785 if (defined($PlotDefs{"color"})) { $color = $PlotDefs{"color"}; }
17861786 if (defined($PlotDefs{"bgcolor"})) {
17871787 $bgcolor = $PlotDefs{"bgcolor"};
@@ -2143,7 +2143,6 @@
21442144 {
21452145 if ($bar ne "") { $PlotDefs{"bar"} = $bar; }
21462146
2147 - # if ($barset ne "") { $PlotDefs{"barset"} = $barset ; }
21482147 if ($color ne "") { $PlotDefs{"color"} = $color; }
21492148 if ($bgcolor ne "") { $PlotDefs{"bgcolor"} = $bgcolor; }
21502149 if ($textcolor ne "") { $PlotDefs{"textcolor"} = $textcolor; }
@@ -2156,8 +2155,6 @@
21572156 if ($mark ne "") { $PlotDefs{"mark"} = $mark; }
21582157 if ($markcolor ne "") { $PlotDefs{"markcolor"} = $markcolor; }
21592158
2160 - # if ($link ne "") { $PlotDefs{"link"} = $link ; }
2161 - # if ($hint ne "") { $PlotDefs{"hint"} = $hint ; }
21622159 &GetData;
21632160 next PlotData;
21642161 }
@@ -2606,8 +2603,7 @@
26072604 $textcolor = $TextDefs{"textcolor"};
26082605 }
26092606
2610 - # warn "data: $data"; # XXX $data is probably not really used
2611 - my $data2; # = $data; # XXX see above
 2607+ my $data2;
26122608 ($data2, $text) = &ExtractText($data2);
26132609 @Attributes = split(" ", $data2);
26142610
@@ -2724,9 +2720,6 @@
27252721 $TextDefs{"pos"} = $pos;
27262722 }
27272723
2728 - # if ($link ne "")
2729 - # { ($text, $link, $hint) = &ProcessWikiLink ($text, $link, $hint) ; }
2730 -
27312724 if ($text eq "") # upd defaults
27322725 {
27332726 if ($pos ne "") { $TextDefs{"pos"} = $pos; }
@@ -3453,14 +3446,6 @@
34543447 if ($Axis{"time"} eq "x") { $AxisBars = "y"; }
34553448 else { $AxisBars = "x"; }
34563449
3457 - # if (($Axis{"time"} eq "y") && ($#Bars > 0))
3458 - # {
3459 - # undef @BarsTmp ;
3460 - # while ($#Bars >= 0)
3461 - # { push @BarsTmp, pop @Bars ; }
3462 - # @Bars = @BarsTmp ;
3463 - # }
3464 -
34653450 my $file_script;
34663451 if ($tmpdir ne "") {
34673452 $file_script = $tmpdir . $pathseparator . "EasyTimeline.txt.$$";
@@ -3469,16 +3454,10 @@
34703455 $file_script = "EasyTimeline.txt";
34713456 }
34723457
3473 - print "Ploticus input file = " . $file_script . "\n";
 3458+ print "Ploticus input file = $file_script\n";
34743459
3475 - # $image_file_fmt = "gif" ;
34763460 open "FILE_OUT", ">", $file_script;
34773461
3478 - #proc settings
3479 - # $script .= "#proc settings\n" ;
3480 - # $script .= " xml_encoding: utf-8\n" ;
3481 - # $script .= "\n" ;
3482 -
34833462 # proc page
34843463 $script .= "#proc page\n";
34853464 $script .= " dopagebox: no\n";
@@ -3689,7 +3668,6 @@
36903669
36913670 $script .= "\n([inc3])\n\n"; # will be replace by rects
36923671
3693 - # %x = %BarWidths; # XXX doesn't seem to be used
36943672 my ($bar, $width);
36953673 foreach my $entry (@PlotLines) {
36963674 ($bar) = split(",", $entry);
@@ -3701,8 +3679,11 @@
37023680 @PlotBarsNow = @PlotLines;
37033681 &PlotBars;
37043682
3705 - my ($scriptPng1, $scriptPng2, $scriptPng3);
3706 - my ($scriptSvg1, $scriptSvg2);
 3683+ my $scriptPng1 = q{};
 3684+ my $scriptPng2 = q{};
 3685+ my $scriptPng3 = q{};
 3686+ my $scriptSvg1 = q{};
 3687+ my $scriptSvg2 = q{};
37073688
37083689 #proc axis
37093690 if ($#Bars > 0) {
@@ -3853,11 +3834,6 @@
38543835 $scriptSvg1 .= "\n";
38553836 }
38563837
3857 - # $script .= "#proc symbol\n" ;
3858 - # $script .= " location: 01/01/1943(s) Korea \n" ;
3859 - # $script .= " symbol: style=fill shape=downtriangle fillcolor=white radius=0.04\n" ;
3860 - # $script .= "\n" ;
3861 -
38623838 #proc axis
38633839 # repeat without grid to get axis on top of bar
38643840 # needed because axis may overlap bar slightly
@@ -3959,16 +3935,17 @@
39603936
39613937 my $map = ($MapSVG) ? "-map" : "";
39623938
3963 - print "Running Ploticus to generate svg file\n";
 3939+ print "Running Ploticus to generate svg file $file_vector\n";
39643940
3965 - # my $cmd = "$pl $map -" . "svg" . " -o $file_vector $file_script -tightcrop -font \"Times\"" ;
3966 - # my $cmd = "$pl $map -" . "svg" . " -o $file_vector $file_script -tightcrop" ;
 3941+ my $escaped_font_file = EscapeShellArg($font_file);
39673942 my $cmd =
39683943 EscapeShellArg($pl)
39693944 . " $map -" . "svg" . " -o "
39703945 . EscapeShellArg($file_vector) . " "
39713946 . EscapeShellArg($file_script)
3972 - . " -tightcrop -xml_encoding UTF-8";
 3947+ . " -tightcrop"
 3948+ . " -font '$escaped_font_file'"
 3949+ . " -xml_encoding UTF-8";
39733950 print "$cmd\n";
39743951 system($cmd);
39753952
@@ -4004,11 +3981,8 @@
40053982 $map = '';
40063983 }
40073984
4008 - # $crop = "-crop 0,0," + $ImageSize{"width"} . "," . $ImageSize{"height"} ;
4009 - print "Running Ploticus to generate bitmap\n";
 3985+ print "Running Ploticus to generate bitmap file $file_bitmap\n";
40103986
4011 - # $cmd = "$pl $map -" . $image_file_fmt . " -o $file_bitmap $file_script -tightcrop" ; # -v $file_bitmap" ;
4012 - # $cmd = "$pl $map -" . $image_file_fmt . " -o $file_bitmap $file_script -tightcrop -diagfile $file_pl_info -errfile $file_pl_err" ;
40133987 $cmd =
40143988 EscapeShellArg($pl)
40153989 . " $map -"
@@ -4076,18 +4050,38 @@
40774051 }
40784052
40794053 if (-e $file_vector) {
4080 - open "FILE_IN", "<", $file_vector;
4081 - my @svg = <FILE_IN>;
4082 - close "FILE_IN";
 4054+ open my $file_vector_handle, '<', $file_vector
 4055+ or Abort("Can't open $file_vector for reading: $OS_ERROR");
 4056+ my @svg = <$file_vector_handle>;
 4057+ close $file_vector_handle
 4058+ or Abort("Can't open $file_vector after reading: $OS_ERROR");
40834059
40844060 foreach (@svg) {
40854061 s/\{\{(\d+)\}\}x+/$textsSVG[$1]/gxe;
4086 - s/\[(\d+)\[ (.*?) \]\d+\]/'<a style="fill:blue;" xlink:href="' . $linksSVG[$1] . '">' . $2 . '<\/a>'/gxe;
 4062+
 4063+ if ($SVG_ONLY) {
 4064+ s{
 4065+ (
 4066+ <text
 4067+ .*?
 4068+ )
 4069+ >
 4070+ \[(\d+)\[
 4071+ (.*?)
 4072+ \]\d+\]
 4073+ }
 4074+ {$1 style="fill:blue;">$3}gx;
 4075+ }
 4076+ else {
 4077+ s/\[(\d+)\[ (.*?) \]\d+\]/'<a style="fill:blue;" xlink:href="' . $linksSVG[$1] . '">' . $2 . '<\/a>'/gxe;
 4078+ }
40874079 }
40884080
4089 - open "FILE_OUT", ">", $file_vector;
4090 - print FILE_OUT @svg;
4091 - close "FILE_OUT";
 4081+ open $file_vector_handle, '>', $file_vector
 4082+ or Abort("Can't open $file_vector for writing: $OS_ERROR");
 4083+ print {$file_vector_handle} @svg;
 4084+ close $file_vector_handle
 4085+ or Abort("Can't open $file_vector after writing: $OS_ERROR");
40924086 }
40934087
40944088 # not for Wikipedia, for offline use:
@@ -4184,7 +4178,6 @@
41854179 }
41864180 else { $ypos = "$at(s)"; $xpos = "[$barcnt](s)"; }
41874181
4188 - # XXX - $shiftx was defined inside the if block.
41894182 my ($shiftx, $shifty);
41904183 if ($shift ne "") {
41914184 ($shiftx, $shifty) = split(",", $shift);
@@ -4270,24 +4263,9 @@
42714264 my $grid = shift;
42724265 my ($color, $from, $till, $start);
42734266
4274 - # %x = %Period; # XXX doesn't seem to be used
4275 -
4276 - # if (($DateFormat =~ /\//) && ($grid))
4277 - # { return ; }
4278 -
4279 - # if (($DateFormat =~ /\//)
4280 - # {
4281 - # }
4282 -
4283 - # if (! $grid) # redefine area, scale linear for time axis, showl whole years always, Ploticus bug
4284 - # {
4285 - # $from = $Period{"from"} ;
4286 - # $till = $Period{"till"} ;
42874267 $from = &DateToFloat($Period{"from"});
42884268 $till = &DateToFloat($Period{"till"});
42894269
4290 - # $from =~ s/.*\///g ; # delete dd mm if present
4291 - # $till =~ s/.*\///g ;
42924270 #proc areadef
42934271 $script .= "#proc areadef\n";
42944272 $script .= " #clone: A\n";
@@ -4306,15 +4284,10 @@
43074285
43084286 $script .= "\n";
43094287
4310 - # }
4311 -
43124288 $script .= "#proc " . $Axis{"time"} . "axis\n";
43134289
43144290 if (($scale eq "Major") && (!$grid)) {
43154291
4316 - # $script .= " stubs: incremental " . $Scales{"Major inc"} . " " . $Scales{"Major unit"} . "\n" ;
4317 - # if ($DateFormat =~ /\//)
4318 - # { $script .= " stubformat: " . $Axis{"format"} . "\n" ; }
43194292 # temp always show whole years (Ploticus autorange bug)
43204293 if ($Scales{"Major stubs"} eq "") # ($DateFormat !~ /\//)
43214294 {
@@ -4324,10 +4297,7 @@
43254298 }
43264299 else { $script .= " stubs: none\n"; }
43274300
4328 - if ($DateFormat !~ /\//)
4329 -
4330 - # { $script .= " ticincrement: " . $Scales{"$scale inc"} . " " . $Scales{"$scale unit"} . "\n" ; }
4331 - {
 4301+ if ($DateFormat !~ /\//) {
43324302 $script .= " ticincrement: " . $Scales{"$scale inc"} . "\n";
43334303 }
43344304 else {
@@ -4340,7 +4310,6 @@
43414311 if (defined($Scales{"$scale start"})) {
43424312 $start = $Scales{"$scale start"};
43434313
4344 - # $start =~ s/.*\///g ; # delete dd mm if present
43454314 $start = &DateToFloat($start);
43464315 if ($Axis{"order"} =~ /reverse/i) {
43474316 my $loop = 0;
@@ -4362,10 +4331,10 @@
43634332 $script .= " signreverse: yes\n";
43644333 }
43654334 }
4366 - else { $script .= " ticlen: 0.02\n"; }
 4335+ else {
 4336+ $script .= " ticlen: 0.02\n";
 4337+ }
43674338
4368 - # $script .= " location: 4\n" ; test
4369 -
43704339 $color .= $Scales{"$scale grid"};
43714340
43724341 if (defined($Colors{$color})) { $color = $Colors{$color}; }
@@ -4804,7 +4773,9 @@
48054774 }
48064775
48074776 # not part of barset ? return
4808 - if ($bar != /\#\d+$/) { return ($false); }
 4777+ if ($bar !~ /\#\d+$/) {
 4778+ return ($false);
 4779+ }
48094780
48104781 # find previous bar in barset
48114782 my $barcnt = $bar;
@@ -4834,10 +4805,7 @@
48354806 return (CheckAttributes($command, "", "canvas,bars"));
48364807 }
48374808
4838 - if ($command =~ /^BarData$/i)
4839 -
4840 - # { return (CheckAttributes ($command, "", "bar,barset,barcount,link,text")) ; }
4841 - {
 4809+ if ($command =~ /^BarData$/i) {
48424810 return (CheckAttributes($command, "", "bar,barset,link,text"));
48434811 }
48444812
@@ -4969,8 +4937,11 @@
49704938
49714939 my $newcommand = $true;
49724940 my $addvalue = $true;
4973 - my $prevcommand;
4974 - if ($command =~ /^$prevcommand$/i) { $newcommand = $false; }
 4941+ state $prevcommand = q{};
 4942+ if (lc $command eq lc $prevcommand) {
 4943+ $newcommand = $false;
 4944+ }
 4945+
49754946 if ((!$newcommand) && ($command =~ /^(?:DrawLines|PlotData|TextData)$/i))
49764947 {
49774948 $addvalue = $false;

Sign-offs

UserFlagDate
Nikerabbitinspected13:18, 20 February 2012

Follow-up revisions

RevisionCommit summaryAuthorDate
r111864Replaced the svgOnly setting with a method parameter to the <timeline> tag, s...amire8014:09, 19 February 2012

Status & tagging log