Commits

Toby Inkster committed 0a8e652

rdf syntax highlighting

Comments (0)

Files changed (2)

+use TOBYINK::Pod::HTML;
+print "TOBYINK::Pod::HTML"->new(code_highlighting => 1, pretty => 1)->file_to_html(__FILE__);
+
+__END__
+
+=head1 EXAMPLES
+
+Here is some Turtle for syntax highlighting...
+
+=for highlighter language=Turtle
+
+   @prefix foaf: <http://xmlns.com/foaf/0.1/>.
+   
+   <http://tobyinkster.co.uk/#i>
+      a foaf:Person ;
+      foaf:name "Toby Inkster".
+
+And here's how you might query it using SPARQL...
+
+=for highlighter language=SPARQL
+
+   PREFIX foaf: <http://xmlns.com/foaf/0.1/>
+   SELECT ?name
+   WHERE {
+      <http://tobyinkster.co.uk/#i> foaf:name ?name.
+   }
+
+And this is the result set you might get:
+
+=for highlighter language=JSON
+
+   {
+      "head": { "vars": ["name"] },
+      "results": { 
+         "bindings": [
+            {
+               "title": { "type": "literal", "value": "Toby Inkster" }
+            }
+         ]
+      }
+   }

lib/TOBYINK/Pod/HTML.pm

 				$data .= $tok->[1] if $tok->[0] eq 'text';
 				$tok = $self->SUPER::get_token;
 			}
-			print {$self->{"output_fh"}} "<!-- for $target $data -->\n";
+			${$self->output_string} .= "<!-- for $target $data -->\n";
 			$tok = $self->SUPER::get_token;
 		}
 		
 		my ($txt, $opt) = @_;
 		
 		return $txt
-			if lc $opt->{language} eq "text";
-			
+			if $opt->{language} =~ /^(text)$/i;
+		
 		return $self->_syntax_highlighting_for_text_via_ppi(@_)
-			if lc $opt->{language} eq "perl";
+			if $opt->{language} =~ /^(perl)$/i;
+		
+		return $self->_syntax_highlighting_for_text_via_shrdf(@_)
+			if $opt->{language} =~ /^(turtle|n.?triples|n.?quads|trig|n3|notation.?3|pret|pretdsl|sparql|sparql.?(update|query)|json|xml)$/i;
 		
 		return $self->_syntax_highlighting_for_text_via_kate(@_);
 	}
 		);
 		return $hlt->html("PPI::Document"->new(\$txt));
 	}
-	
+
+	sub _syntax_highlighting_for_text_via_shrdf
+	{
+		my $self = shift;
+		my ($txt, $opt) = @_;
+		
+		require Syntax::Highlight::RDF;
+		require PPI::HTML;
+		
+		my $hlt = "Syntax::Highlight::RDF"->highlighter($opt->{language});
+		return $hlt->highlight(\$txt);
+	}
+
 	# Does not support line numbers
 	sub _syntax_highlighting_for_text_via_kate
 	{