summaryrefslogtreecommitdiff
path: root/.library/IkiWiki/Plugin/fill_empty_page.pm
blob: b2ce1fc09a5b286a597938c5b6d71bbb17c84921 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# A plugin for ikiwiki to implement filling newly created pages from a template
# file.

# Copyright © 2007 Thomas Schwinge <tschwinge@gnu.org>
#
# This program is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation; either version 2, or (at your option) any later
# version.
# 
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
# or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
# for more details.
# 
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.

# Newly created pages will be filled from a file `empty_page.mdwn' if such a
# file can be found by using the same rules as for the sidebar plugin.
#
# <http://ikiwiki.info/todo/default_content_for_new_post/>

package IkiWiki::Plugin::fill_empty_page;

use warnings;
use strict;
use IkiWiki 2.00;

sub import
{
    hook (type => "formbuilder_setup", id => "fill_empty_page",
	  call => \&formbuilder_setup);
}

sub formbuilder_setup
{
    my %params = @_;
    my $form = $params {form};
    my $page = $form->field ("page");

    return if $form->title ne "editpage";
    return if $form->field("do") ne "create";

    return if defined $form->field ("editcontent");

    # This is obviously not the last conclusion of wisdom.
    my $empty_page_page = bestlink ($page, "empty_page") || return;
    my $empty_page_file = $pagesources {$empty_page_page} || return;
    my $empty_page_type = pagetype ($empty_page_file);
    return unless defined $empty_page_type;

    my $content = readfile (srcfile ($empty_page_file));

    if (defined $content && length $content)
    {
	$form->field (name => "editcontent", value => $content);

	$form->tmpl_param ("page_preview", "Filled automagically from `"
			   . $empty_page_file . "`.");
    }
}

1