new-words

annotate misc/memo-word-changer @ 67:87bb1c5e6616

added de script to misc/
author Igor Chubin <igor@chub.in>
date Wed Mar 28 15:54:30 2012 +0200 (2012-03-28)
parents
children
rev   line source
igor@66 1 #!/usr/bin/perl
igor@66 2
igor@66 3 our $change_matix = '';
igor@66 4
igor@66 5 our $change_matrix = '
igor@66 6 um über mit von vom nach zum zur in an auf
igor@66 7 der das des dem den ein einer einem einen eines
igor@66 8 ';
igor@66 9
igor@66 10 our $change_matrix = '
igor@66 11 router network ip route
igor@66 12 ';
igor@66 13
igor@66 14 if (open(FILE, $ARGV[0])) {
igor@66 15 local $/;
igor@66 16 $change_matrix = <FILE>;
igor@66 17 }
igor@66 18 shift @ARGV;
igor@66 19
igor@66 20 my $BaseURL = $ARGV[0];
igor@66 21 shift;
igor@66 22
igor@66 23 my $Subdir = $ARGV[0];
igor@66 24 shift;
igor@66 25
igor@66 26 my $StateFile = $ARGV[0];
igor@66 27 shift;
igor@66 28
igor@66 29 sub load_state()
igor@66 30 {
igor@66 31 my %state;
igor@66 32 my $entries = 0;
igor@66 33 if (open(FILE, "$StateFile")) {
igor@66 34 while(<FILE>) {
igor@66 35 chomp;
igor@66 36 my ($k,$v) = split /\s+/, $_, 2;
igor@66 37 $state{$k} = $v;
igor@66 38 $entries += 1;
igor@66 39 }
igor@66 40 }
igor@66 41 $state{"ENTRIES"} = $entries;
igor@66 42 return \%state;
igor@66 43 }
igor@66 44
igor@66 45 @colors = qw(ccccff 7aedaa ddeecc ffccee);
igor@66 46
igor@66 47 our %change_matrix;
igor@66 48 our %color;
igor@66 49 our $dropdown_number = 0;
igor@66 50
igor@66 51 $i = 0;
igor@66 52 for (split /\n/, $change_matrix){
igor@66 53 next if /^\s*$/;
igor@66 54 my @words = sort(split /\s+/);
igor@66 55 for my $kw (@words) {
igor@66 56 $change_matrix{$kw} = [ "???", @words ];
igor@66 57 $color{$kw} = $colors[$i%@colors];
igor@66 58 }
igor@66 59 # uppercase
igor@66 60 for (@words) {
igor@66 61 s/(.)(.*)/\U$1\E$2/;
igor@66 62 }
igor@66 63 for my $kw (@words) {
igor@66 64 $change_matrix{$kw} = [ "???", @words ];
igor@66 65 $color{$kw} = $colors[$i%@colors];
igor@66 66 }
igor@66 67 $i += 1;
igor@66 68 };
igor@66 69
igor@66 70 #for (keys(%change_matrix)) {
igor@66 71 # print "$_ => ".join(", ", @{$change_matrix{$_}})."\n";
igor@66 72 # print "$_ => ".$color{$_}."\n";
igor@66 73 #}
igor@66 74 our $CorrectAnswers = 0;
igor@66 75
igor@66 76 sub generate_drop_down($$)
igor@66 77 {
igor@66 78 my $word = $_[0];
igor@66 79 my %state = %{$_[1]};
igor@66 80
igor@66 81 my $name = "dropdown$dropdown_number";
igor@66 82 my $value = $state{$name};
igor@66 83 $dropdown_number += 1;
igor@66 84 my @variants = @{$change_matrix{$word}};
igor@66 85 $options = join("", map {my $selected = ""; $selected='selected="selected"' if $_ eq $value; "<option value=$_ $selected>$_</option>"} @variants);
igor@66 86 my $color = $color{$word};
igor@66 87 if ($word eq $value) {
igor@66 88 $color = "ffffff";
igor@66 89 $CorrectAnswers += 1;
igor@66 90 }
igor@66 91 if ($color) {
igor@66 92 $color = "background:#$color";
igor@66 93 }
igor@66 94 my $disabled = "";
igor@66 95 if ($value eq $word) {
igor@66 96 $disabled = "disabled";
igor@66 97 }
igor@66 98 my $onchange = "onchange='javascript:myAlert(\"$name\", this.form.$name, \"$word\")'";
igor@66 99 return "<form style='display:inline!important;'><select name='$name' $value $disabled $onchange style='font-size:80%; $color'>".$options."</select></form>";
igor@66 100 }
igor@66 101 sub highlight_word($$$)
igor@66 102 {
igor@66 103 my $tag = $_[0];
igor@66 104 my $text = $_[1];
igor@66 105 my $state = $_[2];
igor@66 106
igor@66 107 if ($tag=~/^<[aA]/) {
igor@66 108 return $tag.$text;
igor@66 109 }
igor@66 110 my $re = join("|", keys(%change_matrix));
igor@66 111 $text =~ s@(^|[;> ])($re)(?=([ <]|$))@"$1".generate_drop_down($2, $state)@egms;
igor@66 112 return $tag.$text;
igor@66 113 }
igor@66 114
igor@66 115 my $state = load_state();
igor@66 116 #my %a = %{$state};
igor@66 117 #print join(", ", keys(%a)),"\n";
igor@66 118
igor@66 119 $jquery = "<script type='text/javascript' src='http://ajax.googleapis.com/ajax/libs/jquery/1.7.0/jquery.min.js'></script>";
igor@66 120 local $/;
igor@66 121 $text = <>;
igor@66 122 $text =~ s@(<a.*?>)(.*?)(</a>)@$2@g;
igor@66 123 $text =~ s@(.*)(<body[^>]*>)@$2@ims;
igor@66 124 my $before_body = $1;
igor@66 125 $text =~ s@(</body[^>]*>)@<script type='text/javascript'>memo_init();</script>$1@ims;
igor@66 126 $text =~ s/(<.*?>)([^<]*)(?=<)/highlight_word($1, $2, $state)/egms;
igor@66 127 $text = $before_body.$text;
igor@66 128
igor@66 129 my $wrong_answers = $$state{"ENTRIES"} - $CorrectAnswers;
igor@66 130 $info_div = "
igor@66 131 <div style='
igor@66 132 position:fixed;
igor@66 133 bottom:0;
igor@66 134 right:0;
igor@66 135 color:white;
igor@66 136 background-color:green;
igor@66 137 font-size:70%;
igor@66 138 padding:5pt;
igor@66 139 z-index:1000;
igor@66 140 border-top-left-radius:
igor@66 141 5pt;-moz-border-radius-topleft:5pt;
igor@66 142 '>
igor@66 143 <span id='wrong_answers' style='color:#ff2222;'>$wrong_answers</span>&nbsp;&nbsp;
igor@66 144 <span id='correct_answers'>$CorrectAnswers</span>&nbsp;&nbsp;
igor@66 145 <span id='yet_to_answer'>$dropdown_number</span>
igor@66 146 </div>";
igor@66 147
igor@66 148 $text =~ s@(<body[^>]*>)@<script type="text/javascript">var memo_subdir="$Subdir";</script><script type="text/javascript" src="http://xgu.ru/memo_files/memo.js"></script>$jquery$1$info_div@i;
igor@66 149 $text =~ s@(<head[^>]*>)@$1<base href="$BaseURL">@i;
igor@66 150 print $text;