line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Marky::Bookmarker; |
2
|
|
|
|
|
|
|
$Marky::Bookmarker::VERSION = '0.035'; |
3
|
|
|
|
|
|
|
# ABSTRACT: Marky::Bookmarker - adding bookmarks |
4
|
|
|
|
|
|
|
|
5
|
1
|
|
|
1
|
|
7
|
use common::sense; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
5
|
|
6
|
1
|
|
|
1
|
|
48
|
use YAML::Any qw(DumpFile); |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
4
|
|
7
|
1
|
|
|
1
|
|
346
|
use Path::Tiny; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
54
|
|
8
|
1
|
|
|
1
|
|
6
|
use Text::NeatTemplate; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
20
|
|
9
|
1
|
|
|
1
|
|
3
|
use POSIX qw(strftime); |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
6
|
|
10
|
1
|
|
|
1
|
|
362
|
use IPC::System::Simple qw(run EXIT_ANY); |
|
1
|
|
|
|
|
3085
|
|
|
1
|
|
|
|
|
955
|
|
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
sub new { |
14
|
0
|
|
|
0
|
1
|
|
my $class = shift; |
15
|
0
|
|
|
|
|
|
my %parameters = (@_); |
16
|
0
|
|
0
|
|
|
|
my $self = bless ({%parameters}, ref ($class) || $class); |
17
|
|
|
|
|
|
|
|
18
|
0
|
|
|
|
|
|
$self->_set_defaults(); |
19
|
|
|
|
|
|
|
|
20
|
0
|
|
|
|
|
|
return ($self); |
21
|
|
|
|
|
|
|
} # new |
22
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
sub fields { |
25
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
26
|
|
|
|
|
|
|
|
27
|
0
|
|
|
|
|
|
my @fields = @{$self->{fields}}; |
|
0
|
|
|
|
|
|
|
28
|
0
|
|
|
|
|
|
return @fields; |
29
|
|
|
|
|
|
|
} # fields |
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
sub save_new_bookmark { |
33
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
34
|
0
|
|
|
|
|
|
my %args = @_; |
35
|
|
|
|
|
|
|
|
36
|
0
|
|
|
|
|
|
return $self->_save_new_bookmark(%args); |
37
|
|
|
|
|
|
|
} # save_new_bookmark |
38
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
sub bookmark_form { |
41
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
42
|
0
|
|
|
|
|
|
my %args = @_; |
43
|
|
|
|
|
|
|
|
44
|
0
|
|
|
|
|
|
return $self->_add_bookmark_form(%args); |
45
|
|
|
|
|
|
|
} # bookmark_form |
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
sub bookmarklet { |
49
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
50
|
0
|
|
|
|
|
|
my %args = @_; |
51
|
|
|
|
|
|
|
|
52
|
0
|
|
|
|
|
|
return $self->_make_bookmarklet(%args); |
53
|
|
|
|
|
|
|
} # bookmarklet |
54
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
sub _set_defaults { |
58
|
0
|
|
|
0
|
|
|
my $self = shift; |
59
|
0
|
|
|
|
|
|
my $conf = shift; |
60
|
|
|
|
|
|
|
|
61
|
0
|
|
|
|
|
|
foreach my $key (keys %{$conf}) |
|
0
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
{ |
63
|
0
|
0
|
|
|
|
|
if (defined $conf->{$key}) |
64
|
|
|
|
|
|
|
{ |
65
|
0
|
|
|
|
|
|
$self->{$key} = $conf->{$key}; |
66
|
|
|
|
|
|
|
} |
67
|
|
|
|
|
|
|
} |
68
|
|
|
|
|
|
|
|
69
|
0
|
0
|
|
|
|
|
if (!-d $self->{bookmark_dir}) |
70
|
|
|
|
|
|
|
{ |
71
|
0
|
|
|
|
|
|
die "No bookmark dir at " . $self->{bookmark_dir}; |
72
|
|
|
|
|
|
|
} |
73
|
0
|
0
|
|
|
|
|
if (!defined $self->{fields}) |
74
|
|
|
|
|
|
|
{ |
75
|
0
|
|
|
|
|
|
die "No fields given"; |
76
|
|
|
|
|
|
|
} |
77
|
0
|
0
|
|
|
|
|
if (!defined $self->{titlefield}) |
78
|
|
|
|
|
|
|
{ |
79
|
0
|
|
|
|
|
|
$self->{titlefield} = 'title'; |
80
|
|
|
|
|
|
|
} |
81
|
0
|
0
|
|
|
|
|
$self->{timestampfield} = '' if !defined $self->{timestampfield}; |
82
|
|
|
|
|
|
|
|
83
|
0
|
0
|
|
|
|
|
if (!defined $self->{editform_template}) |
84
|
|
|
|
|
|
|
{ |
85
|
0
|
|
|
|
|
|
$self->{editform_template} =<<'EOT'; |
86
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
EOT |
89
|
0
|
|
|
|
|
|
foreach my $col (@{$self->{fields}}) |
|
0
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
{ |
91
|
0
|
0
|
|
|
|
|
if ($col =~ /(description|summary)/i) |
92
|
|
|
|
|
|
|
{ |
93
|
0
|
|
|
|
|
|
$self->{editform_template} .=<<"EOT"; |
94
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
{\$${col}} |
98
|
|
|
|
|
|
|
|
99
|
|
|
|
|
|
|
EOT |
100
|
|
|
|
|
|
|
} |
101
|
|
|
|
|
|
|
else |
102
|
|
|
|
|
|
|
{ |
103
|
0
|
|
|
|
|
|
$self->{editform_template} .=<<"EOT"; |
104
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
EOT |
108
|
|
|
|
|
|
|
} |
109
|
|
|
|
|
|
|
} |
110
|
0
|
|
|
|
|
|
$self->{editform_template} .=<<'EOT'; |
111
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
EOT |
114
|
|
|
|
|
|
|
} |
115
|
|
|
|
|
|
|
|
116
|
0
|
|
|
|
|
|
return $self; |
117
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
} # _set_defaults |
119
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
|
121
|
|
|
|
|
|
|
sub _add_bookmark_form { |
122
|
0
|
|
|
0
|
|
|
my $self = shift; |
123
|
0
|
|
|
|
|
|
my %args = @_; |
124
|
|
|
|
|
|
|
|
125
|
0
|
|
|
|
|
|
my $tobj = Text::NeatTemplate->new(); |
126
|
|
|
|
|
|
|
my $form = $tobj->fill_in( |
127
|
|
|
|
|
|
|
data_hash=>{%args,}, |
128
|
|
|
|
|
|
|
template=>$self->{editform_template}, |
129
|
0
|
|
|
|
|
|
); |
130
|
0
|
|
|
|
|
|
return $form; |
131
|
|
|
|
|
|
|
} # _add_bookmark_form |
132
|
|
|
|
|
|
|
|
133
|
|
|
|
|
|
|
|
134
|
|
|
|
|
|
|
sub _make_bookmarklet { |
135
|
0
|
|
|
0
|
|
|
my $self = shift; |
136
|
0
|
|
|
|
|
|
my %args = @_; |
137
|
|
|
|
|
|
|
|
138
|
0
|
|
|
|
|
|
my $add_url = $args{action}; |
139
|
0
|
|
|
|
|
|
my $titlefield = $self->{titlefield}; |
140
|
0
|
|
|
|
|
|
my $bookmarklet =<<"EOT"; |
141
|
|
|
|
|
|
|
✚Marky link |
142
|
|
|
|
|
|
|
EOT |
143
|
0
|
|
|
|
|
|
return $bookmarklet; |
144
|
|
|
|
|
|
|
} # _make_bookmarklet |
145
|
|
|
|
|
|
|
|
146
|
|
|
|
|
|
|
|
147
|
|
|
|
|
|
|
sub _construct_filename ($%) { |
148
|
0
|
|
|
0
|
|
|
my $self = shift; |
149
|
0
|
|
|
|
|
|
my %args = @_; |
150
|
|
|
|
|
|
|
|
151
|
0
|
|
|
|
|
|
my $title = $args{title}; |
152
|
0
|
|
|
|
|
|
my $dir = path($self->{bookmark_dir}); |
153
|
|
|
|
|
|
|
|
154
|
0
|
0
|
|
|
|
|
my $basename = ($title ? $title : 'bookmark'); |
155
|
0
|
|
|
|
|
|
$basename =~ s/[^a-zA-Z0-9 ]//g; |
156
|
0
|
|
|
|
|
|
$basename =~ s/\s+/_/g; |
157
|
0
|
|
|
|
|
|
$basename =~ s/_$//; |
158
|
|
|
|
|
|
|
|
159
|
|
|
|
|
|
|
# check if a file of the same name already exists |
160
|
0
|
|
|
|
|
|
my @matching_files = $dir->children(qr/^${basename}/); |
161
|
0
|
|
|
|
|
|
my $count = scalar @matching_files; |
162
|
0
|
0
|
|
|
|
|
if ($count > 0) |
163
|
|
|
|
|
|
|
{ |
164
|
|
|
|
|
|
|
# this gives Foo, Foo002, Foo003 |
165
|
0
|
|
|
|
|
|
$basename = sprintf('%s%00d', $basename, $count + 1); |
166
|
|
|
|
|
|
|
} |
167
|
|
|
|
|
|
|
|
168
|
0
|
|
|
|
|
|
return "${basename}.yml"; |
169
|
|
|
|
|
|
|
} # _construct_filename |
170
|
|
|
|
|
|
|
|
171
|
|
|
|
|
|
|
|
172
|
|
|
|
|
|
|
sub _save_new_bookmark { |
173
|
0
|
|
|
0
|
|
|
my $self = shift; |
174
|
0
|
|
|
|
|
|
my %args = @_; |
175
|
|
|
|
|
|
|
|
176
|
0
|
|
|
|
|
|
my $data = $args{data}; |
177
|
|
|
|
|
|
|
# if we have a timestamp field, figure its value |
178
|
0
|
0
|
|
|
|
|
if ($self->{timestampfield}) |
179
|
|
|
|
|
|
|
{ |
180
|
0
|
|
|
|
|
|
my $now = strftime '%Y-%m-%d %H:%M:%S', localtime; |
181
|
0
|
|
|
|
|
|
$data->{$self->{timestampfield}} = $now; |
182
|
|
|
|
|
|
|
} |
183
|
|
|
|
|
|
|
|
184
|
0
|
|
|
|
|
|
my $filename = $self->_construct_filename(title=>$data->{$self->{titlefield}}); |
185
|
0
|
|
|
|
|
|
my $bm_dir = path($self->{bookmark_dir}); |
186
|
0
|
|
|
|
|
|
my $fullname = $bm_dir->child($filename)->stringify; |
187
|
0
|
|
|
|
|
|
DumpFile($fullname, $data); |
188
|
|
|
|
|
|
|
|
189
|
0
|
0
|
|
|
|
|
if (-x $self->{update_script}) |
190
|
|
|
|
|
|
|
{ |
191
|
0
|
|
|
|
|
|
my $exit_val = run(EXIT_ANY, $self->{update_script}, $fullname); |
192
|
0
|
0
|
|
|
|
|
if ($exit_val != 0) |
193
|
|
|
|
|
|
|
{ |
194
|
0
|
|
|
|
|
|
return 0; |
195
|
|
|
|
|
|
|
} |
196
|
|
|
|
|
|
|
} |
197
|
0
|
|
|
|
|
|
return 1; |
198
|
|
|
|
|
|
|
} # _save_new_bookmark |
199
|
|
|
|
|
|
|
|
200
|
|
|
|
|
|
|
1; # End of Marky::Bookmarker |
201
|
|
|
|
|
|
|
|
202
|
|
|
|
|
|
|
__END__ |