--- deliantra/Deliantra-Client/DC/Pod.pm 2006/07/30 13:16:44 1.3 +++ deliantra/Deliantra-Client/DC/Pod.pm 2006/08/14 18:46:07 1.12 @@ -1,243 +1,179 @@ package CFPlus::Pod; use strict; +use utf8; -use Pod::POM; +use Storable; -use CFPlus; -use CFPlus::UI; +our $VERSION = 1; -our $VERSION = 1.02; # bump if resultant formatting changes +our $goto_document = sub { }; +our %wiki; -our @result; -our $indent; +my $MA_BEG = "\x{fcd0}"; +my $MA_SEP = "\x{fcd1}"; +my $MA_END = "\x{fcd2}"; -package CFPlus::Pod::AsMarkup; +*wiki = Storable::retrieve CFPlus::find_rcfile "docwiki.pst"; -use strict; +sub goto_document($) { + $goto_document->(split /\//, $_[0]); +} -use base "Pod::POM::View::Text"; +sub is_prefix_of($@) { + my ($node, @path) = @_; -*view_seq_file = -*view_seq_code = -*view_seq_bold = sub { "$_[1]" }; -*view_seq_italic = sub { "$_[1]" }; -*view_seq_space = -*view_seq_link = sub { CFPlus::asxml $_[1] }; -*view_seq_zero = -*view_seq_index = sub { }; + return 1 unless @path; -sub view_seq_text { - my $text = $_[1]; - $text =~ s/\s+/ /g; - CFPlus::asxml $text -} + my $kw = lc pop @path; -sub view_item { - ("\t" x ($indent / 4)) - . $_[1]->title->present ($_[0]) - . "\n\n" - . $_[1]->content->present ($_[0]) -} + $node = $node->{parent} + or return 0; -sub view_verbatim { - (join "", - map +("\t" x ($indent / 2)) . "$_\n", - split /\n/, CFPlus::asxml $_[1]) - . "\n" + return ! ! grep $_ eq $kw, @{ $node->{kw} }; } -sub view_textblock { - ("\t" x ($indent / 2)) . "$_[1]\n" -} +sub find(@) { + my (@path) = @_; -sub view_head1 { - "\n\n" . $_[1]->title->present ($_[0]) . "\n\n" - . $_[1]->content->present ($_[0]) -}; + return unless @path; -sub view_head2 { - "\n" . $_[1]->title->present ($_[0]) . "\n\n" - . $_[1]->content->present ($_[0]) -}; + my $kw = lc pop @path; -sub view_head3 { - "\n" . $_[1]->title->present ($_[0]) . "\n\n" - . $_[1]->content->present ($_[0]) -}; + # TODO: make sure results are unique -sub view_over { - local $indent = $indent + $_[1]->indent; - $_[1]->content->present ($_[0]) + grep { is_prefix_of $_, @path } + map @$_, + $kw eq "*" ? @wiki{sort keys %wiki} + : grep $_, $wiki{$kw} } -package CFPlus::Pod::AsParagraphs; +sub full_path_of($) { + my ($node) = @_; -use strict; + my @path; -use base "Pod::POM::View"; - -*view_seq_file = -*view_seq_code = -*view_seq_bold = sub { "$_[1]" }; -*view_seq_italic = sub { "$_[1]" }; -*view_seq_zero = sub { }; -*view_seq_space = sub { my $text = $_[1]; $text =~ s/ / /g; $text }; -*view_seq_index = sub { warn "index<@_>\n"; $result[-1]{index}{$_[1]} = undef }; - -sub view_seq_text { - my $text = $_[1]; - $text =~ s/\s+/ /g; - CFPlus::asxml $text -} - -sub view_seq_link { - my (undef, $link) = @_; - - # TODO: - # http://... - # ref - # pod/ref - - "" . (CFPlus::asxml $_[1]) . ""; -} - -sub view_item { - push @result, { - indent => $indent * 8, - markup => $_[1]->title->present ($_[0]) . "\n\n", - }; - $_[1]->content->present ($_[0]); - () -} - -sub view_verbatim { - push @result, { - indent => $indent * 16, - markup => "" . (CFPlus::asxml $_[1]) . "\n", - }; - () -} - -sub view_textblock { - push @result, { - indent => $indent * 16, - markup => "$_[1]\n", - }; - () -} - -sub view_head1 { - push @result, { - indent => $indent * 16, - markup => "\n\n" . $_[1]->title->present ($_[0]) . "\n", - }; - $_[1]->content->present ($_[0]); - () -}; - -sub view_head2 { - push @result, { - indent => $indent * 16, - markup => "\n\n" . $_[1]->title->present ($_[0]) . "\n", - }; - $_[1]->content->present ($_[0]); - () -}; - -sub view_head3 { - push @result, { - indent => $indent * 16, - markup => "\n\n" . $_[1]->title->present ($_[0]) . "\n", - }; - $_[1]->content->present ($_[0]); - () -}; - -sub view_over { - local $indent = $indent + $_[1]->indent; - push @result, { indent => $indent }; - $_[1]->content->present ($_[0]); - () -} - -sub view_for { - if ($_[1]->format eq "image") { - push @result, { - indent => $indent * 16, - markup => "\x{fffc}", - widget => [new CFPlus::UI::Image path => "pod/" . $_[1]->text], - }; + # skip toplevel hierarchy pod/, because its not a document + while ($node->{parent}) { + unshift @path, $node; + $node = $node->{parent}; } - () -} -sub view { - my ($self, $type, $item) = @_; + @path +} - $item->content->present ($self); +sub full_path($) { + join "/", map $_->{kw}[0], &full_path_of } -package CFPlus::Pod; +sub section_of($) { + my ($node) = @_; -my $pod_cache = CFPlus::db_table "pod_cache"; + my $doc = $node->{doc}; + my $par = $node->{par}; + my $lvl = $node->{level}; -sub load($$$$) { - my ($path, $filtertype, $filterversion, $filtercb) = @_; + my @res; - stat $path - or die "$path: $!"; + do { + my $p = $doc->[$par]; - my $phash = join ",", $filterversion, $VERSION, (stat _)[7,9]; + if (length $p->{markup}) { + push @res, { + markup => $p->{markup}, + indent => $p->{indent}, + }; + } + } while $doc->[++$par]{level} > $lvl; - my ($chash, $pom) = eval { - local $SIG{__DIE__}; - @{ Storable::thaw $pod_cache->get ("$path/$filtertype") } - }; + @res +} - return $pom if $chash eq $phash; +sub section(@) { + map section_of $_, &find +} - my $pod = do { - local $/; - open my $pod, "<:utf8", $_[0] - or die "$_[0]: $!"; - <$pod> - }; +sub thaw_section(\@\%) { + for (@{$_[0]}) { + $_->{markup} =~ s{ + $MA_BEG + ([^$MA_END]+) + $MA_END + }{ + my ($type, @arg) = split /$MA_SEP/o, $1; - #utf8::downgrade $pod; + $_[1]{$type}($_, @arg) + }ogex; + } +} - $pom = $filtercb->(Pod::POM->new->parse_text ($pod)); +my %as_label = ( + image => sub { + my ($par, $path) = @_; - $pod_cache->put ("$path/$filtertype" => Storable::nfreeze [$phash, $pom]); + "img" + }, + link => sub { + my ($par, $text, $link) = @_; - $pom -} + "" . (CFPlus::asxml $text) . "" + }, +); -sub section($$) { - my ($pod, $section) = @_; -} +sub as_label(@) { + thaw_section @_, %as_label; -sub as_markup($) { - my ($pom) = @_; + my $text = + join "\n", + map +("\xa0" x ($_->{indent} / 4)) . $_->{markup}, + @_; - local $indent = 0; + $text =~ s/^\s+//; + $text =~ s/\s+$//; - $pom->present ("CFPlus::Pod::AsMarkup") + $text } -sub as_paragraphs($) { - my ($pom) = @_; +my %as_paragraphs = ( + image => sub { + my ($par, $path, $flags) = @_; - local @result = ( { } ); - local $indent = 0; + push @{ $par->{widget} }, new CFPlus::UI::Image path => $path, + $flags & 1 ? (max_h => $::FONTSIZE * 0.8) : (); - $pom->present ("CFPlus::Pod::AsParagraphs"); + "\x{fffc}" + }, + link => sub { + my ($par, $text, $link) = @_; + + push @{ $par->{widget} }, new CFPlus::UI::Label + markup => "" . (CFPlus::asxml $text) . "", + fontsize => 0.8, + can_hover => 1, + can_events => 1, + padding_x => 0, + padding_y => 0, + tooltip => "Go to " . (CFPlus::asxml $link) . "", + on_button_up => sub { + goto_document $link; + }; + + "\x{fffc}" + }, +); + +sub as_paragraphs(@) { + thaw_section @_, %as_paragraphs; + + @_ +} - [grep exists $_->{markup}, @result] +sub section_paragraphs(@) { + as_paragraphs §ion } -sub pod_paragraphs($) { - load CFPlus::find_rcfile "pod/$_[0].pod", - pod_paragraphs => 1, sub { as_paragraphs $_[0] }; +sub section_label(@) { + as_label §ion } +1