4500c9d30bbe29fcef7aa1a8c4720dc83d354cdf
[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, Dictionary<string, string> context)
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, context);
61
62                         if (hs.IsRawContent (id))
63                                 return hs.GetText (id) ?? string.Empty;
64
65                         DocumentType type = hs.GetDocumentTypeForId (id);
66                         if (cache != null && context != null && cache.IsCached (MakeCacheKey (hs, id, context)))
67                                 return cache.GetCachedString (MakeCacheKey (hs, id, context));
68
69                         IHtmlExporter exporter;
70                         if (!converters.TryGetValue (type, out exporter))
71                                 return MakeHtmlError (string.Format ("Input type '{0}' not supported",
72                                                                      type.ToString ()));
73                         var result = hs.IsGeneratedContent (id) ? 
74                                 exporter.Export (hs.GetCachedText (id), context) :
75                                 exporter.Export (hs.GetCachedHelpStream (id), context);
76
77                         if (cache != null)
78                                 cache.CacheText (MakeCacheKey (hs, id, context), result);
79                         return result;
80                 }
81
82                 string GenerateMultiPart (HelpSource hs, IEnumerable<string> ids, string originalId, Dictionary<string, string> context)
83                 {
84                         var sb = new StringBuilder ();
85                         foreach (var id in ids)
86                                 sb.AppendLine (Generate (hs, id, context));
87
88                         var cache = defaultCache ?? hs.Cache;
89                         if (cache != null)
90                                 cache.CacheText (MakeCacheKey (hs, originalId, null), sb.ToString ());
91                         return sb.ToString ();
92                 }
93
94                 public static string InlineCss {
95                         get {
96                                 if (css_code != null)
97                                         return css_code;
98
99                                 System.Reflection.Assembly assembly = System.Reflection.Assembly.GetAssembly (typeof (HtmlGenerator));
100                                 Stream str_css = assembly.GetManifestResourceStream ("base.css");
101                                 StringBuilder sb = new StringBuilder ((new StreamReader (str_css)).ReadToEnd());
102                                 sb.Replace ("@@FONT_FAMILY@@", "Sans Serif");
103                                 sb.Replace ("@@FONT_SIZE@@", "100%");
104                                 css_code = sb.ToString () + converters.Values
105                                         .Select (c => c.CssCode)
106                                         .Where (css => !string.IsNullOrEmpty (css))
107                                         .DefaultIfEmpty (string.Empty)
108                                         .Aggregate (string.Concat);
109                                 return css_code;
110                         }
111                         set { 
112                                 css_code = value;
113                         }
114                 }
115
116                 string MakeHtmlError (string error)
117                 {
118                         return string.Format ("<html><head></head><body><p>{0}</p></body></html>", error);
119                 }
120
121                 string MakeCacheKey (HelpSource hs, string page, IDictionary<string,string> extraParams)
122                 {
123                         var key = cachePrefix + hs.SourceID + page;
124                         if (extraParams != null && extraParams.Count > 0) {
125                                 var paramPart = string.Join ("-", extraParams.Select (kvp => kvp.Key + kvp.Value));
126                                 key += '_' + paramPart;
127                         }
128                         return key;
129                 }
130         }
131 }