private void WriteExamples(CmdletPageWriter writer, string cmdletName) { XmlDocument document; // lack of examples will be reported in the logs by the native help generator if (!ExamplesCache.TryGetValue(cmdletName, out document)) { return; } var set = document.SelectSingleNode("examples"); if (set == null) { return; } var sb = new StringBuilder(); int exampleIndex = 1; var examples = set.SelectNodes("example"); foreach (XmlNode example in examples) { sb.AppendFormat("<h4>Example {0}</h4>", exampleIndex); sb.Append("<pre class=\"example\">"); var code = example.SelectSingleNode("code"); if (code == null) { Logger.LogError("Unable to find examples <code> tag for cmdlet " + cmdletName); } var codeSample = code.InnerText.Trim('\r', '\n'); codeSample = codeSample.Replace(Environment.NewLine, "<br/>"); sb.AppendFormat("<div class=\"code\">{0}</div>", codeSample); var description = example.SelectSingleNode("description"); if (description == null) { Logger.LogError("Unable to find examples <description> tag for cmdlet " + cmdletName); } // use InnerXml here to allow for <br/> and other layout format tags, these get stripped // if we use InnerText var innerXml = description.InnerXml; // convert <url>link</url> elements to anchors (pshelp strips the tags to leave the link) /*if (innerXml.Contains("<href>")) * { * }*/ sb.AppendFormat("<div class=\"description\">{0}</div>", innerXml); sb.Append("</pre>"); exampleIndex++; } writer.AddPageElement(CmdletPageWriter.ExamplesElementKey, sb.ToString()); }
private void WriteExamples(XmlWriter writer, string cmdletName) { XmlDocument document; if (!ExamplesCache.TryGetValue(cmdletName, out document)) { Console.WriteLine("NO EXAMPLES - {0}", cmdletName); return; } var set = document.SelectSingleNode("examples"); if (set == null || !set.HasChildNodes) { Console.WriteLine("NO EXAMPLES - {0} (file present but empty)", cmdletName); return; } writer.WriteStartElement("examples"); { int exampleIndex = 1; var examples = set.SelectNodes("example"); foreach (XmlNode example in examples) { writer.WriteStartElement("example"); { writer.WriteElementString("title", string.Format(exampleTitleFormat, exampleIndex)); { // code tag CANNOT HAVE PARA TAGS var code = example.SelectSingleNode("code"); if (code == null) { Logger.LogError("Unable to find examples <code> tag for cmdlet " + cmdletName); } writer.WriteRawElementString("code", code.InnerXml); // remarks tag MUST HAVE PARA TAGS var description = example.SelectSingleNode("description"); if (description == null) { Logger.LogError("Unable to find examples <description> tag for cmdlet " + cmdletName); } var correctedText = DocumentationUtils.ProcessLines(description.InnerXml, s => string.Format("<para>{0}</para>", s)); // we use br/ tags to format the description for webhelp, for native help we can replace // with simple newlines correctedText = correctedText.Replace("<br />", "\n"); // xml handler shows us <br/> as <br /> // <url>link</url> elements are stripped to leave the inner link text for the user to copy/paste // (web help converts these to <a href="link" />) correctedText = correctedText.Replace("<url>", ""); correctedText = correctedText.Replace("</url>", ""); var remarks = string.Format(remarksFormat, correctedText); writer.WriteUnescapedElementString("remarks", remarks); } } writer.WriteEndElement(); exampleIndex++; } } writer.WriteEndElement(); }