# HG changeset patch
# User devi
# Date 1141250814 -7200
# Node ID 385499ec544aee4ce33c8bf29d45c1bfd4a3d03a
# Parent  c70be67ed3d4a87c6afe891785936efb33e0633d
Начал работу над текстовым представлением +
редактором журнала.
Пока что в textarea выводится текстовое представление.

Редактировать можно, но сохранять пока нельзя.

Пытался сделать автоматическое позиционирование курсора
на нужную строку.
Во-первых, не работает в Konqueror и с этим, я так понимаю,
ничего пока сделать нельзя.

Во-вторых, неверно вычисляется строка на которую нужно
позиционировать курсор.

Это, я думаю, можно подправить.

Потом, что-то намутил с utf8. Надо будет более детально
это рассмотреть

diff -r c70be67ed3d4 -r 385499ec544a l3-cgi
--- a/l3-cgi	Tue Feb 28 13:11:26 2006 +0200
+++ b/l3-cgi	Thu Mar 02 00:06:54 2006 +0200
@@ -81,22 +81,22 @@
             my $t = $Trainings{$tdate};
             $print .= "<tr>";
             $print .= "<td>".$t->{date}."</td>";
-            $print .= "<td>".$t->{course}."</td>";
+            $print .= "<td colspan='7'>".$t->{course}."</td>";
             $print .= "</tr>\n";
-            $print .= "<tr>";
-            $print .= "<td/>";
-            $print .= "<td><pre>";
             for my $host (sort keys %{$t->{host}}) {
+                my %row;
+                my @row_keys = qw{
+                    name email company host root user
+                };
                 my $h = $t->{host}->{$host};
-                $print .= "$host";
-                $print .= " ".$h->{firstname}." ".$h->{surname}." ";
-                $print .= " ".$h->{company}." ";
-                $print .= "<a href='/cgi-bin/l3/".$tdate."/".$host."/root'>root</a> ";
-                $print .= "<a href='/cgi-bin/l3/".$tdate."/".$host."/".$h->{user}."'>".$h->{user}."</a> ";
-                $print .= "\n";
+                $row{name}= $h->{firstname}." ".$h->{surname};
+                $row{email}= $h->{email};
+                $row{company} = $h->{company};
+                $row{host} =  "$host";
+                $row{root} = "<a href='/cgi-bin/l3/".$tdate."/".$host."/root'>root</a> ";
+                $row{user} = "<a href='/cgi-bin/l3/".$tdate."/".$host."/".$h->{user}."'>".$h->{user}."</a> ";
+                $print .= "<tr>".join("", map("<td style='$_'>$row{$_}</td>", @row_keys))."</tr>";
             }
-            $print .= "</pre><td>";
-            $print .= "</tr>\n";
         }
         $print .= "</table>\n";
         $print .= "</body>\n";
diff -r c70be67ed3d4 -r 385499ec544a l3-frontend
--- a/l3-frontend	Tue Feb 28 13:11:26 2006 +0200
+++ b/l3-frontend	Thu Mar 02 00:06:54 2006 +0200
@@ -3,7 +3,7 @@
 use IO::Socket;
 use lib '.';
 use l3config;
-use locale;
+use utf8;
 
 our @Command_Lines;
 our @Command_Lines_Index;
@@ -41,12 +41,13 @@
 
 sub minutes_passed;
 
-sub print_all;
-sub print_command_lines;
-sub print_files;
-sub print_stat;
-sub print_header;
-sub print_footer;
+sub print_all_txt;
+sub print_all_html;
+sub print_command_lines_html;
+sub print_files_html;
+sub print_stat_html;
+sub print_header_html;
+sub print_footer_html;
 
 main();
 
@@ -64,7 +65,7 @@
     load_sessions_from_xml($Config{"backend_datafile"});
     sort_command_lines;
     process_command_lines;
-    print_all($Config{"output"});
+    print_all_txt($Config{"output"});
     close_mywi_socket;
 }
 
@@ -420,9 +421,9 @@
                 $$cl=0;
             }
         }
-        if ($$cl->{"class"} == "note") {
+        if ($$cl->{"class"} eq "note") {
                 my $note_html = $$cl->{note};
-                $note_html = join ("\n", map ("<p>$_</p>", split (/-\n/, $note)));
+                $note_html = join ("\n", map ("<p>$_</p>", split (/-\n/, $note_html)));
                 $note_html =~ s@(http:[a-zA-Z.0-9/?\_%-]*)@<a href='$1'>$1</a>@g;
                 $note_html =~ s@(www\.[a-zA-Z.0-9/?\_%-]*)@<a href='$1'>$1</a>@g;
                 $$cl->{"note_html"} = $note_html;
@@ -439,7 +440,7 @@
 Разобранный lab-скрипт должен находиться в массиве @Command_Lines
 =cut
 
-sub print_command_lines
+sub print_command_lines_html
 {
 
     my @toc;                # Оглавление
@@ -706,7 +707,7 @@
 }
 
 #############
-# print_all
+# make_new_entries_table
 #
 # Напечатать таблицу неизвестных команд
 #
@@ -757,7 +758,7 @@
 }
 
 #############
-# print_all
+# minutes_passed
 #
 #
 #
@@ -809,23 +810,238 @@
 }
 
 #############
-# print_all
+# print_all_txt
 #
-#
+# Вывести журнал в текстовом формате
 #
 # In:       $_[0]       output_filename
 # Out:
 
+sub print_all_txt
+{
 
-sub print_all
+    my $output_filename=$_[0];
+    my $note_number=0;
+
+    my $result = q();
+    my $this_day_resut = q();
+    
+    my $cl;
+    my $last_tty="";
+    my $last_session="";
+    my $last_day=q();
+    my $last_wday=q();
+    my $in_range=0;
+
+    my $current_command=0;
+
+    my $cursor_position = 0;
+
+
+    if ($Config{filter}) {
+        # Инициализация фильтра
+        for (split /&/,$Config{filter}) {
+            my ($var, $val) = split /=/;
+            $filter{$var} = $val || "";
+        }
+    }
+
+
+COMMAND_LINE:
+    for my $k (@Command_Lines_Index) {
+
+        my $cl=$Command_Lines[$Command_Lines_Index[$current_command++]];
+        next unless $cl;
+
+
+# Пропускаем строки, которые противоречат фильтру
+# Если у нас недостаточно информации о том, подходит строка под  фильтр или нет, 
+# мы её выводим
+
+        for my $filter_key (keys %filter) {
+            next COMMAND_LINE 
+                if defined($cl->{local_session_id})
+                && defined($Sessions{$cl->{local_session_id}}->{$filter_key})
+                && $Sessions{$cl->{local_session_id}}->{$filter_key} ne $filter{$filter_key};
+        }
+
+# Пропускаем строки, выходящие за границу "signature",
+# при условии, что границы указаны
+# Пропускаем неправильные/прерванные/другие команды
+        if ($Config{"from"} && $cl->{"cline"} =~ /$Config{"signature"}\s*$Config{"from"}/) {
+            $in_range=1;
+            next;
+        }
+        if ($Config{"to"} && $cl->{"cline"} =~ /$Config{"signature"}\s*$Config{"to"}/) {
+            $in_range=0;
+            next;
+        }
+        next    if ($Config{"from"} && $Config{"to"}   && !$in_range) 
+                || ($Config{"skip_empty"} =~ /^y/i     && $cl->{"cline"} =~ /^\s*$/ )
+                || ($Config{"skip_wrong"} =~ /^y/i     && $cl->{"err"} != 0)
+                || ($Config{"skip_interrupted"} =~ /^y/i && $cl->{"err"} == 130);
+
+
+#
+##
+## Начинается собственно вывод
+##
+#
+
+### Сначала обрабатываем границы разделов
+### Если тип команды "note", это граница
+
+        if ($cl->{class} eq "note") {
+            $this_day_result .= " === ".$cl->{note_title}." === \n" if $cl->{note_title};
+            $this_day_result .= $cl->{note}."\n";
+            next;
+        }
+
+        my ($sec,$min,$hour,$day,$mon,$year,$wday,$yday,$isdst) = localtime($cl->{time});
+
+        # Добавляем спереди 0 для удобочитаемости
+        $min  = "0".$min  if $min  =~ /^.$/;
+        $hour = "0".$hour if $hour =~ /^.$/;
+        $sec  = "0".$sec  if $sec  =~ /^.$/;
+
+        $class=$cl->{"class"};
+
+# DAY CHANGE
+        if ( $last_day ne $day) {
+            if ($last_day) {
+                $result .= "== ".$Day_Name[$last_wday]." == \n";
+                $result .= $this_day_result;
+            }
+            $last_day   = $day;
+            $last_wday  = $wday;
+            $this_day_result = q();
+        }
+
+# CONSOLE CHANGE
+        if ($cl->{"tty"} && $last_tty ne $cl->{"tty"} && 0) {
+            my $tty = $cl->{"tty"};
+            $this_day_result .= "         #l3: ------- другая консоль ----\n";
+            $last_tty=$cl->{"tty"};
+        }
+
+# Session change
+        if ( $last_session ne $cl->{"local_session_id"}) {
+            $this_day_result .= "# ------------------------------------------------------------"
+                             .  "  l3: local_session_id=".$cl->{"local_session_id"}
+                             .  " ---------------------------------- \n";
+            $last_session=$cl->{"local_session_id"};
+        }
+
+# TIME
+        my @nl_counter = split (/\n/, $result);
+        $cursor_position=length($result) - @nl_counter;
+
+        if ($Config{"show_time"} =~ /^y/i) {
+            $this_day_result .= "$hour:$min:$sec" 
+        }
+
+# COMMAND
+        $this_day_result .= " ".$cl->{"prompt"}.$cl->{"cline"}."\n";
+        if ($cl->{"err"}) {
+            $this_day_result .= "         #l3: err=".$cl->{'err'}."\n";
+        }
+
+# OUTPUT
+        my $last_command = $cl->{"last_command"};
+        if (!( 
+        $Config{"suppress_editors"} =~ /^y/i && grep ($_ eq $last_command, @{$Config{"editors"}}) ||
+        $Config{"suppress_pagers"}  =~ /^y/i && grep ($_ eq $last_command, @{$Config{"pagers"}}) ||
+        $Config{"suppress_terminal"}=~ /^y/i && grep ($_ eq $last_command, @{$Config{"terminal"}})
+            )) {
+            my $output = $cl->{short_output};
+            if ($output) {
+                 $output =~ s/^/         |/mg;
+            }
+            $this_day_result .= $output;
+        }
+
+# DIFF
+        if ( $Config{"show_diffs"} =~ /^y/i && $cl->{"diff"}) {
+            my $diff = $cl->{"diff"};
+            $diff =~ s/^/         |/mg;
+            $this_day_result .= $diff;
+        };
+# SHOT
+        if ($Config{"show_screenshots"} =~ /^y/i && $cl->{"screenshot"}) {
+            $this_day_result .= "         #l3: screenshot=".$cl->{'screenshot'}."\n";
+        }
+
+#NOTES
+        if ( $Config{"show_notes"} =~ /^y/i && $cl->{"note"}) {
+            my $note=$cl->{"note"};
+            $note =~ s/\n/\n#^/msg;
+            $this_day_result .= "#^ == ".$cl->{note_title}." ==\n" if $cl->{note_title};
+            $this_day_result .= "#^ ".$note."\n";
+        }
+
+    }
+    last: {
+        $result .= "== ".$Day_Name[$last_wday]." == \n";
+        $result .= $this_day_result;
+   }
+
+
+
+
+  my $SetCursorPosition_JS = <<JS;
+function setCursorPosition(oInput,oStart,oEnd) {
+    oInput.focus();
+    if( oInput.setSelectionRange ) {
+        oInput.setSelectionRange(oStart,oEnd);
+    } else if( oInput.createTextRange ) {
+        var range = oInput.createTextRange();
+        range.collapse(true);
+        range.moveEnd('character',oEnd);
+        range.moveStart('character',oStart);
+        range.select();
+    }
+}
+JS
+
+    if ($output_filename eq "-") {
+        print 
+               "<html>"
+              ."<script>"
+              .$SetCursorPosition_JS
+              ."</script>"
+              ."<body onLoad='setCursorPosition(document.all.mytextarea, $cursor_position, $cursor_position+10)'>"
+              ."<textarea rows='30' cols='100' wrap='off' id='mytextarea'>$result</textarea>"
+              ."</body>"
+              ."</html>"
+              ;
+    }
+    else {
+        open(OUT, ">", $output_filename)
+            or die "Can't open $output_filename for writing\n";
+        print OUT "<pre>$result</pre>";
+        close(OUT);
+    }
+}
+
+
+#############
+# print_all_html
+#
+#
+#
+# In:       $_[0]       output_filename
+# Out:
+
+
+sub print_all_html
 {
     my $output_filename=$_[0];
 
     my $result;
-    my ($command_lines,$toc)  = print_command_lines;
-    my $files_section         = print_files;
+    my ($command_lines,$toc)  = print_command_lines_html;
+    my $files_section         = print_files_html;
 
-    $result = print_header($toc);
+    $result = print_header_html($toc);
 
 
 #    $result.= join " <br/>", keys %Sessions;
@@ -839,7 +1055,7 @@
     $result.= "<h2 id='stat'>Статистика</h2>"  . print_stat;
     $result.= "<h2 id='help'>Справка</h2>"     . $Html_Help . "<br/>"; 
     $result.= "<h2 id='about'>О программе</h2>". $Html_About. "<br/>"; 
-    $result.= print_footer;
+    $result.= print_footer_html;
 
     if ($output_filename eq "-") {
         print $result;
@@ -853,14 +1069,14 @@
 }
 
 #############
-# print_header
+# print_header_html
 #
 #
 #
 # In:   $_[0]       Содержание
 # Out:              Распечатанный заголовок
 
-sub print_header
+sub print_header_html
 {
     my $toc = $_[0];
     my $course_name = $Config{"course-name"};
@@ -978,14 +1194,14 @@
 
 
 #############
-# print_footer
+# print_footer_html
 #
 #
 #
 #
 #
 
-sub print_footer
+sub print_footer_html
 {
     return "</body>\n</html>\n";
 }
@@ -994,14 +1210,14 @@
 
 
 #############
-# print_stat
+# print_stat_html
 #
 #
 #
 # In:
 # Out:
 
-sub print_stat
+sub print_stat_html
 {
     %StatNames = (
         FirstCommand        => "Время первой команды журнала",
@@ -1106,7 +1322,7 @@
 }
 
 
-sub print_files
+sub print_files_html
 {
     my $result = qq(); 
     my @toc;