Commits

Toby Inkster committed 5b7d617

html output

  • Participants
  • Parent commits bfaae8b

Comments (0)

Files changed (2)

examples/to_html.pl

+use v5.10;
+use strict;
+use warnings;
+
+use Syntax::Highlight::RDF;
+
+my $data = do { local $/ = <DATA> };
+my $hl   = "Syntax::Highlight::RDF"->new;
+
+for my $tok (@{ $hl->tokenize(\$data) })
+{
+	print $tok->TO_HTML;
+}
+
+__DATA__
+@prefix foo: <http://example.com/foo> .
+
+<xyz>
+   foo:bar 123;
+   foo:baz "Yeah\"Baby\"Yeah".
+

lib/Syntax/Highlight/RDF.pm

 
 {
 	no strict 'refs';
-	*{Token."::TO_STRING"} = sub { sprintf "%s[%s]", $_[0]->TYPE, $_[0]->spelling };
+	*{Token."::TO_STRING"} = sub {
+		sprintf "%s[%s]", $_[0]->TYPE, $_[0]->spelling
+	};
+	*{Token."::TO_HTML"}   = sub {
+		require HTML::HTML5::Entities;
+		sprintf "<span class=\"rdf_%s\">%s</span>", lc $_[0]->TYPE, HTML::HTML5::Entities::encode_entities($_[0]->spelling)
+	};
+	*{Whitespace."::TO_HTML"}   = sub {
+		$_[0]->spelling;
+	};
 }
 
 use Moo;