Inheritance: AbstractWrapper
Ejemplo n.º 1
0
        public static string TransformDocumentationToHTML(XElement element, string rootNodeName, AbstractTypeProvider typeProvider, FrameworkVersion version)
        {
            if (element == null)
                return string.Empty;

            var rootNode = element.XPathSelectElement(rootNodeName);
            if (rootNode == null)
                return string.Empty;

            //var crossRefTags = new[] { "see", "seealso" };
            //foreach (var crossRefTag in crossRefTags)
            //{
            //    var crossRefs = rootNode.Descendants(crossRefTag);
            //    if (crossRefs.Any())
            //    {
            //        foreach (var crossRef in crossRefs)
            //        {
            //            var typeName = BaseWriter.GetCrossReferenceTypeName(crossRef);

            //            string target;
            //            var url = BaseWriter.CrossReferenceTypeToUrl(assemblyWrapper, typeName, version, out target);

            //            var href = url != null ? string.Format("<a href=\"{0}\" {2}>{1}</a>", url, typeName, target) : typeName;
            //            crossRef.ReplaceWith(href);
            //        }
            //    }
            //}

            var reader = rootNode.CreateReader();
            reader.MoveToContent();
            var innerXml = reader.ReadInnerXml();

            var innerText = innerXml;
            innerText = innerText.Replace("<summary>", "<p>");
            innerText = innerText.Replace("</summary>", "</p>");
            innerText = innerText.Replace("<para>", "<p>");
            innerText = innerText.Replace("</para>", "</p>");
            //innerText = innerText.Replace("<code", "<pre class=\"code-sample\">");
            //innerText = innerText.Replace("</code>", "</pre>");

            // scan for <see> and <seealso> cross-reference tags and replace with <a> links with the
            // content - which // can be a cref indication to a typename, or a href.
            var scanIndex = innerText.IndexOf(crossReferenceOpeningTagText, StringComparison.Ordinal);
            while (scanIndex >= 0)
            {
                var attrStart = innerText.IndexOf(innerCrefAttributeText, scanIndex, StringComparison.Ordinal);
                if (attrStart >= 0)
                {
                    int crossRefTagEndIndex;
                    var cref = ExtractCrefAttributeContent(innerText, attrStart, out crossRefTagEndIndex);
                    var replacement = BaseWriter.CreateCrossReferenceTagReplacement(typeProvider, cref, version);

                    var oldCrossRefTag = innerText.Substring(scanIndex, crossRefTagEndIndex - scanIndex);
                    innerText = innerText.Replace(oldCrossRefTag, replacement);

                    scanIndex += replacement.Length;
                }
                else
                {
                    attrStart = innerText.IndexOf(innerHrefAttributeText, scanIndex, StringComparison.Ordinal);
                    if (attrStart >= 0)
                    {
                        int crossRefTagEndIndex;
                        var url = ExtractHrefAttributeContent(innerText, attrStart, out crossRefTagEndIndex);
                        var replacement = string.Format("<a href=\"{0}\">{0}</a>", url);

                        var oldCrossRefTag = innerText.Substring(scanIndex, crossRefTagEndIndex - scanIndex);
                        innerText = innerText.Replace(oldCrossRefTag, replacement);

                        scanIndex += replacement.Length;
                    }
                    else
                        scanIndex++;
                }

                scanIndex = innerText.IndexOf(crossReferenceOpeningTagText, scanIndex, StringComparison.Ordinal);
            }

            return innerText;
        }
Ejemplo n.º 2
0
        private static string DocBlobToHTML(XElement rootNode, AbstractTypeProvider typeProvider, FrameworkVersion version)
        {
            using (var textWriter = new StringWriter())
            {
                var writerSettings = new XmlWriterSettings {
                    OmitXmlDeclaration = true
                };
                using (var writer = XmlWriter.Create(textWriter, writerSettings))
                {
                    var reader = rootNode.CreateReader();
                    while (reader.Read())
                    {
                        switch (reader.NodeType)
                        {
                        case XmlNodeType.Element:
                            // handle self-closing element, like <a />
                            // this must be read before any other reading is done
                            var selfClosingElement = reader.IsEmptyElement;

                            // element name substitution, if necessary
                            string elementName;
                            if (!NdocToHtmlElementMapping.TryGetValue(reader.LocalName, out elementName))
                            {
                                elementName = reader.LocalName;
                            }

                            // some elements can't be empty, use this variable for that
                            string emptyElementContents = null;

                            // start element
                            writer.WriteStartElement(elementName);

                            // copy over attributes
                            if (reader.HasAttributes)
                            {
                                for (int i = 0; i < reader.AttributeCount; i++)
                                {
                                    reader.MoveToAttribute(i);
                                    var attributeName  = reader.Name;
                                    var attributeValue = reader.Value;

                                    var isCref = string.Equals(attributeName, crefAttributeName, StringComparison.Ordinal);
                                    var isHref = string.Equals(attributeName, hrefAttributeName, StringComparison.Ordinal);
                                    var isName = string.Equals(attributeName, nameAttributeName, StringComparison.Ordinal);

                                    if (isCref)
                                    {
                                        // replace cref with href
                                        attributeName = hrefAttributeName;

                                        // extract type name from cref value for emptyElementContents
                                        var crefParts = attributeValue.Split(':');
                                        if (crefParts.Length != 2)
                                        {
                                            throw new InvalidOperationException();
                                        }
                                        var typeName   = crefParts[1];
                                        var targetType = typeProvider.GetType(typeName);
                                        if (targetType == null)
                                        {
                                            emptyElementContents = typeName;
                                        }
                                        else
                                        {
                                            emptyElementContents = targetType.CreateReferenceHtml(fullTypeName: true);
                                        }
                                    }
                                    else if (isHref)
                                    {
                                        // extract href value for emptyElementContents
                                        emptyElementContents = attributeValue;
                                    }
                                    else if (isName)
                                    {
                                        emptyElementContents = attributeValue;
                                    }

                                    writer.WriteAttributeString(attributeName, attributeValue);
                                }
                            }

                            // if this is a self-closing element, close it
                            if (selfClosingElement)
                            {
                                // write empty element contents, if any
                                if (!string.IsNullOrEmpty(emptyElementContents))
                                {
                                    writer.WriteRaw(emptyElementContents);
                                }

                                // close element now
                                writer.WriteEndElement();
                            }

                            break;

                        case XmlNodeType.EndElement:
                            writer.WriteEndElement();
                            break;

                        case XmlNodeType.Text:
                            writer.WriteRaw(reader.Value);
                            break;

                        default:
                            throw new InvalidOperationException();
                        }
                    }
                }

                return(textWriter.ToString());
            }
        }
Ejemplo n.º 3
0
 public void SetSecondaryTypeProvider(AbstractTypeProvider typeProvider)
 {
     _secondaryTypeProvider = typeProvider;
 }
Ejemplo n.º 4
0
        public static string TransformDocumentationToHTML(XElement element, string rootNodeName, AbstractTypeProvider typeProvider, FrameworkVersion version)
        {
            if (element == null)
            {
                return(string.Empty);
            }

            var rootNode = element.XPathSelectElement(rootNodeName);

            if (rootNode == null)
            {
                return(string.Empty);
            }

            if (rootNodeName.Equals("seealso", StringComparison.OrdinalIgnoreCase))
            {
                return(SeeAlsoElementToHTML(rootNode, typeProvider, version));
            }
            else
            {
                return(DocBlobToHTML(rootNode, typeProvider, version));
            }
        }
Ejemplo n.º 5
0
 public void SetSecondaryTypeProvider(AbstractTypeProvider typeProvider)
 {
     _secondaryTypeProvider = typeProvider;
 }