new-words

diff new-words.sh @ 2:68722cd6faff

Добавление пометок к словам.

Справа от слова можно поставить пометку,
которая попадает в файл ~/.new-words/notes-LANG.txt.
Потом, при появлении этого слова,
она вспоминается и выводится вместе с ним.
author igor@book.xt.vpn
date Fri Feb 26 21:41:27 2010 +0200 (2010-02-26)
parents 4c9076f87241
children c703b8898696
line diff
     1.1 --- a/new-words.sh	Mon Feb 22 08:34:11 2010 +0200
     1.2 +++ b/new-words.sh	Fri Feb 26 21:41:27 2010 +0200
     1.3 @@ -17,20 +17,21 @@
     1.4  HELP
     1.5  
     1.6  WORK_DIR=~/.new-words/
     1.7 -LANGUAGE=en
     1.8 -VOCABULARY=${LANGUAGE}.txt
     1.9  TEMP1=`mktemp /tmp/news-words-XXXXXXXXXX`
    1.10  TEMP2=`mktemp /tmp/news-words-XXXXXXXXXX`
    1.11  editor=${EDITOR:-vim}
    1.12  
    1.13 -my_name="`echo $0 | sed s/-words// | sed s@.*/@@`"
    1.14 -[ "${my_name}" = "new" ] || VOCABULARY="$my_name".txt
    1.15 +LANGUAGE=en
    1.16 +my_name="`echo $0 | sed s@.*/@@ | sed s/-.*// `"
    1.17 +[ "${my_name}" = "new" ] || LANGUAGE="$my_name"
    1.18  if [ "$1" = "-l" ]
    1.19  then
    1.20      LANGUAGE="$2"
    1.21      VOCABULARY="$LANGUAGE".txt
    1.22      shift 2
    1.23  fi
    1.24 +VOCABULARY=${LANGUAGE}.txt
    1.25 +NOTES_FILE=notes-${LANGUAGE}.txt
    1.26  
    1.27  get_words()
    1.28  {
    1.29 @@ -74,6 +75,101 @@
    1.30  lynx -dump "$1" | perl -p -e 's@http://[a-zA-Z&_.:/0-9%?=,#+()\[\]~-]*@@'
    1.31  }
    1.32  
    1.33 +add_marks()
    1.34 +{
    1.35 +    PERL_SCRIPT_TEMP_NAME=`mktemp /tmp/perl-grep-v-english-XXXXXXXX`
    1.36 +    cat <<'PERL_SCRIPT' > $PERL_SCRIPT_TEMP_NAME
    1.37 +$file = $ARGV[0];
    1.38 +our $dict;
    1.39 +if (open(NOTES, $ENV{NOTES_FILE})) {
    1.40 +    while(<NOTES>) {
    1.41 +        chomp;
    1.42 +        s/^\s+//;
    1.43 +        my ($a,$b)=split /\s+/,$_,2;
    1.44 +        $dict{$a}=$b;
    1.45 +    }
    1.46 +}
    1.47 +if (open(F, $file)) {
    1.48 +    @lines=<F>;
    1.49 +    close(F);
    1.50 +
    1.51 +    if (open(F, ">$file")) {
    1.52 +        for (@lines) {
    1.53 +            m/\s+\S+\s+(\S+)/;
    1.54 +            $name=$1;
    1.55 +            if (defined($dict{$name})) {
    1.56 +                chomp;
    1.57 +                $mark=$dict{$name};
    1.58 +                $space=" "x(30-length($_));
    1.59 +                print F "$_$space$mark\n";
    1.60 +            }
    1.61 +            else {
    1.62 +                print F "$_";
    1.63 +            }
    1.64 +        }
    1.65 +        close(F);
    1.66 +    }
    1.67 +}
    1.68 +PERL_SCRIPT
    1.69 +    [ -e "$NOTES_FILE" ] || touch "$NOTES_FILE"
    1.70 +    export NOTES_FILE
    1.71 +    perl $PERL_SCRIPT_TEMP_NAME "$1"
    1.72 +    rm $PERL_SCRIPT_TEMP_NAME
    1.73 +}
    1.74 +
    1.75 +remove_marks()
    1.76 +{
    1.77 +    PERL_SCRIPT_TEMP_NAME=`mktemp /tmp/perl-grep-v-english-XXXXXXXX`
    1.78 +    cat <<'PERL_SCRIPT' > $PERL_SCRIPT_TEMP_NAME
    1.79 +$file = $ARGV[0];
    1.80 +our %dict;
    1.81 +if (open(F, $file)) {
    1.82 +    @lines=<F>;
    1.83 +    close(F);
    1.84 +
    1.85 +    if (open(F, ">$file")) {
    1.86 +        for (@lines) {
    1.87 +            chomp;
    1.88 +            if (m/(\s+)(\S+)(\s+)(\S+)(\s+)(.*)/) {
    1.89 +                my $name=$4;
    1.90 +                my $comment=$6;
    1.91 +                $dict{$name}=$comment;
    1.92 +                print F "$1$2$3$4\n";
    1.93 +            }
    1.94 +            else {
    1.95 +                print F "$_\n";
    1.96 +            }
    1.97 +        }
    1.98 +    }
    1.99 +}
   1.100 +if (open(NOTES, $ENV{NOTES_FILE})) {
   1.101 +    @lines=<NOTES>;
   1.102 +    close(NOTES);
   1.103 +
   1.104 +    if (open(NOTES, ">".$ENV{NOTES_FILE})) {
   1.105 +        for (@lines) {
   1.106 +            chomp;
   1.107 +            s/^\s+//;
   1.108 +            my ($a,$b)=split /\s+/,$_,2;
   1.109 +            if (not defined($dict{$a}) || ($dict{$a} eq $b)) {
   1.110 +                print NOTES "$_\n";
   1.111 +                if (defined($dict{$a})) { unset($dict{$a}); }
   1.112 +            }
   1.113 +        }
   1.114 +        for (keys %dict) {
   1.115 +            $mark=$dict{$_};
   1.116 +            $space=" "x(30-length($_));
   1.117 +            print NOTES "$_$space$mark\n";
   1.118 +        }
   1.119 +    }
   1.120 +}
   1.121 +PERL_SCRIPT
   1.122 +    [ -e "$NOTES_FILE" ] || touch "$NOTES_FILE"
   1.123 +    export NOTES_FILE
   1.124 +    perl $PERL_SCRIPT_TEMP_NAME "$1"
   1.125 +    rm $PERL_SCRIPT_TEMP_NAME
   1.126 +}
   1.127 +
   1.128  mkdir -p $WORK_DIR
   1.129  cd $WORK_DIR
   1.130  if echo "$1" | grep -q http: 
   1.131 @@ -86,6 +182,7 @@
   1.132      get_words | tee "$TEMP1" > "$TEMP2"
   1.133  fi
   1.134  
   1.135 +add_marks "$TEMP2"
   1.136  if [ "$editor" = vim ]
   1.137  then
   1.138      vim -c 'set keywordprg='"$LANGUAGE" -c 'set iskeyword=@,48-57,/,.,-,_,+,,,#,$,%,~,=' "$TEMP2" < /dev/tty > /dev/tty
   1.139 @@ -93,5 +190,7 @@
   1.140      echo 2
   1.141      $editor "$TEMP2"
   1.142  fi
   1.143 +remove_marks "$TEMP2"
   1.144 +
   1.145  diff "$TEMP1" "$TEMP2" | awk '{print $3}' | sort -u >> "$VOCABULARY"
   1.146  rm -f "$TEMP1" "$TEMP2"