3 # $Id: multimarkdown2RTF.pl 481 2008-01-12 23:06:15Z fletcher $
5 # Required for using MultiMarkdown
7 # Copyright (c) 2006-2008 Fletcher T. Penney
8 # <http://fletcherpenney.net/>
10 # MultiMarkdown Version 2.0.b5
13 # Combine all the steps necessary to process MultiMarkdown text into RTF
14 # Not necessary, but might be easier than stringing the commands together
17 # Known Limitation: The temporary file is erased to 0 bytes, but not removed.
18 # I appreciate input on fixing this....
21 # Add metadata to guarantee we can transform to a complete XHTML
22 $data = "Format: complete\n";
25 # Parse stdin (MultiMarkdown file)
31 # Find name of RTF XSLT, if specified
32 $xslt_file = _RtfXSLT($data);
33 # $xslt_file = "memoir.xslt" if ($xslt_file eq "");
36 # Decide which flavor of SmartyPants to use
37 $language = _Language($data);
38 $SmartyPants = "SmartyPants.pl";
40 $SmartyPants = "SmartyPantsGerman.pl" if ($language =~ /^\s*german\s*$/i);
42 $SmartyPants = "SmartyPantsFrench.pl" if ($language =~ /^\s*french\s*$/i);
44 $SmartyPants = "SmartyPantsSwedish.pl" if ($language =~ /^\s*(swedish|norwegian|finnish|danish)\s*$/i);
46 $SmartyPants = "SmartyPantsDutch.pl" if ($language =~ /^\s*dutch\s*$/i);
49 # Create a pipe and process
50 $me = $0; # Where am I?
53 # Am I running in Windoze?
57 $me =~ s/\\([^\\]*?)$/\\/; # Get just the directory portion
59 $me =~ s/\/([^\/]*?)$/\//; # Get just the directory portion
62 # Create a temp file for textutil (doesn't work on stdin)
63 $temp_file = readpipe("mktemp -t multimarkdownXXXXX");
65 # Process XHTML and convert to rtf
68 $xslt = "| xsltproc -nonet -novalid ..\\XSLT\\$xslt_file -" if ($xslt_file ne "");
69 open (MultiMarkdown, "| cd \"$me\"& .\\MultiMarkdown.pl | .\\$SmartyPants $xslt > \"$temp_file\"& textutil -convert rtf -stdout \"$temp_file\"");
71 $xslt = "| xsltproc -nonet -novalid ../XSLT/$xslt_file -" if ($xslt_file ne "");
72 open (MultiMarkdown, "| cd \"$me\"; ./MultiMarkdown.pl | ./$SmartyPants $xslt > \"$temp_file\"; textutil -convert rtf -stdout \"$temp_file\"");
75 print MultiMarkdown $data;
79 system(" rm \"$temp_file\"");
85 my ($inMetaData, $currentKey) = (1,'');
87 foreach my $line ( split /\n/, $text ) {
88 $line =~ /^$/ and $inMetaData = 0 and next;
90 if ($line =~ /^([a-zA-Z0-9][0-9a-zA-Z _-]*?):\s*(.*)$/ ) {
93 $currentKey =~ s/ //g;
94 $g_metadata{$currentKey} = $temp;
95 if (lc($currentKey) eq "rtfxslt") {
96 $g_metadata{$currentKey} =~ s/\s*(\.xslt)?\s*$/.xslt/;
97 return $g_metadata{$currentKey};
100 if ($currentKey eq "") {
101 # No metadata present
115 my ($inMetaData, $currentKey) = (1,'');
117 foreach my $line ( split /\n/, $text ) {
118 $line =~ /^$/ and $inMetaData = 0 and next;
120 if ($line =~ /^([a-zA-Z0-9][0-9a-zA-Z _-]*?):\s*(.*)$/ ) {
122 $currentKey =~ s/ / /g;
123 $g_metadata{$currentKey} = $2;
124 if (lc($currentKey) eq "language") {
125 return $g_metadata{$currentKey};
128 if ($currentKey eq "") {
129 # No metadata present