home *** CD-ROM | disk | FTP | other *** search
/ PC Professionell 2004 December / PCpro_2004_12.ISO / files / webserver / tsw / TSW_3.4.0.exe / Apache2 / perl / Doc.pm < prev    next >
Encoding:
Perl POD Document  |  2002-06-29  |  5.7 KB  |  268 lines

  1. package DocSet::Doc;
  2.  
  3. use strict;
  4. use warnings;
  5.  
  6. use DocSet::Util;
  7. use DocSet::RunTime;
  8.  
  9. use URI;
  10. use File::Spec::Functions;
  11.  
  12. sub new {
  13.     my $class = shift;
  14.     my $self = bless {}, ref($class)||$class;
  15.     $self->init(@_);
  16.     return $self;
  17. }
  18.  
  19. sub init {
  20.     my($self, %args) = @_;
  21.     while (my($k, $v) = each %args) {
  22.         $self->{$k} = $v;
  23.     }
  24. }
  25.  
  26. sub scan {
  27.     my($self) = @_;
  28.  
  29.     note "+++ Scanning $self->{src_uri}";
  30.     $self->src_read();
  31.  
  32.     $self->retrieve_meta_data();
  33. }
  34.  
  35. sub render {
  36.     my($self, $cache) = @_;
  37.  
  38.     # if the object wasn't stored rescan
  39.     #$self->scan() unless $self->meta;
  40.  
  41.     my $src_uri       = $self->{src_uri};
  42.     my $dst_path      = $self->{dst_path};
  43.  
  44.     my $rel_doc_root  = $self->{rel_doc_root};
  45.     my $abs_doc_root  = $self->{abs_doc_root};
  46.     $abs_doc_root .= "/$rel_doc_root"
  47.         if defined $rel_doc_root &&
  48.            length($rel_doc_root) && $rel_doc_root ne '.';
  49.  
  50.     $abs_doc_root =~ s|^./||; # IE/Mac can't handle path ./../foo
  51.  
  52.     $self->{dir} = {
  53.         abs_doc_root   => $abs_doc_root,
  54.         rel_doc_root   => $rel_doc_root,
  55.         path_from_base => $self->{path_from_base},
  56.     };
  57.  
  58.     $self->{nav} = DocSet::NavigateCache->new($cache->path, $src_uri);
  59.  
  60.     note "Rendering $dst_path";
  61.     $self->convert();
  62.     write_file($dst_path, $self->{output});
  63.  
  64.     # anything that should be done after the target was written?
  65.     $self->postprocess() if $self->can('postprocess');
  66. }
  67.  
  68. # read the source and remember the mod time
  69. # sets $self->{content}
  70. #      $self->{timestamp}
  71. sub src_read {
  72.     my($self) = @_;
  73.  
  74.     # META: at this moment everything is a file path
  75.     my $src_uri = "file://" . $self->{src_path};
  76.     # Win32: fix the path, or it'll be parsed as hostname
  77.     $src_uri =~ s|\\|/|g;
  78.  
  79.     my $u = URI->new($src_uri);
  80.  
  81.     my $scheme = $u->scheme;
  82.  
  83.     if ($scheme eq 'file') {
  84.         my $path = $u->path;
  85.  
  86.         my $content = '';
  87.         read_file($path, \$content);
  88.         $self->{content} = \$content;
  89.  
  90.         # file change timestamp
  91.         # my($mon, $day, $year) = (localtime ( (stat($path))[9] ) )[4,3,5];
  92.         # $self->{timestamp} = sprintf "%02d/%02d/%04d", ++$mon,$day,1900+$year;
  93.         $self->{timestamp} = scalar localtime;
  94.  
  95.     }
  96.     else {
  97.         die "$scheme is not implemented yet";
  98.     }
  99.  
  100.     if (my $sub = $self->can('src_filter')) {
  101.         $self->$sub();
  102.     }
  103.  
  104.  
  105. }
  106.  
  107. sub meta {
  108.     my $self = shift;
  109.  
  110.     if (@_) {
  111.         $self->{meta} = shift;
  112.     }
  113.     else {
  114.         $self->{meta};
  115.     }
  116. }
  117.  
  118. sub toc {
  119.     my $self = shift;
  120.  
  121.     if (@_) {
  122.         $self->{toc} = shift;
  123.     }
  124.     else {
  125.         $self->{toc};
  126.     }
  127. }
  128.  
  129. # search for the source doc with base $base, and resolve it to a relative 
  130. # to abs_doc_root path and return it 
  131. # if not found return undef
  132. sub transform_src_doc {
  133.     my($self, $path) = @_;
  134.  
  135.     if (my $path = find_src_doc($path)) {
  136.         $path = catfile $self->{dir}{abs_doc_root}, $path;
  137.         $path =~ s|/\./|/|; # avoid .././foo links.
  138.         return path2uri($path);
  139.     }
  140.  
  141.     return undef;
  142. }
  143.  
  144. require Carp;
  145. sub croak {
  146.     my($self, @msg) = @_;
  147.     Carp::croak("[render croak] ", @msg, "\n",
  148.                 "[src path] $self->{src_path}\n"
  149.                );
  150.  
  151. }
  152.  
  153. # abstract methods
  154. #sub src_filter {}
  155.  
  156.  
  157.  
  158. 1;
  159. __END__
  160.  
  161. =head1 NAME
  162.  
  163. C<DocSet::Doc> - A Base Document Class
  164.  
  165. =head1 SYNOPSIS
  166.  
  167.    use DocSet::Doc::HTML ();
  168.    my $doc = DocSet::Doc::HTML->new(%args);
  169.    $doc->scan();
  170.    my $meta = $doc->meta();
  171.    my $toc  = $doc->toc();
  172.    $doc->render();
  173.  
  174.    # internal methods
  175.    $doc->src_read();
  176.    $doc->src_filter();
  177.  
  178. =head1 DESCRIPTION
  179.  
  180. This super class implement core methods for scanning a single document
  181. of a given format and rendering it into another format. It provides
  182. sub-classes with hooks that can change the default behavior. Note that
  183. this class cannot be used as it is, you have to subclass it and
  184. implement the required methods listed later.
  185.  
  186. =head1 METHODS
  187.  
  188. =over
  189.  
  190. =item * new
  191.  
  192. =item * init
  193.  
  194. =item * scan
  195.  
  196. scan the document into a parsed tree and retrieve its meta and toc
  197. data if possible.
  198.  
  199. =item * render
  200.  
  201. render the output document and write it to its final destination.
  202.  
  203. =item * src_read
  204.  
  205. Fetches the source of the document. The source can be read from
  206. different media, i.e. a file://, http://, relational DB or OCR :)
  207. (but these are left for subclasses to implement :)
  208.  
  209. A subclass may implement a "source" filter. For example if the source
  210. document is written in an extended POD the source filter may convert
  211. it into a standard POD. If the source includes some template
  212. directives these can be pre-processed as well.
  213.  
  214. The document's content is coming out of this class ready for parsing
  215. and converting into other formats.
  216.  
  217. =item * meta
  218.  
  219. a simple set/get-able accessor to the I<meta> attribute.
  220.  
  221. =item * toc
  222.  
  223. a simple set/get-able accessor to the I<toc> attribute
  224.  
  225. =item * transform_src_doc
  226.  
  227.   my $doc_src_path = $self->transform_src_doc($path);
  228.  
  229. search for the source doc with path of C<$path> at the search paths
  230. defined by the configuration file I<search_paths> attribute (similar
  231. to the C<@INC> search in Perl) and if found resolve it to a relative
  232. to C<abs_doc_root> path and return it. If not found return the
  233. C<undef> value.
  234.  
  235. =back
  236.  
  237. =head1 ABSTRACT METHODS
  238.  
  239. These methods must be implemented by the sub-classes:
  240.  
  241. =over
  242.  
  243. =item retrieve_meta_data
  244.  
  245. Retrieve and set the meta data that describes the input document into
  246. the I<meta> object attribute. Various documents may provide different
  247. meta information. The only required meta field is I<title>.
  248.  
  249. =back
  250.  
  251. These methods can be implemented by the sub-classes:
  252.  
  253. =over
  254.  
  255. =item src_filter
  256.  
  257. A subclass may want to preprocess the source document before it'll be
  258. processed. This method is called after the source has been read. By
  259. default nothing happens.
  260.  
  261. =back
  262.  
  263. =head1 AUTHORS
  264.  
  265. Stas Bekman E<lt>stas (at) stason.orgE<gt>
  266.  
  267. =cut
  268.