Пример #1
0
        /* Function: Load
         * Loads <Style.txt> and returns it as a <Styles.Advanced> object.  If there are any errors found in <Style.txt> they will be added
         * to the list and the function will return null.
         */
        public Styles.Advanced Load(Path path, Errors.ErrorList errors)
        {
            Styles.Advanced style = new Styles.Advanced(path);

            using (ConfigFile file = new ConfigFile())
            {
                if (!file.Open(path,
                               Config.PropertySource.StyleConfigurationFile,
                               ConfigFile.FileFormatFlags.MakeIdentifiersLowercase |
                               ConfigFile.FileFormatFlags.CondenseIdentifierWhitespace, errors))
                {
                    return(null);
                }

                int    errorCount = errors.Count;
                string lcIdentifier, value;
                System.Text.RegularExpressions.Match match;

                while (file.Get(out lcIdentifier, out value))
                {
                    // Inherit

                    if (inheritRegex.IsMatch(lcIdentifier))
                    {
                        style.AddInheritedStyle(value, file.PropertyLocation, null);
                        continue;
                    }


                    // Link

                    match = linkRegex.Match(lcIdentifier);
                    if (match.Success)
                    {
                        PageType pageType = PageType.All;
                        if (match.Groups[1].Success)
                        {
                            PageType?pageTypeTemp = PageTypes.FromName(match.Groups[1].ToString());

                            if (pageTypeTemp == null)
                            {
                                file.AddError(Locale.Get("NaturalDocs.Engine", "ConfigFile.NotAValidIdentifier(identifier)", lcIdentifier));
                            }
                            else
                            {
                                pageType = pageTypeTemp.Value;
                            }
                        }

                        Path linkedFile = value;

                        if (!Styles.Manager.LinkableFileExtensions.Contains(linkedFile.Extension))
                        {
                            file.AddError(Locale.Get("NaturalDocs.Engine", "Style.txt.CantLinkFileWithExtension(extension)",
                                                     linkedFile.Extension));
                        }
                        else if (linkedFile.Extension.ToLower() == "css" && pageType != PageType.All)
                        {
                            file.AddError(Locale.Get("NaturalDocs.Engine", "Style.txt.CantLinkCSSFileToSpecificPageTypes(pageType)",
                                                     PageTypes.NameOf(pageType)));
                        }
                        else
                        {
                            if (linkedFile.IsRelative)
                            {
                                linkedFile = style.Folder + "/" + linkedFile;
                            }

                            if (!System.IO.File.Exists(linkedFile))
                            {
                                file.AddError(Locale.Get("NaturalDocs.Engine", "Style.txt.CantFindLinkedFile(name)", linkedFile));
                            }
                            else if (!style.Folder.Contains(linkedFile))
                            {
                                file.AddError(Locale.Get("NaturalDocs.Engine", "Style.txt.LinkedFileMustBeInStyleFolder(name, folder)", linkedFile, style.Folder));
                            }
                            else
                            {
                                style.AddLinkedFile(linkedFile, file.PropertyLocation, pageType);
                            }
                        }

                        continue;
                    }


                    // OnLoad

                    match = onLoadRegex.Match(lcIdentifier);
                    if (match.Success)
                    {
                        PageType pageType = PageType.All;
                        if (match.Groups[1].Success)
                        {
                            PageType?pageTypeTemp = PageTypes.FromName(match.Groups[1].ToString());

                            if (pageTypeTemp == null)
                            {
                                file.AddError(Locale.Get("NaturalDocs.Engine", "ConfigFile.NotAValidIdentifier(identifier)", lcIdentifier));
                            }
                            else
                            {
                                pageType = pageTypeTemp.Value;
                            }
                        }

                        style.AddOnLoad(value, file.PropertyLocation, pageType);
                        continue;
                    }


                    // Home Page

                    if (homePageRegex.IsMatch(lcIdentifier))
                    {
                        Path   homePageFile = value;
                        string lcExtension  = homePageFile.Extension.ToLower();

                        if (lcExtension != "html" && lcExtension != "htm")
                        {
                            file.AddError(Locale.Get("NaturalDocs.Engine", "Style.txt.HomePageMustHaveHTMLExtension(extension)",
                                                     homePageFile.Extension));
                        }
                        else
                        {
                            if (homePageFile.IsRelative)
                            {
                                homePageFile = style.Folder + "/" + homePageFile;
                            }

                            if (!System.IO.File.Exists(homePageFile))
                            {
                                file.AddError(Locale.Get("NaturalDocs.Engine", "Style.txt.CantFindHomePageFile(name)", homePageFile));
                            }
                            else if (!style.Folder.Contains(homePageFile))
                            {
                                file.AddError(Locale.Get("NaturalDocs.Engine", "Style.txt.HomePageFileMustBeInStyleFolder(name, folder)", homePageFile, style.Folder));
                            }
                            else
                            {
                                style.SetHomePage(homePageFile, file.PropertyLocation);
                            }
                        }

                        continue;
                    }


                    file.AddError(Locale.Get("NaturalDocs.Engine", "ConfigFile.NotAValidIdentifier(identifier)", lcIdentifier));
                }


                // Check for files or JavaScript linked to custom home pages since they wouldn't apply.

                if (style.HomePage != null)
                {
                    if (style.Links != null)
                    {
                        foreach (var link in style.Links)
                        {
                            if (link.Type == PageType.Home)
                            {
                                errors.Add(Locale.Get("NaturalDocs.Engine", "Style.txt.CantUseHomeLinksWithCustomHomePage"), link.PropertyLocation);
                            }
                        }
                    }

                    if (style.OnLoad != null)
                    {
                        foreach (var onLoad in style.OnLoad)
                        {
                            if (onLoad.Type == PageType.Home)
                            {
                                errors.Add(Locale.Get("NaturalDocs.Engine", "Style.txt.CantUseHomeOnLoadWithCustomHomePage"), onLoad.PropertyLocation);
                            }
                        }
                    }
                }


                if (errorCount == errors.Count)
                {
                    return(style);
                }
                else
                {
                    return(null);
                }
            }
        }
Пример #2
0
        /* Function: Load
         * Loads <Style.txt> and returns it as a <Styles.Advanced> object.  If there are any errors found in <Style.txt> they will be added
         * to the list and the function will return null.
         */
        public Styles.Advanced Load(Path path, Errors.ErrorList errors)
        {
            Styles.Advanced style = new Styles.Advanced(path);

            using (ConfigFile file = new ConfigFile())
            {
                if (!file.Open(path,
                               Config.PropertySource.StyleConfigurationFile,
                               ConfigFile.FileFormatFlags.MakeIdentifiersLowercase |
                               ConfigFile.FileFormatFlags.CondenseIdentifierWhitespace, errors))
                {
                    return(null);
                }

                int    errorCount = errors.Count;
                string lcIdentifier, value;
                System.Text.RegularExpressions.Match match;

                while (file.Get(out lcIdentifier, out value))
                {
                    // Inherit

                    if (inheritRegex.IsMatch(lcIdentifier))
                    {
                        style.AddInheritedStyle(value, file.PropertyLocation, null);
                        continue;
                    }


                    // Link

                    match = linkRegex.Match(lcIdentifier);
                    if (match.Success)
                    {
                        PageType pageType = PageType.All;
                        if (match.Groups[1].Success)
                        {
                            PageType?pageTypeTemp = PageTypes.FromName(match.Groups[1].ToString());

                            if (pageTypeTemp == null)
                            {
                                file.AddError(Locale.Get("NaturalDocs.Engine", "ConfigFile.NotAValidIdentifier(identifier)", lcIdentifier));
                            }
                            else
                            {
                                pageType = pageTypeTemp.Value;
                            }
                        }

                        Path linkedFile = value;

                        if (linkableFileExtensionsRegex.IsMatch(linkedFile.Extension))
                        {
                            Path fullLinkedFile = style.Folder + "/" + linkedFile;

                            if (System.IO.File.Exists(fullLinkedFile))
                            {
                                style.AddLinkedFile(fullLinkedFile, file.PropertyLocation, pageType);
                            }
                            else
                            {
                                file.AddError(Locale.Get("NaturalDocs.Engine", "Style.txt.CantFindLinkedFile(name)", fullLinkedFile));
                            }
                        }
                        else
                        {
                            file.AddError(Locale.Get("NaturalDocs.Engine", "Style.txt.CantLinkFileWithExtension(extension)",
                                                     linkedFile.Extension));
                        }

                        continue;
                    }


                    // OnLoad

                    match = onLoadRegex.Match(lcIdentifier);
                    if (match.Success)
                    {
                        PageType pageType = PageType.All;
                        if (match.Groups[1].Success)
                        {
                            PageType?pageTypeTemp = PageTypes.FromName(match.Groups[1].ToString());

                            if (pageTypeTemp == null)
                            {
                                file.AddError(Locale.Get("NaturalDocs.Engine", "ConfigFile.NotAValidIdentifier(identifier)", lcIdentifier));
                            }
                            else
                            {
                                pageType = pageTypeTemp.Value;
                            }
                        }

                        style.AddOnLoad(value, file.PropertyLocation, pageType);
                        continue;
                    }

                    file.AddError(Locale.Get("NaturalDocs.Engine", "ConfigFile.NotAValidIdentifier(identifier)", lcIdentifier));
                }

                if (errorCount == errors.Count)
                {
                    return(style);
                }
                else
                {
                    return(null);
                }
            }
        }