using System;
using System.IO;
using System.Text;
using System.Xml;
using System.Xml.Xsl;
using System.Xml.XPath;
using System.Collections.Generic;
namespace Monodoc.Generators.Html
{
public class Addin2Html : IHtmlExporter
{
public string CssCode {
get {
return string.Empty;
}
}
public string Export (Stream stream, Dictionary extraArgs)
{
using (var reader = new StreamReader (stream))
return Htmlize (GetAddin (reader, extraArgs["AddinID"]),
extraArgs["show"],
extraArgs["AddinID"],
extraArgs["FileID"],
extraArgs["NodeID"]);
}
public string Export (string input, Dictionary extraArgs)
{
return Htmlize (GetAddin (new StringReader (input), extraArgs["AddinID"]),
extraArgs["show"],
extraArgs["AddinID"],
extraArgs["FileID"],
extraArgs["NodeID"]);
}
XmlElement GetAddin (TextReader reader, string addinId)
{
XmlDocument doc = new XmlDocument ();
doc.Load (reader);
XmlElement addin = (XmlElement) doc.SelectSingleNode ("Addins/Addin[@fullId='" + addinId + "']");
return addin != null ? addin : null;
}
public string Htmlize (XmlElement addin, string urlType, string addinId, string fileId, string path)
{
if (urlType == Monodoc.Providers.AddinsHelpSource.AddinPrefix)
return GetAddinTextFromUrl (addin, addinId, fileId);
else if (urlType == Monodoc.Providers.AddinsHelpSource.ExtensionPrefix)
return GetExtensionTextFromUrl (addin, addinId, fileId, path);
else if (urlType == Monodoc.Providers.AddinsHelpSource.ExtensionNodePrefix)
return GetExtensionNodeTextFromUrl (addin, addinId, fileId, path);
return null;
}
protected string GetAddinTextFromUrl (XmlElement addin, string addinId, string fileId)
{
if (addin == null)
return "Add-in not found: " + addinId + "";
StringBuilder sb = new StringBuilder ("");
sb.Append ("").Append (addin.GetAttribute ("name")).Append ("
");
XmlElement docs = (XmlElement) addin.SelectSingleNode ("Description");
if (docs != null)
sb.Append (docs.InnerText);
sb.Append ("");
sb.AppendFormat ("Id | {0} |
", addin.GetAttribute ("addinId"));
sb.AppendFormat ("Namespace | {0} |
", addin.GetAttribute ("namespace"));
sb.AppendFormat ("Version | {0} |
", addin.GetAttribute ("version"));
sb.Append ("
");
sb.Append ("Extension Points:
");
sb.Append ("");
foreach (XmlElement ep in addin.SelectNodes ("ExtensionPoint")) {
sb.AppendFormat ("- {3}
", fileId, addinId, ep.GetAttribute ("path"), ep.GetAttribute ("name"));
}
sb.Append ("
");
sb.Append ("");
return sb.ToString ();
}
protected string GetExtensionTextFromUrl (XmlElement addin, string addinId, string fileId, string path)
{
if (addin == null)
return "Add-in not found: " + addinId + "";
XmlElement ext = (XmlElement) addin.SelectSingleNode ("ExtensionPoint[@path='" + path + "']");
if (ext == null)
return "Extension point not found: " + path + "";
StringBuilder sb = new StringBuilder ("");
sb.Append ("").Append (ext.GetAttribute ("name")).Append ("
");
path = path.Replace ("/", " / ");
sb.Append ("Path: ").Append (path).Append ("
");
XmlElement desc = (XmlElement) ext.SelectSingleNode ("Description");
if (desc != null)
sb.Append (desc.InnerText);
sb.Append ("Extension Nodes:
");
sb.Append ("");
foreach (XmlElement en in ext.SelectNodes ("ExtensionNode")) {
string nid = en.GetAttribute ("id");
string nname = en.GetAttribute ("name");
string sdesc = "";
desc = (XmlElement) en.SelectSingleNode ("Description");
if (desc != null)
sdesc = desc.InnerText;
sb.AppendFormat ("{3} | {4} |
", fileId, addinId, nid, nname, sdesc);
}
sb.Append ("
");
sb.Append ("");
return sb.ToString ();
}
protected string GetExtensionNodeTextFromUrl (XmlElement addin, string addinId, string fileId, string nodeId)
{
if (addin == null)
return "Add-in not found: " + addinId + "";
XmlElement node = (XmlElement) addin.SelectSingleNode ("ExtensionNodeType[@id='" + nodeId + "']");
if (node == null)
return "Extension point not found: " + nodeId + "";
StringBuilder sb = new StringBuilder ("");
sb.Append ("").Append (node.GetAttribute ("name")).Append ("
");
XmlElement desc = (XmlElement) node.SelectSingleNode ("Description");
if (desc != null)
sb.Append (desc.InnerText);
sb.Append ("Attributes:
");
sb.Append ("");
sb.Append ("Name | ");
sb.Append ("Type | ");
sb.Append ("Required | ");
sb.Append ("Localizable | ");
sb.Append ("Description | ");
sb.Append ("
");
sb.Append ("id | ");
sb.Append ("System.String | ");
sb.Append (" | ");
sb.Append (" | ");
sb.Append ("Identifier of the node. | ");
sb.Append ("
");
foreach (XmlElement at in node.SelectNodes ("Attributes/Attribute")) {
sb.Append ("");
sb.AppendFormat ("{0} | ", at.GetAttribute ("name"));
sb.AppendFormat ("{0} | ", at.GetAttribute ("type"));
if (at.GetAttribute ("required") == "True")
sb.Append ("Yes | ");
else
sb.Append (" | ");
if (at.GetAttribute ("localizable") == "True")
sb.Append ("Yes | ");
else
sb.Append (" | ");
string sdesc = "";
desc = (XmlElement) at.SelectSingleNode ("Description");
if (desc != null)
sdesc = desc.InnerText;
sb.AppendFormat ("{0} | ", sdesc);
sb.Append ("
");
}
sb.Append ("
");
XmlNodeList children = node.SelectNodes ("ChildNodes/ExtensionNode");
if (children.Count > 0) {
sb.Append ("Child Nodes:
");
sb.Append ("");
foreach (XmlElement en in children) {
string nid = en.GetAttribute ("id");
string nname = en.GetAttribute ("name");
string sdesc = "";
desc = (XmlElement) en.SelectSingleNode ("Description");
if (desc != null)
sdesc = desc.InnerText;
sb.AppendFormat ("{3} | {4} |
", fileId, addinId, nid, nname, sdesc);
}
sb.Append ("
");
}
sb.Append ("");
return sb.ToString ();
}
}
}