blib/lib/Tk/MarkdownTk.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 Tk::MarkdownTk;
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 FATAL => 'all';
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 =head1 NAME
8  
9 Tk::MarkdownTk - a Tk::Markdown with tk widget tag support
10  
11 =head1 VERSION
12  
13 Version 0.07
14  
15 =cut
16  
17 our $VERSION = '0.07';
18  
19 0 ▶use Tk::Markdown;
Subroutine BEGIN: called CC=1 SCAR=2.6
20 use base qw(Tk::Derived Tk::Markdown);
21 Construct Tk::Widget 'MarkdownTk';
22  
23  
24  
25  
26  
27  
28 =head1 SYNOPSIS
29  
30 use Tk;
31 use Tk::MarkdownTk;
32  
33 my $mw = new MainWindow();
34 my $mdt = $mw->MarkdownTk();
35  
36 $mdt->insert(q{
37 some markdown here
38  
39 with tags like this: <Tk::Button -text="Click me!">
40 });
41  
42 =head1 SUBROUTINES/METHODS
43  
44 =head2 insert
45  
46 Whenever insert is called on the MarkdownTk,
47 some translation is done on the text in order to
48 diplay it nicely as markdown. Tables are reformatted
49 (if the line starts with a bar) and headers are
50 tagged with different fonts.
51  
52 This module is currently under development and
53 there's plenty to do, e.g. links, images, etc.
54  
55 In MarkdownTk, html-ish tags are also transformed
56 into Tk widgets.
57  
58 =cut
59  
60  
61 sub insert
62 {
63 my ($self,$index,$content) = @_;
64 my $res = $self->SUPER::insert($index,Tk::Markdown::FormatMarkdown($content));
65 if(! $self->{inserting}){ ### don't allow recursion...
66 $self->{inserting} = 1;
67 $self->PaintMarkdown();
68 $self->TransformTk();
69 $self->see("1.0");
70 $self->{inserting} = 0;
71 }
72 return $res;
73 }
74  
75 =head2 TransformTk
76  
77 This is called internally. It parses out HTML-like tags that define Widgets to be drawn
78 in the text.
79  
80 eg
81  
82 <Tk::Button -text="Click Me">
83  
84 Also, perl can be run from the document... a Tk::Markdown allows <% %> which
85 is run prior to insertion (good for formatting) but Tk::MarkdownTk adds running
86 perl from <? ?>, which gets replaced inline, after insertion. This is good for
87 adding named subs in the same namespace as the buttons and other tk widgets
88 added to the text area. Eg:
89  
90 <Tk::Button -text="Click me" -command="run_this">
91 <? sub run_this { print "Hello, world!\n"; } ?>
92  
93 So remember:
94
95 % formatted
96
97 ? subs
98  
99  
100 =cut
101  
102 ### look for <tags> (the other kind of tags) to be tranformed into actual widgets
103 sub TransformTk {
104 my $self = shift;
105 ### this is to find <tk::widget attributes>, or <? ?> script.
106 my $re = qr/<Tk\:\:\w+[^>]*>|<\?.*?\?>/s;
107 $self->FindAll('-regexp','-case', $re);
108 my @i = $self->tagRanges('sel');
109 #print map {"$_\n"} @i;
110 for(my $i = @i-2; $i >= 0; $i-=2){
111 my ($s,$e) = ($i[$i], $i[$i+1]);
112 my $string = $self->get($s,$e);
113 if($string =~ /<Tk\:\:(\w+)(.*)>/){
114 my ($w,$a) = ($1,$2);
115 my %a = parseAttrs($a);
116 $self->delete($s,$e);
117 my $t = $self->$w(%a);
118 $self->windowCreate($s,-window=>$t);
119 }
120 elsif($string =~ /<\?=(.*)\?>/s){
121 my $sub;
122 eval("\$sub = sub { $1 }");
123 die "$@ - somewhere in: $1" if $@;
124 $self->delete($s,$e);
125 $self->insert($s,&$sub());
126 }
127 elsif($string =~ /<\?(.*)\?>/s){
128 $self->delete($s,$e);
129 eval("$1");
130 die "$@ - somewhere in: $1" if $@;
131 die $@ if $@;
132 }
133 }
134 }
135  
136 =head2 parseAttrs
137  
138 A helper function for TransformTk.
139  
140 =cut
141  
142 ### parse some attributes
143 sub parseAttrs {
144 my ($attrs) = @_;
145 my %attrs = ();
146 while($attrs =~ /\G.*?([\w-]+)(=""|=''|=".*?[^\\]"|='.*?[^\\]'|=[^"']\S*|)/g){
147 my ($k,$v) = ($1,$2);
148 $v ||= 1;
149 $v =~ s/^=//;
150 $v =~ s/^(["'])(.*)\1$/$2/;
151 if($k =~ /^-(?:command)$/){
152 eval(" \$v = sub { $v }; ");
153 die "$@ - somewhere in: $v" if $@;
154 #print "COMMAND: $v\n";
155 }
156 $attrs{$k} = $v;
157 }
158 return %attrs;
159 }
160  
161  
162 =head2 clipEvents
163  
164 This copied directly from Tk::ROText
165  
166 =cut
167  
168 sub clipEvents
169 {
170 return qw[Copy];
171 }
172  
173 =head2 ClassInit
174  
175 This is copied directly from Tk::ROText.
176  
177 =cut
178  
179 sub ClassInit
180 {
181 my ($class,$mw) = @_;
182 my $val = $class->bindRdOnly($mw);
183 my $cb = $mw->bind($class,'<Next>');
184 $mw->bind($class,'<space>',$cb) if (defined $cb);
185 $cb = $mw->bind($class,'<Prior>');
186 $mw->bind($class,'<BackSpace>', $cb) if (defined $cb);
187 $class->clipboardOperations($mw,'Copy');
188 return $val;
189 }
190  
191 =head2 Populate
192  
193 This is copied and modified from Tk::ROText. The modification is the addition
194 of a call to setDefaultStyles. That's all.
195  
196 =cut
197  
198 sub Populate
199 {
200 my ($self,$args) = @_;
201 $self->SUPER::Populate($args);
202 }
203  
204  
205 =head2 Tk::Widget::ScrlMarkdownTk
206  
207 Copied and adapted from Tk::ROText
208  
209 =cut
210  
211 sub Tk::Widget::ScrlMarkdownTk { shift->Scrolled('MarkdownTk' => @_) }
212  
213  
214  
215 =head1 AUTHOR
216  
217 jimi, C<< <jimi at webu.co.uk> >>
218  
219 =head1 BUGS
220  
221 Please report any bugs or feature requests to C<bug-tk-markdowntk at rt.cpan.org>, or through
222 the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Tk-MarkdownTk>. I will be notified, and then you'll
223 automatically be notified of progress on your bug as I make changes.
224  
225  
226  
227  
228 =head1 SUPPORT
229  
230 You can find documentation for this module with the perldoc command.
231  
232 perldoc Tk::MarkdownTk
233  
234  
235 You can also look for information at:
236  
237 =over 4
238  
239 =item * RT: CPAN's request tracker (report bugs here)
240  
241 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Tk-MarkdownTk>
242  
243 =item * AnnoCPAN: Annotated CPAN documentation
244  
245 L<http://annocpan.org/dist/Tk-MarkdownTk>
246  
247 =item * CPAN Ratings
248  
249 L<http://cpanratings.perl.org/d/Tk-MarkdownTk>
250  
251 =item * Search CPAN
252  
253 L<http://search.cpan.org/dist/Tk-MarkdownTk/>
254  
255 =back
256  
257  
258 =head1 ACKNOWLEDGEMENTS
259  
260  
261 =head1 LICENSE AND COPYRIGHT
262  
263 Copyright 2013 jimi.
264  
265 This program is free software; you can redistribute it and/or modify it
266 under the terms of the the Artistic License (2.0). You may obtain a
267 copy of the full license at:
268  
269 L<http://www.perlfoundation.org/artistic_license_2_0>
270  
271 Any use, modification, and distribution of the Standard or Modified
272 Versions is governed by this Artistic License. By using, modifying or
273 distributing the Package, you accept this license. Do not use, modify,
274 or distribute the Package, if you do not accept this license.
275  
276 If your Modified Version has been derived from a Modified Version made
277 by someone other than you, you are nevertheless required to ensure that
278 your Modified Version complies with the requirements of this license.
279  
280 This license does not grant you the right to use any trademark, service
281 mark, tradename, or logo of the Copyright Holder.
282  
283 This license includes the non-exclusive, worldwide, free-of-charge
284 patent license to make, have made, use, offer to sell, sell, import and
285 otherwise transfer the Package with respect to any patent claims
286 licensable by the Copyright Holder that are necessarily infringed by the
287 Package. If you institute patent litigation (including a cross-claim or
288 counterclaim) against any party alleging that the Package constitutes
289 direct or contributory patent infringement, then this Artistic License
290 to you shall terminate on the date that such litigation is filed.
291  
292 Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER
293 AND CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES.
294 THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
295 PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY
296 YOUR LOCAL LAW. UNLESS REQUIRED BY LAW, NO COPYRIGHT HOLDER OR
297 CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, OR
298 CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE OF THE PACKAGE,
299 EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
300  
301  
302 =cut
303  
304 1; # End of Tk::MarkdownTk
↑ Summary