#!/usr/bin/perl
use strict;
use warnings;
&Rule_0_PrintRule_Preamble();
foreach (@ARGV) {
my @fields = split /[\\\/:]/;
my $file_name = $fields[$#fields];
&Rule_1_linkraw_Check_File($file_name, $file_name);
&Rule_3_titleraw_Check_File($file_name, $file_name);
next unless open FILE, $_;
while (<FILE>) {
chomp;
@fields = split;
my ($g,$o)=(0,0);
pos = 0;
s/\t/$g=8-($o+pos)%8; $o+=$g-1; ' 'x$g/ge;
&Rule_1_linkraw_Input_Line($_, @fields);
&Rule_3_titleraw_Input_Line($_, @fields);
}
close FILE;
}
&Rule_1_linkraw_End_Data();
&Rule_3_titleraw_End_Data();
&Rule_0_PrintRule_Appendix();
INIT {
my $starting_level = 1;
my $current_level = 0;
my $begin_a_block = 0;
sub Rule_1_linkraw_Check_File {
my ($line, @fields) = @_;
my $selected_string;
$begin_a_block = 1;
$current_level = $starting_level;
}
sub Rule_1_linkraw_Input_Line {
my ($line, @fields) = @_;
my $selected_string;
if ($current_level>=1) {
if (defined($fields[0]) && length($fields[0])>24) {
$selected_string = substr($fields[0], 24, 43);
} else {
$selected_string = undef;
}
if (defined($selected_string) && $selected_string eq "http:\/\/www1.xianlai.com\/viewthread.php?tid=") {
$selected_string = $fields[0];
if (defined($selected_string)) {
&Rule_2_link($begin_a_block, $current_level, $selected_string);
$begin_a_block = 0;
}
}
}
}
sub Rule_1_linkraw_End_Data {
my $selected_string = undef;
&Rule_2_link(0, 0, $selected_string);
}
}
INIT {
sub Rule_2_link {
my ($begin_a_block, $current_level, $input_string) = @_;
unless (defined($input_string)) {
&Rule_0_PrintRule_Data(1, 0, 0, $input_string);
return;
}
my $left = 0;
my $right = 0;
my $len = length($input_string);
for ( ; $left<$len; $left=$right+1) {
if (($left=index($input_string, "=\"", $left))<0) {
return;
} else {
$left += 2;
}
if (($right=index($input_string, "&", $left))<0) {
return;
}
if ($right>$left) {
my $selected_string = substr($input_string, $left, $right-$left);
&Rule_0_PrintRule_Data(1, $begin_a_block, $current_level, $selected_string);
$begin_a_block=0;
}
}
}
}
INIT {
my $starting_level = 1;
my $current_level = 0;
my $begin_a_block = 0;
sub Rule_3_titleraw_Check_File {
my ($line, @fields) = @_;
my $selected_string;
$begin_a_block = 1;
$current_level = $starting_level;
}
sub Rule_3_titleraw_Input_Line {
my ($line, @fields) = @_;
my $selected_string;
if ($current_level>=1) {
if (defined($fields[0]) && length($fields[0])>41) {
$selected_string = substr($fields[0], 41, 1);
} else {
$selected_string = undef;
}
if (defined($selected_string) && $selected_string eq "]") {
$begin_a_block = 1;
$current_level = 2;
}
}
if ($current_level>=2) {
$selected_string = $fields[0];
if (defined($selected_string)) {
&Rule_4_ConcatRule($begin_a_block, $current_level, $selected_string);
$begin_a_block = 0;
}
}
if ($current_level>1) {
if (index($line, "<\/A><\/TD>")>=0) {
$current_level = 1;
return;
}
}
}
sub Rule_3_titleraw_End_Data {
my $selected_string = undef;
&Rule_4_ConcatRule(0, 0, $selected_string);
}
}
INIT {
my $was_new_block = 0;
my $concat_string = "";
sub Rule_4_ConcatRule {
my ($begin_a_block, $current_level, $input_string) = @_;
unless (defined($input_string)) {
if ($concat_string ne "") {
&Rule_5_title($was_new_block, 2, $concat_string);
$concat_string = "";
}
&Rule_5_title(0, 0, $input_string);
return;
}
if ($current_level<2) {
if ($concat_string ne "") {
&Rule_5_title($was_new_block, 2, $concat_string);
$concat_string = "";
}
&Rule_5_title($begin_a_block, $current_level, $input_string);
} elsif ($current_level>2 || !$begin_a_block) {
if ($concat_string eq "") {
$was_new_block = $begin_a_block;
}
$concat_string .= $input_string;
} else {
if ($concat_string ne "") {
&Rule_5_title($was_new_block, 2, $concat_string);
}
$was_new_block = 1;
$concat_string = $input_string;
}
}
}
INIT {
sub Rule_5_title {
my ($begin_a_block, $current_level, $input_string) = @_;
unless (defined($input_string)) {
&Rule_0_PrintRule_Data(2, 0, 0, $input_string);
return;
}
my $left = 0;
my $right = 0;
my $len = length($input_string);
for ( ; $left<$len; $left=$right+2) {
if (($left=index($input_string, ">", $left))<0) {
return;
} else {
$left += 1;
}
if (($right=index($input_string, "<\/", $left))<0) {
return;
}
if ($right>$left) {
my $selected_string = substr($input_string, $left, $right-$left);
&Rule_0_PrintRule_Data(2, $begin_a_block, $current_level, $selected_string);
$begin_a_block=0;
}
}
}
}
INIT {
my @data_array;
for (my $i=0; $i<3; $i++) {
push @data_array, [ ];
}
my @line_stuff;
$line_stuff[0] = "[url=";
$line_stuff[1] = "]";
$line_stuff[2] = "[\/url]\n";
my @line_limit;
my $available = 1;
my $finished = 1;
sub Rule_0_PrintRule_Data {
my ($part, $block, $level, $input_string) = @_;
unless (defined($input_string)) {
&Rule_0_PrintRule_End();
return;
}
push @{$data_array[$part]}, $input_string;
&Rule_0_PrintRule_Print($part);
}
sub Rule_0_PrintRule_Print {
my ($part) = @_;
return if @{$data_array[$part]}>1;
$available++;
return if $available<3;
print $line_stuff[0];
for (my $i=1; $i<3; $i++) {
my $output_string = shift @{$data_array[$i]};
$available-- if @{$data_array[$i]}<1;
&Rule_0_PrintRule_Output($i, $output_string);
print $line_stuff[$i];
}
}
sub Rule_0_PrintRule_End {
$finished++;
return if $finished<3;
while ($available>1) {
print $line_stuff[0];
for (my $i=1; $i<3; $i++) {
my $output_string = shift @{$data_array[$i]};
unless (defined($output_string)) {
print "<no_data>";
} else {
$available-- if @{$data_array[$i]}<1;
&Rule_0_PrintRule_Output($i, $output_string);
}
print $line_stuff[$i];
}
}
}
sub Rule_0_PrintRule_Preamble {
print "";
}
sub Rule_0_PrintRule_Output {
my ($i, $output_string) = @_;
unless (defined($line_limit[$i])) {
print "$output_string";
} else {
my $j = 0;
while (1) {
print substr($output_string, $j, $line_limit[$i]);
$j += $line_limit[$i];
if ($j<length($output_string)) {
print "\n";
} else {
last;
}
}
}
}
sub Rule_0_PrintRule_Appendix {
print "";
}
}
[ Last edited by 慧慧(huihui) on 2006-7-12 at 11:55 ] |