Commits

Toby Inkster  committed cacce52

DWIM

  • Participants
  • Parent commits e2e6eb6

Comments (0)

Files changed (4)

File examples/model_to_html.pl

+use v5.10;
+use strict;
+use warnings;
+
+use RDF::Trine;
+use Syntax::Highlight::RDF;
+
+my $hl = "Syntax::Highlight::RDF"->highlighter("Turtle");
+warn $hl->_serializer;
+
+"RDF::Trine::Parser::Turtle"
+	-> new
+	-> parse_file_into_model(
+		"http://localhost/",
+		\*DATA,
+		(my $model = "RDF::Trine::Model"->new),
+	);
+
+say "<style type='text/css'>";
+say ".$_ { $Syntax::Highlight::RDF::STYLE{$_} }" for sort keys %Syntax::Highlight::RDF::STYLE;
+say "</style>";
+say "<pre>", $hl->highlight($model, "http://www.example.net/"), "</pre>";
+
+__DATA__
+@base <http://www.example.org/> .
+@prefix foo: <http://example.com/foo#> .
+@prefix quux: <quux#>.
+
+<xyz>
+   foo:bar 123;
+   foo:baz "Yeah\"Baby\"Yeah";
+   foo:bum quux:quuux.
+

File lib/Syntax/Highlight/JSON2.pm

 	has _remaining  => (is => 'rw');
 	
 	use IO::Detect qw( as_filehandle );
+	use Scalar::Util qw( blessed );
 		
 	sub _peek
 	{
 			: $self->_pull_token('"', Unknown);
 	}
 	
+	sub _serializer
+	{
+		require RDF::Trine::Serializer::RDFJSON;
+		return "RDF::Trine::Serializer::RDFJSON";
+	}
+	
+	sub _scalarref
+	{
+		my $self = shift;
+		my ($thing) = @_;
+		
+		if (blessed $thing and $thing->isa("RDF::Trine::Model"))
+		{
+			$thing = $thing->as_hashref;
+		}
+		
+		if (blessed $thing and $thing->isa("RDF::Trine::Iterator") and $thing->can("as_json"))
+		{
+			my $t = $thing->as_json;
+			$thing = \$t
+		}
+		
+		if (blessed $thing and $thing->isa("RDF::Trine::Iterator") and $self->can("_serializer"))
+		{
+			my $t = $self->_serializer->new->serialize_iterator_to_string($thing);
+			$thing = \$t
+		}
+		
+		if (!blessed $thing and ref $thing =~ /^(HASH|ARRAY)$/)
+		{
+			require JSON;
+			my $t = JSON::to_json($thing, { pretty => 1, canonical => 1 });
+			$thing = \$t;
+		}
+		
+		unless (ref $thing eq 'SCALAR')
+		{
+			my $fh = as_filehandle($thing);
+			local $/;
+			my $t = <$fh>;
+			$thing = \$t;
+		}
+		
+		return $thing;
+	}
+	
 	sub tokenize
 	{
 		my $self = shift;
 		ref $self or WrongInvocant->throw("this is an object method!");
 		
-		my ($text_ref) = @_;
-		$self->_remaining(
-			ref($text_ref) eq 'SCALAR'
-				? $text_ref
-				: do { local $/; my $h = as_filehandle($text_ref); \(my $t = <$h>) }
-		);
+		$self->_remaining( $self->_scalar_ref(@_) );
 		$self->_tokens([]);
 		
 		# Declare this ahead of time for use in the big elsif!
 		
 		return $self->_tokens;
 	}
-
+	
 	sub highlight
 	{
 		my $self = shift;
 		ref $self or WrongInvocant->throw("this is an object method!");
+		
 		$self->tokenize(@_);
 		return join "", map $_->TO_HTML, @{$self->_tokens};
 	}

File lib/Syntax/Highlight/RDF.pm

 use Moo;
 
 use IO::Detect qw( as_filehandle );
+use Scalar::Util qw( blessed );
 
 use constant {
 	MODE_NTRIPLES       => 0,
 		if ${$self->_remaining} =~ m/^($quote_char{3}.*?$quote_char{3})/ms;
 }
 
+sub _scalarref
+{
+	my $self = shift;
+	my ($thing) = @_;
+	
+	if (blessed $thing and $thing->isa("RDF::Trine::Model") and $self->can("_serializer"))
+	{
+		my $t = $self->_serializer->new->serialize_model_to_string($thing);
+		$thing = \$t
+	}
+	
+	if (blessed $thing and $thing->isa("RDF::Trine::Iterator") and $self->can("_serializer"))
+	{
+		my $t = $self->_serializer->new->serialize_iterator_to_string($thing);
+		$thing = \$t
+	}
+	
+	unless (ref $thing eq 'SCALAR')
+	{
+		my $fh = as_filehandle($thing);
+		local $/;
+		my $t = <$fh>;
+		$thing = \$t;
+	}
+	
+	return $thing;
+}
+
 sub tokenize
 {
 	my $self = shift;
 	ref $self or WrongInvocant->throw("this is an object method!");
 	
-	my ($text_ref) = @_;
-	$self->_remaining(
-		ref($text_ref) eq 'SCALAR'
-			? $text_ref
-			: do { local $/; my $h = as_filehandle($text_ref); \(my $t = <$h>) }
-	);
+	$self->_remaining( $self->_scalarref(@_) );
 	$self->_tokens([]);
 	
 	# Calculate these each time in case somebody wants to play with
 	use Moo;
 	extends "Syntax::Highlight::RDF";
 	use constant mode => Syntax::Highlight::RDF::MODE_NTRIPLES;
+	sub _serializer
+	{
+		require RDF::Trine::Serializer::NTriples;
+		"RDF::Trine::Serializer::NTriples";
+	}
 }
 
 {
 	extends "Syntax::Highlight::RDF";
 	use constant mode => Syntax::Highlight::RDF::MODE_NTRIPLES
 	                  |  Syntax::Highlight::RDF::MODE_TURTLE;
+	sub _serializer
+	{
+		eval { require RDF::TrineX::Serializer::MockTurtleSoup }
+			and return "RDF::TrineX::Serializer::MockTurtleSoup";
+		require RDF::Trine::Serializer::Turtle;
+		"RDF::Trine::Serializer::Turtle";
+	}
 }
 
 {
 	use constant mode => Syntax::Highlight::RDF::MODE_NTRIPLES
 	                  |  Syntax::Highlight::RDF::MODE_TURTLE
 	                  |  Syntax::Highlight::RDF::MODE_NOTATION_3;
+	sub _serializer
+	{
+		require RDF::Trine::Serializer::Notation3;
+		"RDF::Trine::Serializer::Notation3";
+	}
 }
 
 {
 	use Moo;
 	extends "Syntax::Highlight::RDF";
 	use constant mode => Syntax::Highlight::RDF::MODE_NTRIPLES;
+	sub _serializer
+	{
+		require RDF::Trine::Serializer::NQuads;
+		"RDF::Trine::Serializer::NQuads";
+	}
 }
 
 {
 Highlight some RDF.
 
 C<< $input >> may be a file handle, filename or a scalar ref of text.
-C<< $base >> is an optional base for resolving relative URIs.
+(Most highlighters will also attempt to Do What You Mean if passed something
+else sane like an RDF::Trine::Model.) C<< $base >> is an optional base for
+resolving relative URIs.
 
 Returns a string of HTML.
 

File lib/Syntax/Highlight/XML.pm

 	has _remaining  => (is => 'rw');
 	
 	use IO::Detect qw( as_filehandle );
+	use Scalar::Util qw( blessed );
 		
 	sub _peek
 	{
 		my $data = (${$self->_remaining} =~ /^(.*?)</ms) ? $1 : ${$self->_remaining};
 		$self->_pull_token($data, $data =~ /\S/ms ? Data : Data_Whitespace);
 	}
-
+	
 	sub _pull_attribute_value
 	{
 		my $self = shift;
 			remaining => ${$self->_remaining},
 		);
 	}
-
+	
+	sub _serializer
+	{
+		require RDF::Trine::Serializer::RDFXML;
+		return "RDF::Trine::Serializer::RDFXML";
+	}
+	
+	sub _scalarref
+	{
+		my $self = shift;
+		my ($thing) = @_;
+		
+		if (blessed $thing and $thing->isa("RDF::Trine::Model") and $self->can("_serializer"))
+		{
+			my $t = $self->_serializer->new->serialize_model_to_string($thing);
+			$thing = \$t
+		}
+		
+		if (blessed $thing and $thing->isa("RDF::Trine::Iterator") and $thing->can("as_xml"))
+		{
+			my $t = $thing->as_xml;
+			$thing = \$t
+		}
+		
+		if (blessed $thing and $thing->isa("RDF::Trine::Iterator") and $self->can("_serializer"))
+		{
+			my $t = $self->_serializer->new->serialize_iterator_to_string($thing);
+			$thing = \$t
+		}
+		
+		if (blessed $thing and $thing->isa("XML::LibXML::Node"))
+		{
+			my $t = $thing->toString;
+			$thing = \$t
+		}
+		
+		unless (ref $thing eq 'SCALAR')
+		{
+			my $fh = as_filehandle($thing);
+			local $/;
+			my $t = <$fh>;
+			$thing = \$t;
+		}
+		
+		return $thing;
+	}
+	
 	sub tokenize
 	{
 		my $self = shift;
 		ref $self or WrongInvocant->throw("this is an object method!");
 		
-		my ($text_ref) = @_;
-		$self->_remaining(
-			ref($text_ref) eq 'SCALAR'
-				? $text_ref
-				: do { local $/; my $h = as_filehandle($text_ref); \(my $t = <$h>) }
-		);
+		$self->_remaining( $self->_scalarref(@_) );
 		$self->_tokens([]);
-
+		
 		my ($TAG, $ATTR);
 		
 		# Declare this ahead of time for use in the big elsif!
 	}
 	
 	sub _fixup { 1 };
-
+	
 	sub highlight
 	{
 		my $self = shift;
 		ref $self or WrongInvocant->throw("this is an object method!");
+		
 		$self->tokenize(@_);
 		$self->_fixup;
 		return join "", map $_->TO_HTML, @{$self->_tokens};