This is an automated email from the git hooks/post-receive script. js pushed a commit to annotated tag debian/0.106-1 in repository librdf-rdfa-generator-perl.
commit 3167cf252b67336ab14c87612ed189dd229a9ce4 Author: Toby Inkster <m...@tobyinkster.co.uk> Date: Thu May 6 23:09:18 2010 +0000 Beginnings of work on pretty-printer. --HG-- branch : RDF-RDFa-Generator extra : convert_revision : svn%3A3fe8e991-6959-4966-b76d-b07eca2b6e37/RDF-RDFa-Generator%40331 --- example1-out.html | 3 + example1.css | 33 ++++++ example1.pl | 10 +- lib/RDF/RDFa/Generator/HTML/Head.pm | 58 +++++---- lib/RDF/RDFa/Generator/HTML/Pretty.pm | 217 ++++++++++++++++++++++++++++++++++ 5 files changed, 291 insertions(+), 30 deletions(-) diff --git a/example1-out.html b/example1-out.html new file mode 100644 index 0000000..0b4f5fd --- /dev/null +++ b/example1-out.html @@ -0,0 +1,3 @@ +<link rel="stylesheet" type="text/css" href="example1.css"/> + +<div xmlns="http://www.w3.org/1999/xhtml" xmlns:foaf="http://xmlns.com/foaf/0.1/" xmlns:xhv="http://www.w3.org/1999/xhtml/vocab#" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"><div about="_:rD88B06CC596111DF9444C19270FD0CE2r0"><h3 class="blank">_:rD88B06CC596111DF9444C19270FD0CE2r0</h3><span class="rdf-type" rel="rdf:type"><img about="http://xmlns.com/foaf/0.1/Person" alt="http://xmlns.com/foaf/0.1/Person" src="data:image/png;charset=binary;base64,iVBORw0KGgoAAAANSUhEUgAAACAAAA [...] diff --git a/example1.css b/example1.css new file mode 100644 index 0000000..fe9c401 --- /dev/null +++ b/example1.css @@ -0,0 +1,33 @@ +/* <link rel="stylesheet" type="text/css" href="example1.css"/> */ + +div > div +{ + background: #fd9; + border: 1px solid #db7; + margin: 1em; + padding: 0.67em; +} + +div > div h3 +{ + margin: 0; + padding: 0; +} + +div > div span.rdf-type +{ + float: right; + text-align: right; + width: 40px; + position: relative; + top: -1.67em; +} + +div > div dl dt +{ + margin: 0.5em 0 0; + padding: 0; + font-weight: bold; + font-style: italic; + font-size: smaller; +} diff --git a/example1.pl b/example1.pl index d1086fa..9d6554c 100644 --- a/example1.pl +++ b/example1.pl @@ -1,11 +1,12 @@ use lib "lib"; use RDF::TrineShortcuts; -use RDF::RDFa::Generator::HTML::Hidden; +use RDF::RDFa::Generator::HTML::Pretty; my $graph = rdf_parse(<<TURTLE, type=>'turtle'); \@prefix foaf: <http://xmlns.com/foaf/0.1/> . +\@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> . <http://example.net/> @@ -16,13 +17,14 @@ my $graph = rdf_parse(<<TURTLE, type=>'turtle'); a foaf:Person ; foaf:name "Joe Bloggs" ; foaf:plan "To conquer the world!"\@en - ] . + ] ; + foaf:segment "Hello <b xmlns='http://www.w3.org/1999/xhtml'>World</b>"^^rdf:XMLLiteral . TURTLE -my $gen = RDF::RDFa::Generator::HTML::Hidden->new(base=>'http://example.net/'); +my $gen = RDF::RDFa::Generator::HTML::Pretty->new(base=>'http://example.net/'); foreach my $n ($gen->nodes($graph)) { print $n->toString . "\n"; -} \ No newline at end of file +} diff --git a/lib/RDF/RDFa/Generator/HTML/Head.pm b/lib/RDF/RDFa/Generator/HTML/Head.pm index 9ac29a3..9694e4d 100644 --- a/lib/RDF/RDFa/Generator/HTML/Head.pm +++ b/lib/RDF/RDFa/Generator/HTML/Head.pm @@ -131,19 +131,8 @@ sub _process_predicate return $self; } - my ($uri_prefix, $uri_suffix) = ($st->predicate->uri =~ m'^(.+?)([A-Za-z0-9_-]*)$'); - if (length $uri_suffix and !length $uri_prefix) - { - ($uri_prefix, $uri_suffix) = ($uri_suffix, $uri_prefix); - } - - unless (defined $prefixes->{$uri_prefix}) - { - $prefixes->{$uri_prefix} = $self->{ns}->{$uri_prefix} || 'ns' . (1 + scalar keys %$prefixes); - } - my $curie_prefix = $prefixes->{$uri_prefix}; - - $node->setAttribute($attr, sprintf('%s:%s', $curie_prefix, $uri_suffix)); + $node->setAttribute($attr, + $self->_make_curie($st->predicate->uri, $prefixes)); return $self; } @@ -182,19 +171,8 @@ sub _process_object if (defined $st->object->literal_datatype) { - my ($uri_prefix, $uri_suffix) = ($st->object->literal_datatype =~ m'^(.+?)([A-Za-z0-9_-]*)$'); - if (length $uri_suffix and !length $uri_prefix) - { - ($uri_prefix, $uri_suffix) = ($uri_suffix, $uri_prefix); - } - - unless (defined $prefixes->{$uri_prefix}) - { - $prefixes->{$uri_prefix} = $self->{ns}->{$uri_prefix} || 'ns' . (1 + scalar keys %$prefixes); - } - my $curie_prefix = $prefixes->{$uri_prefix}; - - $node->setAttribute('datatype', sprintf('%s:%s', $curie_prefix, $uri_suffix)); + $node->setAttribute('datatype', + $self->_make_curie($st->object->literal_datatype, $prefixes)); } else { @@ -204,16 +182,40 @@ sub _process_object return $self; } +sub _make_curie +{ + my ($self, $uri, $prefixes) = @_; + + my ($uri_prefix, $uri_suffix) = ($uri =~ m'^(.+?)([A-Za-z0-9_-]*)$'); + if (length $uri_suffix and !length $uri_prefix) + { + ($uri_prefix, $uri_suffix) = ($uri_suffix, $uri_prefix); + } + + unless (defined $prefixes->{$uri_prefix}) + { + $prefixes->{$uri_prefix} = $self->{ns}->{$uri_prefix} || 'ns' . (1 + scalar keys %$prefixes); + } + my $curie_prefix = $prefixes->{$uri_prefix}; + + return sprintf('%s:%s', $curie_prefix, $uri_suffix); +} + 1; __DATA__ +bibo http://purl.org/ontology/bibo/ cc http://creativecommons.org/ns# ctag http://commontag.org/ns# +dbp http://dbpedia.org/property/ dc http://purl.org/dc/terms/ doap http://usefulinc.com/ns/doap# +fb http://developers.facebook.com/schema/ foaf http://xmlns.com/foaf/0.1/ geo http://www.w3.org/2003/01/geo/wgs84_pos# gr http://purl.org/goodrelations/v1# +ical http://www.w3.org/2002/12/cal/ical# +og http://opengraphprotocol.org/schema/ owl http://www.w3.org/2002/07/owl# rdf http://www.w3.org/1999/02/22-rdf-syntax-ns# rdfa http://www.w3.org/ns/rdfa# @@ -224,6 +226,10 @@ rss http://purl.org/rss/1.0/ sioc http://rdfs.org/sioc/ns# skos http://www.w3.org/2004/02/skos/core# tag http://www.holygoat.co.uk/owl/redwood/0.1/tags/ +v http://rdf.data-vocabulary.org/# +vann http://purl.org/vocab/vann/ +vcard http://www.w3.org/2006/vcard/ns# +void http://rdfs.org/ns/void# xfn http://vocab.sindice.com/xfn# xhv http://www.w3.org/1999/xhtml/vocab# xsd http://www.w3.org/2001/XMLSchema# diff --git a/lib/RDF/RDFa/Generator/HTML/Pretty.pm b/lib/RDF/RDFa/Generator/HTML/Pretty.pm index e69de29..37b49e9 100644 --- a/lib/RDF/RDFa/Generator/HTML/Pretty.pm +++ b/lib/RDF/RDFa/Generator/HTML/Pretty.pm @@ -0,0 +1,217 @@ +package RDF::RDFa::Generator::HTML::Pretty; + +use 5.008; +use base qw'RDF::RDFa::Generator::HTML::Hidden'; +use common::sense; +use constant XHTML_NS => 'http://www.w3.org/1999/xhtml'; +use XML::LibXML qw':all'; + +sub nodes +{ + my ($proto, $model) = @_; + my $self = (ref $proto) ? $proto : $proto->new; + + my $stream = $self->_get_stream($model); + my @nodes; + + my $root_node = XML::LibXML::Element->new('div'); + $root_node->setNamespace(XHTML_NS, undef, 1); + + my $prefixes = {}; + my $subjects = {}; + while (my $st = $stream->next) + { + my $s = $st->subject->is_resource ? + $st->subject->uri : + ('_:'.$st->subject->blank_identifier); + push @{ $subjects->{$s} }, $st; + } + + foreach my $s (keys %$subjects) + { + my $subject_node = $root_node->addNewChild(XHTML_NS, 'div'); + + $self->_process_subject($subjects->{$s}->[0], $subject_node, $prefixes); + $self->_resource_heading($subjects->{$s}->[0]->subject, $subject_node, $subjects->{$s}, $prefixes); + $self->_resource_classes($subjects->{$s}->[0]->subject, $subject_node, $subjects->{$s}, $prefixes); + $self->_resource_statements($subjects->{$s}->[0]->subject, $subject_node, $subjects->{$s}, $prefixes); + ## TODO Query $model for statements that act as special notes for the subject (in a separate graph) + #$self->_resource_notes($subjects->{$s}->[0]->subject, $subject_node, $model); + } + + if ($self->{'version'} == 1.1 + and $self->{'prefix_attr'}) + { + my $prefix_string = ''; + while (my ($u,$p) = each(%$prefixes)) + { + $prefix_string .= sprintf("%s: %s ", $p, $u); + } + if (length $prefix_string) + { + $root_node->setAttribute('prefix', $prefix_string); + } + } + else + { + while (my ($u,$p) = each(%$prefixes)) + { + $root_node->setNamespace($u, $p, 0); + } + } + + push @nodes, $root_node; + return @nodes if wantarray; + my $nodelist = XML::LibXML::NodeList->new; + $nodelist->push(@nodes); + return $nodelist; +} + +sub _resource_heading +{ + my ($self, $subject, $node, $statements, $prefixes) = @_; + + my $heading = $node->addNewChild(XHTML_NS, 'h3'); + $heading->appendTextNode( $subject->is_resource ? $subject->uri : ('_:'.$subject->blank_identifier) ); + $heading->setAttribute('class', $subject->is_resource ? 'resource' : 'blank' ); + + return $self; +} + +## TODO +## <span rel="rdf:type"><img about="[foaf:Person]" src="fsfwfwfr.png" +## title="http://xmlns.com/foaf/0.1/Person" /></span> + +sub _resource_classes +{ + my ($self, $subject, $node, $statements, $prefixes) = @_; + + my @statements = sort { + $a->predicate->uri cmp $b->predicate->uri + or $a->object->uri cmp $b->object->uri + } + grep { + $_->predicate->uri eq 'http://www.w3.org/1999/02/22-rdf-syntax-ns#type' + and $_->object->is_resource + } + @$statements; + + return unless @statements; + + my $SPAN = $node->addNewChild(XHTML_NS, 'span'); + $SPAN->setAttribute('class', 'rdf-type'); + $SPAN->setAttribute('rel', $self->_make_curie('http://www.w3.org/1999/02/22-rdf-syntax-ns#type', $prefixes)); + + foreach my $st (@statements) + { + my $IMG = $SPAN->addNewChild(XHTML_NS, 'img'); + $IMG->setAttribute('about', $st->object->uri); + $IMG->setAttribute('alt', $st->object->uri); + $IMG->setAttribute('src', $self->_img($st->object->uri)); + $IMG->setAttribute('title', $st->object->uri); + } + + return $self; +} + + +sub _resource_statements +{ + my ($self, $subject, $node, $statements, $prefixes) = @_; + + my @statements = sort { + $a->predicate->uri cmp $b->predicate->uri + or $a->object->uri cmp $b->object->uri + } + grep { + $_->predicate->uri ne 'http://www.w3.org/1999/02/22-rdf-syntax-ns#type' + or !$_->object->is_resource + } + @$statements; + + return unless @statements; + + my $DL = $node->addNewChild(XHTML_NS, 'dl'); + + my $current_property = undef; + foreach my $st (@statements) + { + unless ($st->predicate->uri eq $current_property) + { + my $DT = $DL->addNewChild(XHTML_NS, 'dt'); + $DT->setAttribute('title', $st->predicate->uri); + $DT->appendTextNode($self->_make_curie($st->predicate->uri, $prefixes)); + } + + my $DD = $DL->addNewChild(XHTML_NS, 'dd'); + + if ($st->object->is_resource) + { + $DD->setAttribute('rel', $self->_make_curie($st->predicate->uri, $prefixes)); + $DD->setAttribute('class', 'resource'); + + my $A = $DD->addNewChild(XHTML_NS, 'a'); + $A->setAttribute('href', $st->object->uri); + $A->appendTextNode($st->object->uri); + } + elsif ($st->object->is_blank) + { + $DD->setAttribute('rel', $self->_make_curie($st->predicate->uri, $prefixes)); + $DD->setAttribute('class', 'blank'); + + my $A = $DD->addNewChild(XHTML_NS, 'span'); + $A->setAttribute('about', '_:'.$st->object->blank_identifier); + $A->appendTextNode('_:'.$st->object->blank_identifier); + } + elsif ($st->object->is_literal + && !$st->object->has_datatype) + { + $DD->setAttribute('property', $self->_make_curie($st->predicate->uri, $prefixes)); + $DD->setAttribute('class', 'plain-literal'); + $DD->setAttribute('xml:lang', $st->object->literal_value_language); + $DD->appendTextNode($st->object->literal_value); + } + elsif ($st->object->is_literal + && $st->object->has_datatype + && $st->object->literal_datatype eq 'http://www.w3.org/1999/02/22-rdf-syntax-ns#XMLLiteral') + { + $DD->setAttribute('property', $self->_make_curie($st->predicate->uri, $prefixes)); + $DD->setAttribute('class', 'typed-literal datatype-xmlliteral'); + $DD->setAttribute('datatype', $self->_make_curie($st->object->literal_datatype, $prefixes)); + $DD->appendWellBalancedChunk($st->object->literal_value); + } + elsif ($st->object->is_literal + && $st->object->has_datatype) + { + $DD->setAttribute('property', $self->_make_curie($st->predicate->uri, $prefixes)); + $DD->setAttribute('class', 'typed-literal'); + $DD->setAttribute('datatype', $self->_make_curie($st->object->literal_datatype, $prefixes)); + $DD->appendTextNode($st->object->literal_value); + } + } + + return $self; +} + +sub _img +{ + my ($self, $type) = @_; + + my $icons = { + 'http://xmlns.com/foaf/0.1/Document' => 'data:image/png;charset=binary;base64,iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAYAAABzenr0AAAABHNCSVQICAgIfAhkiAAAAz1JREFUWIXll0FPG0cUx38zuzgmllM7TQhRFFVVghGRkIBYqJz6HfoFyIEb36HXHnPiGIlP0I/QWy6oQkKqBAdXTghxD03BwnaK1zs7OeBZz65n2TVKT32StTPjnfd+83/PM2P4v5tID+zt7f3k+/4+UPU8DyklURShlIo/YRjGT7t90xjQC8Pw1f7+/q92PH+KSIg3Ozs71XGbMAzxfR+tdeI9u1+wXd3d3X0D3AyglKoBtNtthBAopahUKgRBYAARYiKc3Xb1DcjCwgJBENTS300BaK3jIMZZuu16ZgXPsymAKIoSQe1ArVYr/r5IUM/zaDQaicUVAjAqpEGWlpb+ [...] + 'http://xmlns.com/foaf/0.1/Person' => 'data:image/png;charset=binary;base64,iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAYAAABzenr0AAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAAN1wAADdcBQiibeAAAABl0RVh0U29mdHdhcmUAd3d3Lmlua3NjYXBlLm9yZ5vuPBoAAAakSURBVFiFrZZbbBTXGcf/58yZy+7Mrr2brC9rm0tck4CqSoVElUwAG3pRH9oktJVaqVKf+tI8RLRYlqxKqGlj5K4VUbVJFXp56UMTUERV5YFLEnDADWmJCm1MSowNwsaXxWa9u97rnEsfdme9i80S4hzp04xmdr//73zznfM/RCmFhxlDsUEHwPcM3dhDCNnJBY8yxq4LIc9z7o4AONHfN8A/bT7yMABDscG9jOl/jUZbnQ0dG/1NkWYEgw4SiSTu3Imr8Ynx [...] + 'http://xmlns.com/foaf/0.1/Group' => 'data:image/png;charset=binary;base64,iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAYAAABzenr0AAAABmJLR0QA/wD/AP+gvaeTAAAAB3RJTUUH1gsKFTktQWg0swAAB8FJREFUWIXlV1tsFNcZ/mbO3HZ2d/bitfGFi7HXDuZaSAqikDYIVUFCabi4BEopD3nrG1JKGlAqiwhh16h+qPrQVkrVh7RVUiyoooBKKJcQATFp7OILF8uG2JBiK7az6/XO5Vz6MLtjY7kEqvapR/o1u2eOzvnO93/n+88A/+9N+roBbW1tcY/n9yiKuplSWgYAiqKMUOqdU+XQHw8cODDxPwPQ0nJ0k6Krb659bq1aX7dEsawYACCbzeDOndv0ascVjzreW6+/fvj8fx3A8ePNG81I5MiObTtD4XAEsgzIsgJZlsE5B8CRzU7i [...] + 'http://www.w3.org/2003/01/geo/wgs84_pos#SpatialThing' => 'data:image/png;charset=binary;base64,iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAYAAABzenr0AAAABmJLR0QA/wD/AP+gvaeTAAAAB3RJTUUH1ggCFigiAlzmVAAACOFJREFUWIXNl1mMHNUVhr97q6qre7qnZx/PGC+z2YxNMIY4xg5LBBgTFsdRgEBAWV4iImV5iBSx5MUgBZCQEFFQeIyIgiwZJTJxLLM4RpYMNvFgG8cGj2c8mz3Ts3ePp5fqqntv5aF72h6zZFGk5EhHVVLfqv+v//zn3L7wPw7xry5cum1HVURH77Rt+aCQ8hpM2KyNqbWkzCDFZGjMaaXM677lvTO2Z0f+v0agY9vzKyzk84bwm11trcGGL7VVtzYlRU0iRlUsQr7gM5ctkJq6GPacGprvH0o5ErFbY5 [...] + '?' => 'data:image/png;charset=binary;base64,iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAYAAABzenr0AAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAAN1wAADdcBQiibeAAAABl0RVh0U29mdHdhcmUAd3d3Lmlua3NjYXBlLm9yZ5vuPBoAAAg8SURBVFiFxZZpkFTVFcd/973u9153z0zP0M4is4DsOsoIErZYbiVIQFQUEZcKlAtlkiIRNfFDPlgVKxWTEsWlQpmgMZW4oIIaFQrEpeLIIoLIZHCAYVicGWbvmen1vX7v3XzoxW6GqLFSlVt16t537nL+53/POfcJKSX/z+b5PpsWPi1KDJ9W7Uq3TCpqjz5sdry6Ria+z1niuzCw7DWhmmF9gdDErcKRi0v0cl9lWY0V9FfKoViv0j14SotafX0uzmvSka+8 [...] + }; + + my $equiv = { + 'http://xmlns.com/foaf/0.1/PersonalProfileDocument' => 'http://xmlns.com/foaf/0.1/Document' , + 'http://www.w3.org/2003/01/geo/wgs84_pos#Point' => 'http://www.w3.org/2003/01/geo/wgs84_pos#SpatialThing' , + 'http://www.w3.org/2006/vcard/ns#Location' => 'http://www.w3.org/2003/01/geo/wgs84_pos#SpatialThing' , + }; + + return $icons->{$type} || $icons->{ $equiv->{$type} } || $icons->{'?'}; +} + +1; -- Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-perl/packages/librdf-rdfa-generator-perl.git _______________________________________________ Pkg-perl-cvs-commits mailing list Pkg-perl-cvs-commits@lists.alioth.debian.org http://lists.alioth.debian.org/cgi-bin/mailman/listinfo/pkg-perl-cvs-commits