new-words

diff misc/memo-word-changer @ 66:53ba2847501f

added misc scripts
author Igor Chubin <igor@chub.in>
date Tue Mar 27 14:16:46 2012 +0200 (2012-03-27)
parents
children
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/misc/memo-word-changer	Tue Mar 27 14:16:46 2012 +0200
     1.3 @@ -0,0 +1,150 @@
     1.4 +#!/usr/bin/perl
     1.5 +
     1.6 +our $change_matix = '';
     1.7 +
     1.8 +our $change_matrix = '
     1.9 +um über mit von vom nach zum zur in an auf
    1.10 +der das des dem den ein einer einem einen eines
    1.11 +';
    1.12 +
    1.13 +our $change_matrix = '
    1.14 +router network ip route
    1.15 +';
    1.16 +
    1.17 +if (open(FILE, $ARGV[0])) {
    1.18 +    local $/;
    1.19 +    $change_matrix = <FILE>;
    1.20 +}
    1.21 +shift @ARGV;
    1.22 +
    1.23 +my $BaseURL = $ARGV[0];
    1.24 +shift;
    1.25 +
    1.26 +my $Subdir = $ARGV[0];
    1.27 +shift;
    1.28 +
    1.29 +my $StateFile = $ARGV[0];
    1.30 +shift;
    1.31 +
    1.32 +sub load_state()
    1.33 +{
    1.34 +    my %state;
    1.35 +    my $entries = 0;
    1.36 +    if (open(FILE, "$StateFile")) {
    1.37 +        while(<FILE>) {
    1.38 +            chomp;
    1.39 +            my ($k,$v) = split /\s+/, $_, 2;
    1.40 +            $state{$k} = $v;
    1.41 +            $entries += 1;
    1.42 +        }
    1.43 +    }
    1.44 +    $state{"ENTRIES"} = $entries;
    1.45 +    return \%state;
    1.46 +}
    1.47 +
    1.48 +@colors = qw(ccccff 7aedaa ddeecc ffccee);
    1.49 +
    1.50 +our %change_matrix;
    1.51 +our %color;
    1.52 +our $dropdown_number = 0;
    1.53 +
    1.54 +$i = 0;
    1.55 +for (split /\n/, $change_matrix){
    1.56 +    next if /^\s*$/;
    1.57 +    my @words = sort(split /\s+/);
    1.58 +    for my $kw (@words) {
    1.59 +        $change_matrix{$kw} = [ "???", @words ];
    1.60 +        $color{$kw} = $colors[$i%@colors];
    1.61 +    }
    1.62 +    # uppercase
    1.63 +    for (@words) {
    1.64 +        s/(.)(.*)/\U$1\E$2/;
    1.65 +    }
    1.66 +    for my $kw (@words) {
    1.67 +        $change_matrix{$kw} = [ "???", @words ];
    1.68 +        $color{$kw} = $colors[$i%@colors];
    1.69 +    }
    1.70 +    $i += 1;
    1.71 +};
    1.72 +
    1.73 +#for (keys(%change_matrix)) {
    1.74 +#    print "$_ => ".join(", ", @{$change_matrix{$_}})."\n";
    1.75 +#    print "$_ => ".$color{$_}."\n";
    1.76 +#}
    1.77 +our $CorrectAnswers = 0;
    1.78 +
    1.79 +sub generate_drop_down($$)
    1.80 +{
    1.81 +    my $word = $_[0];
    1.82 +    my %state = %{$_[1]};
    1.83 +
    1.84 +    my $name = "dropdown$dropdown_number";
    1.85 +    my $value = $state{$name};
    1.86 +    $dropdown_number += 1;
    1.87 +    my @variants = @{$change_matrix{$word}};
    1.88 +    $options = join("", map {my $selected = ""; $selected='selected="selected"' if $_ eq $value; "<option value=$_ $selected>$_</option>"} @variants);
    1.89 +    my $color = $color{$word};
    1.90 +    if ($word eq $value) {
    1.91 +        $color = "ffffff";
    1.92 +        $CorrectAnswers += 1;
    1.93 +    }
    1.94 +    if ($color) {
    1.95 +        $color = "background:#$color";
    1.96 +    }
    1.97 +    my $disabled = "";
    1.98 +    if ($value eq $word) {
    1.99 +        $disabled = "disabled";
   1.100 +    }
   1.101 +    my $onchange = "onchange='javascript:myAlert(\"$name\", this.form.$name, \"$word\")'";
   1.102 +    return "<form style='display:inline!important;'><select name='$name' $value $disabled $onchange style='font-size:80%; $color'>".$options."</select></form>"; 
   1.103 +}
   1.104 +sub highlight_word($$$)
   1.105 +{
   1.106 +    my $tag = $_[0];
   1.107 +    my $text = $_[1];
   1.108 +    my $state = $_[2];
   1.109 +
   1.110 +    if ($tag=~/^<[aA]/) {
   1.111 +        return $tag.$text;
   1.112 +    }
   1.113 +    my $re = join("|", keys(%change_matrix));
   1.114 +    $text =~ s@(^|[;> ])($re)(?=([ <]|$))@"$1".generate_drop_down($2, $state)@egms;
   1.115 +    return $tag.$text;
   1.116 +}
   1.117 +
   1.118 +my $state = load_state();
   1.119 +#my %a = %{$state};
   1.120 +#print join(", ", keys(%a)),"\n";
   1.121 +
   1.122 +$jquery = "<script type='text/javascript' src='http://ajax.googleapis.com/ajax/libs/jquery/1.7.0/jquery.min.js'></script>";
   1.123 +local $/;
   1.124 +$text = <>;
   1.125 +$text =~ s@(<a.*?>)(.*?)(</a>)@$2@g;
   1.126 +$text =~ s@(.*)(<body[^>]*>)@$2@ims;
   1.127 +my $before_body = $1;
   1.128 +$text =~ s@(</body[^>]*>)@<script type='text/javascript'>memo_init();</script>$1@ims;
   1.129 +$text =~ s/(<.*?>)([^<]*)(?=<)/highlight_word($1, $2, $state)/egms;
   1.130 +$text = $before_body.$text;
   1.131 +
   1.132 +my $wrong_answers = $$state{"ENTRIES"} - $CorrectAnswers;
   1.133 +$info_div = "
   1.134 +    <div style='
   1.135 +        position:fixed;
   1.136 +        bottom:0;
   1.137 +        right:0;
   1.138 +        color:white;
   1.139 +        background-color:green;
   1.140 +        font-size:70%;
   1.141 +        padding:5pt;
   1.142 +        z-index:1000;
   1.143 +        border-top-left-radius:
   1.144 +        5pt;-moz-border-radius-topleft:5pt;
   1.145 +        '>
   1.146 +            <span id='wrong_answers' style='color:#ff2222;'>$wrong_answers</span>&nbsp;&nbsp;
   1.147 +            <span id='correct_answers'>$CorrectAnswers</span>&nbsp;&nbsp;
   1.148 +            <span id='yet_to_answer'>$dropdown_number</span>
   1.149 +    </div>";
   1.150 +
   1.151 +$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;
   1.152 +$text =~ s@(<head[^>]*>)@$1<base href="$BaseURL">@i;
   1.153 +print $text;