File: Error2Html.cs

package info (click to toggle)
mono 6.14.1%2Bds2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,282,732 kB
  • sloc: cs: 11,182,461; xml: 2,850,281; ansic: 699,123; cpp: 122,919; perl: 58,604; javascript: 30,841; asm: 21,845; makefile: 19,602; sh: 10,973; python: 4,772; pascal: 925; sql: 859; sed: 16; php: 1
file content (110 lines) | stat: -rw-r--r-- 3,014 bytes parent folder | download | duplicates (11)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
using System;
using System.IO;
using System.Linq;
using System.Xml;
using System.Xml.XPath;
using System.Collections.Generic;

namespace Monodoc.Generators.Html
{
	public class Error2Html : IHtmlExporter
	{
		public string Export (string input, Dictionary<string, string> extraArgs)
		{
			return Htmlize (new XPathDocument (new StringReader (input)));
		}

		public string Export (Stream input, Dictionary<string, string> extraArgs)
		{
			return Htmlize (new XPathDocument (input));
		}

		public string CssCode {
			get {
				return @"
					 #error_ref { 
					    background: #debcb0; 
					    border: 2px solid #782609; 
					 }
					 div.summary {
						 font-size: 110%;
						 font-weight: bolder;
					 }
					 div.details {
						 font-size: 110%;
						 font-weight: bolder;
					 }
					 div.code_example {
						background: #f5f5dd;
						border: 1px solid black;
						padding-left: 1em;
						padding-bottom: 1em;
						margin-top: 1em;
						white-space: pre;
						margin-bottom: 1em;
					 }
					 div.code_ex_title {
						position: relative;
						top: -1em;
						left: 30%;
						background: #cdcd82;
						border: 1px solid black;
						color: black;
						font-size: 65%;
						text-transform: uppercase;
						width: 40%;
						padding: 0.3em;
						text-align: center;
					 }";
			}
		}

		public string Htmlize (IXPathNavigable doc)
		{
			var navigator = doc.CreateNavigator ();
			var errorName = navigator.SelectSingleNode ("//ErrorDocumentation/ErrorName");
			var details = navigator.SelectSingleNode ("//ErrorDocumentation/Details");

			StringWriter sw = new StringWriter ();
			XmlWriter w = new XmlTextWriter (sw);
			
			WriteElementWithClass (w, "div", "header");
			w.WriteAttributeString ("id", "error_ref");
			WriteElementWithClass (w, "div", "subtitle", "Compiler Error Reference");
			WriteElementWithClass (w, "div", "title", "Error " + (errorName == null ? string.Empty : errorName.Value));
			w.WriteEndElement ();

			if (details != null) {
				WriteElementWithClass (w, "div", "summary", "Summary");

				var summary = details.SelectSingleNode ("/Summary");
				w.WriteValue (summary == null ? string.Empty : summary.Value);
				
				WriteElementWithClass (w, "div", "details", "Details");
				var de = details.SelectSingleNode ("/Details");
				w.WriteValue (de == null ? string.Empty : de.Value);
			}
			
			foreach (XPathNavigator xmp in navigator.Select ("//ErrorDocumentation/Examples/string")) {
				WriteElementWithClass (w, "div", "code_example");
				WriteElementWithClass (w, "div", "code_ex_title", "Example");
				w.WriteRaw (Mono.Utilities.Colorizer.Colorize (xmp.Value, "c#"));;
				w.WriteEndElement ();
			}
			
			w.Close ();
			
			return sw.ToString ();
		}

		void WriteElementWithClass (XmlWriter w, string element, string cls, string content = null)
		{
			w.WriteStartElement (element);
			w.WriteAttributeString ("class", cls);
			if (!string.IsNullOrEmpty (content)) {
				w.WriteValue (content);
				w.WriteEndElement ();
			}
		}
	}
}