//=====================================================================

        /// <summary>
        /// This is used to set the state of a menu command on the View Help menu
        /// </summary>
        /// <param name="command">The command object</param>
        /// <param name="format">The help file format</param>
        private static void SetViewHelpCommandState(OleMenuCommand command, HelpFileFormats? format)
        {
            Array activeProjects = null;
            DTE dte = Utility.GetServiceFromPackage<DTE, DTE>(false);
            bool visible = false, enabled = false;

            if(dte != null)
            {
                Solution s = dte.Solution;

                // Hide the menu option if a SHFB project is not loaded
                if(s != null)
                {
                    visible = s.Projects.Cast<Project>().Any(
                        p => p.UniqueName.EndsWith(".shfbproj", StringComparison.OrdinalIgnoreCase));

                    // Check the active project for the specified help format if visible
                    if(visible)
                    {
                        try
                        {
                            activeProjects = dte.ActiveSolutionProjects as Array;
                        }
                        catch
                        {
                            // The above can throw an exception while the project is loading which
                            // we should ignore.
                        }

                        if(activeProjects != null && activeProjects.Length > 0)
                        {
                            Project p = activeProjects.GetValue(0) as Project;

                            if(p != null && p.Object != null && p.UniqueName.EndsWith(".shfbproj",
                              StringComparison.OrdinalIgnoreCase))
                            {
                                SandcastleBuilderProjectNode pn = (SandcastleBuilderProjectNode)p.Object;
                                string projectHelpFormat = (pn.GetProjectProperty("HelpFileFormat") ??
                                    HelpFileFormats.HtmlHelp1.ToString());

                                enabled = (!pn.BuildInProgress && (format == null || projectHelpFormat.IndexOf(
                                    format.ToString(), StringComparison.OrdinalIgnoreCase) != -1));
                            }
                        }
                    }
                }
            }

            command.Visible = visible;
            command.Enabled = enabled;
        }
Ejemplo n.º 2
0
        /// <summary>
        /// This is used to convert the collection to a string and append it
        /// to the specified string builder.
        /// </summary>
        /// <param name="format">The help file format to use</param>
        /// <param name="sb">The string builder to which the information is
        /// appended.</param>
        internal void ConvertToString(HelpFileFormats format, StringBuilder sb)
        {
            string guid, url, orderAttr, titleAttr;

            switch (format)
            {
            case HelpFileFormats.HtmlHelp1:
                if (children.Count == 0)
                {
                    sb.AppendFormat("<LI><OBJECT type=\"text/sitemap\">\r\n" +
                                    "<param name=\"Name\" value=\"{0}\">\r\n" +
                                    "<param name=\"Local\" value=\"{1}\">\r\n" +
                                    "</OBJECT></LI>\r\n", WebUtility.HtmlEncode(this.Title),
                                    WebUtility.HtmlEncode(this.DestinationFile));
                }
                else
                {
                    if (String.IsNullOrEmpty(this.DestinationFile))
                    {
                        sb.AppendFormat("<LI><OBJECT type=\"text/sitemap\">\r\n" +
                                        "<param name=\"Name\" value=\"{0}\">\r\n" +
                                        "</OBJECT></LI>\r\n",
                                        WebUtility.HtmlEncode(this.Title));
                    }
                    else
                    {
                        sb.AppendFormat("<LI><OBJECT type=\"text/sitemap\">\r\n" +
                                        "<param name=\"Name\" value=\"{0}\">\r\n" +
                                        "<param name=\"Local\" value=\"{1}\">\r\n" +
                                        "</OBJECT></LI>\r\n",
                                        WebUtility.HtmlEncode(this.Title),
                                        WebUtility.HtmlEncode(this.DestinationFile));
                    }

                    sb.Append("<UL>\r\n");
                    children.ConvertToString(format, sb);
                    sb.Append("</UL>\r\n");
                }
                break;

            case HelpFileFormats.MSHelp2:
            case HelpFileFormats.Website:
            case HelpFileFormats.OpenXml:
                if (!String.IsNullOrEmpty(this.DestinationFile) && format == HelpFileFormats.Website)
                {
                    url = this.DestinationFile.Replace('\\', '/');
                }
                else
                {
                    url = this.DestinationFile;
                }

                if (children.Count == 0)
                {
                    sb.AppendFormat("<HelpTOCNode Url=\"{0}\" Title=\"{1}\" />\r\n",
                                    WebUtility.HtmlEncode(url), WebUtility.HtmlEncode(this.Title));
                }
                else
                {
                    // Use a GUID to uniquely identify the entries with
                    // children.  This allows the ASP.NET web page to find
                    // them to load the child nodes dynamically.
                    guid = Guid.NewGuid().ToString();

                    // If there is no file for the root node, define the title
                    // property instead.
                    if (String.IsNullOrEmpty(url))
                    {
                        sb.AppendFormat("<HelpTOCNode Id=\"{0}\" Title=\"{1}\">\r\n",
                                        guid, WebUtility.HtmlEncode(this.Title));
                    }
                    else
                    {
                        sb.AppendFormat("<HelpTOCNode Id=\"{0}\" Url=\"{1}\" Title=\"{2}\">\r\n",
                                        guid, url, WebUtility.HtmlEncode(this.Title));
                    }

                    children.ConvertToString(format, sb);
                    sb.Append("</HelpTOCNode>\r\n");
                }
                break;

            case HelpFileFormats.MSHelpViewer:
                if (String.IsNullOrEmpty(this.DestinationFile))
                {
                    url       = WebUtility.HtmlEncode(this.Id);
                    titleAttr = String.Format(CultureInfo.InvariantCulture, " title=\"{0}\"",
                                              WebUtility.HtmlEncode(this.Title));
                }
                else
                {
                    url       = WebUtility.HtmlEncode(Path.GetFileNameWithoutExtension(this.DestinationFile));
                    titleAttr = String.Empty;
                }

                if (this.SortOrder != -1)
                {
                    orderAttr = String.Format(CultureInfo.InvariantCulture, " sortOrder=\"{0}\"", this.SortOrder);
                }
                else
                {
                    orderAttr = String.Empty;
                }

                if (children.Count == 0)
                {
                    sb.AppendFormat("<topic id=\"{0}\" file=\"{0}\"{1}{2} />\r\n", url, orderAttr, titleAttr);
                }
                else
                {
                    sb.AppendFormat("<topic id=\"{0}\" file=\"{0}\"{1}{2}>\r\n", url, orderAttr, titleAttr);

                    children.ConvertToString(format, sb);
                    sb.Append("</topic>\r\n");
                }
                break;

            default:
                throw new InvalidOperationException("Unknown TOC help format: " + format.ToString());
            }
        }