File: HTML.pm

package info (click to toggle)
libtk-codetext-perl 0.3.4-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 296 kB
  • sloc: perl: 1,787; sh: 51; makefile: 8
file content (204 lines) | stat: -rw-r--r-- 4,271 bytes parent folder | download | duplicates (2)
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
package Tk::CodeText::HTML;

use vars qw($VERSION);
$VERSION = '0.2';

use strict;
use base('Tk::CodeText::Template');

sub new {
	my ($proto, $rules) = @_;
	my $class = ref($proto) || $proto;
	if (not defined($rules)) {
		$rules =  [
			['Text'],
			['Tag', -foreground => 'brown'],
			['Attr', -foreground => 'darkblue'],
			['Comment', -foreground => 'lightblue'],
			['Value', -foreground => 'orange'],
			['String', -foreground => 'red'],
			['SpChar', -foreground => 'magenta'],
		];
	};
	my $self = $class->SUPER::new($rules);
	$self->stackPush('Text');
	bless ($self, $class);
	return $self;
}

sub highlight {
	my $hlt = shift;
	my @in = split //, shift;
	$hlt->snippetParse;
	my $out = $hlt->out;
	@$out = ();
	foreach my $c (@in) {
		if ($c eq '<') {
			if ($hlt->stackTop eq 'Text') {
#				print "opening Tag\n";
				$hlt->snippetParse;
				$hlt->snippetAppend($c);
				$hlt->stackPush('Tag');
			} else {
				$hlt->snippetAppend($c)
			}
		} elsif ($c eq '>') {
			if ($hlt->stackTop eq 'Tag') {
#				print "closing Tag\n";
				$hlt->snippetAppend($c);
				$hlt->snippetParse;
				$hlt->stackPull;
			} elsif (($hlt->stackTop eq 'Value') or ($hlt->stackTop eq 'Attr') or ($hlt->stackTop eq 'Comment')) {
#				print "closing Tag\n";
				$hlt->snippetParse;
				$hlt->stackPull;
				$hlt->snippetAppend($c);
				$hlt->snippetParse;
				$hlt->stackPull;
			} else {
				$hlt->snippetAppend($c);
			}
		} elsif ($c eq '"') {
			if (($hlt->stackTop eq 'Value') or ($hlt->stackTop eq 'Comment')) {
#				print "opening String\n";
				$hlt->snippetParse;
				$hlt->snippetAppend($c);
				$hlt->stackPush('String');
			} elsif ($hlt->stackTop eq 'String') {
#				print "closing String\n";
				$hlt->snippetAppend($c);
				$hlt->snippetParse;
				$hlt->stackPull;
			} else {
				$hlt->snippetAppend($c);
			}
		} elsif ($c eq '!') {
			if ($hlt->stackTop eq 'Tag') {
#				print "opening Comment\n";
				$hlt->snippetParse;
				$hlt->snippetAppend($c);
				$hlt->stackPush('Comment');
			} else {
				$hlt->snippetAppend($c);
			}
		} elsif ($c eq '&') {
			if ($hlt->stackTop eq 'Text') {
#				print "opening SpChar\n";
				$hlt->snippetParse;
				$hlt->snippetAppend($c);
				$hlt->stackPush('SpChar');
			} else {
				$hlt->snippetAppend($c);
			}
		} elsif ($c eq ';') {
			if ($hlt->stackTop eq 'SpChar') {
#				print "closing SpChar\n";
				$hlt->snippetAppend($c);
				$hlt->snippetParse;
				$hlt->stackPull;
			} else {
				$hlt->snippetAppend($c);
			}
		} elsif ($c eq '=') {
			if ($hlt->stackTop eq 'Attr') {
#				print "opening Value\n";
				$hlt->snippetParse;
				$hlt->stackPull;
				$hlt->snippetAppend($c);
				$hlt->snippetParse;
				$hlt->stackPush('Value');
			} else {
				$hlt->snippetAppend($c);
			}
		} elsif ($c =~ /\s/) {
			if ($hlt->stackTop eq 'Tag') {
#				print "opening Attr\n";
				$hlt->snippetParse;
				$hlt->snippetAppend($c);
				$hlt->stackPush('Attr');
			} elsif ($hlt->stackTop eq 'Value') {
				$hlt->snippetParse;
				$hlt->snippetAppend($c);
				$hlt->stackPull;
				$hlt->stackPush('Attr');
			} elsif ($hlt->stackTop eq 'SpChar') {
				$hlt->snippetParse;
				$hlt->snippetAppend($c);
				$hlt->stackPull;
			} else {
				$hlt->snippetAppend($c);
			}
		} else {
			$hlt->snippetAppend($c);
		}
	}
	$hlt->snippetParse;
	return @$out;
}

1;

__END__


=head1 NAME

Tk::CodeText::HTML - a Plugin for HTML syntax highlighting

=head1 SYNOPSIS

 require Tk::CodeText::HTML;
 my $sh = new Tk::CodeText::HTML($textwidget, [
   ['Text'],
   ['Tag', -foreground => 'brown'],
   ['Attr', -foreground => 'darkblue'],
   ['Comment', -foreground => 'lightblue'],
   ['Value', -foreground => 'orange'],
   ['String', -foreground => 'red'],
   ['SpChar', -foreground => 'magenta'],
 ]);

=head1 DESCRIPTION

Tk::CodeText::HTML is a  plugin module that provides syntax highlighting
for HTML to a Tk::CodeText text widget.

It works quite fine, but can use refinement and optimization.

It inherits Tk::CodeText::None. See also there.

=head1 METHODS

=over 4

=item B<highlight>(I<$string>);

returns a list of string snippets and tags that can be inserted
in a Tk::Text like widget instantly.

=item B<syntax>

returns 'HTML'.

=back

=cut

=head1 AUTHOR

Hans Jeuken (haje@toneel.demon.nl)

=cut

=head1 BUGS

Unknown

=cut