Esempio n. 1
0
        static Dictionary<string, string> msHelpContent = new Dictionary<string, string>(); // _Members.htm pages only

        /// <summary>
        /// For all ms-help URLs, verify that URL is correct.  If not, search for correct cpref? number to 
        /// be part of correct URL.  For Class Members pages, cache page content to be used for fixing method/property/etc. URLs
        /// </summary>
        /// <param name="line">ms-help URL to verify</param>
        /// <param name="documentor">MSDN IDocumentor class containing MsdnUrlValidator form</param>
        /// <returns>corrected url or original url, if no corrections made</returns>
        private static string VerifyUrl(string line, Msdn2Documenter documentor)
        {
            // If URL has already been verified, just replace initial URL with previous correction.
            // For Class Members pages, retrieve page content from cache.
            if (baseUrlCorrections.ContainsKey(line))
            {
                string correction = baseUrlCorrections[line];
                if (correction.IndexOf("_Members.htm") > 0)
                {
                    documentor.urlValidator.WebPage = msHelpContent[correction];
                }
                return correction;
            }

            string newLine = line;
            string initialUrl = line.Substring(6, line.Length - 7); // Remove href=" + trailing "
            string uri = initialUrl;
            bool valid = documentor.urlValidator.ValidateFile(uri);
            if (!valid)
            {
                // We know cpref2 (file) is not correct, check cpref3 thru cpref19
                // Cache page content for Class Members pages
                for (int i = 3; i <= 19; i++)
                {
                    string cpref = "cpref" + i.ToString();
                    uri = initialUrl.Replace("cpref2", cpref);
                    valid = documentor.urlValidator.ValidateFile(uri);
                    if (valid)
                    {
                        newLine = line.Replace(initialUrl, uri);
                        baseUrlCorrections[line] = newLine;
                        if (line.IndexOf("_Members.htm") > 0)
                        {
                            msHelpContent[newLine] = documentor.urlValidator.WebPage;
                        }
                        break;
                    }
                }
            }
            else // We don't need to validate this line again, preserve valid url and page content for Class Member pages
            {
                baseUrlCorrections[line] = newLine;
                if (line.IndexOf("_Members.htm") > 0)
                {
                    msHelpContent[newLine] = documentor.urlValidator.WebPage;
                }
            }
            return newLine;
        }
Esempio n. 2
0
        /// <summary>
        /// Correct .NET 2.0 HREFs (i.e. translate from V1.x HREFs to V2.0 HREFs.  This is needed because the
        /// help file HREF format has changed for .NET 2.0
        /// </summary>
        /// <param name="line">HREF url to be updated</param>
        /// <param name="documentor"></param>
        /// <param name="filePath"></param>
        /// <param name="validLocalUrl"></param>
        /// <returns>Updated url</returns>
        private static string FixV20HRef(string line, Msdn2Documenter documentor, string filePath, ref bool validLocalUrl)
        {
            validLocalUrl = true; // Only for local URLs, ignore otherwise

            // Class URLs
            string url = line.Substring(6); // Remove HREF="
            int index = url.IndexOf("\"");
            url = url.Substring(0, index); // Remove trailing "
            if (url.IndexOf("ms-help://") == -1)
            {
                validLocalUrl = File.Exists(filePath + url);
                return line; // not a MS-Help URL
            }

            if (url.IndexOf("Class") != -1 && url.IndexOf("Topic") != -1)
            {
                url = url.Substring(url.IndexOf("html/") + 5); // Remove html/
                string partialUrl = url.Replace("Topic.htm", ""); // Remove Topic.htm
                string methodName = partialUrl.Substring(partialUrl.IndexOf("Class") + 5); // Retrieve name of method to look for duplicates

                partialUrl = partialUrl.Substring(0, partialUrl.IndexOf("Class"));

                StringBuilder newLink = new StringBuilder();
                string ns = "";
                for (int i = partialUrl.Length - 1; i > 0; i--)
                {
                    if (Char.IsUpper(partialUrl[i]))
                    {
                        ns = partialUrl.Substring(0, i);
                        if (namespaces.ContainsKey(ns))
                        {
                            string className = partialUrl.Replace(ns, "");
                            string fullObjectName = namespaces[ns] + className;

                            newLink.Append("T_");
                            newLink.Append(namespaces[ns] + className);
                            newLink.Append("_Members.htm");
                            break;
                        }
                    }
                }
                line = line.Replace(url, newLink.ToString());
                line = VerifyMethodUrl(line, documentor, methodName);
            }
            else if (line.IndexOf("_") == -1) // Namespace URL
            {
                url = url.Substring(url.IndexOf("html/") + 5); // Remove html/
                StringBuilder newLink = new StringBuilder();
                newLink.Append("N_");
                for(int i = 0; i < url.Length; i++)
                {
                    if (i > 0)
                        if (!char.IsUpper(url[i - 1]) && char.IsUpper(url[i]))
                            newLink.Append("_");
                    newLink.Append(url[i]);
                }
                line = line.Replace(url, newLink.ToString());
                line = VerifyUrl(line, documentor);
            }
            else // otherwise, just verify URL is in correct file
            {
                line = VerifyUrl(line, documentor);
            }
            return line;
        }
Esempio n. 3
0
        /// <summary>
        /// Read HTML file one line at a time.  If HREF is found, call Fix20HRef to update for V2.0 formatting
        /// </summary>
        /// <param name="project">project object containing properties for help file</param>
        /// <param name="fullPath">Fully qualified HTML file path</param>
        /// <param name="encoding">Current encoding for the HTML file</param>
        /// <param name="documentor">MSDN IDocumentor needed by helper methods</param>
        public static void UpdateHtmlHrefs(Project project, string fullPath, Encoding encoding, Msdn2Documenter documentor)
        {
            InitializeNamespaces(project);

            // For generated .html file, create a temporary (.tmp) file with corrected ms-help URLs.  When
            // finished correcting URLs, swap temporary file for generated one, then delete original file.
            string filePath = fullPath.Substring(0,fullPath.LastIndexOf("\\") + 1);
            string tempFile = fullPath + ".tmp";
            using (StreamReader streamReader = new StreamReader(File.Open(fullPath, FileMode.Open), encoding))
            using (StreamWriter streamWriter = new StreamWriter(File.Open(tempFile, FileMode.Create), encoding))
            {
                // Search generated .html file for href links.  For each link, call FixV20HRef to verify/correct link.
                string line;
                do
                {
                    line = streamReader.ReadLine();
                    if (line != null)
                    {
                        int index = 0;
                        while (index >= 0 && index < line.Length && line.IndexOf("href=\"",index) > 0)
                        {
                            index = line.IndexOf("href=\"", index);
                            int indexEnd = line.IndexOf("\"", index + 6);
                            string href = line.Substring(index, (indexEnd - index + 1));
                            bool validLocalUrl = false;
                            if (href.IndexOf("ms-help://") == -1)
                            {
                                line = FixLocalUrl(line,href,filePath,index);
                            }
                            else
                            {
                                string newHRef = FixV20HRef(href, documentor, filePath, ref validLocalUrl);
                                line = line.Replace(href, newHRef);
                            }
                            index++; // look for another HREF
                        }
                        streamWriter.WriteLine(line);
                    }
                }
                while (line != null);
            }
            // Swap updated HTML file with original HTML file, then delete original file.
            System.IO.File.Replace(tempFile, fullPath, fullPath + ".bak");
            System.IO.File.Delete(fullPath + ".bak");
        }
Esempio n. 4
0
        /// <summary>
        /// Verify URL is in correct file (w/ VerifyURL), then use content from Class Members help page to find 
        /// actual URL for method/property/etc.
        /// </summary>
        /// <param name="line">ms-help URL to verify</param>
        /// <param name="documentor">MSDN IDocumentor class containing MsdnUrlValidator form</param>
        /// <param name="methodName">method/property/etc. for URL</param>
        /// <returns>corrected url or original url, if no corrections made</returns>
        private static string VerifyMethodUrl(string line, Msdn2Documenter documentor, string methodName)
        {
            // Find correct Class member page and get page content
            string url = VerifyUrl(line, documentor);

            // Retrieve page content, search for method/property name.  After name is found,
            // use URL from <a> tag immediately preceeding display text to update URL with 
            // correct value.
            string contents = documentor.urlValidator.WebPage;
            int index = contents.IndexOf(">" + methodName + "<");
            if (index > 2)
            {
                // We need the URL between ""
                index--; // Should be "
                int start = index - 1;
                while (start > 0)
                {
                    if (contents[start] == '"')
                        break;
                    start--;
                }
                // If we found what we're looking for, update URL with link from page content
                if (start > 0)
                {
                    string partial = contents.Substring(start + 1, index - start - 1);
                    index = url.LastIndexOf("/");
                    url = url.Substring(0, index + 1);
                    url += partial;
                    url += '"';
                }
            }
            return url;
        }