Bloom.Book.Layout.GetConfigurationsFromConfigurationOptionsString C# (CSharp) Method

GetConfigurationsFromConfigurationOptionsString() public static method

At runtime, this string comes out of a dummy css 'content' line. For unit tests, it just comes from the test.
public static GetConfigurationsFromConfigurationOptionsString ( string contents ) : List
contents string
return List
        public static List<Layout> GetConfigurationsFromConfigurationOptionsString(string contents)
        {
            var layouts = new List<Layout>();

            contents = "{\"root\": " + contents + "}";
            //I found it really hard to work with the json libraries, so I just convert it to xml. It's weird xml, but at least it's not like trying to mold smoke.
            XmlDocument doc = (XmlDocument)JsonConvert.DeserializeXmlNode(contents);
            var root = doc.SelectSingleNode("root");

            foreach (XmlElement element in root.SelectNodes("layouts"))
            {
                foreach (var sizeAndOrientation in element.ChildNodes)
                {
                    if (sizeAndOrientation is XmlText)
                    {
                        layouts.Add(new Layout() { SizeAndOrientation = SizeAndOrientation.FromString(((XmlText)sizeAndOrientation).InnerText) });
                    }
                    else if (sizeAndOrientation is XmlElement)
                    {
                        SizeAndOrientation soa = SizeAndOrientation.FromString(((XmlElement)sizeAndOrientation).Name);
                        foreach (XmlElement option in ((XmlElement)sizeAndOrientation).ChildNodes)
                        {
                            if (option.Name.ToLowerInvariant() != "styles")
                                continue;//we don't handle anything else yet
                            layouts.Add(new Layout() { SizeAndOrientation = soa, Style = option.InnerText });
                            //								List<string> choices = null;
                            //								if (!soa.Options.TryGetValue(option.Name, out choices))
                            //								{
                            //									choices = new List<string>();
                            //								}
                            //								else
                            //								{
                            //									soa.Options.Remove(option.Name);
                            //								}
                            //
                            //								foreach (XmlText choice in option.ChildNodes)
                            //								{
                            //									choices.Add(choice.Value);
                            //								}
                            //								soa.Options.Add(option.Name, choices);
                        }
                        //							layouts.Add(soa);
                    }
                }
            }

            return layouts;
        }

Usage Example

        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") ||
                    fileName.ToLowerInvariant().Contains("origami"))
                {
                    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")
            });
        }
All Usage Examples Of Bloom.Book.Layout::GetConfigurationsFromConfigurationOptionsString