replace java.io.File with java.nio.file.*
[from now] 2014/08/04 18:03:15

diff --git a/src/main/groovy/gaiden/BindingBuilder.groovy b/src/main/groovy/gaiden/BindingBuilder.groovy
index 0755d57..aad2b8a 100644
--- a/src/main/groovy/gaiden/BindingBuilder.groovy
+++ b/src/main/groovy/gaiden/BindingBuilder.groovy
@@ -18,8 +18,9 @@ package gaiden

 import gaiden.context.PageBuildContext
 import groovy.transform.CompileStatic
+import groovy.transform.TypeCheckingMode
+import groovy.xml.MarkupBuilder

-import java.nio.file.Path
 import java.nio.file.Paths

 import static org.apache.commons.lang3.StringEscapeUtils.*
@@ -34,23 +35,15 @@ class BindingBuilder {

     private static final String EMPTY_STRING = ""

-    private String title
-    private Path tocOutputFilePath
-
-    private Path outputDirectory
+    private GaidenConfig gaidenConfig
+    private PageReferenceFactory pageReferenceFactory

     private String content
-    private Path sourcePath
-    private Path outputPath
-
+    private PageSource pageSource
     private PageBuildContext pageBuildContext

-    private PageReferenceFactory pageReferenceFactory
-
-    BindingBuilder(String title, Path outputDirectory, Path tocOutputFilePath, PageReferenceFactory pageReferenceFactory) {
-        this.title = title
-        this.outputDirectory = outputDirectory
-        this.tocOutputFilePath = tocOutputFilePath
+    BindingBuilder(GaidenConfig gaidenConfig, PageReferenceFactory pageReferenceFactory) {
+        this.gaidenConfig = gaidenConfig
         this.pageReferenceFactory = pageReferenceFactory
     }

@@ -61,12 +54,13 @@ class BindingBuilder {
      */
     Map<String, Object> build() {
         [
-            title   : title,
-            content : content,
-            tocPath : tocPath,
-            resource: resourceMethod,
-            prevPage: prevPage,
-            nextPage: nextPage,
+            title    : escapeHtml4(gaidenConfig.title),
+            content  : content,
+            tocPath  : tocPath,
+            resource : resourceMethod,
+            prevPage : prevPage,
+            nextPage : nextPage,
+            toc      : toc,
         ]
     }

@@ -78,19 +72,8 @@ class BindingBuilder {
         this
     }

-    /**
-     * Sets the output path.
-     */
-    BindingBuilder setOutputPath(Path outputPath) {
-        this.outputPath = outputPath
-        this
-    }
-
-    /**
-     * Sets the page path.
-     */
-    BindingBuilder setSourcePath(Path sourcePath) {
-        this.sourcePath = sourcePath
+    BindingBuilder setPageSource(PageSource pageSource) {
+        this.pageSource = pageSource
         this
     }

@@ -103,14 +86,14 @@ class BindingBuilder {
     }

     private Map getPrevPage() {
-        if (!sourcePath || !pageBuildContext) {
+        if (!pageSource.path || !pageBuildContext) {
             return [
                 path : EMPTY_STRING,
                 title: EMPTY_STRING,
             ]
         }

-        def previousTocNode = pageBuildContext.toc.findTocNode(sourcePath)?.previous
+        def previousTocNode = pageBuildContext.toc.findTocNode(pageSource.path)?.previous
         if (!previousTocNode) {
             return [
                 path : EMPTY_STRING,
@@ -125,7 +108,7 @@ class BindingBuilder {
             ]
         }

-        def relativePath = outputPath.parent.relativize(previousTocNode.pageSource.outputPath)
+        def relativePath = pageSource.outputPath.parent.relativize(previousTocNode.pageSource.outputPath)
         return [
             path : relativePath.toString() + previousTocNode.pageReference.hash,
             title: escapeHtml4(previousTocNode.title)
@@ -133,14 +116,14 @@ class BindingBuilder {
     }

     private Map getNextPage() {
-        if (!sourcePath || !pageBuildContext) {
+        if (!pageSource.path || !pageBuildContext) {
             return [
                 path : EMPTY_STRING,
                 title: EMPTY_STRING,
             ]
         }

-        def nextTocNode = pageBuildContext.toc.findTocNode(sourcePath)?.next
+        def nextTocNode = pageBuildContext.toc.findTocNode(pageSource.path)?.next
         if (!nextTocNode) {
             return [
                 path : EMPTY_STRING,
@@ -155,7 +138,7 @@ class BindingBuilder {
             ]
         }

-        def relativePath = outputPath.parent.relativize(nextTocNode.pageSource.outputPath)
+        def relativePath = pageSource.outputPath.parent.relativize(nextTocNode.pageSource.outputPath)
         return [
             path : relativePath.toString() + nextTocNode.pageReference.hash,
             title: escapeHtml4(nextTocNode.title)
@@ -169,14 +152,81 @@ class BindingBuilder {
                 return resourceFilePath
             }

-            def src = outputDirectory.resolve(outputPath)
-            def dest = outputDirectory.resolve(resourceFile.toString().substring(1))
+            def src = gaidenConfig.outputDirectory.resolve(pageSource.outputPath)
+            def dest = gaidenConfig.outputDirectory.resolve(resourceFile.toString().substring(1))

             return src.parent.relativize(dest).toString()
         }
     }

     private String getTocPath() {
-        outputPath.parent.relativize(tocOutputFilePath).toString()
+        pageSource.outputPath.parent.relativize(gaidenConfig.tocOutputFile).toString()
+    }
+
+    private String getToc() {
+        buildContent()
+    }
+
+    @CompileStatic(TypeCheckingMode.SKIP)
+    private String buildContent() {
+        def writer = new StringWriter()
+        def printer = new IndentPrinter(writer, " " * 4)
+        def builder = new MarkupBuilder(printer)
+
+        builder.doubleQuotes = true // surrounds an attribute with double quotes
+
+        buildContentOfToc(builder, pageBuildContext.toc.tocNodes)
+
+        writer.toString()
+    }
+
+    private String resolveOutputPath(PageReference pageReference) {
+        def destPageSource = pageBuildContext.documentSource.findPageSource(pageReference)
+//        println "*" * 10
+//        println pageSource.path
+//        println pageReference.path
+//        println path
+//        println pageReference.path
+//        println destPageSource?.path
+//        if (!destPageSource) {
+//            if (!PathUtils.getExtension(pageReference.path) || PathUtils.getExtension(pageReference.path) in SourceCollector.PAGE_SOURCE_EXTENSIONS) {
+//                return null
+//            }
+//            return path.toString()
+//        }
+//
+
+        return pageSource.outputPath.parent.relativize(destPageSource.outputPath).toString() + pageReference.hash
+
+
+    }
+
+    @CompileStatic(TypeCheckingMode.SKIP)
+    private void buildContentOfToc(MarkupBuilder builder, List<TocNode> tocNodes) {
+        if (!tocNodes) {
+            return
+        }
+
+        builder.ul {
+            tocNodes.each { TocNode tocNode ->
+                if (tocNode.path.startsWith("#")) {
+                    li(tocNode.title) {
+                        buildContentOfToc(builder, tocNode.children)
+                    }
+                } else {
+                    def outputPath = resolveOutputPath(tocNode.pageReference)
+                    if (outputPath) {
+                        li {
+                            a(href: outputPath, tocNode.title)
+                            buildContentOfToc(builder, tocNode.children)
+                        }
+                    } else {
+                        li(tocNode.title) {
+                            buildContentOfToc(builder, tocNode.children)
+                        }
+                    }
+                }
+            }
+        }
     }
 }
diff --git a/src/main/groovy/gaiden/PageBuilder.groovy b/src/main/groovy/gaiden/PageBuilder.groovy
index d4d366e..0b2f886 100644
--- a/src/main/groovy/gaiden/PageBuilder.groovy
+++ b/src/main/groovy/gaiden/PageBuilder.groovy
@@ -61,11 +61,10 @@ class PageBuilder {
     private String buildPage(PageBuildContext context, PageSource pageSource) {
         def content = markdownProcessor.markdownToHtml(context, pageSource)

-        def binding = new BindingBuilder(gaidenConfig.title, gaidenConfig.outputDirectory, gaidenConfig.tocOutputFile, pageReferenceFactory)
+        def binding = new BindingBuilder(gaidenConfig, pageReferenceFactory)
             .setContent(content)
             .setPageBuildContext(context)
-            .setOutputPath(pageSource.outputPath)
-            .setSourcePath(pageSource.path)
+            .setPageSource(pageSource)
             .build()

         templateEngine.make(binding)
diff --git a/src/main/groovy/gaiden/TocBuilder.groovy b/src/main/groovy/gaiden/TocBuilder.groovy
index 0fe5f20..e6a4d23 100644
--- a/src/main/groovy/gaiden/TocBuilder.groovy
+++ b/src/main/groovy/gaiden/TocBuilder.groovy
@@ -69,13 +69,13 @@ class TocBuilder {
         def tocNode = toTocNodes(context, tocSourceNode.children())
         def tocContent = buildContent(context, tocNode)

-        def binding = new BindingBuilder(gaidenConfig.title, gaidenConfig.outputDirectory, gaidenConfig.tocOutputFile, pageReferenceFactory)
-            .setContent(tocContent)
-            .setOutputPath(gaidenConfig.tocOutputFile)
-            .build()
+//        def binding = new BindingBuilder(gaidenConfig, pageReferenceFactory)
+//            .setContent(tocContent)
+//            .setOutputPath(gaidenConfig.tocOutputFile)
+//            .build()

-        def content = templateEngine.make(binding)
-        new Toc(path: gaidenConfig.tocOutputFile, content: content, tocNodes: tocNodes)
+//        def content = templateEngine.make(binding)
+        new Toc(path: gaidenConfig.tocOutputFile, content: "", tocNodes: tocNodes)
     }

     @CompileStatic(TypeCheckingMode.SKIP)
@@ -141,13 +141,14 @@ class TocBuilder {
         return gaidenConfig.tocOutputFile.parent.relativize(pageSource.outputPath).toString() + pageReference.hash
     }

-    private List<TocNode> toTocNodes(BuildContext context, List<Node> nodes) {
+    private List<TocNode> toTocNodes(BuildContext context, List<Node> nodes, int depth = 1) {
         nodes.collect { Node node ->
             def tocNode = new TocNode()
             tocNode.path = Paths.get(node.name() as String)
             tocNode.title = node.attributes().title
             tocNode.pageReference = pageReferenceFactory.get(tocNode.path)
             tocNode.pageSource = context.documentSource.findPageSource(tocNode.pageReference)
+            tocNode.depth = depth

             if (!tocNodes.empty) {
                 def previous = tocNodes.last()
@@ -156,7 +157,7 @@ class TocBuilder {
             }
             tocNodes << tocNode

-            def children = toTocNodes(context, node.children())
+            def children = toTocNodes(context, node.children(), depth + 1)
             children.each { TocNode child ->
                 child.parent = tocNode
             }
diff --git a/src/main/groovy/gaiden/TocNode.groovy b/src/main/groovy/gaiden/TocNode.groovy
index 3edc573..8723a35 100644
--- a/src/main/groovy/gaiden/TocNode.groovy
+++ b/src/main/groovy/gaiden/TocNode.groovy
@@ -46,5 +46,7 @@ class TocNode {
     /** The {@code List} of TOC nodes children */
     List<TocNode> children

+    int depth
+
     PageReference pageReference
 }