blib/lib/Khonsu/Syntax.pm

filter: Statementstmt 81.8% 9 / 11 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 4 / 4 Podpod n/a totaltotal 86.6% 13 / 15 CC 1 SCAR 0.7
CC
1
Cov
82%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.1
↑ Summary
1 package Khonsu::Syntax;
2  
3 1 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 our $VERSION = '0.02';
8  
9 0 ▶use Syntax::Kamelon;
Subroutine BEGIN: called CC=1 SCAR=2.6
10  
11 use base 'Khonsu::Text';
12  
13 sub attributes {
14 my ($a) = shift;
15 return (
16 $a->SUPER::attributes(),
17 syntax => { $a->RW, $a->STR },
18 line_numbers => { $a->RW, $a->BOOL },
19 line_number_width => { $a->RW, $a->NUM, default => sub { 15 } },
20 highlight => { $a->RW, $a->HR, default => sub {
21 return {
22 Alert => '#fff',
23 Annotation => '#ff0000',
24 Attribute => '#ffff00',
25 BaseN => '#00ff00',
26 BuiltIn => '#ad9b04',
27 Char => '#00ffff',
28 Comment => '#dadada',
29 CommentVar => '#000',
30 Constant => '#ff00ff',
31 ControlFlow => '#00a4ff',
32 DataType => '#aa7700',
33 DecVal => '#ff0000',
34 Documentation => '#000',
35 Error => '#fff000',
36 Extension => '#ff00ff',
37 Float => '#ff0000',
38 Function => '#000',
39 Import => '#04ffc1',
40 Information => '#0000ff',
41 Keyword => '#006699',
42 Normal => '#000',
43 Operator => '#0000FF',
44 Others => '#00ffff',
45 Preprocessor => '#0066ff',
46 RegionMarker => '#000',
47 SpecialChar => '#00a4ff',
48 SpecialString => '#ff0000',
49 String => '#0000ff',
50 Variable => '#aa7700',
51 VerbatimString => '#000',
52 Warning => '#edac33',
53 Line => '#dadada'
54 };
55 } }
56 );
57 }
58  
59 sub add {
60 my ($self, $file, %attributes) = @_;
61  
62 $attributes{y} ||= $file->page->y;
63  
64 $self->set_attributes(%attributes);
65  
66 my $kam = Syntax::Kamelon->new(
67 syntax => $self->syntax || 'Perl',
68 );
69  
70 $kam->Parse($self->text);
71  
72 my %points = $self->get_points();
73 my $highlight = $self->highlight;
74 my $line_number = 0;
75 for my $line (@{ $kam->{FORMATTER}->{LINES} }) {
76 if ($self->line_numbers) {
77 $line_number++;
78 $self->SUPER::add($file,
79 margin => 0,
80 font => { colour => $highlight->{Line} },
81 text => "$line_number |",
82 y => $points{y},
83 x => $points{x},
84 w => $self->line_number_width,
85 align => 'right'
86 );
87 $self->align('');
88 }
89 if (! scalar @{$line}) {
90 $file->page->y($file->page->y + $self->font->line_height);
91 } else {
92 for my $part (@{$line}) {
93 $self->SUPER::add($file,
94 font => { colour => $highlight->{$part->{tag}} },
95 text => $part->{text},
96 margin => 0,
97 ($self->end_w ? (
98 x => $self->end_w,
99 w => $file->page->width()
100 ) : ()),
101 y => $points{y},
102 );
103 }
104 }
105 $self->end_w(0);
106 $points{y} = $file->page->y;
107 }
108  
109 return $file;
110 }
111  
112 sub update_highlighting {
113 my ($self, %highlighting) = @_;
114 my $highlight = $self->highlight;
115 $highlight = {%{$highlight}, %highlighting};
116 $self->highlight($highlight);
117 return $self;
118 }
119  
120 1;
121  
122 __END__
↑ Summary