Commits

Toby Inkster  committed 17fb48e Draft

split some functionality out from R:R:G:D:S into a role that can be reused by other R:R:Gs.

  • Participants
  • Parent commits b50ddb5

Comments (0)

Files changed (2)

File lib/Demiblog4/RDFa/ResourceOutput.pm

+package Demiblog4::RDFa::ResourceOutput;
+
+use Demiblog4::Sugar
+	-types => -all,
+	-moose => -role;
+
+use DateTimeX::Auto qw(dt);
+use Demiblog4::Formatter::Demiblog3Textile;
+use Demiblog4::Formatter::HTML;
+use Demiblog4::Formatter::Markdown;
+
+use constant XHTML => 'http://www.w3.org/1999/xhtml';
+
+has entitler => (
+	is         => read_only,
+	isa        => Object,
+	required   => true,
+	handles    => qwh{ _get_title title },
+);
+
+has heading_level => (
+	is         => read_only,
+	isa        => Num,
+	required   => true,
+	default    => 1,
+);
+
+has strftime_pattern => (
+	is         => read_only,
+	isa        => Str,
+	lazy_build => true,
+);
+
+method _build_strftime_pattern
+{
+	# Tue, 14 Aug 2012, 07:40:00
+	'%a, %e %b %Y, %T'
+}
+
+method _get_formatter ($st)
+{
+	if ($st->predicate->uri eq 'http://ontologi.es/demiblog#textile_body')
+		{ return Demiblog4::Formatter::Demiblog3Textile::->new }
+	elsif ($st->predicate->uri eq 'http://ontologi.es/demiblog#markdown_body')
+		{ return Demiblog4::Formatter::Markdown::->new }
+	elsif ($st->predicate->uri eq 'http://purl.org/rss/1.0/modules/content/encoded')
+		{ return Demiblog4::Formatter::HTML::->new }
+	
+	return;
+}
+
+method _table_row ($st, $prefixes, $primary_subject)
+{
+	my $tr = XML::LibXML::Element::->new('tr');
+	my $th = XML::LibXML::Element::->new('th');
+	my $td = XML::LibXML::Element::->new('td');
+	$_->setNamespace(XHTML, '', true) for ($tr, $th, $td);
+	$tr->appendChild($_) for ($th, $td);
+	
+	my $curie = $prefixes->get_curie( $st->predicate->uri );
+	
+	if ($st->subject->is_resource and $st->subject->uri eq $primary_subject)
+	{
+		$th->appendText($curie);
+		$self->_table_cell($td, $prefixes, $st);
+	}
+	else
+	{
+		$th->appendText("is $curie of");
+		my $rev = RDF::Trine::Statement::->new(
+			$st->object,
+			$st->predicate,
+			$st->subject,
+		);
+		$self->_table_cell($td, $prefixes, $rev);
+		$td->{rev} = delete $td->{rel};
+	}
+	
+	return $tr;
+}
+
+method _table_cell ($elem, $prefixes, $st)
+{
+	$self->_process_predicate($st, $elem, $prefixes);
+	$self->_process_object($st, $elem, $prefixes);
+	
+	my $obj = $st->object;
+	if ($obj->is_literal)
+	{
+		my $success;
+		if ($st->object->literal_datatype eq 'http://www.w3.org/2001/XMLSchema#dateTime')
+		{
+			eval {
+				my $dt = dt($st->object->literal_value)->strftime($self->strftime_pattern);
+				my $t = $elem->addNewChild(XHTML, 'time');
+				$t->{datetime} = $st->object->literal_value;
+				$t->appendText($dt);
+				$success++;
+			};
+		}
+		
+		unless ($success)
+		{
+			my $s = $elem->addNewChild(XHTML, 'span');
+			$s->appendText($st->object->literal_value);
+		}
+		
+		if ($obj->has_language)
+		{
+			$elem->appendText(q[ ]);
+			my $lang = $elem->addNewChild(XHTML, 'small');
+			$lang->{class} = 'language';
+			$lang->appendText( $obj->literal_value_language );
+		}
+		elsif ($obj->has_datatype)
+		{
+			$elem->appendText(q[ ]);
+			my $dt = $elem->addNewChild(XHTML, 'small');
+			$dt->{class} = 'datatype';
+			$dt->appendText( $prefixes->get_curie($obj->literal_datatype) );
+		}
+	}
+	
+	elsif ($obj->is_resource)
+	{
+		my $anchor = $elem->addNewChild(XHTML, 'a');
+		$anchor->{href} = $obj->uri;
+		$anchor->appendText($self->_get_title($obj) || $obj->uri);
+	}
+
+	else
+	{
+		my $label = $elem->addNewChild(XHTML, 'span');
+		$label->appendText($self->_get_title($obj) || $obj->as_ntriples);
+	}
+}
+
+method resource_output ($input, $primary_subject)
+{
+	my $table_prefixes   = RDF::Prefixes->new($self->{namespaces});
+	my $heading_prefixes = RDF::Prefixes->new($self->{namespaces});
+	
+	my $article = XML::LibXML::Element::->new('section');
+	my $header  = XML::LibXML::Element::->new('header');
+	my $heading = XML::LibXML::Element::->new('h' . $self->heading_level);
+	my $table   = XML::LibXML::Element::->new('table');
+	my $div_uri = XML::LibXML::Element::->new('div');
+	$_->setNamespace(XHTML, '', true) for ($heading, $header, $article, $table);
+
+	my $literal_for_heading = RDF::Trine::Node::Literal::->new(
+		$self->_get_title(
+			RDF::Trine::Node::Resource::->new($primary_subject),
+		),
+	);
+	$article->{class} = 'resource';
+	$article->appendChild($header);
+	$header->appendChild($heading);
+	$header->appendChild($div_uri);
+	$div_uri->appendText($primary_subject);
+	$div_uri->{class} = 'article_uri';
+	$heading->appendText($literal_for_heading->literal_value);
+	$heading->{'xml:lang'} = $heading->{'lang'} = $literal_for_heading->literal_value_language
+		if $literal_for_heading->has_language;
+	$heading->{'datatype'} = $heading_prefixes->get_curie($literal_for_heading->literal_datatype)
+		if $literal_for_heading->has_datatype;
+	
+	my @rows;
+	my $iter = $input->get_statements((undef)x4);
+	while (my $st = $iter->next)
+	{
+		my $fmt = $self->_get_formatter($st);
+		
+		if ($st->predicate->uri eq 'http://purl.org/dc/terms/language')
+		{
+			if ($st->object->is_resource and $st->object->uri =~ m{^http://www\.lingvoj\.org/lang/(\S+)$})
+			{
+				$article->{'xml:lang'} = $article->{'lang'} = $1;
+			}
+			elsif ($st->object->is_literal)
+			{
+				$article->{'xml:lang'} = $article->{'lang'} = $st->object->literal_value;
+			}
+		}
+		
+		if ($st->object->equal( $literal_for_heading ))
+		{
+			$heading->{property} = join q[ ], grep defined, (
+				$heading->{property},
+				$heading_prefixes->get_curie($st->predicate->uri),
+			);
+			next;
+		}
+		
+		if ($fmt)
+		{
+			my $content_element = $article->addNewChild(XHTML, 'div');
+			my $content_chunk   = $fmt->to_dom($st->object->literal_value, 1);
+			$content_element->appendChild($_) for $content_chunk->childNodes;
+			$content_element->{class} = 'content';
+			$article->setNodeName('article');
+			next;
+		}
+		
+		push @rows, [
+			$st->predicate->uri,
+			$self->_table_row($st, $table_prefixes, $primary_subject),
+		];
+	}
+	
+	$table->{about} = $heading->{about} = $primary_subject;
+	$table->appendChild($_->[1]) for sort { $a->[0] cmp $b->[0] } @rows;
+
+	use Data::Dumper; Dumper($table_prefixes, $heading_prefixes); # i know
+	while (my ($u,$p) = each %$table_prefixes)
+		{ $table->setNamespace($p, $u, false) }
+	while (my ($u,$p) = each %$heading_prefixes)
+		{ $heading->setNamespace($p, $u, false) }
+	
+	$article->appendChild($table);
+	return $article;
+}
+

File lib/RDF/RDFa/Generator/Demiblog4/Standard.pm

 	-types => -all;
 use MooseX::NonMoose;
 extends 'RDF::RDFa::Generator::HTML::Hidden';
-
-use DateTimeX::Auto qw(dt);
-use Demiblog4::Formatter::Demiblog3Textile;
-use Demiblog4::Formatter::HTML;
-use Demiblog4::Formatter::Markdown;
-
-use constant XHTML => 'http://www.w3.org/1999/xhtml';
+with 'Demiblog4::RDFa::ResourceOutput';
 
 # vaguely interesting inherited attributes
 has qwa[ base namespaces title ] => (is => read_only);
 	required   => true,
 );
 
-has entitler => (
-	is         => read_only,
-	isa        => Object,
-	required   => true,
-	handles    => qwh{
-		_get_title   title
-	},
-);
-
-has heading_level => (
-	is         => read_only,
-	isa        => Num,
-	required   => true,
-	default    => 1,
-);
-
-has strftime_pattern => (
-	is         => read_only,
-	isa        => Str,
-	lazy_build => true,
-);
-
-method _build_strftime_pattern
-{
-	# Tue, 14 Aug 2012, 07:40:00
-	'%a, %e %b %Y, %T'
-}
-
 method nodes ($input)
 {
 	my ($primary_data, $secondary_data) = $self->_split_data($input);
 	
 	(
-		$self->_primary_nodes($primary_data),
+		$self->resource_output($primary_data, $self->primary_subject),
 		$self->SUPER::nodes($secondary_data),
 	)
 }
 
-method _get_formatter ($st)
-{
-	if ($st->predicate->uri eq 'http://ontologi.es/demiblog#textile_body')
-		{ return Demiblog4::Formatter::Demiblog3Textile::->new }
-	elsif ($st->predicate->uri eq 'http://ontologi.es/demiblog#markdown_body')
-		{ return Demiblog4::Formatter::Markdown::->new }
-	elsif ($st->predicate->uri eq 'http://purl.org/rss/1.0/modules/content/encoded')
-		{ return Demiblog4::Formatter::HTML::->new }
-	
-	return;
-}
-
-method _primary_nodes ($input)
-{
-	my $table_prefixes   = RDF::Prefixes->new($self->{namespaces});
-	my $heading_prefixes = RDF::Prefixes->new($self->{namespaces});
-	
-	my $article = XML::LibXML::Element::->new('section');
-	my $header  = XML::LibXML::Element::->new('header');
-	my $heading = XML::LibXML::Element::->new('h' . $self->heading_level);
-	my $table   = XML::LibXML::Element::->new('table');
-	my $div_uri = XML::LibXML::Element::->new('div');
-	$_->setNamespace(XHTML, '', true) for ($heading, $header, $article, $table);
-
-	my $literal_for_heading = RDF::Trine::Node::Literal::->new(
-		$self->_get_title(
-			RDF::Trine::Node::Resource::->new($self->primary_subject),
-		),
-	);
-	$article->{class} = 'resource';
-	$article->appendChild($header);
-	$header->appendChild($heading);
-	$header->appendChild($div_uri);
-	$div_uri->appendText($self->primary_subject);
-	$div_uri->{class} = 'article_uri';
-	$heading->appendText($literal_for_heading->literal_value);
-	$heading->{'xml:lang'} = $heading->{'lang'} = $literal_for_heading->literal_value_language
-		if $literal_for_heading->has_language;
-	$heading->{'datatype'} = $heading_prefixes->get_curie($literal_for_heading->literal_datatype)
-		if $literal_for_heading->has_datatype;
-	
-	my @rows;
-	my $iter = $input->get_statements((undef)x4);
-	while (my $st = $iter->next)
-	{
-		my $fmt = $self->_get_formatter($st);
-		
-		if ($st->predicate->uri eq 'http://purl.org/dc/terms/language')
-		{
-			if ($st->object->is_resource and $st->object->uri =~ m{^http://www\.lingvoj\.org/lang/(\S+)$})
-			{
-				$article->{'xml:lang'} = $article->{'lang'} = $1;
-			}
-			elsif ($st->object->is_literal)
-			{
-				$article->{'xml:lang'} = $article->{'lang'} = $st->object->literal_value;
-			}
-		}
-		
-		if ($st->object->equal( $literal_for_heading ))
-		{
-			$heading->{property} = join q[ ], grep defined, (
-				$heading->{property},
-				$heading_prefixes->get_curie($st->predicate->uri),
-			);
-			next;
-		}
-		
-		if ($fmt)
-		{
-			my $content_element = $article->addNewChild(XHTML, 'div');
-			my $content_chunk   = $fmt->to_dom($st->object->literal_value, 1);
-			$content_element->appendChild($_) for $content_chunk->childNodes;
-			$content_element->{class} = 'content';
-			$article->setNodeName('article');
-			next;
-		}
-		
-		push @rows, [
-			$st->predicate->uri,
-			$self->_table_row($st, $table_prefixes),
-		];
-	}
-	
-	$table->{about} = $heading->{about} = $self->primary_subject;
-	$table->appendChild($_->[1]) for sort { $a->[0] cmp $b->[0] } @rows;
-
-	use Data::Dumper; Dumper($table_prefixes, $heading_prefixes); # i know
-	while (my ($u,$p) = each %$table_prefixes)
-		{ $table->setNamespace($p, $u, false) }
-	while (my ($u,$p) = each %$heading_prefixes)
-		{ $heading->setNamespace($p, $u, false) }
-	
-	$article->appendChild($table);
-	return $article;
-}
-
-method _table_row ($st, $prefixes)
-{
-	my $tr = XML::LibXML::Element::->new('tr');
-	my $th = XML::LibXML::Element::->new('th');
-	my $td = XML::LibXML::Element::->new('td');
-	$_->setNamespace(XHTML, '', true) for ($tr, $th, $td);
-	$tr->appendChild($_) for ($th, $td);
-	
-	my $curie = $prefixes->get_curie( $st->predicate->uri );
-	
-	if ($st->subject->is_resource and $st->subject->uri eq $self->primary_subject)
-	{
-		$th->appendText($curie);
-		$self->_table_cell($td, $prefixes, $st);
-	}
-	else
-	{
-		$th->appendText("is $curie of");
-		my $rev = RDF::Trine::Statement::->new(
-			$st->object,
-			$st->predicate,
-			$st->subject,
-		);
-		$self->_table_cell($td, $prefixes, $rev);
-		$td->{rev} = delete $td->{rel};
-	}
-	
-	return $tr;
-}
-
-method _table_cell ($elem, $prefixes, $st)
-{
-	$self->_process_predicate($st, $elem, $prefixes);
-	$self->_process_object($st, $elem, $prefixes);
-	
-	my $obj = $st->object;
-	if ($obj->is_literal)
-	{
-		my $success;
-		if ($st->object->literal_datatype eq 'http://www.w3.org/2001/XMLSchema#dateTime')
-		{
-			eval {
-				my $dt = dt($st->object->literal_value)->strftime($self->strftime_pattern);
-				my $t = $elem->addNewChild(XHTML, 'time');
-				$t->{datetime} = $st->object->literal_value;
-				$t->appendText($dt);
-				$success++;
-			};
-		}
-		
-		unless ($success)
-		{
-			my $s = $elem->addNewChild(XHTML, 'span');
-			$s->appendText($st->object->literal_value);
-		}
-		
-		if ($obj->has_language)
-		{
-			$elem->appendText(q[ ]);
-			my $lang = $elem->addNewChild(XHTML, 'small');
-			$lang->{class} = 'language';
-			$lang->appendText( $obj->literal_value_language );
-		}
-		elsif ($obj->has_datatype)
-		{
-			$elem->appendText(q[ ]);
-			my $dt = $elem->addNewChild(XHTML, 'small');
-			$dt->{class} = 'datatype';
-			$dt->appendText( $prefixes->get_curie($obj->literal_datatype) );
-		}
-	}
-	
-	elsif ($obj->is_resource)
-	{
-		my $anchor = $elem->addNewChild(XHTML, 'a');
-		$anchor->{href} = $obj->uri;
-		$anchor->appendText($self->_get_title($obj) || $obj->uri);
-	}
-
-	else
-	{
-		my $label = $elem->addNewChild(XHTML, 'span');
-		$label->appendText($self->_get_title($obj) || $obj->as_ntriples);
-	}
-}
-
 method _split_data ($input)
 {
 	my $primary   = RDF::Trine::Model::->new;
 	($primary, $secondary)
 }
 
-1;