summary refs log tree commit diff stats
path: root/plugins/perl/syntax_highlight
blob: 96d570036c3e80ee88fa697e64380b12d7f073f0 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#!/usr/bin/env perl
use 5.010;
use strict;
use warnings;
use Text::VimColor;
use HTML::TokeParser::Simple;
use HTML::Entities qw(decode_entities);
use Path::Class;

my $html_file = shift;
my $reader = file( $html_file )->openr;
unlink $html_file;
my $writer = file( $html_file )->openw;

my $parser = HTML::TokeParser::Simple->new( $reader );

while( my $token = $parser->get_token ) {

	my $class_name = $token->get_attr( "class" );

	if( $token->is_start_tag( "div" )
		&& ( $class_name && $class_name =~ qr/\bexample\b/ )
	) {
		my $start_tag = $token;
		$start_tag->set_attr( class => $class_name . " synNormal" );
		my @content;
		my $end_tag;
		
		EXAMPLE:
		while( $token = $parser->get_token ) {
			if( $token->is_end_tag( "div" ) ) {
				$end_tag = $token;
				last EXAMPLE;
			}

			if( $token->is_text ) {
				push @content, decode_entities( $token->as_is );
			}
		}

		my $code = join "", @content;
#		say $code;
		my $vim = Text::VimColor->new(
			string => $code,
			filetype => "perl",
			vim_options => [qw( -RXZ -i NONE -u NONE -N -n)],
		);
		my $html = $vim->html;
		$html =~ s/^\s+//;
		$html =~ s/\s+$//;

		print $writer $start_tag->as_is;

		my $lines = $html =~ tr/\n/\n/;

		say $writer "<div class='line_number'>";
		for my $line ( 0 .. $lines ) {
			say $writer "<div>",1 + $line,"</div>";
		}
		say $writer "</div>";

		print $writer "<div class='content'><pre>";
		say $writer $html;
		say $writer "</pre></div>";
		print $writer $end_tag->as_is;
	} else {
		print $writer $token->as_is;
	}
}