Skip to content

Instantly share code, notes, and snippets.

@hideo55
Created October 6, 2011 13:11
Show Gist options
  • Save hideo55/1267360 to your computer and use it in GitHub Desktop.
Save hideo55/1267360 to your computer and use it in GitHub Desktop.
#!/usr/bin/env perl
use strict;
use warnings;
use utf8;
use Docca;
use Data::Section::Simple qw/get_data_section/;
my $docca = Docca->new(
from => { Xatena => { converter => { hatena_compatible => 1 }, }, },
to => { PDF => { bin_name => '/usr/local/bin/wkhtmltopdf', }, },
tx => { path => [qw/./], cache => 0 },
);
if( @ARGV != 2 ){
print <<'USAGE';
Usage: xtena2pdf.pl <input_file> <output_file>
USAGE
exit(1);
}
my ($infile,$outfile) = @ARGV;
open my $fh, '<', $infile || die $!;
my $in = do{ local $/; <$fh> };
close $fh;
my $template = get_data_section('template.html');
my $res = $docca->convert( source => $in, template => ¥$template )->from('Xatena')
->to( 'PDF', { encoding => 'utf-8' } );
open $fh, '>', $outfile || die $!;
print $fh $res;
close($fh);
exit 0;
__END__
@@ template.html
<html>
<head>
<style type="text/css">
h3{
border-bottom: 1px dashed red;
}
h4{
margin: 2px 0px 2px 0px;
}
h5{
text-decoration: underline;
font-size: 120%;
margin: 5px 0 0 0;
}
.str { color: #080; }
.kwd { color: #008; }
.com { color: #800; }
.typ { color: #606; }
.lit { color: #066; }
.pun { color: #660; }
.pln { color: #000; }
.tag { color: #008; }
.atn { color: #606; }
.atv { color: #080; }
.dec { color: #606; }
pre.prettyprint { padding: 2px; border: 1px solid #888; }
ul{
margin-top: 1px;
margin-bottom: 2px;
}
li{
padding-top:1px;
padding-bottom: 1px;
}
pre.code{
background-color: #f0f0f0;
line-height: 150%;
}
</style>
<script type="text/javascript" src="http://google-code-prettify.googlecode.com/svn/trunk/src/prettify.js"></script>
<script type="text/javascript">
(function(onload) { // load
if (window.addEventListener) {
window.addEventListener('load', onload, false);
} else if (window.attachEvent) {
window.attachEvent('onload', onload, false);
} else {
window.onload = onload;
}
})(function() {
if (typeof prettyPrint === 'function') {
var pre = document.getElementsByTagName('pre');
for (var n = pre.length; n --> 0;) {
pre[n].className = (pre[n].className || '').split(/[ ¥t¥r¥n]+/).concat('prettyprint').join(' ')
}
prettyPrint();
}
});
</script>
</head>
<body><: $body :></body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment