package Kwiki::EscapeURI;
use strict;
use warnings;
use Kwiki::Plugin -Base;
use mixin 'Kwiki::Installer';

use URI::Escape qw(uri_escape_utf8);

our $VERSION = '0.02';

const class_id => 'escape_uri';
const class_title => "uri escape to raw UTF8 links";

sub init {
    super;
    my $formatter = $self->hub->load_class('formatter');
    $formatter->table->{forced} = 'Kwiki::EscapeURI::ForcedLink';
}

sub register {
    my $registry = shift;
    $registry->add(preload => 'escape_uri');
    $registry->add(hook => 'page:kwiki_link', pre => 'uri_hook');
}

sub uri_hook {
    my $hook = pop;
    $hook->code(undef);
    my ($label) = @_;
    my $page_uri = uri_escape_utf8($self->uri);
    $label = $self->title
      unless defined $label;
    my $class = $self->active
      ? '' : ' class="empty"';
    qq(<a href="?$page_uri"$class>$label</a>);
}

package Kwiki::EscapeURI::ForcedLink;
use base 'Kwiki::Formatter::ForcedLink';

use URI::Escape qw(uri_escape_utf8);

sub html {
    $self->matched =~ $self->pattern_start;
    my $target = $1;
    my $page_uri = uri_escape_utf8($target);
    my $class = $self->hub->pages->new_from_name($target)->exists
      ? '' : ' class="empty"';
    my $text = $self->escape_html($target);
    return qq(<a href="?$page_uri"$class>$text</a>);
}

package Kwiki::EscapeURI;
