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, Dictionary<string, string> context)
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, context);
62 if (hs.IsRawContent (id))
63 return hs.GetText (id) ?? string.Empty;
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));
69 IHtmlExporter exporter;
70 if (!converters.TryGetValue (type, out exporter))
71 return MakeHtmlError (string.Format ("Input type '{0}' not supported",
73 var result = hs.IsGeneratedContent (id) ?
74 exporter.Export (hs.GetCachedText (id), context) :
75 exporter.Export (hs.GetCachedHelpStream (id), context);
78 cache.CacheText (MakeCacheKey (hs, id, context), result);
82 string GenerateMultiPart (HelpSource hs, IEnumerable<string> ids, string originalId, Dictionary<string, string> context)
84 var sb = new StringBuilder ();
85 foreach (var id in ids)
86 sb.AppendLine (Generate (hs, id, context));
88 var cache = defaultCache ?? hs.Cache;
90 cache.CacheText (MakeCacheKey (hs, originalId, null), sb.ToString ());
91 return sb.ToString ();
94 public static string InlineCss {
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);
116 string MakeHtmlError (string error)
118 return string.Format ("<html><head></head><body><p>{0}</p></body></html>", error);
121 string MakeCacheKey (HelpSource hs, string page, IDictionary<string,string> extraParams)
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;