forked from wikimedia/mediawiki-extensions-timeline
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEasyTimeline.pl
executable file
·5336 lines (4627 loc) · 167 KB
/
EasyTimeline.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/perl
# Copyright (C) 2004 Erik Zachte , email xxx\@chello.nl (nospam: xxx=epzachte)
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License version 2
# as published by the Free Software Foundation.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
# See the GNU General Public License for more details, at
# http://www.fsf.org/licenses/gpl.html
# history:
# 1.5 May 27 2004 :
# - when a chart contains only one bar this bar was always centered in the image
# now AlignBars works well in this case aslo ("justify" treated as "center")
# - interwiki links reinstalled e.g. [[de:Gorbachev]]
# - error msgs corrected
# - minimum image size fixed
# - line numbering adapted <timeline>spaces<br> does not count as line one in Wikipedia
# - line breaks in wiki links parsed correctly [[Vladimir~Ilyich~Lenin]]
# - partial url shown as hint for external link (in GIF/PNG)
# - BarData: no attribute 'text:..' supplied -> default to space = show no text on axis
# - PlotData: new attribute 'anchor:..'
# - revert html encoding of '<' & '>' by MediaWiki
# 1.6 May 28 2004 :
# - SVG decode special chars in SVG input fixed
# - BarData: new attributes 'barset:..' and 'barcount:..' # autoincrement bar id
# - PlotData: new attribute 'barset:..'
# - LineData: new attribute 'layer:..', draw lines to back or front of bars and texts
# 1.7
# - EscapeShellArg (Tim Starling)
# 1.8 June .. 2004 :
# - optional autosizing of image (implied when auto incrementing bar count (also new))
# - presentation left-right order of bars reversed on TimeAxis = orientation:vertical
# - TimeAxis option 'order:[normal|reverse]' added
# - BarData: option barcount replaced by auto incrementing bar count and 'break' and 'skip' attributes
# - DrawLines -> LineData (command renamed, but also restructured like PlotData, TextData)
# - new drawing options for LineData, now also lines parallel to time axis, or between arbitrary points
# - Preset command added (specify default settings with 'Preset =', two sets to start with)
# - 'text' attribute parsing bugs (# or : in text gave problems, spaces got lost)
# - PlotArea new attributes 'top' and 'right' make it possible to define plot area margins only
# so resizing image does not imply adjusting PlotArea 'width' and 'height'
# - PlotData option 'shift': only changing x or y value is now possible, e.g. shift=(,10)
# - command ScaleMajor: subs for time axis can now be specified verbatim in option 'text'
# - extra validation checks, defaults, etc
# - function PlotScale now provides workaround for Ploticus bug: auto incrementing dates failed
# 1.9 June 2004
# - stub display order fixed on non time axis
# 1.10 July 2004
# - tempory debug code (removed)
# 1.11 August 2004
# - dot in folder name in input path was misunderstood as start of file extension
# - utf-8 chars within 160-255 range are translated to extended ascii
# however internal font used by Ploticus has strange mapping so some are replaced
# by underscore or unaccented version of character
# this is a make do solution until full unicode support with external fonts will be added
#
# 1.12 June 2009
# - Don't send -mapfile to ploticus without also sending -csmap, this creates an XSS
# vulnerability
#
# 1.13 Jan 2010
# - change svg encoding from iso-8859-1 -> UTF-8
# - allow font to be specified using -f option as opposed to hardcoded FreeSans.
use 5.010;
use strict;
our $VERSION = '1.90';
use Time::Local;
use Getopt::Std;
use Cwd;
use English '-no_match_vars';
# Global variables.
# Many of these should be refactored.
# Whether to generate only an SVG file and have it converted to PNG later.
# To enable it in the MediaWiki extension, use <timeline method="svg2png">
my $SVG_ONLY = 0;
my @PlotLines;
my $CntErrors = 0;
my @Errors;
my @Info;
my @Warnings;
my $file_in;
my $file_name;
my $file_bitmap;
my $file_vector;
my $file_png;
my $file_htmlmap;
my $file_html;
my $file_errors;
my %options;
my $listinput;
my $linkmap;
my $makehtml;
my $bypass;
my $showmap;
my $tmpdir;
my $ploticus_command;
my $articlepath;
my $font_file;
my $true = 1;
my $false = 0;
my $LinkColor = "brightblue";
my $MapPNG = $false; # switched when link or hint found
my $MapSVG = $false; # switched when link found
my $WarnTextOutsideArea = 0;
my $WarnOnRightAlignedText = 0;
my $hPerc = &EncodeInput("\%");
my $hAmp = &EncodeInput("\&");
my $hAt = &EncodeInput("\@");
my $hDollar = &EncodeInput("\$");
my $hBrO = &EncodeInput("\(");
my $hBrC = &EncodeInput("\)");
my $hSemi = &EncodeInput("\;");
my $hIs = &EncodeInput("\=");
my $hLt = &EncodeInput("\<");
my $hGt = &EncodeInput("\>");
my $file;
my $image_file_fmt;
my $env;
my $pathseparator;
my $LineNo;
my $InputParsed;
my $CommandNext = q();
my $Command;
my $DateFormat;
my $Line;
my $NoData;
my %Consts; # see sub GetDefine
my %Colors; # see sub StoreColor
my %BackgroundColors;
my %Axis;
my @Bars;
my %BarLegend;
my %BarLink;
my @LegendData;
my %LineDefs;
my $AlignBars;
my %ColorLabels;
my %Period;
my @DrawLines;
my %Image;
my %Legend;
my %PlotArea;
my %PlotDefs;
my @PlotBars;
my @PlotText;
my $MaxBarWidth;
my %BarWidths;
my $Preset;
my @PresetList;
my %Scales;
my %TextDefs;
# These two must definitely be refactored
my %Attributes;
my @Attributes;
my $firstcmd;
my @PlotTextsPng;
my @PlotTextsSvg;
my @linksSVG;
my @textsSVG;
my @TextData;
my ($sign, $posy1, $posy2);
my $script;
my @PlotBarsNow;
my $command;
# BEGIN
local $| = 1; # flush screen output
print "EasyTimeline version $VERSION\n"
. "Copyright (C) 2004 Erik Zachte\n"
. "Email xxx\@chello.nl (nospam: xxx=epzachte)\n\n"
. "This program is free software; you can redistribute it\n"
. "and/or modify it under the terms of the \n"
. "GNU General Public License version 2 as published by\n"
. "the Free Software Foundation\n"
. "------------------------------------------------------\n";
&SetImageFormat;
&ParseArguments;
&InitFiles;
open "FILE_IN", "<", $file_in;
my @lines = <FILE_IN>;
close "FILE_IN";
&ParseScript;
if ($CntErrors == 0) {
&WritePlotFile;
}
if ($CntErrors == 1) {
&Abort("1 error found");
}
elsif ($CntErrors > 1) {
&Abort("$CntErrors errors found");
}
else {
if (@Info) {
print "\nINFO\n";
print @Info;
print "\n";
}
if (@Warnings) {
print "\nWARNING(S)\n";
print @Warnings;
print "\n";
}
if (!(-e $file_bitmap)) {
print "\nImage $file_bitmap not created.\n";
if ((!(-e "pl.exe")) && (!(-e "pl"))) {
print
"\nPloticus not found in local folder. Is it on your system path?\n";
}
}
elsif (!(-e $file_vector)) {
print "\nImage $file_vector not created.\n";
}
else { print "\nREADY\nNo errors found.\n"; }
}
exit;
sub ParseArguments {
getopt("iTAPef", \%options);
&Abort("Specify input file as: -i filename") if (!defined($options{"i"}));
$file_in = $options{"i"};
$listinput = $options{"l"}; # list all input lines (not recommended)
$linkmap = $options{"m"}; # make clickmap for inclusion in html
# make test html file with gif/png + svg output
$makehtml = $options{"h"};
# do not use in Wikipedia:bypass some checks
$bypass = $options{"b"};
# debug: shows clickable areas in gif/png
$showmap = $options{"d"};
# The following parameters are used by MediaWiki
# to pass config settings from LocalSettings.php to
# the perl script
# For MediaWiki: temp directory to use
$tmpdir = $options{"T"};
# For MediaWiki: full path of ploticus command
$ploticus_command = $options{"P"};
# For MediaWiki: Path of an article, relative to this servers root
$articlepath = $options{"A"};
# font to use. Must be in environemnt variable
# GDFONTPATH unless builtin "ascii" font
$font_file = $options{"f"};
if (!defined $options{"f"}) {
$font_file = "ascii";
}
if (!defined $options{"A"}) {
$articlepath = "http://en.wikipedia.org/wiki/\$1";
}
if (defined $options{"s"}) {
$SVG_ONLY = 1;
}
if (!-e $file_in) {
&Abort("Input file '" . $file_in . "' not found.");
}
}
sub InitFiles {
print "\nInput: Script file $file_in\n";
$file = $file_in;
# 1.10 dot ignore dots in folder names ->
$file =~ s/\.[^\\\/\.]*$//; # remove extension
$file_name = $file;
$file_bitmap = $file . "." . $image_file_fmt;
$file_vector = $file . ".svg";
$file_png = $file . ".png";
$file_htmlmap = $file . ".map";
$file_html = $file . ".html";
$file_errors = $file . ".err";
print "Output: Image files $file_bitmap & $file_vector\n";
if ($linkmap) {
print
" Map file $file_htmlmap (add to html for clickable map)\n";
}
if ($makehtml) { print " HTML test file $file_html\n"; }
# remove previous output
if (-e $file_bitmap) { unlink $file_bitmap; }
if (-e $file_vector) { unlink $file_vector; }
if (-e $file_png) { unlink $file_png; }
if (-e $file_htmlmap) { unlink $file_htmlmap; }
if (-e $file_html) { unlink $file_html; }
if (-e $file_errors) { unlink $file_errors; }
}
sub SetImageFormat {
$env = "";
if ($OSNAME =~ /darwin/i) {
$env = "Linux";
$image_file_fmt = "png";
$pathseparator = "/";
}
elsif ($OSNAME =~ /win/i) {
$env = "Windows";
$image_file_fmt = "gif";
$pathseparator = "\\";
}
else {
$env = "Linux";
$image_file_fmt = "png";
$pathseparator = "/";
}
if ($env ne "") {
print
"\nOS $env detected -> create image in $image_file_fmt format.\n";
}
else {
print
"\nOS not detected. Assuming Windows -> create image in $image_file_fmt format.\n";
$env = "Windows";
}
}
sub ParseScript {
my $command; # local version, $Command = global
$LineNo = 0;
$InputParsed = $false;
$CommandNext = "";
$DateFormat = "x.y";
$firstcmd = $true;
&GetCommand;
&StoreColor("white", &EncodeInput("gray(0.999)"), "");
&StoreColor("barcoldefault", &EncodeInput("rgb(0,0.6,0)"), "");
while (!$InputParsed) {
if ($Command =~ /^\s*$/) { &GetCommand; next; }
if (!($Command =~ /$hIs/)) {
&Error("Invalid statement. No '=' found.");
&GetCommand;
next;
}
if ($Command =~ /$hIs.*$hIs/) {
&Error("Invalid statement. Multiple '=' found.");
&GetCommand;
next;
}
my ($name, $value) = split($hIs, $Command);
$name =~ s/^\s*(.*?)\s*$/$1/;
if ($name =~ /PlotDividers/i) {
&Error(
"Command 'PlotDividers' has been renamed to 'LineData', please adjust."
);
&GetCommand;
next;
}
if ($name =~ /DrawLines/i) {
&Error(
"Command 'DrawLines' has been renamed to 'LineData', please adjust.\n"
. " Reason for change is consistency: LineData now follows the same syntax rules as PlotData and TextData."
);
&GetCommand;
next;
}
if (
(!($name =~ /^(?:Define)\s/))
&& (
!(
$name =~ /^(?:AlignBars|BarData|
BackgroundColors|Colors|DateFormat|LineData|
ScaleMajor|ScaleMinor|
LegendLeft|LegendTop|
ImageSize|PlotArea|Legend|
Period|PlotData|Preset|
TextData|TimeAxis)$/xi
)
)
)
{
&ParseUnknownCommand;
&GetCommand;
next;
}
$value =~ s/^\s*(.*?)\s*//;
if (!($name =~ /^(?:BarData|Colors|LineData|PlotData|TextData)$/i)) {
if ((!(defined($value))) || ($value eq "")) {
if ($name =~ /Preset/i) {
&Error("$name definition incomplete. No value specified\n"
. " At the moment only one preset exists: 'TimeVertical_OneBar_UnitYear'.\n"
. " See also meta.wikipedia.org/wiki/EasyTimeline/Presets"
);
}
else {
&Error(
"$name definition incomplete. No attributes specified"
);
}
&GetCommand;
next;
}
}
if ($name =~
/^(?:BackgroundColors|Colors|Period|ScaleMajor|ScaleMinor|TimeAxis)$/i
)
{
my @attributes = split(" ", $value);
foreach my $attribute (@attributes) {
my ($attrname, $attrvalue) = split("\:", $attribute);
if (
!(
$name . "-" . $attrname =~
/^(?:Colors-Value|Colors-Legend|
Period-From|Period-Till|
ScaleMajor-Color|ScaleMajor-Unit|ScaleMajor-Increment|ScaleMajor-Start|
ScaleMinor-Color|ScaleMinor-Unit|ScaleMinor-Increment|ScaleMinor-Start|
BackgroundColors-Canvas|BackgroundColors-Bars|
TimeAxis-Orientation|TimeAxis-Format)$/xi
)
)
{
&Error(
"$name definition invalid. Unknown attribute '$attrname'."
);
&GetCommand;
next;
}
if ((!defined($attrvalue)) || ($attrvalue eq "")) {
&Error(
"$name definition incomplete. No value specified for attribute '$attrname'."
);
&GetCommand;
next;
}
}
}
if ($Command =~ /^AlignBars/i) { &ParseAlignBars; }
elsif ($Command =~ /^BackgroundColors/i) { &ParseBackgroundColors; }
elsif ($Command =~ /^BarData/i) { &ParseBarData; }
elsif ($Command =~ /^Colors/i) { &ParseColors; }
elsif ($Command =~ /^DateFormat/i) { &ParseDateFormat; }
elsif ($Command =~ /^Define/i) { &ParseDefine; }
elsif ($Command =~ /^ImageSize/i) { &ParseImageSize; }
elsif ($Command =~ /^Legend/i) { &ParseLegend; }
elsif ($Command =~ /^LineData/i) { &ParseLineData; }
elsif ($Command =~ /^Period/i) { &ParsePeriod; }
elsif ($Command =~ /^PlotArea/i) { &ParsePlotArea; }
elsif ($Command =~ /^PlotData/i) { &ParsePlotData; }
elsif ($Command =~ /^Preset/i) { &ParsePreset; }
elsif ($Command =~ /^Scale/i) { &ParseScale; }
elsif ($Command =~ /^TextData/i) { &ParseTextData; }
elsif ($Command =~ /^TimeAxis/i) { &ParseTimeAxis; }
&GetCommand;
$firstcmd = $false;
}
if ($CntErrors == 0) { &DetectMissingCommands; }
if ($CntErrors == 0) { &ValidateAndNormalizeDimensions; }
}
sub GetLine {
if ($#lines < 0) {
$InputParsed = $true;
return ("");
}
# running in Wikipedia context and first line empty ?
# skip first line without incrementing line count
# this is part behind <timeline> and will not be thought of as line 1
if (defined $options{"A"}) {
if (($#lines >= 0) && ($lines[0] =~ /^\s*$/)) {
$Line = shift(@lines);
}
}
my $commentstart;
$Line = "";
while (($#lines >= 0) && ($Line =~ /^\s*$/)) {
$LineNo++;
$Line = shift(@lines);
chomp($Line);
if ($listinput) {
print "$LineNo: " . &DecodeInput($Line) . "\n";
}
# preserve '#' within double quotes
$Line =~ s/(\"[^\"]*\")/$a=$1,$a=~s^\#^\%\?\+^g,$a/ge;
$Line =~ s/#>.*?<#//g;
if ($Line =~ /#>/) {
$commentstart = $LineNo;
$Line =~ s/#>.*?$//;
}
elsif ($Line =~ /<#/) {
undef $commentstart;
$Line =~ s/^.*?<#//x;
}
elsif (defined($commentstart)) { $Line = ""; next; }
# remove single line comments (keep html char tags, like  )
$Line =~ s/\&\#/\&\$\%/g;
$Line =~ s/\#.*$//;
$Line =~ s/\&\$\%/\&\#/g;
$Line =~ s/\%\?\+/\#/g;
$Line =~ s/\s*$//g;
$Line =~ s/\t/ /g;
}
if ($Line !~ /^\s*$/) {
$Line = &EncodeInput($Line);
if (!($Line =~ /^\s*Define/i)) {
$Line =~ s/($hDollar[a-zA-Z0-9]+)/&GetDefine($Line,$1)/ge;
}
}
if (($#lines < 0) && (defined($commentstart))) {
&Error2(
"No matching end of comment found for comment block starting at line $commentstart.\n"
. "Text between \#> and <\# (multiple lines) or following \# (single line) will be treated as comment."
);
}
return ($Line);
}
sub GetCommand {
undef(%Attributes);
$Command = "";
if ($CommandNext ne "") {
$Command = $CommandNext;
$CommandNext = "";
}
else { $Command = &GetLine; }
if ($Command =~ /^\s/) {
&Error(
"New command expected instead of data line (= line starting with spaces). Data line(s) ignored.\n"
);
$Command = &GetLine;
while (($#lines >= 0) && ($Command =~ /^\s/)) { $Command = &GetLine; }
}
if ($Command =~ /^[^\s]/) {
my $line = $Command;
$line =~ s/^.*$hIs\s*//;
&CollectAttributes($line);
}
}
sub GetData {
undef(%Attributes);
$Command = "";
$NoData = $false;
my $line = &GetLine;
if ($line =~ /^[^\s]/) {
$CommandNext = $line;
$NoData = $true;
return ("");
}
if ($line =~ /^\s*$/) {
$NoData = $true;
return ("");
}
$line =~ s/^\s*//g;
&CollectAttributes($line);
}
sub CollectAttributes {
my $line = shift;
# replace colon (:), would conflict with syntax
$line =~ s/(\slink\:[^\s\:]*)\:/$1'colon'/i;
$line =~ s/(\stext\:[^\s\:]*)\:/$1'colon'/i;
$line =~ s/(https?)\:/$1'colon'/i;
my $text;
($line, $text) = &ExtractText($line);
$text =~ s/'colon'/:/;
$line =~ s/( $hBrO .+? $hBrC )/&RemoveSpaces($1)/gxe;
$line =~ s/\s*\:\s*/:/g;
$line =~ s/([a-zA-Z0-9\_]+)\:/lc($1) . ":"/gxe;
my @Fields = split(" ", $line);
my ($name, $value);
foreach my $field (@Fields) {
if ($field =~ /\:/) {
($name, $value) = split(":", $field);
$name =~ s/^\s*(.*)\s*$/lc($1)/gxe;
$value =~ s/^\s*(.*)\s*$/$1/gxe;
if ( ($name ne "bar")
&& ($name ne "text")
&& ($name ne "link")
&& ($name ne "legend"))
{
$value = lc($value);
}
if ($name eq "link") # restore colon
{
$value =~ s/'colon'/:/;
}
if ($value eq "") {
if ($name =~ /Text/i) { $value = " "; }
else {
&Error(
"No value specified for attribute '$name'. Attribute ignored."
);
}
}
else { $Attributes{$name} = $value; }
}
else {
if (defined($Attributes{"single"})) {
&Error(
"Invalid attribute '$field' ignored.\nSpecify attributes as 'name:value' pair(s)."
);
}
else {
$field =~ s/^\s*(.*)\s*$/$1/gxe;
$Attributes{"single"} = $field;
}
}
}
if ( (defined $name and $name ne "")
and (defined $Attributes{"single"} and $Attributes{"single"} ne ""))
{
&Error( "Invalid attribute '"
. $Attributes{"single"}
. "' ignored.\nSpecify attributes as 'name:value' pairs.");
delete($Attributes{"single"});
}
if ((defined($text)) && ($text ne "")) {
$Attributes{"text"} = &ParseText($text);
}
}
sub GetDefine {
my $command = shift;
my $const = shift;
$const = lc($const);
my $value = $Consts{ lc($const) };
if (!defined($value)) {
&Error("Unknown constant. 'Define $const = ... ' expected.");
return ($const);
}
return ($value);
}
sub ParseAlignBars {
&CheckPreset("AlignBars");
my $align = $Attributes{"single"};
if (!($align =~ /^(?:justify|early|late)$/i)) {
&Error(
"AlignBars value '$align' invalid. Specify 'justify', 'early' or 'late'."
);
return;
}
$AlignBars = lc($align);
}
sub ParseBackgroundColors {
if (!&ValidAttributes("BackgroundColors")) {
&GetData;
next;
}
&CheckPreset("BackGroundColors");
foreach my $attribute (keys %Attributes) {
my $attrvalue = $Attributes{$attribute};
if ($attribute =~ /Canvas/i) {
if (!&ColorPredefined($attrvalue)) {
if (!defined($Colors{ lc($attrvalue) })) {
&Error(
"BackgroundColors definition invalid. Attribute '$attribute': unknown color '$attrvalue'.\n"
. " Specify command 'Color' before this command."
);
return;
}
}
if (defined($Colors{ lc($attrvalue) })) {
$Attributes{"canvas"} = $Colors{ lc($attrvalue) };
}
else {
$Attributes{"canvas"} = lc($attrvalue);
}
}
elsif ($attribute =~ /Bars/i) {
if (!defined($Colors{ lc($attrvalue) })) {
&Error(
"BackgroundColors definition invalid. Attribute '$attribute' unknown color '$attrvalue'.\n"
. " Specify command 'Color' before this command.");
return;
}
$Attributes{"bars"} = lc($attrvalue);
}
}
%BackgroundColors = %Attributes;
}
sub ParseBarData {
&GetData;
if ($NoData) {
&Error(
"Data expected for command 'BarData', but line is not indented.\n"
);
return;
}
my ($bar, $text, $link, $hint, $barset); # , $barcount) ;
BarData:
while ((!$InputParsed) && (!$NoData)) {
if (!&ValidAttributes("BarData")) { &GetData; next; }
$bar = "";
$link = "";
$hint = "";
$barset = ""; # $barcount = "" ;
# warn "data: $data";
my $data2; # = $data;
($data2, $text) = &ExtractText($data2);
@Attributes = split(" ", $data2);
foreach my $attribute (keys %Attributes) {
my $attrvalue = $Attributes{$attribute};
if ($attribute =~ /^Bar$/i) {
$bar = $attrvalue;
}
elsif ($attribute =~ /^BarSet$/i) {
$barset = $attrvalue;
}
# elsif ($attribute =~ /^BarCount$/i)
# {
# $barcount = $attrvalue ;
# if (($barcount !~ /^\d?\d?\d$/) || ($barcount < 2) || ($barcount > 200))
# { &Error ("BarData attribute 'barcount' invalid. Specify a number between 2 and 200\n") ;
# &GetData ; next BarData ; }
# }
elsif ($attribute =~ /^Text$/i) {
$text = $attrvalue;
$text =~ s/\\n/~/gs;
if ($text =~ /\~/) {
&Warning( "BarData attribute 'text' contains ~ (tilde).\n"
. "Tilde will not be translated into newline character (only in PlotData)"
);
}
if ($text =~ /\^/) {
&Warning( "BarData attribute 'text' contains ^ (caret).\n"
. "Caret will not be translated into tab character (only in PlotData)"
);
}
}
elsif ($attribute =~ /^Link$/i) {
$link = &ParseText($attrvalue);
if ($link =~ /\[.*\]/) {
&Error(
"BarData attribute 'link' contains implicit (wiki style) link.\n"
. "Use implicit link style with attribute 'text' only.\n"
);
&GetData;
next BarData;
}
$link = &EncodeURL(&NormalizeURL($link));
$MapPNG = $true;
}
}
if (($bar eq "") && ($barset eq "")) {
&Error(
"BarData attribute missing. Specify either 'bar' of 'barset'.\n"
);
&GetData;
next BarData;
}
if (($bar ne "") && ($barset ne "")) {
&Error(
"BarData attributes 'bar' and 'barset' are mutually exclusive.\nSpecify one of these per data line\n"
);
&GetData;
next BarData;
}
# if (($barset ne "") && ($barcount eq ""))
# { &Error ("BarData attribute 'barset' specified without attribute 'barcount'.\n") ;
# &GetData ; next BarData ; }
# if (($barset eq "") && ($barcount ne ""))
# { &Error ("BarData attribute 'barcount' specified without attribute 'barset'.\n") ;
# &GetData ; next BarData ; }
if (($barset ne "") && ($link ne "")) {
&Error(
"BarData attribute 'link' not valid in combination with attribute 'barset'.\n"
);
&GetData;
next BarData;
}
if ($link ne "") {
if ($text =~ /\[.*\]/) {
&Warning(
"BarData contains implicit link(s) in attribute 'text' and explicit attribute 'link'.\n"
. "Implicit link(s) ignored.");
$text =~ s/\[+ (?:[^\|]* \|)? ([^\]]*) \]+/$1/gx;
}
if ($hint eq "") { $hint = &ExternalLinkToHint($link); }
}
if (($bar ne "") && ($bar !~ /[a-zA-Z0-9\_]+/)) {
&Error(
"BarData attribute bar:'$bar' invalid.\nUse only characters 'a'-'z', 'A'-'Z', '0'-'9', '_'\n"
);
&GetData;
next BarData;
}
if ($bar ne "") {
if ($Axis{"time"} eq "x") { push @Bars, $bar; }
else { unshift @Bars, $bar; }
if ($text ne "") { $BarLegend{ lc($bar) } = $text; }
else { $BarLegend{ lc($bar) } = " "; }
if ($link ne "") { $BarLink{ lc($bar) } = $link; }
}
else {
# for ($b = 1 ; $b <= $barcount ; $b++)
# {
# $bar = $barset . "#" . $b ;
$bar = $barset . "#1";
if ($Axis{"time"} eq "x") { push @Bars, $bar; }
else { unshift @Bars, $bar; }
if ($text ne "") { $BarLegend{ lc($bar) } = $text . " - " . $b; }
else { $BarLegend{ lc($bar) } = " "; }
# }
}
&GetData;
}
}
sub ParseColors {
my $colorname;
&GetData;
if ($NoData) {
&Error(
"Data expected for command 'Colors', but line is not indented.\n"
);
return;
}
Colors:
while ((!$InputParsed) && (!$NoData)) {
if (!&ValidAttributes("Colors")) { &GetData; next; }
&CheckPreset("Colors");
my $addtolegend = $false;
my $legendvalue = "";
my $colorvalue = "";
foreach my $attribute (keys %Attributes) {
my $attrvalue = $Attributes{$attribute};
if ($attribute =~ /Id/i) {
$colorname = $attrvalue;
}
elsif ($attribute =~ /Legend/i) {
$addtolegend = $true;
$legendvalue = $attrvalue;
if ($legendvalue =~ /^[yY]$/) {
push @LegendData, $colorname;
}
elsif (!($attrvalue =~ /^[nN]$/)) {
$legendvalue = &ParseText($legendvalue);
push @LegendData, $legendvalue;
}
}
elsif ($attribute =~ /Value/i) {
$colorvalue = $attrvalue;
if ($colorvalue =~ /^white$/i) {
$colorvalue = "gray" . $hBrO . "0.999" . $hBrC;
}
}
}
if (&ColorPredefined($colorvalue)) {
&StoreColor($colorname, $colorvalue, $legendvalue);
&GetData;
next Colors;
}
if ($colorvalue =~ /^[a-z]+$/i) {
if (!($colorvalue =~ /^(?:gray|rgb|hsb)/i)) {
&Error(
"Color value invalid: unknown constant '$colorvalue'.");
&GetData;
next Colors;
}
}
if (!($colorvalue =~ /^(?:gray|rgb|hsb) $hBrO .+? $hBrC/xi)) {
&Error(
"Color value invalid. Specify constant or 'gray/rgb/hsb(numeric values)' "
);
&GetData;
next Colors;
}
if ($colorvalue =~ /^gray/i) {
if ($colorvalue =~ /gray $hBrO (?:0|1|0\.\d+) $hBrC/xi) {
&StoreColor($colorname, $colorvalue, $legendvalue);
}
else {
&Error(