just kick off another build
[moon.git] / tools / xaml2html / xaml2html.cs
blob824cf3c3d543ec344d37079c55ac6800c3cb7711
1 //
2 // xaml2html.cs: Tool to embed xaml in html
3 //
4 // Takes a given xaml file and embeds it in a simple stand-alone html page.
5 //
6 // Authors:
7 // Michael Dominic K. (mdk@mdk.am)
8 // Sebastien Pouliot <sebastien@ximian.com>
9 //
11 // See LICENSE file in the Moonlight distribution for licensing details
13 using System;
14 using System.Collections.Generic;
15 using System.IO;
16 using System.Xml;
17 using System.Reflection;
19 using NDesk.Options;
21 class XamlToHtml {
23 static readonly string html_template = "<html xmlns=\"http://www.w3.org/1999/xhtml\" xml:lang=\"en\">\n" +
24 "<head>\n" +
25 "<title>@TITLE@</title>\n" +
26 "<meta>@META@</meta>\n" +
27 "</head>\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" +
32 "</object>\n" +
33 "<script type=\"text/xaml\" id=\"xamlContent\">\n" +
34 "<?xml version=\"1.0\"?>\n" +
35 "@XAML@" +
36 "</script>\n" +
37 "</body>\n" +
38 "</html>\n";
40 static int verbose = 0;
42 static void Help ()
44 Console.WriteLine ("Usage is: xaml2html [--v] [--chain] [file.[xaml|.xap] ...]|\n");
47 static string FindMasterCanvasAttribute (string xml, string attribute, string def)
49 try {
50 XmlDocument document = new XmlDocument ();
51 document.LoadXml (xml);
53 XmlNode node = document.GetElementsByTagName ("Canvas") [0];
54 return node.Attributes [attribute].InnerText;
55 } catch {
58 // Failed, return default
59 return def;
62 static bool ProcessFile (string file, string next)
64 try {
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");
73 // Substitute
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);
80 if (next != null) {
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);
84 } else {
85 html_content = html_content.Replace ("@META@", String.Empty);
88 File.WriteAllText (xaml_basename + ".html", html_content);
90 if (verbose > 0)
91 Console.WriteLine ("Written {0}", xaml_basename + ".html");
93 return true;
95 catch (Exception e) {
96 if (verbose > 0)
97 Console.WriteLine ("Failed to embed {0}, file missing?", file);
98 if (verbose > 1)
99 Console.WriteLine ("Exception: {0}", e);
100 return false;
104 static int Main (string [] args)
106 bool chain = false;
108 if (args.Length < 1){
109 Help ();
110 return 1;
113 var p = new OptionSet () {
114 { "-v", v => verbose++ },
115 { "-chain", v => chain = true }
118 List<string> files = null;
119 try {
120 files = p.Parse (args);
121 } catch (OptionException) {
122 Console.WriteLine ("Try `xaml2html --help' for more information.");
123 return 1;
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))
129 return 127;
132 return 0;