Name:
	WikiLinking
FullName:
	Wiki (one-to-one) hyperlinking support
URI:
	IW:SuikaWiki:"SuikaWiki"
Initialize:
	my $NS_XHTML1 = 'http://www.w3.org/1999/xhtml';

{
Name:
	wikiview/link-to-wikipage
	wikiview-resource/link-to-wikipage
	wikiform-input/link-to-wikipage
FullName:
	#text
Format:
	$r = __PACKAGE__->to_wikipage_in_html ({
		label	=> $p->{label},
	} => {
		base	=> ($p->{base_page}||$o->{page}),
		page_name_relative	=> $p->{page},
		page_anchor_no	=> $p->{page_anchor_no},
		wiki_mode	=> $p->{mode},
	}, {
		o	=> $o,
	});
}

{
Name:
	wikipage-link/link-to-it
FullName:
	Link (make a source anchor) to "it"
Format:
	$r = SuikaWiki::Markup::XML->new (namespace_uri => $NS_XHTML1, local_name => 'a');
	  $r->set_attribute (href => $o->{link}->{dest}->{uri});
	  
	  $r->append_node ($p->{label}, node_or_text => 1);
	  $r->set_attribute (title => $p->{description}) if $p->{description};
	  my @class = split /\s+/, $p->{class};
	  if (substr ($o->{link}->{dest}->{uri}, 0, length ($o->uri ('wiki')))
	   eq $o->uri ('wiki')) {
	    push @class, 'wiki';	## Link to the page within the Wiki
	  }
	  $r->set_attribute (class => join ' ', @class) if @class;
}

{
Name:
	wikipage-link/link-base-page
FullName:
	Base WikiPage of the link
Format:
	$r = SuikaWiki::Markup::XML->new (type => '#text', value => $o->{link}->{dest}->{base});
}

{
Name:
	wikipage-link/link-dest-anchor-no
FullName:
	The number of destination anchor of the link
Format:
	$r = SuikaWiki::Markup::XML->new (type => '#text', value => $o->{link}->{dest}->{page_anchor_no});
}

{
Name:
	wikipage-link/if-linked-wikipage-exist
FullName:
	If, if not WikiPage of the link is exist...
Format:
	my $formatter = SuikaWiki::Plugin->formatter ('page_link');
	return $formatter->replace (($o->_database_exist ($o->{link}->{dest}->{page_name}) ? $p->{true} : $p->{false}), $o, {formatter => $formatter});
}

{
Name:
	wikipage-link/if-link-has-dest-anchor-no
FullName:
	If, if not WikiPage of the link has the number of the destination anchor...
Format:
	my $formatter = SuikaWiki::Plugin->formatter ('page_link');
	return $formatter->replace (($o->{link}->{dest}->{page_anchor_no} ? $p->{true} : $p->{false}), $o, {formatter => $formatter});
}

{
Name:
	wikipage-link/-bare-text
FullName:
	#text
Format:
	$r = SuikaWiki::Markup::XML->new (type => '#text', value => $p->{-bare_text});
}

MODULE:
	## Public functions
	sub to_wikipage_in_html ($$$;$) {
	  my (undef, $src, $dest, $option) = @_;
	  $dest->{page_name} = SuikaWiki::Plugin::WikiNamespace->resolve_relative_name
	                       ($dest->{base} => $dest->{page_name_relative});
	  $dest->{uri} = SuikaWiki::Plugin->_uri_wiki_page	## TODO: use formal API
	                 ($dest->{page_name}, mode => $dest->{wiki_mode},
	                  anchor => $dest->{page_anchor_no}, up_to_date => $dest->{_up_to_date},
	                  with_lm => $dest->{_with_lm});
	  ## Making label/description from template
	  my $o = $option->{o};
	  $o->{link} = {
	  	src	=> $src,
	  	dest	=> $dest,
	  	option	=> $option,
	  };
	  my $formatter = SuikaWiki::Plugin->formatter ('page_link');
	  return $formatter->replace ($src->{label}, $o, {formatter => $formatter});
	}
	
	## Obsolete (but still used) public functions
	package main;
	our (%interwiki, %uri, %database, %fmt, %PageName);
	
	## %option
	## -- page options: mode, with_lm, up_to_date, anchor,
	##                  add_param (discouraged), href (discouraged),
	##                  base
	## -- source anchor options: label, anchor
	sub make_wikilink ($%) {
	  my ($name, %option) = @_;
	  
	  my $dname = length $option{label} ? $option{label} : $name;
	  $name = SuikaWiki::Plugin::WikiNamespace->resolve_relative_name ($option{base} => $name);
	  #$name ||= $PageName{FrontPage};
	  
	  my $r = SuikaWiki::Markup::XML->new (namespace_uri => $NS_XHTML1, local_name => 'a');
	  $r->set_attribute (href => SuikaWiki::Plugin->_uri_wiki_page ($name, %option));
	  if ($main::database{$name}) {
	    if ($option{anchor}) {
	      $r->append_text (qq($dname>>$option{anchor}));
	    } else {
	      $r->append_text ($dname);
	    }
	    $r->set_attribute (title => $name.SuikaWiki::Plugin::WikiPage->_get_subjectline ($name));
	    $r->set_attribute (class => 'wiki');
	  } else {
	    $r->set_attribute (class => 'wiki not-exist');
	    $r->set_attribute (title => $name.SuikaWiki::Plugin->resource ('Title-Summary Delimiter').&main::Resource('JumpAndEditWikiPage'));
	    $r->append_text ($dname);
	    
	    ## TODO: In pure XML mode, don't output this (should be displayed by style sheet!)
	    for ($r->append_new_node (namespace_uri => $NS_XHTML1, local_name => 'span')) {
	      $_->set_attribute (class => 'mark');
	      $_->append_text (&main::Resource('JumpAndEditWikiPageMark'));
	    }
	  }
	  return $r;
	}

	sub make_urilink ($;%) {
	  require URI;
	  my $uri = shift;
	  if ($uri =~ s/^IW://) {	## InterWiki (not URI)
	    $uri = &unescape ($uri);
	    if ($uri =~ /^([^\x00-\x2F\x3A-\x40\x5B-\x60\x7B-\x7F]+|"(?:\\.|[^"\\])+"):([^\x00-\x2F\x3A-\x40\x5B-\x60\x7B-\x7F]+|"(?:\\.|[^"\\])+")$/) {
	      my ($site, $name) = ($1, $2);
	      for ($site, $name) {
	        if (s/^"//) { s/"$//; s/\\(.)/$1/g }
	      }
	      &init_InterWikiName () unless $interwiki{'[[]]'};
	      if ($interwiki{$site}) {
	        &load_formatter ('interwiki');
	        my $uri = &escape ($fmt{interwiki}->replace ($interwiki{$site} => {site => $site, name => $name}));
	        $site = &escape ($site); $name = &escape ($name);
	        qq(&lt;<a href="$uri" class="out-of-wiki interwiki" title="$name ($site); URI: &lt;$uri&gt;"><span class="interwiki-site">$site:</span><span class="interwiki-name">$name</span></a>&gt;);
	      } else {
	        qq(&lt;@{[&Resource('Error:UnknownInterWikiName=',escape=>1)]}@{[&escape ($site)]}&gt;);
	      }
	    } else {
	      qq(&lt;@{[&Resource('Error:InvalidInterWiki=',escape=>1)]}@{[&escape($uri)]}&gt;);
	    }
	  } elsif ($uri =~ /^urn:/) {	## URN
	    my $uri2 = &escape (URI->new ('/uri-res/N2L?'.&unescape ($uri), 'http')->canonical);
	    qq(&lt;<a href="$uri2" title="URI: &lt;$uri&gt; (via &lt;$uri2&gt;)" class="out-of-wiki urn uri long">$uri</a>&gt;);
	  } elsif ($uri =~ s/^MAIL://) {	## mail address (not URI)
	    my $uri2 = &escape (URI->new ('mailto:'.&unescape ($uri))->canonical);
	    qq(&lt;<a href="$uri2" class="out-of-wiki mail uri long">$uri</a>&gt;);
	  } elsif ($uri =~ s/^IMG(?:\([^)]+\))?://) {	## image (not URI itself)
	    my $uri2 = &escape (URI->new (&unescape ($uri))->canonical);
	    qq(<img src="$uri2" alt="" title="URI: &lt;$uri2&gt;" class="out-of-wiki" />);
	  } else {	## misc. URI
	    CGI::Carp::warningsToBrowser (0);
	    my $uri2 = &escape (URI->new (&unescape ($uri))->canonical);
	    CGI::Carp::warningsToBrowser (1);
	    qq(&lt;<a href="$uri2" title="URI: &lt;$uri2&gt;" class="out-of-wiki uri long">$uri</a>&gt;);
	  }
	}
	
	sub main::init_InterWikiName {
	  my @content = split /\n/, $main::database{$main::PageName{InterWikiName}};
	  for (@content) {
	    if (/^([^#]\S*)\s+(\S[^\x0A\x0D]+)/) {
	      $main::interwiki{$1} = $2;
	    }
	  }
	  $main::interwiki{'[[]]'} = 1;	## dummy
	}

POD:SEE ALSO:
	SuikaWiki::Plugin::WikiPage (Wiki page support),
	SuikaWiki::Plugin::WikiNamespace (WikiNamespace support),
	SuikaWiki::Plugin::Map (n-to-n linking and page-to-page relationship)
POD:LICENSE:
	Copyright 2003 Wakaba <w@suika.fam.cx>
	
	%%GNUGPL2%%