Accept attributes in inline image syntax
[multimarkdown] / bin / multimarkdown2XHTML.pl
1 #!/usr/bin/env perl
2 #
3 # $Id: multimarkdown2XHTML.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 XHTML
14 # Not necessary, but might be easier than stringing the commands together
15 # manually
16
17 # TODO: Add an option to turn SmartyPants off?
18
19 # Add metadata to guarantee we can transform to a complete XHTML
20 $data = "Format: complete\n";
21
22
23 # Parse stdin (MultiMarkdown file)
24 undef $/;
25 $data .= <>;
26
27
28 # Find name of XHTML File if specified
29 $xslt_file = _XhtmlXSLT($data);
30 $xslt_file = "" if ($xslt_file eq "");
31 $xslt = "";
32
33 # Decide which flavor of SmartyPants to use
34 $language = _Language($data);
35 $SmartyPants = "SmartyPants.pl";
36
37 $SmartyPants = "SmartyPantsGerman.pl" if ($language =~ /^\s*german\s*$/i);
38
39 $SmartyPants = "SmartyPantsFrench.pl" if ($language =~ /^\s*french\s*$/i);
40
41 $SmartyPants = "SmartyPantsSwedish.pl" if ($language =~ /^\s*(swedish|norwegian|finnish|danish)\s*$/i);
42
43 $SmartyPants = "SmartyPantsDutch.pl" if ($language =~ /^\s*dutch\s*$/i);
44
45
46 # Create a pipe and process
47 $me = $0;                               # Where am I?
48
49 # Am I running in Windoze?
50 my $os = $^O;
51
52 if ($os =~ /MSWin/) {
53         $me =~ s/\\([^\\]*?)$/\\/;      # Get just the directory portion
54 } else {
55         $me =~ s/\/([^\/]*?)$/\//;      # Get just the directory portion        
56 }
57
58 if ($os =~ /MSWin/) {
59         $xslt = "| xsltproc -nonet -novalid ..\\XSLT\\$xslt_file -" if ($xslt_file ne "");
60         open (MultiMarkdown, "| cd \"$me\"& .\\MultiMarkdown.pl | .\\$SmartyPants $xslt");
61 } else {
62         $xslt = "| xsltproc -nonet -novalid ../XSLT/$xslt_file -" if ($xslt_file ne "");
63         open (MultiMarkdown, "| cd \"$me\"; ./MultiMarkdown.pl | ./$SmartyPants $xslt");
64 }
65 print MultiMarkdown $data;
66
67 close(MultiMarkdown);
68
69
70 sub _XhtmlXSLT {
71         my $text = shift;
72         
73         my ($inMetaData, $currentKey) = (1,'');
74         
75         foreach my $line ( split /\n/, $text ) {
76                 $line =~ /^$/ and $inMetaData = 0 and next;
77                 if ($inMetaData) {
78                         if ($line =~ /^([a-zA-Z0-9][0-9a-zA-Z _-]*?):\s*(.*)$/ ) {
79                                 $currentKey = $1;
80                                 my $temp = $2;
81                                 $currentKey =~ s/ //g;
82                                 $g_metadata{$currentKey} = $temp;
83                                 if (lc($currentKey) eq "xhtmlxslt") {
84                                         $g_metadata{$currentKey} =~ s/\s*(\.xslt)?\s*$/.xslt/;
85                                         return $g_metadata{$currentKey};
86                                 }
87                         } else {
88                                 if ($currentKey eq "") {
89                                         # No metadata present
90                                         $inMetaData = 0;
91                                         next;
92                                 }
93                         }
94                 }
95         }
96                 
97         return;
98 }
99
100 sub _Language {
101         my $text = shift;
102         
103         my ($inMetaData, $currentKey) = (1,'');
104         
105         foreach my $line ( split /\n/, $text ) {
106                 $line =~ /^$/ and $inMetaData = 0 and next;
107                 if ($inMetaData) {
108                         if ($line =~ /^([a-zA-Z0-9][0-9a-zA-Z _-]*?):\s*(.*)$/ ) {
109                                 $currentKey = $1;
110                                 $currentKey =~ s/  / /g;
111                                 $g_metadata{$currentKey} = $2;
112                                 if (lc($currentKey) eq "language") {
113                                         return $g_metadata{$currentKey};
114                                 }
115                         } else {
116                                 if ($currentKey eq "") {
117                                         # No metadata present
118                                         $inMetaData = 0;
119                                         next;
120                                 }
121                         }
122                 }
123         }
124                 
125         return;
126 }