| 10 | | $registry->add(action => 'doolittle_index'); |
|---|
| 11 | | $registry->add(command => 'doolittle_build_index', |
|---|
| 12 | | description => 'Rebuild document index',); |
|---|
| 13 | | $registry->add(hook => 'cgi:set_default_page_name', |
|---|
| 14 | | pre => 'page_name_hook'); |
|---|
| 15 | | $registry->add(hook => 'pages:all', post => 'pages_all'); |
|---|
| 16 | | $registry->add(hook => 'page:load_content', post => 'load_content'); |
|---|
| 17 | | $registry->add(hook => 'archive:fetch', post => 'archive_fetch'); |
|---|
| 18 | | $registry->add(hook => 'archive:commit', pre => 'archive_pre_commit'); |
|---|
| 19 | | $registry->add(hook => 'archive:commit', post => 'archive_post_commit'); |
|---|
| 20 | | $registry->add(hook => 'page:store_content', pre => 'store_content'); |
|---|
| 21 | | $registry->add(hook => 'hub:action', post => 'action'); |
|---|
| | 8 | $registry->add( preload => 'doolittle' ); |
|---|
| 30 | | |
|---|
| 31 | | sub doolittle_index { |
|---|
| 32 | | $self->template->process($self->screen_template, |
|---|
| 33 | | content_pane => 'doolittle_content.html'); |
|---|
| 34 | | } |
|---|
| 35 | | |
|---|
| 36 | | sub pages_all { |
|---|
| 37 | | my $hook = pop; |
|---|
| 38 | | grep { |
|---|
| 39 | | -l "database/" . $_->id and $_->id ne '.index' |
|---|
| 40 | | } $hook->returned; |
|---|
| 41 | | } |
|---|
| 42 | | |
|---|
| 43 | | sub handle_doolittle_build_index { |
|---|
| 44 | | my $index = io->catfile($self->config->database_directory, ".index"); |
|---|
| 45 | | for my $source_file ($index->chomp->getlines) { |
|---|
| 46 | | next if $source_file =~ /^(#|\s*$)/; |
|---|
| 47 | | next unless -f $source_file; |
|---|
| 48 | | unless ($self->extract_pod(io($source_file)->all)) { |
|---|
| 49 | | warn "No pod for $source_file\n"; |
|---|
| 50 | | next; |
|---|
| 51 | | } |
|---|
| 52 | | my ($module) = $source_file =~ /.*\/lib\/(.*)$/; |
|---|
| 53 | | $module =~ s/\//-/g; |
|---|
| 54 | | $module =~ s/\.pm$//; |
|---|
| 55 | | my $link_file = $self->config->database_directory . "/$module"; |
|---|
| 56 | | next if -e $link_file; |
|---|
| 57 | | symlink($source_file, $link_file) |
|---|
| 58 | | or die "Can't make symlink for $link_file\n:$!"; |
|---|
| 59 | | } |
|---|
| 60 | | } |
|---|
| 61 | | |
|---|
| 62 | | sub page_name_hook { |
|---|
| 63 | | my $hook = pop; |
|---|
| 64 | | $hook->cancel; |
|---|
| 65 | | return shift || ''; |
|---|
| 66 | | } |
|---|
| 67 | | |
|---|
| 68 | | sub extract_pod { |
|---|
| 69 | | my $content = shift; |
|---|
| 70 | | return $content; |
|---|
| 71 | | $content =~ /.*\n__DATA__\n(?:.*?\n)?(=\w+.*?)(?:\n=cut|\z)(?:.*)\z/s |
|---|
| 72 | | or return; |
|---|
| 73 | | return $1; |
|---|
| 74 | | |
|---|
| 75 | | } |
|---|
| 76 | | |
|---|
| 77 | | sub store_pod { |
|---|
| 78 | | die "FIXME"; |
|---|
| 79 | | my ($content, $pod) = @_; |
|---|
| 80 | | $content =~ |
|---|
| 81 | | s/(.*\n__DATA__\n(?:.*?\n)?)(=\w+.*?)((?:\n=cut|\z)(?:.*)\z)/$1$pod$3/s |
|---|
| 82 | | or die; |
|---|
| 83 | | return $content; |
|---|
| 84 | | } |
|---|
| 85 | | |
|---|
| 86 | | sub archive_pre_commit { |
|---|
| 87 | | my $page = shift; |
|---|
| 88 | | my $hook = pop; |
|---|
| 89 | | my $page_source = readlink($page->file_path) |
|---|
| 90 | | or die $!; |
|---|
| 91 | | $page->{page_source} = $page_source; |
|---|
| 92 | | } |
|---|
| 93 | | |
|---|
| 94 | | sub archive_post_commit { |
|---|
| 95 | | my $page = shift; |
|---|
| 96 | | my $hook = pop; |
|---|
| 97 | | my $link = $page->file_path; |
|---|
| 98 | | my $page_source = $page->{page_source}; |
|---|
| 99 | | unlink $link |
|---|
| 100 | | or die $!; |
|---|
| 101 | | symlink($page_source, $link); |
|---|
| 102 | | } |
|---|
| 103 | | |
|---|
| 104 | | sub archive_fetch { |
|---|
| 105 | | my $hook = pop; |
|---|
| 106 | | my $content = $hook->returned; |
|---|
| 107 | | |
|---|
| 108 | | my $local = $self->hub->doolittle; |
|---|
| 109 | | my $pod = $local->extract_pod($content) |
|---|
| 110 | | or die; |
|---|
| 111 | | return $pod; |
|---|
| 112 | | } |
|---|
| 113 | | |
|---|
| 114 | | sub load_content { |
|---|
| 115 | | my $content = $self->content; |
|---|
| 116 | | my $page_name = $self->id; |
|---|
| 117 | | my $local = $self->hub->doolittle; |
|---|
| 118 | | |
|---|
| 119 | | my $pod = $local->extract_pod($content) |
|---|
| 120 | | or die $local->invalid_doolittle_page($page_name); |
|---|
| 121 | | $self->content($pod); |
|---|
| 122 | | return $self; |
|---|
| 123 | | } |
|---|
| 124 | | |
|---|
| 125 | | sub store_content { |
|---|
| 126 | | die "FIXME"; |
|---|
| 127 | | my $hook = pop; |
|---|
| 128 | | $hook->cancel; |
|---|
| 129 | | my $local = $self->hub->doolittle; |
|---|
| 130 | | my $file = io($self->file_path)->utf8; |
|---|
| 131 | | my $old_content = $file->all; |
|---|
| 132 | | my $pod = $self->content; |
|---|
| 133 | | if ($pod) { |
|---|
| 134 | | $pod =~ s/\r//g; |
|---|
| 135 | | $pod =~ s/\n*\z/\n/; |
|---|
| 136 | | } |
|---|
| 137 | | my $content = $local->store_pod($old_content, $pod); |
|---|
| 138 | | unless ($self->force) { |
|---|
| 139 | | return if $file->exists and |
|---|
| 140 | | $content eq $old_content; |
|---|
| 141 | | } |
|---|
| 142 | | $file->print($content); |
|---|
| 143 | | return $self; |
|---|
| 144 | | } |
|---|
| 145 | | |
|---|
| 146 | | sub invalid_doolittle_page { |
|---|
| 147 | | my $name = shift; |
|---|
| 148 | | die "Invalid Doolittle source for page '$name'"; |
|---|
| 149 | | } |
|---|
| 150 | | |
|---|
| 151 | | __DATA__ |
|---|
| 152 | | |
|---|
| 153 | | |
|---|
| 154 | | __template/tt2/doolittle_content.html__ |
|---|
| 155 | | <h1>Welcome to Doolittle</h1> |
|---|
| 156 | | <ul> |
|---|
| 157 | | [% FOR page = hub.pages.all %] |
|---|
| 158 | | <li>[% page.kwiki_link %]</li> |
|---|
| 159 | | [% END %] |
|---|
| 160 | | </ul> |
|---|