#!/usr/bin/perl

use strict;
use Zim;

print << "EOT" and exit unless @ARGV == 3;
usage: $0 ZIM_DIR HTML_DIR TEMPLATE

	The template file should contain '\%PAGE\%'
EOT

my ($zim_dir, $html_dir, $template) = @ARGV;

my $zim = Zim->new($zim_dir);



sub htmlify_page {
	my $page = $zim->load_page( shift );
	return if $page->status eq 'new';
	my $file = $page->filename;
	$file =~ s/^$zim_dir/$html_dir/;

	open HTML, ">$file" or die $!;
	while (defined ($_ = $page->read_block)) {
		my $html;
		if (ref $_) {
			my ($tag, @node) = @_;
			if ($tag =~ /^head(\d)$/) {
				$html = "<h$1>@node</h$1>";
			}
			elsif ($tag eq 'pre') {
				$html = "<pre>@node</pre>";
			}
			elsif ($tag eq 'link') {
				# FIXME resolv link
				$html = "<a href='$node[1]'>$node[0]</a>";
			}
			else { $html = "@node" }
		}
		else { $html = $_ }
		print HTML $html;
	}
	close HTML;
}

