Merge pull request #530 from jmp75/visualstudio-build
[mono.git] / mcs / class / monodoc / Monodoc / generators / HtmlGenerator.cs
1 using System;
2 using System.IO;
3 using System.Text;
4 using System.Linq;
5 using System.Collections.Generic;
6
7 using Monodoc;
8
9 namespace Monodoc.Generators
10 {
11         using Html;
12
13         interface IHtmlExporter
14         {
15                 string CssCode { get; }
16                 string Export (Stream input, Dictionary<string, string> extras);
17                 string Export (string input, Dictionary<string, string> extras);
18         }
19
20         public class HtmlGenerator : IDocGenerator<string>
21         {
22                 const string cachePrefix = "htmlcached#";
23
24                 static string css_code;
25
26                 IDocCache defaultCache;
27                 static Dictionary<DocumentType, IHtmlExporter> converters;
28
29                 static HtmlGenerator ()
30                 {
31                         converters = new Dictionary<DocumentType, IHtmlExporter> {
32                                 { DocumentType.EcmaXml, new Ecma2Html () },
33                                 { DocumentType.Man, new Man2Html () },
34                                 { DocumentType.TocXml, new Toc2Html () },
35                                 { DocumentType.EcmaSpecXml, new Ecmaspec2Html () },
36                                 { DocumentType.ErrorXml, new Error2Html () },
37                                 { DocumentType.Html, new Idem () },
38                                 { DocumentType.MonoBook, new MonoBook2Html () },
39                                 { DocumentType.AddinXml, new Addin2Html () },
40                                 { DocumentType.PlainText, new Idem () },
41                         };
42                 }
43
44                 public HtmlGenerator (IDocCache defaultCache)
45                 {
46                         this.defaultCache = defaultCache;
47                 }
48
49                 public string Generate (HelpSource hs, string id)
50                 {
51                         if (hs == null || string.IsNullOrEmpty (id))
52                                 return MakeHtmlError (string.Format ("Your request has found no candidate provider [hs=\"{0}\", id=\"{1}\"]",
53                                                                      hs == null ? "(null)" : hs.Name, id ?? "(null)"));
54                         var cache = defaultCache ?? hs.Cache;
55                         if (cache != null && cache.IsCached (MakeCacheKey (hs, id, null)))
56                                 return cache.GetCachedString (MakeCacheKey (hs, id, null));
57
58                         IEnumerable<string> parts;
59                         if (hs.IsMultiPart (id, out parts))
60                                 return GenerateMultiPart (hs, parts, id);
61
62                         if (hs.IsRawContent (id))
63                                 return hs.GetText (id) ?? string.Empty;
64
65                         Dictionary<string, string> extraParams = null;
66                         DocumentType type = hs.GetDocumentTypeForId (id, out extraParams);
67                         if (cache != null && extraParams != null && cache.IsCached (MakeCacheKey (hs, id, extraParams)))
68                                 return cache.GetCachedString (MakeCacheKey (hs, id, extraParams));
69
70                         IHtmlExporter exporter;
71                         if (!converters.TryGetValue (type, out exporter))
72                                 return MakeHtmlError (string.Format ("Input type '{0}' not supported",
73                                                                      type.ToString ()));
74                         var result = hs.IsGeneratedContent (id) ? 
75                                 exporter.Export (hs.GetCachedText (id), extraParams) :
76                                 exporter.Export (hs.GetCachedHelpStream (id), extraParams);
77
78                         if (cache != null)
79                                 cache.CacheText (MakeCacheKey (hs, id, extraParams), result);
80                         return result;
81                 }
82
83                 string GenerateMultiPart (HelpSource hs, IEnumerable<string> ids, string originalId)
84                 {
85                         var sb = new StringBuilder ();
86                         foreach (var id in ids)
87                                 sb.AppendLine (Generate (hs, id));
88
89                         var cache = defaultCache ?? hs.Cache;
90                         if (cache != null)
91                                 cache.CacheText (MakeCacheKey (hs, originalId, null), sb.ToString ());
92                         return sb.ToString ();
93                 }
94
95                 public static string InlineCss {
96                         get {
97                                 if (css_code != null)
98                                         return css_code;
99
100                                 System.Reflection.Assembly assembly = System.Reflection.Assembly.GetAssembly (typeof (HtmlGenerator));
101                                 Stream str_css = assembly.GetManifestResourceStream ("base.css");
102                                 StringBuilder sb = new StringBuilder ((new StreamReader (str_css)).ReadToEnd());
103                                 sb.Replace ("@@FONT_FAMILY@@", "Sans Serif");
104                                 sb.Replace ("@@FONT_SIZE@@", "100%");
105                                 css_code = sb.ToString () + converters.Values
106                                         .Select (c => c.CssCode)
107                                         .Where (css => !string.IsNullOrEmpty (css))
108                                         .DefaultIfEmpty (string.Empty)
109                                         .Aggregate (string.Concat);
110                                 return css_code;
111                         }
112                         set { 
113                                 css_code = value;
114                         }
115                 }
116
117                 string MakeHtmlError (string error)
118                 {
119                         return string.Format ("<html><head></head><body><p>{0}</p></body></html>", error);
120                 }
121
122                 string MakeCacheKey (HelpSource hs, string page, IDictionary<string,string> extraParams)
123                 {
124                         var key = cachePrefix + hs.SourceID + page;
125                         if (extraParams != null && extraParams.Count > 0) {
126                                 var paramPart = string.Join ("-", extraParams.Select (kvp => kvp.Key + kvp.Value));
127                                 key += '_' + paramPart;
128                         }
129                         return key;
130                 }
131         }
132 }