Skip to content

Instantly share code, notes, and snippets.

@tkusano
Created December 28, 2009 09:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tkusano/264607 to your computer and use it in GitHub Desktop.
Save tkusano/264607 to your computer and use it in GitHub Desktop.
#! /usr/bin/perl
=head1
html2kindle.pl - Convert xml2rfc HTML to Kindle friendly HTML.
=head1 SYNOPSIS
Download XML files in RFC 2629 format of RFC or Internet Draft.
Run xml2rfc and generate HTML files from XML files.
% ./html2kindle.pl RFC-generated-by-xml2rfc.html > RFC-for-kindle.html
Import RFC-for-kindle.html into MOBIPOCKET Creator on Windows, and
build .prc file, copy .prc to Kindle.
=head1 AUTHOR
KUSANO, Takayuki L<http://www.asahi-net.or.jp/~AE5T-KSN/>
=head1 SEE ALSO
xml2rfc L<http://xml.resource.org/>
Mobipocket Creator L<http://www.mobipocket.com/dev/>
Internet Draft (in XML) L<http://www.ietf.org/id/>
RFC (in XML) L<http://xml.resource.org/public/rfc/xml/>
=head1 LICENSE
GPL ver.2
=cut
use strict;
use warnings;
my $in_style = 0;
my $in_pre = 0;
while (<>) {
chomp;
if ($in_style) {
if (/^\-+><\/style>/) {
$in_style = 0;
}
next;
}
if ($in_pre) {
if (m{^</pre>(.*)$}) {
$in_pre = 0;
my $rest = $1;
if ( is_pre_start($rest) ) {
$in_pre = 1;
next;
}
print $rest, "\n";
next;
}
else {
s/ /&nbsp;/g;
printf "<small><code>%s</code></small><br />\n", $_;
next;
}
}
if (/^<style type=/) {
$in_style = 1;
next;
}
elsif ( is_pre_start($_) ) {
$in_pre = 1;
next;
}
elsif (m{<a href="\034toc">&nbsp;TOC&nbsp;</a>}) {
next;
}
s{<hr\s*/>}{};
print $_, "\n";
}
sub is_pre_start {
my $str = shift;
if ( $str =~ m{^(.*)<div style='.*'><pre>\s*$} ) {
printf "%s<div>\n", $1;
return 1;
}
elsif ( $str =~ m{^(.*)<pre>\s*$} ) {
printf "%s\n", $1;
return 1;
}
return 0;
}
# end of script
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment