2 // xaml2html.cs: Tool to embed xaml in html
4 // Takes a given xaml file and embeds it in a simple stand-alone html page.
7 // Michael Dominic K. (mdk@mdk.am)
8 // Sebastien Pouliot <sebastien@ximian.com>
11 // See LICENSE file in the Moonlight distribution for licensing details
14 using System
.Collections
.Generic
;
17 using System
.Reflection
;
23 static readonly string html_template
= "<html xmlns=\"http://www.w3.org/1999/xhtml\" xml:lang=\"en\">\n" +
25 "<title>@TITLE@</title>\n" +
26 "<meta>@META@</meta>\n" +
28 "<body bgcolor=\"#eeeeee\">\n" +
29 "<object type=\"application/x-silverlight\" data=\"data:,\" id=\"slControl\" width=\"@WIDTH@\" height=\"@HEIGHT@\">\n" +
30 "<param name=\"background\" value=\"#FFFFFF\"/>\n" +
31 "<param name=\"source\" value=\"#xamlContent\"/>\n" +
33 "<script type=\"text/xaml\" id=\"xamlContent\">\n" +
34 "<?xml version=\"1.0\"?>\n" +
40 static int verbose
= 0;
44 Console
.WriteLine ("Usage is: xaml2html [--v] [--chain] [file.[xaml|.xap] ...]|\n");
47 static string FindMasterCanvasAttribute (string xml
, string attribute
, string def
)
50 XmlDocument document
= new XmlDocument ();
51 document
.LoadXml (xml
);
53 XmlNode node
= document
.GetElementsByTagName ("Canvas") [0];
54 return node
.Attributes
[attribute
].InnerText
;
58 // Failed, return default
62 static bool ProcessFile (string file
, string next
)
65 bool is_xap
= file
.EndsWith (".xap");
66 string xaml_basename
= Path
.GetFileNameWithoutExtension (file
);
67 string xaml_content
= File
.ReadAllText (file
);
68 string html_content
= is_xap
? (new StreamReader (Assembly
.GetExecutingAssembly ().GetManifestResourceStream ("sl2template.html"))).ReadToEnd () : html_template
;
70 string canvas_width
= FindMasterCanvasAttribute (xaml_content
, "Width", is_xap
? "1600" : "640");
71 string canvas_height
= FindMasterCanvasAttribute (xaml_content
, "Height", is_xap
? "1200" : "480");
74 html_content
= html_content
.Replace ("@XAML@", xaml_content
);
75 html_content
= html_content
.Replace ("@XAP_FILE@", Path
.GetFileName (file
));
76 html_content
= html_content
.Replace ("@TITLE@", xaml_basename
);
77 html_content
= html_content
.Replace ("@WIDTH@", canvas_width
);
78 html_content
= html_content
.Replace ("@HEIGHT@", canvas_height
);
81 next
= Path
.GetFileNameWithoutExtension (next
) + ".html";
82 string refresh
= String
.Format ("<meta http-equiv=\"refresh\" content=\"5;URL={0}\">", next
);
83 html_content
= html_content
.Replace ("@META@", refresh
);
85 html_content
= html_content
.Replace ("@META@", String
.Empty
);
88 File
.WriteAllText (xaml_basename
+ ".html", html_content
);
91 Console
.WriteLine ("Written {0}", xaml_basename
+ ".html");
97 Console
.WriteLine ("Failed to embed {0}, file missing?", file
);
99 Console
.WriteLine ("Exception: {0}", e
);
104 static int Main (string [] args
)
108 if (args
.Length
< 1){
113 var p
= new OptionSet () {
114 { "-v", v => verbose++ }
,
115 { "-chain", v => chain = true }
118 List
<string> files
= null;
120 files
= p
.Parse (args
);
121 } catch (OptionException
) {
122 Console
.WriteLine ("Try `xaml2html --help' for more information.");
126 for (int i
=0; i
< files
.Count
; i
++) {
127 string next
= (chain
&& (i
< files
.Count
- 1)) ? files
[i
+ 1] : null;
128 if (!ProcessFile (files
[i
], next
))