1 |
# -*- perl -*- |
2 |
|
3 |
=head1 NAME |
4 |
|
5 |
SuikaWiki::SrcFormat --- SuikaWiki: Data source (input) format supports |
6 |
|
7 |
=cut |
8 |
|
9 |
package SuikaWiki::SrcFormat; |
10 |
use strict; |
11 |
our $VERSION = do{my @r=(q$Revision: 1.1 $=~/\d+/g);sprintf "%d."."%02d" x $#r,@r}; |
12 |
require SuikaWiki::Plugin; |
13 |
|
14 |
sub new ($$) { |
15 |
my ($class, $source) = @_; |
16 |
my $self = bless {}, $class; |
17 |
($self->{magic}, $self->{content}) = $self->_magic_and_content ($source); |
18 |
if (substr ($self->{magic}, 0, 2) eq '#?') { ## #?Type/Type/Type param="val" |
19 |
my $magic = $self->{magic}; |
20 |
$magic =~ s!^\#\?([0-9A-Za-z_.+-]+(?:/[0-9A-Za-z_.+-]+)*)\s*!!; |
21 |
$self->{type} = [split m!/!, $1]; |
22 |
if ($magic =~ s!([0-9A-Za-z_.+-]+)="((?:[^"]|\\.)+)"\s*!!) { |
23 |
$self->{param}->{$1} = $2; |
24 |
$self->{param}->{$1} =~ s/\\(.)/$1/ge; |
25 |
} |
26 |
} elsif (substr ($self->{magic}, 0, 2) eq '<?') { |
27 |
if ($self->{magic} =~ m!^<\?xml\s!) { |
28 |
$self->{content} = $source; |
29 |
$self->{type} = ['xml']; |
30 |
} else { ## <?foo/bar/baz?> |
31 |
# not defined yet |
32 |
$self->{type} = []; |
33 |
} |
34 |
} else { ## /* Type/Type/Type */ |
35 |
if ($self->{magic} =~ m!([0-9A-Za-z_.+-]+(?:/[0-9A-Za-z_.+-]+)*)\s*!) { |
36 |
$self->{type} = [split m!/!, $1]; |
37 |
} |
38 |
} |
39 |
$self; |
40 |
} |
41 |
|
42 |
sub _magic_and_content ($$) { |
43 |
my ($magic, $page) = ('', $_[1]); |
44 |
$magic = $1 if $page =~ s!^((?:\#\?|/\*|<\?)[^\x02\x0A\x0D]+)[\x02\x0A\x0D]+!!s; |
45 |
($magic, $page); |
46 |
} |
47 |
|
48 |
sub _get_converter ($%) { |
49 |
my ($self, %o) = @_; |
50 |
my $fmt = SuikaWiki::Plugin->formatter ('format'); |
51 |
for my $i (reverse 0..$#{$self->{type}}) { |
52 |
my $name = join '/', @{$self->{type}}[0..$i]; |
53 |
return $fmt->{$name . '_to_' . $o{to}} if ref $fmt->{$name . '_to_' . $o{to}}; |
54 |
} |
55 |
if ($o{from_default} && !$self->{type}->[0]) { |
56 |
return $fmt->{$o{from_default} . '_to_' . $o{to}} if ref $fmt->{$o{from_default} . '_to_' . $o{to}}; |
57 |
} |
58 |
return undef; |
59 |
} |
60 |
|
61 |
sub convert ($%) { |
62 |
my ($self, %o) = @_; |
63 |
my $converter = $self->_get_converter (%o); |
64 |
if ($converter) { |
65 |
## obsoleted parameters |
66 |
$o{from} = $self->{type}; $o{magic} = $self->{magic}; $o{content} = $self->{content}; |
67 |
return &$converter ($self, bless (\%o, 'SuikaWiki::Plugin')); |
68 |
} else { |
69 |
my $r = SuikaWiki::Markup::XML->new (namespace_uri => 'http://www.w3.org/1999/xhtml', local_name => 'pre'); |
70 |
$r->append_text ($self->{content}); |
71 |
return $r; |
72 |
} |
73 |
} |
74 |
|
75 |
=back |
76 |
|
77 |
=head1 LICENSE |
78 |
|
79 |
Copyright 2003 Wakaba <w@suika.fam.cx> |
80 |
|
81 |
This program is free software; you can redistribute it and/or |
82 |
modify it under the same terms as Perl itself. |
83 |
|
84 |
=cut |
85 |
|
86 |
1; # $Date: 2003/07/19 04:45:55 $ |