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