Bloom.Book.SizeAndOrientation.GetLayoutChoices C# (CSharp) Method

GetLayoutChoices() public static method

public static GetLayoutChoices ( HtmlDom dom, IFileLocator fileLocator ) : IEnumerable
dom HtmlDom
fileLocator IFileLocator
return IEnumerable
        public static IEnumerable<Layout> GetLayoutChoices(HtmlDom dom, IFileLocator fileLocator)
        {
            //here we walk through all the stylesheets, looking for one with the special style which tells us which page/orientations it supports
            foreach (XmlElement link in dom.SafeSelectNodes("//link[@rel='stylesheet']"))
            {
                var fileName = link.GetStringAttribute("href");
                if (fileName.ToLowerInvariant().Contains("mode") || fileName.ToLowerInvariant().Contains("page") ||
                    fileName.ToLowerInvariant().Contains("matter") || fileName.ToLowerInvariant().Contains("languagedisplay"))
                    continue;

                fileName = fileName.Replace("file://", "").Replace("%5C", "/").Replace("%20", " ");
                var path = fileLocator.LocateFile(fileName);
                if(string.IsNullOrEmpty(path))
                {
                    // We're looking for a block of json that is typically found in Basic Book.css or a comparable place for
                    // a book based on some other template. Caling code is prepared for not finding this block.
                    // It seems safe to ignore a reference to some missing style sheet.
                    NonFatalProblem.Report(ModalIf.None, PassiveIf.Alpha, "Could not find " + fileName+" while looking for size choices");
                    continue;
                }
                var contents = RobustFile.ReadAllText(path);
                var start = contents.IndexOf("STARTLAYOUTS");
                if (start < 0)
                     continue; //yield break; // continue;//move on to the next stylesheet
                start += "STARTLAYOUTS".Length;
                var end = contents.IndexOf("ENDLAYOUTS",start);
                var s = contents.Substring(start, end - start);

                IEnumerable<Layout> layouts = null;

                try
                {
                    layouts = Layout.GetConfigurationsFromConfigurationOptionsString(s);
                }
                catch (Exception e)
                {
                    throw new ApplicationException("Problem parsing the 'layouts' comment of " + fileName + ". The contents were\r\n" + s, e);
                }

                foreach (var p in layouts)
                {
                    yield return p;
                }
                yield break;

            }

            //default to A5Portrait
            yield return new Layout {SizeAndOrientation = FromString("A5Portrait")};
        }

Usage Example

Example #1
0
        private static Layout EnsureLayoutIsAmongValidChoices(HtmlDom dom, Layout layout, IFileLocator fileLocator)
        {
            var layoutChoices = SizeAndOrientation.GetLayoutChoices(dom, fileLocator);

            if (layoutChoices.Any(l => l.SizeAndOrientation.ClassName == layout.SizeAndOrientation.ClassName))
            {
                return(layout);
            }
            return(layoutChoices.Any() ?  layoutChoices.First() : layout);
        }