Commit | Line | Data |
9e609156 |
1 | #!/usr/local/bin/perl |
2 | |
3 | use strict ; |
4 | use warnings ; |
5 | |
6 | use CMS::Simple ; |
7 | use CMS::Simple::Filter::Markup ; |
8 | |
9 | use HTML::Entities; |
10 | use File::Slurp ; |
11 | use Data::Dumper ; |
12 | |
13 | |
14 | my $cont_file = shift @ARGV || 'compshare.cont' ; |
15 | |
16 | |
17 | my @filters = ( |
18 | # { |
19 | # name => 'html_escape', |
20 | # tags => [ qw( code ) ], |
21 | # code => \&html_escape, |
22 | # }, |
23 | { |
24 | name => 'markup', |
25 | tags => [ qw( text email image ) ], |
26 | code => \&CMS::Simple::Filter::Markup::filter_markup, |
27 | }, |
28 | ) ; |
29 | |
30 | |
31 | my $conf = { |
32 | |
33 | output_suffix => '.html', |
34 | |
35 | contents => { |
36 | |
37 | # page_header => 'header.cont', |
38 | # page_footer => 'footer.cont', |
39 | # book => 'compshare.cont', |
40 | }, |
41 | |
42 | filters => \@filters, |
43 | |
44 | default_template => 'base_page', |
45 | |
46 | # this maps loaded contents to location in this page's data tree. |
47 | # the location will be multilevel later on |
48 | |
49 | # the page key name is the base name for the output file |
50 | |
51 | pages => { |
52 | |
53 | chapters => { |
54 | template => 'chapters', |
55 | }, |
56 | |
57 | index => { |
58 | |
59 | template => 'index', |
60 | }, |
61 | }, |
62 | } ; |
63 | |
64 | |
65 | load_book_content( $conf, $cont_file ) ; |
66 | |
67 | #print Dumper $conf ; |
68 | #print Dumper $conf->{pages} ; |
69 | |
70 | my $cms = CMS::Simple->new( $conf ) ; |
71 | |
72 | $cms->build_all_pages() ; |
73 | |
74 | exit ; |
75 | |
76 | |
77 | sub load_book_content { |
78 | |
79 | my( $conf, $cont_file ) = @_ ; |
80 | |
81 | my $cont_text = read_file( "contents/$cont_file" ) ; |
82 | my $content = CMS::Simple::Parse::parse_content( $cont_text ) ; |
83 | |
84 | process_book_content( $conf, $content ) ; |
85 | |
86 | #print Dumper $content ; |
87 | |
88 | } |
89 | |
90 | sub process_book_content { |
91 | |
92 | my( $conf, $book_cont ) = @_ ; |
93 | |
94 | my $book_title = $book_cont->{book}{title} ; |
95 | |
96 | my $chap_conts = $book_cont->{chapter} ; |
97 | $chap_conts = [ $chap_conts ] unless ref $chap_conts eq 'ARRAY' ; |
98 | |
99 | my $chap_num = '01' ; |
100 | |
101 | foreach my $chap_cont ( @{$chap_conts} ) { |
102 | |
103 | $chap_cont->{number} = $chap_num++ ; |
104 | $chap_cont->{book_title} = $book_title ; |
105 | |
106 | process_chap_content( $conf, $chap_cont ) ; |
107 | } |
108 | |
109 | process_index_page( $conf, $chap_conts ) ; |
110 | |
111 | #print Dumper $conf ; |
112 | |
113 | } |
114 | |
115 | sub process_index_page { |
116 | |
117 | my( $conf, $chap_conts ) = @_ ; |
118 | |
119 | my $index_cont = $conf->{pages}{index}{contents} ||= {} ; |
120 | |
121 | foreach my $chap_cont ( @{$chap_conts} ) { |
122 | |
123 | my $page_titles = $chap_cont->{page_title} ; |
124 | |
125 | push( @{$index_cont->{chapter}}, { |
126 | |
127 | chap_title => $chap_cont->{title}, |
128 | chap_num => $chap_cont->{number}, |
129 | page => $page_titles, |
130 | } ) ; |
131 | } |
132 | |
133 | print Dumper $index_cont ; |
134 | |
135 | } |
136 | |
137 | |
138 | |
139 | sub process_chap_content { |
140 | |
141 | my( $conf, $chap_data ) = @_ ; |
142 | |
143 | my $chap_title = $chap_data->{title} ; |
144 | |
145 | my $chap_page_cont = $conf->{pages}{chapters}{contents} ||= {} ; |
146 | |
147 | push( @{$chap_page_cont->{chap_title}}, { |
148 | number => $chap_data->{number}, |
149 | title => $chap_title |
150 | } ) ; |
151 | |
152 | my $page_conts = $chap_data->{page} ; |
153 | $page_conts = [ $page_conts ] unless ref $page_conts eq 'ARRAY' ; |
154 | |
155 | my $page_num = '01' ; |
156 | |
157 | foreach my $page_cont ( @{$page_conts} ) { |
158 | |
159 | $page_cont->{number} = $page_num++ ; |
160 | |
161 | #print Dumper $page_cont ; |
162 | |
163 | process_page_content( $conf, $chap_data, $page_cont ) ; |
164 | } |
165 | } |
166 | |
167 | |
168 | sub process_page_content { |
169 | |
170 | my( $conf, $chap_data, $page_data ) = @_ ; |
171 | |
172 | my $chap_title = $chap_data->{title} ; |
173 | my $page_title = $page_data->{title} ; |
174 | |
175 | |
176 | #print "TITLE $page_title\n" ; |
177 | |
178 | my $chap_num = $chap_data->{number} ; |
179 | my $page_num = $page_data->{number} ; |
180 | |
181 | my $page_name = "page-$chap_num$page_num" ; |
182 | my $page_url = "$page_name.html" ; |
183 | |
184 | push( @{$chap_data->{page_title}}, { |
185 | number => $page_num, |
186 | title => $page_title, |
187 | page_link => $page_url, |
188 | } ) ; |
189 | |
190 | my $page = { |
191 | |
192 | name => $page_name, |
193 | template => 'page', |
194 | } ; |
195 | |
196 | my $header_cont = { |
197 | |
198 | header_title => { |
199 | text => $page_title, |
200 | }, |
201 | |
202 | book_title => $chap_data->{book_title} || 'FOO', |
203 | index_link => { |
204 | text => 'index.html', |
205 | }, |
206 | chap_link => 'chapters.html', |
207 | } ; |
208 | |
209 | if ( $page_num > 1 ) { |
210 | |
211 | my $prev_page_num = sprintf "%02d", $page_num - 1 ; |
212 | $header_cont->{prev_link} = { |
213 | prev_page => "page-$chap_num$prev_page_num.html", |
214 | } ; |
215 | } |
216 | |
217 | if ( $page_num < 50 ) { |
218 | |
219 | my $next_page_num = sprintf "%02d", $page_num + 1 ; |
220 | $header_cont->{next_link} = { |
221 | next_page => "page-$chap_num$next_page_num.html", |
222 | } ; |
223 | } |
224 | |
225 | |
226 | #print Dumper $page_data ; |
227 | |
228 | my $items = process_page_items( $page_data ) ; |
229 | |
230 | my $page_cont = { |
231 | page => { |
232 | item => $items, |
233 | }, |
234 | } ; |
235 | |
236 | set_page_header_data( $page_cont, $header_cont ) ; |
237 | |
238 | $page->{contents} = $page_cont ; |
239 | #print Dumper $page_cont ; |
240 | |
241 | $conf->{pages}{$page_name} = $page ; |
242 | |
243 | } |
244 | |
245 | sub process_page_items { |
246 | |
247 | my( $page_data ) = @_ ; |
248 | |
249 | my $items = $page_data->{item} ; |
250 | $items = [ $items ] unless ref $items eq 'ARRAY' ; |
251 | |
252 | foreach my $item ( @{$items} ) { |
253 | |
254 | |
255 | if ( ref $item ) { |
256 | |
257 | $item->{title} = { |
258 | text => $item->{title}, |
259 | } ; |
260 | } |
261 | else { |
262 | |
263 | $item = { |
264 | title => { |
265 | text => $item, |
266 | }, |
267 | } ; |
268 | } |
269 | |
270 | process_subitems( $item ) ; |
271 | process_code( $item ) ; |
272 | } |
273 | |
274 | #print Dumper $items ; |
275 | |
276 | return $items ; |
277 | } |
278 | |
279 | sub process_subitems { |
280 | |
281 | my( $item ) = @_ ; |
282 | |
283 | my $subitems = $item->{subitem} ; |
284 | |
285 | return unless $subitems ; |
286 | |
287 | $subitems = [ $subitems ] unless ref $subitems eq 'ARRAY' ; |
288 | |
289 | $item->{subitems} = [ |
290 | map { |
291 | |
292 | subitem => { |
293 | text => $_ |
294 | } |
295 | }, @{$subitems} |
296 | ] ; |
297 | } |
298 | |
299 | sub process_code { |
300 | |
301 | my( $item ) = @_ ; |
302 | |
303 | my $code = $item->{code} ; |
304 | |
305 | return unless $code ; |
306 | |
307 | # append a newline if needed - scalar content loses a trailing newline |
308 | |
309 | #print "CODE: [$code]\n" ; |
310 | |
311 | $code =~ s/(?<!\n)\z/\n/ ; |
312 | $code =~ s/^/\n\n/ ; |
313 | |
314 | $code = encode_entities( $code, '<>&"') ; |
315 | |
316 | $item->{code} = { |
317 | text => $code |
318 | } ; |
319 | } |
320 | |
321 | |
322 | |
323 | sub set_page_header_data { |
324 | |
325 | my( $page_cont, $data ) = @_ ; |
326 | |
327 | for my $header ( qw( html_header page_header page_footer ) ) { |
328 | |
329 | @{$page_cont->{$header}}{keys %{$data}} = values %{$data} ; |
330 | } |
331 | } |
332 | |