5 using System.Collections.Generic;
9 namespace Monodoc.Generators
13 interface IHtmlExporter
15 string CssCode { get; }
16 string Export (Stream input, Dictionary<string, string> extras);
17 string Export (string input, Dictionary<string, string> extras);
20 public class HtmlGenerator : IDocGenerator<string>
22 const string cachePrefix = "htmlcached#";
24 static string css_code;
26 IDocCache defaultCache;
27 static Dictionary<DocumentType, IHtmlExporter> converters;
29 static HtmlGenerator ()
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 () },
44 public HtmlGenerator (IDocCache defaultCache)
46 this.defaultCache = defaultCache;
49 public string Generate (HelpSource hs, string id)
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));
58 IEnumerable<string> parts;
59 if (hs.IsMultiPart (id, out parts))
60 return GenerateMultiPart (hs, parts, id);
62 if (hs.IsRawContent (id))
63 return hs.GetText (id) ?? string.Empty;
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));
70 IHtmlExporter exporter;
71 if (!converters.TryGetValue (type, out exporter))
72 return MakeHtmlError (string.Format ("Input type '{0}' not supported",
74 var result = hs.IsGeneratedContent (id) ?
75 exporter.Export (hs.GetCachedText (id), extraParams) :
76 exporter.Export (hs.GetCachedHelpStream (id), extraParams);
79 cache.CacheText (MakeCacheKey (hs, id, extraParams), result);
83 string GenerateMultiPart (HelpSource hs, IEnumerable<string> ids, string originalId)
85 var sb = new StringBuilder ();
86 foreach (var id in ids)
87 sb.AppendLine (Generate (hs, id));
89 var cache = defaultCache ?? hs.Cache;
91 cache.CacheText (MakeCacheKey (hs, originalId, null), sb.ToString ());
92 return sb.ToString ();
95 public static string InlineCss {
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);
117 string MakeHtmlError (string error)
119 return string.Format ("<html><head></head><body><p>{0}</p></body></html>", error);
122 string MakeCacheKey (HelpSource hs, string page, IDictionary<string,string> extraParams)
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;