public static ConceptualLinkInfo Create(XPathNavigator node) { BuildComponentExceptions.NotNull(node, "node"); ConceptualLinkInfo info = new ConceptualLinkInfo(); string tempText = node.GetAttribute("target", String.Empty); int anchorStart = tempText.IndexOf("#"); if (anchorStart > 0) { // We retrieve the anchor text with the #... info._anchor = tempText.Substring(anchorStart); info._target = tempText.Substring(0, anchorStart); } else { info._target = tempText; } info._text = node.ToString().Trim(); return(info); }
public override void Apply(XmlDocument document, string key) { // find links XPathNodeIterator linkIterator = document.CreateNavigator().Select(conceptualLinks); if (linkIterator == null || linkIterator.Count == 0) { return; } // copy them to an array, because enumerating through an XPathNodeIterator // fails when the nodes in it are altered XPathNavigator[] linkNodes = BuildComponentUtilities.ConvertNodeIteratorToArray(linkIterator); foreach (XPathNavigator linkNode in linkNodes) { ConceptualLinkInfo link = ConceptualLinkInfo.Create(linkNode); // determine url, text, and link type string url = null; string text = null; ConceptualLinkType type = ConceptualLinkType.None; bool isValidLink = validGuid.IsMatch(link.Target); if (isValidLink) { // a valid link; try to fetch target info TargetInfo target = _targetController[link.Target]; if (target == null) { // no target found; issue warning, set link style to none, and text to in-source fall-back //type = LinkType.None; //type = LinkType.Index; text = BrokenLinkDisplayText(link.Target, link.Text); WriteMessage(MessageLevel.Warn, String.Format( "Unknown conceptual link target '{0}'.", link.Target)); } else { // found target; get url, text, and type from stored info url = target.Url; if (link.IsAnchored) { url += link.Anchor; } if (_showText && !String.IsNullOrEmpty(link.Text)) { text = link.Text; } else { text = target.Text; } type = target.Type; } } else { // not a valid link; issue warning, set link style to none, and text to invalid target //type = LinkType.None; text = BrokenLinkDisplayText(link.Target, link.Text); WriteMessage(MessageLevel.Warn, String.Format( "Invalid conceptual link target '{0}'.", link.Target)); } // Override the type, if valid... if (_baseLinkType != ConceptualLinkType.Null && type != ConceptualLinkType.None) { type = _baseLinkType; } // write opening link tag and target info XmlWriter writer = linkNode.InsertAfter(); switch (type) { case ConceptualLinkType.None: writer.WriteStartElement("span"); writer.WriteAttributeString("class", "nolink"); break; case ConceptualLinkType.Local: writer.WriteStartElement("a"); writer.WriteAttributeString("href", url); break; case ConceptualLinkType.Index: writer.WriteStartElement("mshelp", "link", "http://msdn.microsoft.com/mshelp"); writer.WriteAttributeString("keywords", link.Target.ToLower()); writer.WriteAttributeString("tabindex", "0"); break; case ConceptualLinkType.Id: string xhelp = String.Format("ms-xhelp://?Id={0}", link.Target); writer.WriteStartElement("a"); writer.WriteAttributeString("href", xhelp); break; } // write the link text writer.WriteString(text); // write the closing link tag writer.WriteEndElement(); writer.Close(); // delete the original tag linkNode.DeleteSelf(); } }
private void ProcessConceptualLink(XPathNavigator linkNode, string key) { // extract link information ConceptualLinkInfo link = ConceptualLinkInfo.Create(linkNode); if (link == null) { this.WriteMessage(MessageLevel.Warn, "Invalid conceptualLink element."); #if DEBUG this.WriteMessage(MessageLevel.Warn, linkNode.OuterXml); #endif } else { // determine url, text, and link type string url = null; string text = null; ConceptualLinkType type = ConceptualLinkType.None; bool isValidLink = validGuid.IsMatch(link.Target); if (isValidLink) { // a valid link; try to fetch target info TargetInfo target = _targetController[link.Target]; if (target == null) { // no target found; issue warning, set link style to none, and text to in-source fall-back //type = LinkType.None; //type = LinkType.Index; text = BrokenLinkDisplayText(link.Target, link.Text); WriteMessage(MessageLevel.Warn, String.Format( "Unknown conceptual link target '{0}'.", link.Target)); } else { // found target; get url, text, and type from stored info url = target.Url; if (link.IsAnchored) { url += link.Anchor; } if (_showText && !String.IsNullOrEmpty(link.Text)) { text = link.Text; } else { text = target.Text; } type = target.Type; } } else { // not a valid link; issue warning, set link style to none, and text to invalid target //type = LinkType.None; text = BrokenLinkDisplayText(link.Target, link.Text); WriteMessage(MessageLevel.Warn, String.Format( "Invalid conceptual link target '{0}'.", link.Target)); } // Override the type, if valid... if (_baseLinkType != ConceptualLinkType.Null && type != ConceptualLinkType.None) { type = _baseLinkType; } // write opening link tag and target info XmlWriter writer = linkNode.InsertAfter(); switch (type) { case ConceptualLinkType.None: writer.WriteStartElement("span"); writer.WriteAttributeString("class", "nolink"); break; case ConceptualLinkType.Local: writer.WriteStartElement("a"); writer.WriteAttributeString("href", url); break; case ConceptualLinkType.Index: writer.WriteStartElement("mshelp", "link", "http://msdn.microsoft.com/mshelp"); writer.WriteAttributeString("keywords", link.Target.ToLower()); writer.WriteAttributeString("tabindex", "0"); break; case ConceptualLinkType.Id: string xhelp = String.Format("ms-xhelp://?Id={0}", link.Target); writer.WriteStartElement("a"); writer.WriteAttributeString("href", xhelp); break; } // write the link text writer.WriteString(text); // write the closing link tag writer.WriteEndElement(); writer.Close(); } }