Skip to content

Instantly share code, notes, and snippets.

@JEEN
Created December 4, 2010 17:25
Show Gist options
  • Select an option

  • Save JEEN/728340 to your computer and use it in GitHub Desktop.

Select an option

Save JEEN/728340 to your computer and use it in GitHub Desktop.
epub epub epub
use strict;
use warnings;
use EBook::EPUB;
use Path::Class;
use Data::Section::Simple;
use File::Temp;
my $title = 'Girls';
my $author = '@eeyees';
my $epub = EBook::EPUB->new;
$epub->add_title($title);;
$epub->add_author($author);
$epub->add_language('ko');
$epub->add_identifier('9999999999', 'ISBN');
my $dir = Path::Class::Dir->new('.');
my $idx = 0;
while(my $elm = $dir->next) {
next unless $elm =~ /(jpg|png)$/;
my $image_path = 'image/'.$elm->basename;
my $id = $epub->copy_image($elm, $image_path, 'image/png');
my $html = generate_html({
title => $title,
author => $author,
fileid => $id,
img => $image_path,
img_width => 560,
img_height => 720,
});
my ($fh, $tmpfile) = File::Temp::tempfile();
print $fh $html;
close $fh;
$epub->copy_xhtml($tmpfile, (sprintf 'page%04d.html', ++$idx));
}
$epub->pack_zip('test.epub');
sub generate_html {
my $obj = shift;
my $html = Data::Section::Simple::get_data_section('page.html');
for my $key (keys %{ $obj }) {
$html =~ s!<% $key %>!$obj->{$key}!g;
}
$html;
}
__DATA__
@@ page.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8"/>
<meta name="title" content="<% title %>"/>
<meta name="creator" content="<% author %>"/>
<title><% fileid %></title>
</head>
<body style="margin: 0">
<table summary="" border="0" style="width: 100%" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td align="center"><img src="<% img %>" width="<% img_width %>" height="<% img_height %>" alt="<% fileid %>"/></td>
</tr>
</tbody>
</table>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment