| 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 | if (my $sub = $self->can('retrieve_meta_data')) { | ||
| 33 | $self->$sub(); | ||
| 34 | } | ||
| 35 | } | ||
| 36 | |||
| 37 | sub render { | ||
| 38 | my ($self, $cache) = @_; | ||
| 39 | |||
| 40 | # if the object wasn't stored rescan | ||
| 41 | #$self->scan() unless $self->meta; | ||
| 42 | |||
| 43 | my $src_uri = $self->{src_uri}; | ||
| 44 | my $dst_path = $self->{dst_path}; | ||
| 45 | |||
| 46 | my $rel_doc_root = $self->{rel_doc_root}; | ||
| 47 | my $abs_doc_root = $self->{abs_doc_root}; | ||
| 48 | $abs_doc_root .= "/$rel_doc_root" | ||
| 49 | if defined $rel_doc_root && | ||
| 50 | length($rel_doc_root) && $rel_doc_root ne '.'; | ||
| 51 | |||
| 52 | $abs_doc_root =~ s|^./||; # IE/Mac can't handle path ./../foo | ||
| 53 | |||
| 54 | $self->{dir} = { | ||
| 55 | abs_doc_root => $abs_doc_root, | ||
| 56 | rel_doc_root => $rel_doc_root, | ||
| 57 | path_from_base => $self->{path_from_base}, | ||
| 58 | }; | ||
| 59 | |||
| 60 | $self->{nav} = DocSet::NavigateCache->new($cache->path, $src_uri); | ||
| 61 | |||
| 62 | note "Rendering $dst_path"; | ||
| 63 | $self->convert(); | ||
| 64 | write_file($dst_path, $self->{output}); | ||
| 65 | |||
| 66 | # anything that should be done after the target was written? | ||
| 67 | $self->postprocess() if $self->can('postprocess'); | ||
| 68 | } | ||
| 69 | |||
| 70 | # read the source and remember the mod time | ||
| 71 | # sets $self->{content} | ||
| 72 | # $self->{timestamp} | ||
| 73 | sub src_read { | ||
| 74 | my ($self) = @_; | ||
| 75 | |||
| 76 | # META: at this moment everything is a file path | ||
| 77 | my $src_uri = "file://" . $self->{src_path}; | ||
| 78 | # Win32: fix the path, or it'll be parsed as hostname | ||
| 79 | $src_uri =~ s|\\|/|g; | ||
| 80 | |||
| 81 | my $u = URI->new($src_uri); | ||
| 82 | |||
| 83 | my $scheme = $u->scheme; | ||
| 84 | |||
| 85 | if ($scheme eq 'file') { | ||
| 86 | my $path = $u->path; | ||
| 87 | |||
| 88 | my $content = ''; | ||
| 89 | read_file($path, \$content); | ||
| 90 | $self->{content} = \$content; | ||
| 91 | |||
| 92 | # file change timestamp | ||
| 93 | # my ($mon, $day, $year) = (localtime ( (stat($path))[9] ) )[4,3,5]; | ||
| 94 | # $self->{timestamp} = sprintf "%02d/%02d/%04d", ++$mon,$day,1900+$year; | ||
| 95 | $self->{timestamp} = scalar localtime; | ||
| 96 | |||
| 97 | } | ||
| 98 | else { | ||
| 99 | die "$scheme is not implemented yet"; | ||
| 100 | } | ||
| 101 | |||
| 102 | if (my $sub = $self->can('src_filter')) { | ||
| 103 | $self->$sub(); | ||
| 104 | } | ||
| 105 | |||
| 106 | |||
| 107 | } | ||
| 108 | |||
| 109 | sub meta { | ||
| 110 | my $self = shift; | ||
| 111 | |||
| 112 | if (@_) { | ||
| 113 | $self->{meta} = shift; | ||
| 114 | } | ||
| 115 | else { | ||
| 116 | $self->{meta}; | ||
| 117 | } | ||
| 118 | } | ||
| 119 | |||
| 120 | sub toc { | ||
| 121 | my $self = shift; | ||
| 122 | |||
| 123 | if (@_) { | ||
| 124 | $self->{toc} = shift; | ||
| 125 | } | ||
| 126 | else { | ||
| 127 | $self->{toc}; | ||
| 128 | } | ||
| 129 | } | ||
| 130 | |||
| 131 | # search for the source doc with base $base, and resolve it to a relative | ||
| 132 | # to abs_doc_root path and return it | ||
| 133 | # if not found return undef | ||
| 134 | sub transform_src_doc { | ||
| 135 | my ($self, $path) = @_; | ||
| 136 | |||
| 137 | if (my $path = find_src_doc($path)) { | ||
| 138 | $path = catfile $self->{dir}{abs_doc_root}, $path; | ||
| 139 | $path =~ s|/\./|/|; # avoid .././foo links. | ||
| 140 | return path2uri($path); | ||
| 141 | } | ||
| 142 | |||
| 143 | return undef; | ||
| 144 | } | ||
| 145 | |||
| 146 | require Carp; | ||
| 147 | sub croak { | ||
| 148 | my ($self, @msg) = @_; | ||
| 149 | Carp::croak("[render croak] ", @msg, "\n", | ||
| 150 | "[src path] $self->{src_path}\n" | ||
| 151 | ); | ||
| 152 | |||
| 153 | } | ||
| 154 | |||
| 155 | # abstract methods | ||
| 156 | #sub src_filter {} | ||
| 157 | |||
| 158 | |||
| 159 | |||
| 160 | 1; | ||
| 161 | __END__ |