#!/usr/bin/perl
# card2latex
# by Sam Watkins <sam@shallow.net>
# This software is in the public domain.
# see http://cards.sourceforge.net/ for details

$fontsize = "footnotesize"; # tiny scriptsize footnotesize small normalsize large Large LARGE huge Huge

$text = join "", <STDIN>;
@sides = split /^-+\r?\n/m, $text;
$sides[0] =~ s/^(.+?) /{\\bf $1} /;
$sides[1] =~ s/^(.+?) \(/{\\bf $1} (/;
for (@sides) {
	tr/\r//d;
	s/(\S+)$/{\\it $1}/m;
	s/_/\\_/gs;
	s/\$/\\\$/gs;
	s/</\$<\$/gs;
	s/>/\$>\$/gs;
	s/&/\\&/gs;
	s/%/\\%/gs;
	s/#/\\#/gs;
	s/~/\\~{}/gs;
	s/ -- / --- /gs;
	s/ - / -- /gs;
	s/^- /-- /gm;
	s/^$/~/gm;
	s/(  +)/spaces($1)/gse;
	s/^( +)/spaces($1)/gse;
	s/\*(\S+)\*/{\\bf $1}/gs;
	chomp;
	s/\n/ \\\\\n/gs;

	push @out, "{\\$fontsize\{%\n$_}}\n";
}
print join(("-"x60)."\n", @out);

sub spaces {
	return "\\mbox{".("~"x(length($_[0])))."}";
}
