html' },
'ics' => { content_type => 'text/calendar', layout => 0 },
'json' => { content_type => 'application/json', layout => 0 },
'mp' => { content_type => 'text/html' },
'ram' =>
handler-coderef is optional. For
example:
respond_to('html', 'txt', 'xml' => \&xml_handler, 'json');
Once a format is selected (TODO, document this) the handler is then invoked;
if no handler wa
~ /^(xml|atom|rss)$/) {
return $self->to_xml();
} elsif ($format eq 'json') {
return $self->to_json();
} elsif ($format eq 'yaml') {
return $self->to_yaml();
} elsi
html();
} else {
croak "Unsupported format";
}
}
sub to_json
{
my ($self) = @_;
return '{' . _to_json(@{$self->{raw}}) . '}';
}
sub to_yaml
{
my ($self) = @_;
retur
{
my ($self) = @_;
return "<?xml version=\"1.0\"?>\n" . _to_xml(@{$self->{raw}});
}
## JSON with HTML syntax highlighting
sub to_html
{
my ($self) = @_;
return "<!DOCTYPE html PUBL
as JSON:
{"br":null}
{"body":{"class":"episode"}}
{"strong":"important content"}
{"a":{"href":"/foo","$":"Link to foo"}}
Note the last two lines. If a tag contains attributes, the JSON val
ue
will be a hash containing those attributes. If a tag doesn't contain
attributes, the JSON value will be either null or a string. If a tag
contains both attributes and a string value, the string w
is converted to a "$" in the JSON/YAML
serialisations, eg:
TAG("po:microsite", "rdf:resource" => "/foo");
As XML:
<po:microsite rdf:resource="/foo"/>
And as JSON:
{"po$microsite":{"rdf$res