summaryrefslogtreecommitdiff
path: root/lib/App/Dthumb/Data.pm.PL
blob: 168397a0ee7f9e6633f2f9ca1333b6119046d9de (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
181
182
183
184
185
186
187
188
189
#!/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 q{.}) {
		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;

use strict;
use warnings;

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

our $VERSION = '0.2';

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

sub set_vars {
	my ($self, %vars) = @_;

	while (my ($key, $value) = each(%vars)) {
		$self->{replace}->{$key} = $value;
	}
}

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

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 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

=head1 DESCRIPTION

Since there seems to be no nice way of installing additional data with
Module::Build, this modules uses Data::Section to save all B<dthumb> data
(images, javascript, stylesheets...) in its __DATA__.  It provides methods to
retrieve this data and optionally replace certain variables in it with other
content.

While building B<App::Dthumb>, this module reads all files in the F<share>
directory and saves them in its __DATA__ section.

=head1 METHODS

=over

=item $data = App::Dthumb::Data->new()

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

=item $data->set_vars(I<%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.

=item $data->list_archived()

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

=item $data->get($filename)

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

=back

=head1 DIAGNOSTICS

None.

=head1 DEPENDENCIES

=over

=item * Data::Section

=back

=head1 BUGS AND LIMITATIONS

So far, none are known.  This module has proved to work fine with both binary
and ASCII files.

However, since it encodes binary files with base64, you shouldn't use it with
too many / too large files, otherwise you'll end up with a rather big module
file.

=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__