Accept attributes in inline image syntax
[multimarkdown] / bin / multimarkdown2RTF.pl
1 #!/usr/bin/env perl
2 #
3 # $Id: multimarkdown2RTF.pl 481 2008-01-12 23:06:15Z fletcher $
4 #
5 # Required for using MultiMarkdown
6 #
7 # Copyright (c) 2006-2008 Fletcher T. Penney
8 #       <http://fletcherpenney.net/>
9 #
10 # MultiMarkdown Version 2.0.b5
11 #
12
13 # Combine all the steps necessary to process MultiMarkdown text into RTF
14 # Not necessary, but might be easier than stringing the commands together
15 # manually
16 #
17 # Known Limitation: The temporary file is erased to 0 bytes, but not removed.
18 # I appreciate input on fixing this....
19
20
21 # Add metadata to guarantee we can transform to a complete XHTML
22 $data = "Format: complete\n";
23
24
25 # Parse stdin (MultiMarkdown file)
26
27 undef $/;
28 $data .= <>;
29
30
31 # Find name of RTF XSLT, if specified
32 $xslt_file = _RtfXSLT($data);
33 # $xslt_file = "memoir.xslt" if ($xslt_file eq "");
34 $xslt = "";
35
36 # Decide which flavor of SmartyPants to use
37 $language = _Language($data);
38 $SmartyPants = "SmartyPants.pl";
39
40 $SmartyPants = "SmartyPantsGerman.pl" if ($language =~ /^\s*german\s*$/i);
41
42 $SmartyPants = "SmartyPantsFrench.pl" if ($language =~ /^\s*french\s*$/i);
43
44 $SmartyPants = "SmartyPantsSwedish.pl" if ($language =~ /^\s*(swedish|norwegian|finnish|danish)\s*$/i);
45
46 $SmartyPants = "SmartyPantsDutch.pl" if ($language =~ /^\s*dutch\s*$/i);
47
48
49 # Create a pipe and process
50 $me = $0;                               # Where am I?
51
52
53 # Am I running in Windoze?
54 my $os = $^O;
55
56 if ($os =~ /MSWin/) {
57         $me =~ s/\\([^\\]*?)$/\\/;      # Get just the directory portion
58 } else {
59         $me =~ s/\/([^\/]*?)$/\//;      # Get just the directory portion        
60 }
61
62 # Create a temp file for textutil (doesn't work on stdin)
63 $temp_file = readpipe("mktemp -t multimarkdownXXXXX");
64
65 # Process XHTML and convert to rtf
66
67 if ($os =~ /MSWin/) {
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\"");
70 } else {
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\"");
73 }
74
75 print MultiMarkdown $data;
76
77 close(MultiMarkdown);
78
79 system(" rm \"$temp_file\"");
80
81
82 sub _RtfXSLT {
83         my $text = shift;
84         
85         my ($inMetaData, $currentKey) = (1,'');
86         
87         foreach my $line ( split /\n/, $text ) {
88                 $line =~ /^$/ and $inMetaData = 0 and next;
89                 if ($inMetaData) {
90                         if ($line =~ /^([a-zA-Z0-9][0-9a-zA-Z _-]*?):\s*(.*)$/ ) {
91                                 $currentKey = $1;
92                                 my $temp = $2;
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};
98                                 }
99                         } else {
100                                 if ($currentKey eq "") {
101                                         # No metadata present
102                                         $inMetaData = 0;
103                                         next;
104                                 }
105                         }
106                 }
107         }
108                 
109         return;
110 }
111
112 sub _Language {
113         my $text = shift;
114         
115         my ($inMetaData, $currentKey) = (1,'');
116         
117         foreach my $line ( split /\n/, $text ) {
118                 $line =~ /^$/ and $inMetaData = 0 and next;
119                 if ($inMetaData) {
120                         if ($line =~ /^([a-zA-Z0-9][0-9a-zA-Z _-]*?):\s*(.*)$/ ) {
121                                 $currentKey = $1;
122                                 $currentKey =~ s/  / /g;
123                                 $g_metadata{$currentKey} = $2;
124                                 if (lc($currentKey) eq "language") {
125                                         return $g_metadata{$currentKey};
126                                 }
127                         } else {
128                                 if ($currentKey eq "") {
129                                         # No metadata present
130                                         $inMetaData = 0;
131                                         next;
132                                 }
133                         }
134                 }
135         }
136                 
137         return;
138 }