[monkeydoc] Change namespace base from MonkeyDoc to Monodoc.
[mono.git] / mcs / tools / monkeydoc / MonkeyDoc / generators / html / Error2Html.cs
1 using System;
2 using System.IO;
3 using System.Linq;
4 using System.Xml;
5 using System.Xml.XPath;
6 using System.Collections.Generic;
7
8 namespace Monodoc.Generators.Html
9 {
10         public class Error2Html : IHtmlExporter
11         {
12                 public string Export (string input, Dictionary<string, string> extraArgs)
13                 {
14                         return Htmlize (new XPathDocument (new StringReader (input)));
15                 }
16
17                 public string Export (Stream input, Dictionary<string, string> extraArgs)
18                 {
19                         return Htmlize (new XPathDocument (input));
20                 }
21
22                 public string CssCode {
23                         get {
24                                 return @"
25                                          #error_ref { 
26                                             background: #debcb0; 
27                                             border: 2px solid #782609; 
28                                          }
29                                          div.summary {
30                                                  font-size: 110%;
31                                                  font-weight: bolder;
32                                          }
33                                          div.details {
34                                                  font-size: 110%;
35                                                  font-weight: bolder;
36                                          }
37                                          div.code_example {
38                                                 background: #f5f5dd;
39                                                 border: 1px solid black;
40                                                 padding-left: 1em;
41                                                 padding-bottom: 1em;
42                                                 margin-top: 1em;
43                                                 white-space: pre;
44                                                 margin-bottom: 1em;
45                                          }
46                                          div.code_ex_title {
47                                                 position: relative;
48                                                 top: -1em;
49                                                 left: 30%;
50                                                 background: #cdcd82;
51                                                 border: 1px solid black;
52                                                 color: black;
53                                                 font-size: 65%;
54                                                 text-transform: uppercase;
55                                                 width: 40%;
56                                                 padding: 0.3em;
57                                                 text-align: center;
58                                          }";
59                         }
60                 }
61
62                 public string Htmlize (IXPathNavigable doc)
63                 {
64                         var navigator = doc.CreateNavigator ();
65                         var errorName = navigator.SelectSingleNode ("//ErrorDocumentation/ErrorName");
66                         var details = navigator.SelectSingleNode ("//ErrorDocumentation/Details");
67
68                         StringWriter sw = new StringWriter ();
69                         XmlWriter w = new XmlTextWriter (sw);
70                         
71                         WriteElementWithClass (w, "div", "header");
72                         w.WriteAttributeString ("id", "error_ref");
73                         WriteElementWithClass (w, "div", "subtitle", "Compiler Error Reference");
74                         WriteElementWithClass (w, "div", "title", "Error " + (errorName == null ? string.Empty : errorName.Value));
75                         w.WriteEndElement ();
76
77                         if (details != null) {
78                                 WriteElementWithClass (w, "div", "summary", "Summary");
79
80                                 var summary = details.SelectSingleNode ("/Summary");
81                                 w.WriteValue (summary == null ? string.Empty : summary.Value);
82                                 
83                                 WriteElementWithClass (w, "div", "details", "Details");
84                                 var de = details.SelectSingleNode ("/Details");
85                                 w.WriteValue (de == null ? string.Empty : de.Value);
86                         }
87                         
88                         foreach (XPathNavigator xmp in navigator.Select ("//ErrorDocumentation/Examples/string")) {
89                                 WriteElementWithClass (w, "div", "code_example");
90                                 WriteElementWithClass (w, "div", "code_ex_title", "Example");
91                                 w.WriteRaw (Mono.Utilities.Colorizer.Colorize (xmp.Value, "c#"));;
92                                 w.WriteEndElement ();
93                         }
94                         
95                         w.Close ();
96                         
97                         return sw.ToString ();
98                 }
99
100                 void WriteElementWithClass (XmlWriter w, string element, string cls, string content = null)
101                 {
102                         w.WriteStartElement (element);
103                         w.WriteAttributeString ("class", cls);
104                         if (!string.IsNullOrEmpty (content)) {
105                                 w.WriteValue (content);
106                                 w.WriteEndElement ();
107                         }
108                 }
109         }
110 }