| 1 | # -*- perl -*- | ||
| 2 | |||
| 3 | # | ||
| 4 | # $Id: MakeOnePageHTML.pm,v 1.3 2004/12/22 11:23:07 eserte Exp $ | ||
| 5 | # Author: Slaven Rezic | ||
| 6 | # | ||
| 7 | # Copyright (C) 2004 Slaven Rezic. All rights reserved. | ||
| 8 | # This package is free software; you can redistribute it and/or | ||
| 9 | # modify it under the same terms as Perl itself. | ||
| 10 | # | ||
| 11 | # Mail: slaven@rezic.de | ||
| 12 | # WWW: http://www.rezic.de/eserte/ | ||
| 13 | # | ||
| 14 | |||
| 15 | package WebEditor::OldFeatures::MakeOnePageHTML; | ||
| 16 | |||
| 17 | =head1 NAME | ||
| 18 | |||
| 19 | WebEditor::OldFeatures::MakeOnePageHTML - create a postscript file from the site | ||
| 20 | |||
| 21 | =head1 SYNOPSIS | ||
| 22 | |||
| 23 | use WebEditor::OldFeatures::MakeOnePageHTML; | ||
| 24 | WebEditor::OldFeatures::MakeOnePageHTML::makeonepagehtml($webeditor_oldcontroller_object, %args); | ||
| 25 | WebEditor::OldFeatures::MakeOnePageHTML::makeonepagehtml_send($webeditor_oldcontroller_object, %args); | ||
| 26 | |||
| 27 | =head1 DESCRIPTION | ||
| 28 | |||
| 29 | This module provides the B<makeonepagehtml> function which creates a | ||
| 30 | single page HTML a web.editor site. | ||
| 31 | |||
| 32 | =head2 HTML TEMPLATES | ||
| 33 | |||
| 34 | The HTML templates should be the same as the normal pagetype templates | ||
| 35 | with the suffix C<_oph.tpl.html> instead of C<.tpl.html>. Usually these | ||
| 36 | templates contain only partial HTML without C<< <html> >>, C<< <head> | ||
| 37 | >> and C<< <body> >> tags and should have no navigational elements. | ||
| 38 | |||
| 39 | The templates receive the normal parameters as in B<makehtmlpage> and | ||
| 40 | additionally the parameters C<level> (useful in constructing the C<< | ||
| 41 | <h...> >> tag) and C<omit_title> (true if it is advisable to omit the | ||
| 42 | C<< <h...> >> tag). | ||
| 43 | |||
| 44 | Additionally, the following templates should be defined: | ||
| 45 | |||
| 46 | =over | ||
| 47 | |||
| 48 | =item one_page_html_header.tpl.html | ||
| 49 | |||
| 50 | The overall header of the HTML document. Here goes the C<< <head> >> | ||
| 51 | definitions and the opening C<< <html> >> and C<< <body> >> tags. | ||
| 52 | |||
| 53 | The template receives the normal TemplateVars and additionally the | ||
| 54 | current C<lang> parameter. | ||
| 55 | |||
| 56 | =item one_page_html_footer.tpl.html | ||
| 57 | |||
| 58 | The overall footer of the HTML document. Here goes the closing C<< | ||
| 59 | <html> >> and C<< <body> >> tags along with footer text for the | ||
| 60 | document. | ||
| 61 | |||
| 62 | The template receives the normal TemplateVars and additionally the | ||
| 63 | current C<lang> parameter. | ||
| 64 | |||
| 65 | =item one_page_html_headline.tpl.html | ||
| 66 | |||
| 67 | A template for a folder object with no own content. Typically this | ||
| 68 | will contain only one C<< <h...>title</h...> >> element. | ||
| 69 | |||
| 70 | This template receives the normal TemplateVars and additionally the | ||
| 71 | C<lang>, C<object> (the current L<WE::Obj> object), C<title> and | ||
| 72 | C<level>. | ||
| 73 | |||
| 74 | =back | ||
| 75 | |||
| 76 | =head2 FUNCTIONS | ||
| 77 | |||
| 78 | The B<makeonepagehtml> function arguments: | ||
| 79 | |||
| 80 | =over | ||
| 81 | |||
| 82 | =item -lang => $lang | ||
| 83 | |||
| 84 | The language to process. By default the first site language is used. | ||
| 85 | |||
| 86 | =item -debug => $bool | ||
| 87 | |||
| 88 | Turn on debug mode if true. This will leave the temporary files after | ||
| 89 | processing and show the command line arguments. | ||
| 90 | |||
| 91 | =item -o => $output_file | ||
| 92 | |||
| 93 | Specify an output file. If not set, then the output will be returned | ||
| 94 | by the B<makeonepagehtml> function. | ||
| 95 | |||
| 96 | =back | ||
| 97 | |||
| 98 | The B<makeonepagehtml_send> function arguments are same as for | ||
| 99 | B<makeonepagehtml>. This function automatically creates an HTTP header | ||
| 100 | and prints the output to STDOUT. | ||
| 101 | |||
| 102 | =head1 AUTHOR | ||
| 103 | |||
| 104 | Slaven Rezic. | ||
| 105 | |||
| 106 | =cut | ||
| 107 | |||
| 108 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 109 | 1 | ▶ | use vars qw($VERSION); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 110 | $VERSION = sprintf("%d.%02d", q$Revision: 1.3 $ =~ /(\d+)\.(\d+)/); | ||
| 111 | |||
| 112 | 0 | ▶ | use CGI (); |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 113 | use HTML::Entities (); | ||
| 114 | use Template; | ||
| 115 | |||
| 116 | use WE_Frontend::Plugin::Linear; | ||
| 117 | use WE::Util::LangString qw(langstring); | ||
| 118 | |||
| 119 | sub makeonepagehtml { | ||
| 120 | my($self, %args) = @_; | ||
| 121 | |||
| 122 | my $objdb = $self->Root->ObjDB; | ||
| 123 | my $root_object = $objdb->root_object; | ||
| 124 | my $c = $self->C; | ||
| 125 | |||
| 126 | my @objids; | ||
| 127 | my $curr_id = $root_object->Id; | ||
| 128 | |||
| 129 | while (1) { | ||
| 130 | my $doc_id = $curr_id; | ||
| 131 | # XXX code doubled in WE_Frontend::Plugin::WE_Navigation::Object | ||
| 132 | # XXX IndexDoc handling missing! | ||
| 133 | my $curr_obj = $objdb->get_object($curr_id); | ||
| 134 | if ($curr_obj->is_folder) { | ||
| 135 | if (defined $curr_obj->IndexDoc) { | ||
| 136 | $doc_id = $curr_obj->IndexDoc; | ||
| 137 | } else { | ||
| 138 | my $autoindexdoc = $c->project->features->{autoindexdoc}; | ||
| 139 | if (defined $autoindexdoc && $autoindexdoc eq 'first') { | ||
| 140 | my(@children_ids) = $objdb->get_released_children($curr_id); | ||
| 141 | if (@children_ids) { | ||
| 142 | $doc_id = $children_ids[0]->Id; | ||
| 143 | } else { | ||
| 144 | undef $doc_id; | ||
| 145 | } | ||
| 146 | } | ||
| 147 | } | ||
| 148 | } | ||
| 149 | push @objids, {Id => $curr_id, DocId => $doc_id}; | ||
| 150 | |||
| 151 | if (defined $doc_id) { | ||
| 152 | # XXX document WE_Frontend::Plugin::Linear how to use in | ||
| 153 | # offline (non-TT) mode | ||
| 154 | my $linear = WE_Frontend::Plugin::Linear->new | ||
| 155 | (undef, | ||
| 156 | { rootdb => $self->Root, | ||
| 157 | objdb => $objdb, | ||
| 158 | objid => $doc_id, | ||
| 159 | }, | ||
| 160 | ); | ||
| 161 | my $next = $linear->next; | ||
| 162 | if (defined $next) { | ||
| 163 | $curr_id = $next->o->Id; | ||
| 164 | } else { | ||
| 165 | last; | ||
| 166 | } | ||
| 167 | } else { | ||
| 168 | die "This is a non-linear site. The folder object <" . langstring($curr_obj->Title) . "> (Id $curr_id) has no Index document."; | ||
| 169 | } | ||
| 170 | } | ||
| 171 | |||
| 172 | my $lang = $args{-lang} || $c->project->sitelanguages->[0] || "en"; | ||
| 173 | my $debug = $args{-debug}; | ||
| 174 | |||
| 175 | my $html = ""; | ||
| 176 | my $t = Template->new($self->TemplateConf); | ||
| 177 | |||
| 178 | # XXX OK to hardcode template name? | ||
| 179 | $t->process | ||
| 180 | ($c->paths->site_templatebase . "/one_page_html_header.tpl.html", | ||
| 181 | { %{ $self->TemplateVars }, | ||
| 182 | lang => $lang, | ||
| 183 | }, \$html) or die $t->error; | ||
| 184 | |||
| 185 | for my $def (@objids) { | ||
| 186 | my $id = $def->{Id}; | ||
| 187 | my $doc_id = $def->{DocId}; | ||
| 188 | |||
| 189 | # XXX code doubled from makehtmlpage... | ||
| 190 | my $content = $objdb->content($doc_id); | ||
| 191 | my $outdata = $self->_get_outdata($content); | ||
| 192 | my $obj = $objdb->get_object($doc_id); | ||
| 193 | my $mainid = $obj->Version_Parent || $doc_id; | ||
| 194 | my $template = $c->project->templatefortype->{ $outdata->{'data'}->{'pagetype'} }; | ||
| 195 | if (!defined $template) { | ||
| 196 | die "No template for pagetype $outdata->{'data'}->{'pagetype'} defined"; | ||
| 197 | } | ||
| 198 | |||
| 199 | my $folder_title; | ||
| 200 | my $folder_done; | ||
| 201 | if ($id != $doc_id) { | ||
| 202 | my $folder_obj = $objdb->get_object($id); | ||
| 203 | $folder_title = langstring($folder_obj->Title, $lang); | ||
| 204 | my $template_file = $c->paths->site_templatebase . "/one_page_html_headline.tpl.html"; | ||
| 205 | $html .= "<!-- Folder id $id, title " . HTML::Entities::encode($folder_title) . ", template file $template_file -->\n"; | ||
| 206 | $t->process | ||
| 207 | ($template_file, | ||
| 208 | { %{ $self->TemplateVars }, | ||
| 209 | lang => $lang, | ||
| 210 | objid => $id, | ||
| 211 | object => $folder_obj, | ||
| 212 | title => $folder_title, | ||
| 213 | level => ($objdb->depth($folder_obj))[0], | ||
| 214 | pagetype => "_folder", | ||
| 215 | }, \$html) or die $t->error; | ||
| 216 | $folder_done = 1; | ||
| 217 | } | ||
| 218 | |||
| 219 | my $page_title = langstring($obj->Title, $lang); | ||
| 220 | $html .= "<!-- Page id $id, title " . HTML::Entities::encode($page_title) . " -->\n"; | ||
| 221 | |||
| 222 | $outdata->{'data'}->{'language'} = $lang; | ||
| 223 | my $keywords = langstring($obj->{Keywords}, $lang) || undef; | ||
| 224 | #warn "Using template ".$c->paths->site_templatebase."/".$template."\n"; | ||
| 225 | $template =~ s/\.tpl\.html$/_oph\.tpl\.html/; | ||
| 226 | |||
| 227 | $t->process($c->paths->site_templatebase."/".$template, | ||
| 228 | { %{ $self->TemplateVars }, | ||
| 229 | objid => $mainid, | ||
| 230 | lang => $lang, | ||
| 231 | keywords => $keywords, | ||
| 232 | level => ($objdb->depth($mainid))[0], | ||
| 233 | omit_title => (defined $page_title && $page_title eq $folder_title), | ||
| 234 | omit_pagebreak => $folder_done, | ||
| 235 | %$outdata }, | ||
| 236 | \$html) | ||
| 237 | or die "Template process failed: " . $t->error . "\n"; | ||
| 238 | } | ||
| 239 | |||
| 240 | # XXX OK to hardcode template name? | ||
| 241 | $html .= "<!-- footer -->\n"; | ||
| 242 | $t->process | ||
| 243 | ($c->paths->site_templatebase . "/one_page_html_footer.tpl.html", | ||
| 244 | { %{ $self->TemplateVars }, | ||
| 245 | lang => $lang, | ||
| 246 | }, | ||
| 247 | \$html) | ||
| 248 | or die $t->error; | ||
| 249 | |||
| 250 | if (defined $args{-o}) { | ||
| 251 | open(HTML, ">$args{-o}") or die "Can't write to $args{-o}: $!"; | ||
| 252 | print HTML $html; | ||
| 253 | close HTML; | ||
| 254 | } else { | ||
| 255 | return $html; | ||
| 256 | } | ||
| 257 | } | ||
| 258 | |||
| 259 | sub makeonepagehtml_send { | ||
| 260 | my($self, %args) = @_; | ||
| 261 | |||
| 262 | my $html = makeonepagehtml($self, %args); | ||
| 263 | |||
| 264 | my $q = CGI->new; | ||
| 265 | print $q->header("text/html"); | ||
| 266 | print $html; | ||
| 267 | return 1; | ||
| 268 | } | ||
| 269 | |||
| 270 | 1; | ||