/// <summary> /// Recursively gets a list of the *.htm* files in a folder. /// </summary> /// <param name="dir"></param> /// <returns></returns> private static List<ContentRoot> TraverseDirectory(string dir) { var docFiles = new List<ContentRoot>(); // for this directory -- string[] files = Directory.GetFiles(dir); ContentRoot page; string[] rootPages = files.Where(a=> a.EndsWith("index.htm", StringComparison.OrdinalIgnoreCase) || a.EndsWith("index.html", StringComparison.OrdinalIgnoreCase) || a.EndsWith("@root.htm", StringComparison.OrdinalIgnoreCase) || a.EndsWith("@root.html", StringComparison.OrdinalIgnoreCase) ).ToArray(); if (rootPages.Length > 1) throw new Exception(String.Format("Ambiguous root pages in directory '{0}' : [{1}]", dir, String.Join(", ", rootPages))); string[] subdirectories = Directory.GetDirectories(dir); if (rootPages.Length > 0) { // this is a root content-item (ignore sibiling items and assign any child dirs to this item) ContentRoot pg = new ContentRoot() { cfile = rootPages[0] }; docFiles.Add(pg); foreach (var subdirectory in subdirectories) pg.ChildPages.AddRange(TraverseDirectory(subdirectory)); } else { // each page is a non-root content-item; need to generate a dummy root for them if (subdirectories.Length > 0) throw new Exception(String.Format("Directory {0} can't contain subdirectories because it doesn't have a root page", dir)); ContentRoot dummy = new ContentRoot() { cfile = dir }; docFiles.Add(dummy); foreach (var file in files.Where(a=> a.EndsWith(".htm", StringComparison.OrdinalIgnoreCase) || a.EndsWith(".html", StringComparison.OrdinalIgnoreCase)) ) dummy.ChildPages.Add(new ContentRoot() { cfile = file }); foreach (var subdirectory in subdirectories) dummy.ChildPages.AddRange(TraverseDirectory(subdirectory)); } return docFiles; }
static void ProcessContentItem(ContentRoot root) { // depth-first root.ChildPages.ForEach(a => ProcessContentItem(a)); string filename = root.cfile; if (String.IsNullOrWhiteSpace(filename)) return; // skip if (Directory.Exists(filename)) return; // is a default contentitem if (!File.Exists(filename)) return; Console.WriteLine("Processing {0}", Path.GetFileName(filename)); XDocument doc = ReadXML(filename); if (doc != null) { ParseOptions po = ReadOptions(doc); root.Zones.AddRange(ReadZones(doc, po)); var titles = doc.Descendants("title").ToList(); if (titles.Count > 0) root.Title = titles.First().InnerXml(); } root.cparsed = true; }