| 1 | package Marky::Bookmarker; | ||||||||||||||||||||
| 2 | 3 | $Marky::Bookmarker::VERSION = '0.0604'; | |||||||||||||||||||
| 3 | # ABSTRACT: Marky::Bookmarker - adding bookmarks | ||||||||||||||||||||
| 4 | |||||||||||||||||||||
| 5 | 3 | ▶ | use common::sense; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 6 | 3 | ▶ | use YAML::Any qw(DumpFile); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 7 | 3 | ▶ | use Path::Tiny; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 8 | 3 | ▶ | use Text::NeatTemplate; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 9 | 3 | ▶ | use POSIX qw(strftime); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 10 | 3 | ▶ | use IPC::System::Simple qw(run EXIT_ANY); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 11 | |||||||||||||||||||||
| 12 | |||||||||||||||||||||
| 13 | 0 | ▶ | sub new { | ||||||||||||||||||
|
Subroutine
new: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||
| 14 | 0 | my $class = shift; | |||||||||||||||||||
| 15 | 0 | my %parameters = (@_); | |||||||||||||||||||
| 16 | 0 | ▶ | my $self = bless ({%parameters}, ref ($class) || $class); | ||||||||||||||||||
|
Condition0%0/3
ref $class || $class
MC/DC0%0/2
ref $class || $class
ref $class
$class
Truth table0%0/3
A || B
A= ref $classB= $class
| |||||||||||||||||||||
| 17 | |||||||||||||||||||||
| 18 | 0 | $self->_set_defaults(); | |||||||||||||||||||
| 19 | |||||||||||||||||||||
| 20 | 0 | return ($self); | |||||||||||||||||||
| 21 | } # new | ||||||||||||||||||||
| 22 | |||||||||||||||||||||
| 23 | |||||||||||||||||||||
| 24 | 0 | ▶ | sub fields { | ||||||||||||||||||
|
Subroutine
fields: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 25 | 0 | my $self = shift; | |||||||||||||||||||
| 26 | |||||||||||||||||||||
| 27 | 0 | my @fields = @{$self->{fields}}; | |||||||||||||||||||
| 28 | 0 | return @fields; | |||||||||||||||||||
| 29 | } # fields | ||||||||||||||||||||
| 30 | |||||||||||||||||||||
| 31 | |||||||||||||||||||||
| 32 | 0 | ▶ | sub save_new_bookmark { | ||||||||||||||||||
|
Subroutine
save_new_bookmark: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 33 | 0 | my $self = shift; | |||||||||||||||||||
| 34 | 0 | my %args = @_; | |||||||||||||||||||
| 35 | |||||||||||||||||||||
| 36 | 0 | return $self->_save_new_bookmark(%args); | |||||||||||||||||||
| 37 | } # save_new_bookmark | ||||||||||||||||||||
| 38 | |||||||||||||||||||||
| 39 | |||||||||||||||||||||
| 40 | 0 | ▶ | sub bookmark_form { | ||||||||||||||||||
|
Subroutine
bookmark_form: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 41 | 0 | my $self = shift; | |||||||||||||||||||
| 42 | 0 | my %args = @_; | |||||||||||||||||||
| 43 | |||||||||||||||||||||
| 44 | 0 | return $self->_add_bookmark_form(%args); | |||||||||||||||||||
| 45 | } # bookmark_form | ||||||||||||||||||||
| 46 | |||||||||||||||||||||
| 47 | |||||||||||||||||||||
| 48 | 0 | ▶ | sub bookmarklet { | ||||||||||||||||||
|
Subroutine
bookmarklet: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 49 | 0 | my $self = shift; | |||||||||||||||||||
| 50 | 0 | my %args = @_; | |||||||||||||||||||
| 51 | |||||||||||||||||||||
| 52 | 0 | return $self->_make_bookmarklet(%args); | |||||||||||||||||||
| 53 | } # bookmarklet | ||||||||||||||||||||
| 54 | |||||||||||||||||||||
| 55 | |||||||||||||||||||||
| 56 | |||||||||||||||||||||
| 57 | 0 | ▶ | sub _set_defaults { | ||||||||||||||||||
|
Subroutine
_set_defaults: not called CC=10 SCAR=47.0
| |||||||||||||||||||||
| 58 | 0 | my $self = shift; | |||||||||||||||||||
| 59 | 0 | my $conf = shift; | |||||||||||||||||||
| 60 | |||||||||||||||||||||
| 61 | 0 | foreach my $key (keys %{$conf}) | |||||||||||||||||||
| 62 | { | ||||||||||||||||||||
| 63 | 0 | ▶ | if (defined $conf->{$key}) | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 64 | { | ||||||||||||||||||||
| 65 | 0 | $self->{$key} = $conf->{$key}; | |||||||||||||||||||
| 66 | } | ||||||||||||||||||||
| 67 | } | ||||||||||||||||||||
| 68 | |||||||||||||||||||||
| 69 | 0 | ▶ | if (!-d $self->{bookmark_dir}) | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 70 | { | ||||||||||||||||||||
| 71 | 0 | die "No bookmark dir at " . $self->{bookmark_dir}; | |||||||||||||||||||
| 72 | } | ||||||||||||||||||||
| 73 | 0 | ▶ | if (!defined $self->{fields}) | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 74 | { | ||||||||||||||||||||
| 75 | 0 | die "No fields given"; | |||||||||||||||||||
| 76 | } | ||||||||||||||||||||
| 77 | 0 | ▶ | if (!defined $self->{titlefield}) | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 78 | { | ||||||||||||||||||||
| 79 | 0 | $self->{titlefield} = 'title'; | |||||||||||||||||||
| 80 | } | ||||||||||||||||||||
| 81 | 0 | ▶ | $self->{timestampfield} = '' if !defined $self->{timestampfield}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 82 | |||||||||||||||||||||
| 83 | 0 | ▶ | if (!defined $self->{editform_template}) | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 84 | { | ||||||||||||||||||||
| 85 | 0 | $self->{editform_template} =<<'EOT'; | |||||||||||||||||||
| 86 | <div class="editform"> | ||||||||||||||||||||
| 87 | <form action="{$action}" method="post"> | ||||||||||||||||||||
| 88 | EOT | ||||||||||||||||||||
| 89 | 0 | foreach my $col (@{$self->{fields}}) | |||||||||||||||||||
| 90 | { | ||||||||||||||||||||
| 91 | 0 | ▶ | if ($col =~ /(description|summary)/i) | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 92 | { | ||||||||||||||||||||
| 93 | 0 | $self->{editform_template} .=<<"EOT"; | |||||||||||||||||||
| 94 | <div class="item"> | ||||||||||||||||||||
| 95 | <label>$col</label><br/> | ||||||||||||||||||||
| 96 | <textarea name="$col" rows="4" cols="50"> | ||||||||||||||||||||
| 97 | {\$${col}} | ||||||||||||||||||||
| 98 | </textarea></div> | ||||||||||||||||||||
| 99 | EOT | ||||||||||||||||||||
| 100 | } | ||||||||||||||||||||
| 101 | else | ||||||||||||||||||||
| 102 | { | ||||||||||||||||||||
| 103 | 0 | $self->{editform_template} .=<<"EOT"; | |||||||||||||||||||
| 104 | <div class="item"> | ||||||||||||||||||||
| 105 | <label>$col</label> <input type="text" name="$col" value="{\$${col}}" size="50"/> | ||||||||||||||||||||
| 106 | </div> | ||||||||||||||||||||
| 107 | EOT | ||||||||||||||||||||
| 108 | } | ||||||||||||||||||||
| 109 | } | ||||||||||||||||||||
| 110 | 0 | $self->{editform_template} .=<<'EOT'; | |||||||||||||||||||
| 111 | <input type="submit" value="Save"> | ||||||||||||||||||||
| 112 | </form></div> | ||||||||||||||||||||
| 113 | EOT | ||||||||||||||||||||
| 114 | } | ||||||||||||||||||||
| 115 | |||||||||||||||||||||
| 116 | 0 | return $self; | |||||||||||||||||||
| 117 | |||||||||||||||||||||
| 118 | } # _set_defaults | ||||||||||||||||||||
| 119 | |||||||||||||||||||||
| 120 | |||||||||||||||||||||
| 121 | 0 | ▶ | sub _add_bookmark_form { | ||||||||||||||||||
|
Subroutine
_add_bookmark_form: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 122 | 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 | 0 | ▶ | sub _make_bookmarklet { | ||||||||||||||||||
|
Subroutine
_make_bookmarklet: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 135 | 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 | <a class="button" onclick="alert('Drag this link to your bookmarks toolbar, or right-click it and choose Bookmark This Link...');return false;" href="javascript:javascript:(function(){var%20bm_url%20=%20location.href;var%20title%20=%20document.title%20||%20bm_url;window.open('${add_url}?url='%20+%20encodeURIComponent(bm_url)+'&${titlefield}='%20+%20encodeURIComponent(title)+'&description='%20+%20encodeURIComponent(document.getSelection())+'&source=bookmarklet','_blank','menubar=no,toolbar=no,dialog=1');})();"><b>✚Marky link</b></a> | ||||||||||||||||||||
| 142 | EOT | ||||||||||||||||||||
| 143 | 0 | return $bookmarklet; | |||||||||||||||||||
| 144 | } # _make_bookmarklet | ||||||||||||||||||||
| 145 | |||||||||||||||||||||
| 146 | |||||||||||||||||||||
| 147 | 0 | ▶ | sub _construct_filename ($%) { | ||||||||||||||||||
|
Subroutine
_construct_filename: not called CC=3 SCAR=24.8
| |||||||||||||||||||||
| 148 | 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 | ▶ | my $basename = ($title ? $title : 'bookmark'); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 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 | ▶ | if ($count > 0) | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 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 | 0 | ▶ | sub _save_new_bookmark { | ||||||||||||||||||
|
Subroutine
_save_new_bookmark: not called CC=4 SCAR=30.0
| |||||||||||||||||||||
| 173 | 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 | ▶ | if ($self->{timestampfield}) | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 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 | ▶ | if (-x $self->{update_script}) | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 190 | { | ||||||||||||||||||||
| 191 | 0 | my $exit_val = run(EXIT_ANY, $self->{update_script}, $fullname); | |||||||||||||||||||
| 192 | 0 | ▶ | if ($exit_val != 0) | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 193 | { | ||||||||||||||||||||
| 194 | 0 | return 0; | |||||||||||||||||||
| 195 | } | ||||||||||||||||||||
| 196 | } | ||||||||||||||||||||
| 197 | 0 | return 1; | |||||||||||||||||||
| 198 | } # _save_new_bookmark | ||||||||||||||||||||
| 199 | |||||||||||||||||||||
| 200 | 3 | 1; # End of Marky::Bookmarker | |||||||||||||||||||
| 201 | |||||||||||||||||||||
| 202 | __END__ | ||||||||||||||||||||