Commits

Cesar Lopez-Nataren committed 9c930eb

Rewrite the lone ols siblings

Comments (0)

Files changed (1)

GatherImageMapping/Program.cs

         }
 
         private static void FixList(XDoc doc) {
+
+            // Fix the ol's that do not have li's, only siblings floating
+            foreach(var ol in doc["//ol"]) {
+                var olWithLoneChildren = true;
+                var olElements = ol["child::node()"];
+                foreach(var olElement in olElements) {
+                    if(olElement.Name.EqualsInvariantIgnoreCase("li")) {
+                        olWithLoneChildren = false;
+                        break;
+                    }
+                }
+                if(!olWithLoneChildren) {
+                    continue;
+                }
+                var nodes = new List<XDoc>();
+                foreach(var node in ol["child::node()"]) {
+                    node.Remove();
+                    nodes.Add(node);
+                }
+                var id = Guid.NewGuid();
+                var newLi = new XDoc("li");
+                newLi.Start("blockquote")
+                    .Attr("tempid", id.ToString())
+                    .Attr("class", "Note-Indented")
+                .End();
+                ol.Add(newLi);
+                var blockquote = ol[string.Format(".//*[@tempid='{0}']", id)];
+                foreach(var n in nodes) {
+                    if("Note-Indented".EqualsInvariantIgnoreCase(n[".//@class"].AsText)) {
+                        continue;
+                    }
+                    blockquote.Add(n);
+                }
+                blockquote["@tempid"].Remove();
+            }
+
+            // Fix the ol's that have li's with siblings
             foreach(var ol in doc["//ol"]) {
                 var olElements = ol["child::node()"];
-                IDictionary<XDoc, IList<XDoc>> xmlElementToChildren = new Dictionary<XDoc, IList<XDoc>>();
+                var liChildren = new Dictionary<XDoc, IList<XDoc>>();
                 var stack = new Stack<XDoc>();
 
                 // Gather the children
                 foreach(var olElement in olElements) {
                     if(olElement.Name.EqualsInvariantIgnoreCase("li")) {
                         IList<XDoc> elementChildren;
-                        if(!xmlElementToChildren.TryGetValue(olElement, out elementChildren)) {
+                        if(!liChildren.TryGetValue(olElement, out elementChildren)) {
                             elementChildren = new List<XDoc>();
-                            xmlElementToChildren[olElement] = elementChildren;
+                            liChildren[olElement] = elementChildren;
                         }
                         stack.Push(olElement);
                     } else {
                             continue;
                         }
                         var currentContext = stack.Peek();
-                        if(!xmlElementToChildren.TryGetValue(currentContext, out elementChildren)) {
+                        if(!liChildren.TryGetValue(currentContext, out elementChildren)) {
                             continue;
                         }
                         olElement.Remove();
                 }
 
                 // Properly nest the children
-                foreach(var kvp in xmlElementToChildren) {
+                foreach(var kvp in liChildren) {
                     var element = kvp.Key;
                     var children = kvp.Value;
                     var current = element;