GitHub PushEvent at 2014-06-17 08:07:58
Repository: gaiden
Actor: yamkazu
Payload: {"shas":[["4d0d47dba9916b7c37e771f14dc4b6a8ccae743c","c13ce7fdddbdbb8217793f0827c8f8e0f4ae220b@gmail.com","[from now] 2014/06/17 17:06:28\n\ndiff --git a/src/main/groovy/gaiden/kai/TocNode.groovy b/src/main/groovy/gaiden/kai/TocNode.groovy\nindex 759bbee..0ac3147 100644\n--- a/src/main/groovy/gaiden/kai/TocNode.groovy\n+++ b/src/main/groovy/gaiden/kai/TocNode.groovy\n@@ -11,7 +11,10 @@ class TocNode {\n\n     String title\n     Path path\n-//    List<TocNode> children\n-    List<Map> children\n+\n+    List<Integer> numbers\n\n+    TocNode parent\n+    TocNode previous\n+    TocNode next\n }\ndiff --git a/src/main/groovy/gaiden/kai/TocSource.groovy b/src/main/groovy/gaiden/kai/TocSource.groovy\nindex 3ab142e..dbb535a 100644\n--- a/src/main/groovy/gaiden/kai/TocSource.groovy\n+++ b/src/main/groovy/gaiden/kai/TocSource.groovy\n@@ -1,6 +1,5 @@\n package gaiden.kai\n\n-import groovy.json.JsonBuilder\n import groovy.transform.CompileStatic\n import groovy.transform.TypeCheckingMode\n\n@@ -10,30 +9,32 @@ import java.nio.file.Paths\n @CompileStatic\n class TocSource {\n\n-//    List<TocNode> nodes\n-    List<Map> nodes\n+    List<TocNode> tocNodes = []\n\n     @CompileStatic(TypeCheckingMode.SKIP)\n     TocSource(Path tocFile, String inputEncoding) {\n         def tocBuilder = new NodeBuilder()\n         def rootNode = tocBuilder.toc(new GroovyShell().evaluate(\"{ -> ${tocFile.getText(inputEncoding)} }\"))\n-        nodes = buildNodes(rootNode.children())\n+        buildNodes(rootNode.children())\n     }\n\n-//    private List<TocNode> buildNodes(List<Node> nodes) {\n-    private List<Map> buildNodes(List<Node> nodes) {\n-        nodes.collect { Node node ->\n-//            def tocNode = new TocNode()\n-            def tocNode = [:]\n-//            tocNode.path = Paths.get(node.name() as String)\n-            tocNode.path = node.name() as String\n+    @CompileStatic(TypeCheckingMode.SKIP)\n+    private void buildNodes(List<Node> nodes, TocNode parent = null) {\n+        def parentNumbers = parent?.numbers ?: []\n+        nodes.eachWithIndex { Node node, Integer index ->\n+            def tocNode = new TocNode()\n+            tocNode.path = Paths.get(node.name() as String)\n             tocNode.title = node.attributes().title\n-            tocNode.children = buildNodes(node.children())\n-            tocNode\n-        }\n-    }\n+            tocNode.parent = parent\n+            tocNode.numbers = parentNumbers + (index + 1)\n+            if (tocNodes) {\n+                def previous = tocNodes.last()\n+                tocNode.previous = previous\n+                previous.next = tocNode\n+            }\n+            tocNodes << tocNode\n\n-    String toJson() {\n-        new JsonBuilder(nodes).toPrettyString()\n+            buildNodes(node.children(), tocNode)\n+        }\n     }\n }\ndiff --git a/src/test/groovy/gaiden/kai/TocSourceSpec.groovy b/src/test/groovy/gaiden/kai/TocSourceSpec.groovy\nindex 03775bb..ca9e158 100644\n--- a/src/test/groovy/gaiden/kai/TocSourceSpec.groovy\n+++ b/src/test/groovy/gaiden/kai/TocSourceSpec.groovy\n@@ -1,5 +1,7 @@\n package gaiden.kai\n\n+import groovy.json.JsonBuilder\n+\n import java.lang.Void as Should\n\n class TocSourceSpec extends GaidenSpec {\n@@ -20,8 +22,26 @@ class TocSourceSpec extends GaidenSpec {\n             def tocSource = new TocSource(tocFile, \"UTF-8\")\n\n         then:\n-//            tocSource.nodes == []\n-//\n-            tocSource.toJson() == \"\"\n+            toJson(tocSource) == \"\"\n+    }\n+\n+    private String toJson(TocSource tocSource) {\n+        def tocNodeMaps = tocSource.tocNodes.collect { TocNode tocNode ->\n+            def tocNodeMap = [:]\n+            tocNodeMap[\"title\"] = tocNode.title\n+            tocNodeMap[\"path\"] = tocNode.path.toString()\n+            tocNodeMap[\"numbers\"] = tocNode.numbers.join(\".\")\n+            if (tocNode.parent) {\n+                tocNodeMap[\"parent\"] = tocNode.parent.path.toString()\n+            }\n+            if (tocNode.next) {\n+                tocNodeMap[\"next\"] = tocNode.next.path.toString()\n+            }\n+            if (tocNode.previous) {\n+                tocNodeMap[\"previous\"] = tocNode.previous.path.toString()\n+            }\n+            tocNodeMap\n+        }\n+        new JsonBuilder(tocNodeMaps).toPrettyString()\n     }\n }","Kazuki YAMAMOTO",true]],"size":1,"ref":"refs/heads/kai","head":"4d0d47dba9916b7c37e771f14dc4b6a8ccae743c"}