/
WriteWeb.pl
236 lines (207 loc) · 7.33 KB
/
WriteWeb.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
#!/usr/bin/perl -w
use strict;
use warnings;
use XML::Twig;
use Data::Dumper;
use utf8;
our ($prev_rec, $cur_rec, $next_rec);
main();
sub main {
my @chapters = map { { basename => "chap$_" } } 1..12;
BuildIndex(\@chapters);
for (my $i=0; $i < @chapters; $i++) {
my $prev = $i < 1 ? undef : $chapters[$i-1];
my $next = $i >= $#chapters ? undef : $chapters[$i+1];
local $prev_rec = $prev;
local $next_rec = $next;
local $cur_rec = $chapters[$i];
Chapter($cur_rec);
}
}
# each chapter record should have the following fields:
# filename, is_annex, title, sections=>[]
sub BuildIndex {
my $chapters = shift;
my $chapter_num = 1;
my $i_writter = IndexWritter1->new();
foreach my $rec (@$chapters) {
$rec->{title} = undef;
$rec->{sections} = [];
$rec->{infile} = $rec->{basename} . ".xml";
$rec->{outfile} = $rec->{basename} . ".html";
$rec->{number} = $chapter_num;
$rec->{"section_num"} = 1;
my $twig=XML::Twig->new(twig_roots =>
{
title => sub {
$rec->{title} = $_->text();
},
section => sub {
push @{$rec->{sections}}, $_->text();
},
},
);
$twig->parsefile("source_xml/" . $rec->{infile}); # build it
$rec->{extTitle} = "פרק" . " $chapter_num: " . $rec->{title};
$i_writter->Chapter($rec->{extTitle}, $rec->{outfile});
foreach my $sec (@{$rec->{sections}}) {
$i_writter->Section($sec);
}
$chapter_num++;
}
}
sub Chapter {
my ($current) = @_;
my $twig=XML::Twig->new(twig_handlers =>
{ text => \&Handle_Text,
"/html/body/code" => \&Handle_Code,
"text/code" => \&Handle_code_inside_text,
html => \&Handle_Main,
"english-line" => \&English_line,
title => \&Handle_Title,
section => \&Handle_Section,
description => \&Handle_Description,
},
);
$twig->parsefile( "source_xml/" . $current->{infile} ); # build it
$twig->print_to_file( "web_target/" . $current->{outfile} ); # output the twig
}
sub get_create_head {
my ($twig) = @_;
my $root = $twig->root();
my $head = $root->first_child( 'head');
if (not $head) {
# head still not exists, create it
$head = $root->insert_new_elt("first_child", "head");
}
return $head;
}
sub Handle_Description {
my ($twig, $elm) = @_;
my $head = get_create_head($twig);
die "Description is too long in $cur_rec->{infile}" if length($elm->text()) > 164;
$head->insert_new_elt("last_child", "meta", { description => $elm->text() });
$elm->delete();
}
sub Handle_Title {
my ($twig, $elm) = @_;
my $title_text = $cur_rec->{extTitle};
$elm->set_text($title_text);
my $head = get_create_head($twig);
$head->insert_new_elt("first_child", "title", {}, $title_text);
$elm->set_tag("h1");
BuildNavigation($elm, "after");
}
sub BuildNavigation {
my ($elm, $position) = @_;
my $last_in_hebrew = "הקודם :";
my $next_in_hebrew = "הבא :";
my $up_in_hebrew = "חזרה לתוכן העניינים";
my $table = $elm->insert_new_elt($position, "table");
my $row = $table->insert_new_elt("last_child", "tr");
my $ref = sub {
my ($text, $link) = @_;
my $cell = $row->insert_new_elt("last_child", "td");
$cell->insert_new_elt("last_child", "a", { href => $link }, $text);
};
if ($prev_rec) {
$ref->("[$last_in_hebrew ".$prev_rec->{title}."]", $prev_rec->{outfile});
}
$ref->("[$up_in_hebrew]", "index.html");
if ($next_rec) {
$ref->("[$next_in_hebrew ".$next_rec->{title}."]", $next_rec->{outfile});
}
}
sub Handle_Section {
my ($twig, $elm) = @_;
my $sec_num = join ".", $cur_rec->{number}, $cur_rec->{"section_num"};
$elm->set_tag("h3");
$elm->set_text( $sec_num . " " . $elm->text() ); # . '<a name="sec'.$cur_rec->{"section_num"}.'"></a>' );
$elm->insert_new_elt("last_child", "a", { name => "sec".$cur_rec->{"section_num"} });
$cur_rec->{"section_num"}++;
}
sub Handle_Text {
my ($twig, $elm) = @_;
$elm->set_tag("p");
$elm->set_att(dir => "rtl");
$elm->set_att(lang=>"he");
my $div = $elm->wrap_in("div");
$div->set_att(style => "padding-right: 30px");
}
sub Handle_Code {
my ($twig, $elm) = @_;
$elm->set_tag("pre");
my $c = $elm->text();
$c =~ s/^[\s\n]*//;
$c =~ s/[\s\n]*$//;
$elm->set_text($c);
my $div = $elm->wrap_in("div");
$div->set_att(dir => "ltr");
}
sub Handle_code_inside_text {
my ($twig, $elm) = @_;
$elm->set_tag("span");
$elm->set_att(dir => "ltr");
$elm->set_att(lang=>"en");
}
sub Handle_Main {
my ($twig, $elm) = @_;
$elm->set_att(dir => "rtl");
BuildNavigation($elm, "last_child");
#$elm->insert_new_elt("last_child", "br");
my $copy_text1 = 'נכתב ע"י שמואל פומברג, כל הזכויות שמורות ©';
my $copy_text2 = ' ראה פרק 1.5 לתנאי רשיון';
my $span = $elm->insert_new_elt("last_child", "p", {}, $copy_text1.$copy_text2);
}
sub English_line {
my ($twig, $elm) = @_;
$elm->set_tag("div");
$elm->set_att(dir=>"ltr");
$elm->set_att(align=>"left");
}
package IndexWritter1;
# each chapter record contains:
# { sections => [], file => '', name => '' }
# each section record contains:
# { name => 'name' }
sub new {
my ($class) = @_;
my $self = { Chapters => [], current => undef };
return bless $self, $class;
}
sub Chapter {
my ($self, $chapter_name, $chapter_file) = @_;
my $new_chap = { sections => [], file => $chapter_file, name => $chapter_name };
push @{$self->{Chapters}}, $new_chap;
$self->{current} = $new_chap;
}
sub Section {
my ($self, $section_name) = @_;
my $sec_rec = { name => $section_name };
push @{$self->{current}->{sections}}, $sec_rec;
}
sub DESTROY {
my $self = shift;
my $chapter_num = 0;
my $section_num = 0;
my $ref = sub {
my ($twig, $elm) = @_;
$elm->set_tag("span");
foreach my $chap_rec (@{$self->{Chapters}}) {
$chapter_num++;
$section_num = 0;
my $h3 = $elm->insert_new_elt("last_child", "h3");
$h3->insert_new_elt("last_child", "a", { href => $chap_rec->{file} }, $chap_rec->{name});
foreach my $sec_rec (@{$chap_rec->{sections}}) {
$section_num++;
my $name = $section_num . " " . $sec_rec->{name};
my $url = join '', $chap_rec->{file}, "#sec", $section_num;
$elm->insert_new_elt("last_child", "a", { href => $url }, $name);
$elm->insert_new_elt("last_child", "br");
}
}
};
my $twig = XML::Twig->new(twig_handlers => { insert => $ref } );
$twig->parsefile( 'web_helpers/index_tamplate.xml' );
$twig->print_to_file( "web_target/index.html" );
}