Kwiki-Purple

 view release on metacpan or  search on metacpan

lib/Kwiki/Purple.pm  view on Meta::CPAN

    $self->get_text;
}

sub Spoon::Formatter::Unit::get_text {
    my $self = shift;
    my $inner;
    if (@{$self->units}) {
        $inner = join '', map {
            ref($_)
              ? $_->to_text
                ? $_->to_text
                : $_->matched
              : $_;
        } @{$self->units};
    } else {
        $inner = $self->matched;
    }
    $self->text_start . $inner . $self->text_end;
}

# XXX should be a way to determine these rather than beat on it like this
sub Spoon::Formatter::Unit::text_start {''}
sub Spoon::Formatter::Unit::text_end {''}
sub Spoon::Formatter::Block::text_end {"\n\n"}
sub Spoon::Formatter::WaflBlock::text_start {'.' . shift->method . "\n"}
sub Spoon::Formatter::WaflBlock::text_end {'.' . shift->method . "\n"}


# XXX presumably much or all of this can be moved into 
# the ::Formatter::Purple:: classes? 
sub start_nid {
    my $formatter = $self->hub->formatter;
    my $table = $formatter->table;
    my $start = 'html_start';
    my $end = 'html_end';
    no strict 'refs';
    no warnings 'redefine';
    for my $type (@{$self->hook_classes}) {
        my $class = $table->{$type};
        my $start = "$class\::$start";
        my $end = "$class\::$end";
        *{"$start"} = 
         sub {
             my $self = shift;
             my $nid = $self->units->[-1];
             my $level = '';
             my $element = $type;
             if ($type eq 'heading') {
                 $level = $self->level;
                 $element = 'h';
             }
             my $nid_value =
               (ref($nid) and $nid->isa('Kwiki::Purple::Nid::Wafl'))
               ? ' id="nid' . $nid->nid . '"'
               : '';
             qq(<${element}${level}${nid_value}>);
         };
    }
}

sub update_nids {
    my $hook = pop;
    my $page = $self;
    $self = $self->hub->purple;
    $self->update($page);
}

sub update {
    my $page = shift;
    my $formatter = $self->hub->formatter;
    my %hooks;
    unless ($self->hooked) {
        $self->hooked(1);
        my $table = $formatter->table;
        for my $class (@$table{@{$self->hook_classes}}) {
            $hooks{$class} = $self->hub->add_hook(
                $class . '::unit_match', post => 'purple:check_nid'
            );
        }
    }
    my $units = $self->hub->formatter->text_to_parsed($page->content);
    $page->content($units->to_text);
    for my $class (keys(%hooks)) {
        $hooks{$class}->unhook;
    }
}

sub check_nid {
    my $hook = pop;
    my $unit = $self;
    my $text = $unit->text;
    my $nid;
    $nid = $1 if ($text =~ /{nid ([A-Z0-9]+)}\s*/);

    $self = $self->hub->purple;

    my $page = $self->hub->pages->current;

    unless ($nid) {
        $nid = $self->next_nid($page);
        $text =~ s/(?: |=)*(\n{0,2})\n*$/ {nid $nid}$1/;
        $unit->text($text);
    }

    $self->write_node($nid, $unit->text);
}

sub write_node {
    # we just went to the trouble of adding the nid, but let's
    # go ahead and remove it before storing
    my $nid = shift;
    my $text = shift;
    my $page_id = $self->hub->pages->current->id;
    $text =~ s/\s*{nid [A-Z0-9]+}\s*$//;
    io($self->plugin_directory . '/' . $nid)->print($text);
    io($self->plugin_directory . '/' . $nid . '.name')->print($page_id);
}

sub get_node {
    my $nid = $self->cgi->nid;
    my $format = $self->cgi->format || $self->default_retrieval_format;
    $self->retrieve_node($nid, $format);
}
sub retrieve_node {
    my $nid = shift;
    my $format = shift;
    my $method = 'retrieve_node_' . $format;
    $self->$method($nid);



( run in 0.548 second using v1.01-cache-2.11-cpan-13bb782fe5a )