summaryrefslogtreecommitdiff
path: root/lib/App/Dthumb/Data.pm.PL
blob: eaf914ec3ce75442a88c5572298f8eb0dfc8dd7c (plain)
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
#!/usr/bin/env perl
use strict;
use warnings;
use 5.010;
use autodie;
use MIME::Base64 qw(encode_base64);

my ($out_file) = @ARGV;

open(my $out_fh, '>', $out_file);
opendir(my $share_dh, 'share');

print {$out_fh} <DATA>;

for my $file (readdir($share_dh)) {
	if (substr($file, 0, 1) eq '.') {
		next;
	}

	open(my $fh, '<', "share/${file}");
	my $content = do { local $/ = undef; <$fh> };
	close($fh);

	if ($file =~ qr{ \. (png | gif) $ }ox) {
		$content = encode_base64($content);
	}

	printf {$out_fh} (
		"______[ %s ]______\n%s\n",
		$file,
		$content,
	);
}
closedir($share_dh);
close($out_fh);


__DATA__
package App::Dthumb::Data;


=head1 NAME

App::Dthumb::Data - Retrieve installed data (like lightbox images)

=head1 SYNOPSIS

    use App::Dthumb::Data;
    my $data = App::Dthumb::Data->new();
    
    $data->set_vars(
    	title => 'Something funky',
    );
    
    print $data->get('html_start.dthumb');
    
    open(my $fh, '>', 'close.png');
    print {$fh} $data->get('close.png');
    close($fh);

=head1 VERSION

This manual documents B<App::Dthumb::Data> version 0.2

=cut


use strict;
use warnings;
use base 'Exporter';

use Data::Section -setup;
use MIME::Base64 qw(decode_base64);

our @EXPORT_OK = ();
our $VERSION = '0.2';


=head1 METHODS

=head2 new

Returns a new B<App::Dthumb> object. Does not take any arguments.

=cut


sub new {
	my ($obj) = @_;
	my $ref = {};
	return bless($ref, $obj);
}


=head2 set_vars(%vars)

Set replacement variables.  For each hash key, when outputting data using the
B<get> function, dthumb will replace occurences of "<!-- $key -->" or "/* $key
*/" (the dollar sign is literal) with its value.

=cut


sub set_vars {
	my ($self, %vars) = @_;
	$self->{replace} = \%vars;
}


=head2 list_archived

Returns an array of all saved data.  That is, all files which do not end in
".dthumb".

=cut


sub list_archived {
	my ($self) = @_;
	return grep { ! /\.dthumb$/ } $self->section_data_names();
}


=head2 get($filename)

Returns the exact content of share/$filename (undef if no such file was
saved).

=cut


sub get {
	my ($self, $name) = @_;
	my $data = $self->section_data($name);

	if (not $data) {
		return undef;
	}

	$data = ${$data};

	chomp($data);

	if ($name =~ qr{ \. (png | gif) $ }ox) {
		return decode_base64($data);
	}

	while (my ($key, $value) = each %{$self->{replace}}) {
		$data =~ s{
			( \<\!-- | /\* )
				\s+ \$ $key \s+
			( --\> | \*/ )
		}{$value}gx;
	}

	return $data;
}

1;

=head1 DEPENDENCIES

=over

=item * Data::Section

=back

=head1 AUTHOR

Copyright (C) 2011 by Daniel Friesel E<lt>derf@chaosdorf.deE<gt>

=head1 LICENSE

    0. You just DO WHAT THE FUCK YOU WANT TO.

=cut

__DATA__