-} # }}}
-
-package IkiWiki::PageSpec;
-
-sub parsesize ($) { #{{{
- my $size=shift;
- no warnings;
- my $base=$size+0; # force to number
- use warnings;
- my $multiple=1;
- if ($size=~/kb?$/i) {
- $multiple=2**10;
- }
- elsif ($size=~/mb?$/i) {
- $multiple=2**20;
- }
- elsif ($size=~/gb?$/i) {
- $multiple=2**30;
- }
- elsif ($size=~/tb?$/i) {
- $multiple=2**40;
- }
- return $base * $multiple;
-} #}}}
-
-sub match_maxsize ($$;@) { #{{{
- shift;
- my $maxsize=eval{parsesize(shift)};
- if ($@) {
- return IkiWiki::FailReason->new("unable to parse maxsize (or number too large)");
- }
-
- my %params=@_;
- if (! exists $params{file}) {
- return IkiWiki::FailReason->new("no file specified");
- }
-
- if (-s $params{file} > $maxsize) {
- return IkiWiki::FailReason->new("file too large");
- }
- else {
- return IkiWiki::SuccessReason->new("file not too large");
- }
-} #}}}
-
-sub match_minsize ($$;@) { #{{{
- shift;
- my $minsize=eval{parsesize(shift)};
- if ($@) {
- return IkiWiki::FailReason->new("unable to parse minsize (or number too large)");
- }
-
- my %params=@_;
- if (! exists $params{file}) {
- return IkiWiki::FailReason->new("no file specified");
- }
-
- if (-s $params{file} < $minsize) {
- return IkiWiki::FailReason->new("file too small");
- }
- else {
- return IkiWiki::SuccessReason->new("file not too small");
+ elsif ($form->submitted eq "Insert Links") {
+ my $page=quotemeta(Encode::decode_utf8($q->param("page")));
+ my $add="";
+ foreach my $f ($q->param("attachment_select")) {
+ $f=Encode::decode_utf8($f);
+ $f=~s/^$page\///;
+ $add.="[[$f]]\n";
+ }
+ $form->field(name => 'editcontent',
+ value => $form->field('editcontent')."\n\n".$add,
+ force => 1) if length $add;
+ }
+
+ # Generate the attachment list only after having added any new
+ # attachments.
+ $form->tmpl_param("attachment_list" => [attachment_list($form->field('page'))]);
+}
+
+sub attachment_location ($) {
+ my $page=shift;
+
+ # Put the attachment in a subdir of the page it's attached
+ # to, unless that page is an "index" page.
+ $page=~s/(^|\/)index//;
+ $page.="/" if length $page;
+
+ return $page;
+}
+
+sub attachment_list ($) {
+ my $page=shift;
+ my $loc=attachment_location($page);
+
+ my @ret;
+ foreach my $f (values %pagesources) {
+ if (! defined pagetype($f) &&
+ $f=~m/^\Q$loc\E[^\/]+$/ &&
+ -e "$config{srcdir}/$f") {
+ push @ret, {
+ "field-select" => '<input type="checkbox" name="attachment_select" value="'.$f.'" />',
+ link => htmllink($page, $page, $f, noimageinline => 1),
+ size => IkiWiki::Plugin::filecheck::humansize((stat(_))[7]),
+ mtime => displaytime($IkiWiki::pagemtime{$f}),
+ mtime_raw => $IkiWiki::pagemtime{$f},
+ };
+ }