new-words

annotate misc/word-changer.pl @ 68:846240941452

added -C key: compress to lines; fixed bug with #90-line
author Igor Chubin <igor@chub.in>
date Sun Sep 23 16:07:29 2012 +0300 (2012-09-23)
parents
children
rev   line source
igor@66 1 our $change_matrix = '
igor@66 2 um über mit von vom nach zum zur in an auf
igor@66 3 der das des dem den ein einer einem einen eines
igor@66 4 ';
igor@66 5
igor@66 6 @colors = qw(ccccff 7aedaa ddeecc ffccee);
igor@66 7
igor@66 8 our %change_matrix;
igor@66 9 our %color;
igor@66 10 our $dropdown_number = 0;
igor@66 11
igor@66 12 $i = 0;
igor@66 13 for (split /\n/, $change_matrix){
igor@66 14 next if /^\s*$/;
igor@66 15 my @words = sort(split /\s+/);
igor@66 16 for my $kw (@words) {
igor@66 17 $change_matrix{$kw} = [ "???", @words ];
igor@66 18 $color{$kw} = $colors[$i%@colors];
igor@66 19 }
igor@66 20 # uppercase
igor@66 21 for (@words) {
igor@66 22 s/(.)(.*)/\U$1\E$2/;
igor@66 23 }
igor@66 24 for my $kw (@words) {
igor@66 25 $change_matrix{$kw} = [ "???", @words ];
igor@66 26 $color{$kw} = $colors[$i%@colors];
igor@66 27 }
igor@66 28 $i += 1;
igor@66 29 };
igor@66 30
igor@66 31 for (keys(%change_matrix)) {
igor@66 32 print "$_ => ".join(", ", @{$change_matrix{$_}})."\n";
igor@66 33 print "$_ => ".$color{$_}."\n";
igor@66 34 }
igor@66 35
igor@66 36 sub generate_drop_down($)
igor@66 37 {
igor@66 38 my $name = "dropdown$dropdown_number";
igor@66 39 $dropdown_number += 1;
igor@66 40 my $word = $_[0];
igor@66 41 my @variants = @{$change_matrix{$word}};
igor@66 42 $options = join("", map {"<option value=$_>$_</option>"} @variants);
igor@66 43 my $color = $color{$word};
igor@66 44 if ($color) {
igor@66 45 $color = "background:#$color";
igor@66 46 }
igor@66 47 my $onchange = "onchange='javascript:myAlert(this.form.$name, \"$word\")'";
igor@66 48 return "<form style='display:inline!important;'><select name='$name' $onchange style='font-size:80%; $color'>".$options."</select></form>";
igor@66 49 }
igor@66 50 sub highlight_word($$)
igor@66 51 {
igor@66 52 my $tag = $_[0];
igor@66 53 my $text = $_[1];
igor@66 54 if ($tag=~/^<[aA]/) {
igor@66 55 return $tag.$text;
igor@66 56 }
igor@66 57 my $re = join("|", keys(%change_matrix));
igor@66 58 #$text =~ s@ ([Aa]uf) @<input contenteditable=True style='text-color:#eecccc' />@g;
igor@66 59 $text =~ s@(^|[;> ])($re)(?=([ <]|$))@"$1".generate_drop_down($2)@eg;
igor@66 60 return $tag.$text;
igor@66 61 }
igor@66 62
igor@66 63 $jquery = "<script type='text/javascript' src='http://ajax.googleapis.com/ajax/libs/jquery/1.7.0/jquery.min.js'></script>";
igor@66 64 local $/;
igor@66 65 $text = <>;
igor@66 66 $text =~ s@(<a.*?>)(.*?)(</a>)@$2@g;
igor@66 67 $text =~ s/(<.*?>)([^<]*)(?=<)/highlight_word($1, $2)/eg;
igor@66 68 $info_div = "
igor@66 69 <div style='
igor@66 70 position:fixed;
igor@66 71 bottom:0;
igor@66 72 right:0;
igor@66 73 color:white;
igor@66 74 background-color:green;
igor@66 75 font-size:70%;
igor@66 76 padding:5pt;
igor@66 77 z-index:1000;
igor@66 78 border-top-left-radius:
igor@66 79 5pt;-moz-border-radius-topleft:5pt;
igor@66 80 '>
igor@66 81 <span id='wrong_answers' style='color:#ff2222;'>0</span>&nbsp;&nbsp;
igor@66 82 <span id='correct_answers'>0</span>&nbsp;&nbsp;
igor@66 83 <span id='yet_to_answer'>$dropdown_number</span>
igor@66 84 </div>";
igor@66 85
igor@66 86 $text =~ s@(<body[^>]*>)@<script type="text/javascript" src="Kiew.js"></script>$jquery$1$info_div@i;
igor@66 87 print $text;