[from now] 2014/08/03 10:20:49

diff --git a/src/main/groovy/gaiden/BindingBuilder.groovy b/src/main/groovy/gaiden/BindingBuilder.groovy
index 75ecf33..c636e38 100644
--- a/src/main/groovy/gaiden/BindingBuilder.groovy
+++ b/src/main/groovy/gaiden/BindingBuilder.groovy
@@ -107,7 +107,7 @@ private String getPrevLink() {
             return EMPTY_STRING
         }

-        def previousTocNode = pageBuildContext.toc.findTocNode(pageReferenceFactory.get(sourcePath))?.previous
+        def previousTocNode = pageBuildContext.toc.findTocNode(sourcePath)?.previous
         if (!previousTocNode) {
             return EMPTY_STRING
         }
@@ -116,7 +116,7 @@ private String getPrevLink() {
             return escapeHtml4("<< $previousTocNode.title")
         }

-        def relativePath = outputPath.relativize(previousTocNode.pageSource.outputPath)
+        def relativePath = outputPath.parent.relativize(previousTocNode.pageSource.outputPath)

         def sb = new StringBuilder()
         sb << "<a href='${relativePath.toString() + previousTocNode.pageReference.hash}' class='prev'>"
@@ -130,7 +130,7 @@ private String getNextLink() {
             return EMPTY_STRING
         }

-        def nextTocNode = pageBuildContext.toc.findTocNode(pageReferenceFactory.get(sourcePath))?.next
+        def nextTocNode = pageBuildContext.toc.findTocNode(sourcePath)?.next
         if (!nextTocNode) {
             return EMPTY_STRING
         }
@@ -139,7 +139,7 @@ private String getNextLink() {
             return escapeHtml4("$nextTocNode.title >>")
         }

-        def relativePath = outputPath.relativize(nextTocNode.pageSource.outputPath)
+        def relativePath = outputPath.parent.relativize(nextTocNode.pageSource.outputPath)

         def sb = new StringBuilder()
         sb << "<a href='${relativePath.toString() + nextTocNode.pageReference.hash}' class='next'>"
diff --git a/src/main/groovy/gaiden/Toc.groovy b/src/main/groovy/gaiden/Toc.groovy
index 52b768c..b22a46a 100644
--- a/src/main/groovy/gaiden/Toc.groovy
+++ b/src/main/groovy/gaiden/Toc.groovy
@@ -47,7 +47,7 @@
      * @param pageReference the page reference
      * @return TOC node found
      */
-    TocNode findTocNode(PageReference pageReference) {
-        tocNodes.find { it.pageSource?.path == pageReference.path }
+    TocNode findTocNode(Path path) {
+        tocNodes.find { it.pageSource?.path == path }
     }
 }
diff --git a/src/main/groovy/gaiden/TocBuilder.groovy b/src/main/groovy/gaiden/TocBuilder.groovy
index 4f155c6..1ae6cfd 100644
--- a/src/main/groovy/gaiden/TocBuilder.groovy
+++ b/src/main/groovy/gaiden/TocBuilder.groovy
@@ -145,7 +145,8 @@ private String resolveOutputPath(BuildContext context, Path path) {
             def tocNode = new TocNode()
             tocNode.path = Paths.get(node.name() as String)
             tocNode.title = node.attributes().title
-            tocNode.pageSource = context.documentSource.findPageSource(pageReferenceFactory.get(tocNode.path))
+            tocNode.pageReference = pageReferenceFactory.get(tocNode.path)
+            tocNode.pageSource = context.documentSource.findPageSource(tocNode.pageReference)

             if (!tocNodes.empty) {
                 def previous = tocNodes.last()
[from now] 2014/08/03 16:02:31

diff --git a/src/main/groovy/gaiden/BindingBuilder.groovy b/src/main/groovy/gaiden/BindingBuilder.groovy
index c636e38..0755d57 100644
--- a/src/main/groovy/gaiden/BindingBuilder.groovy
+++ b/src/main/groovy/gaiden/BindingBuilder.groovy
@@ -65,8 +65,8 @@
             content : content,
             tocPath : tocPath,
             resource: resourceMethod,
-            prevLink: prevLink,
-            nextLink: nextLink,
+            prevPage: prevPage,
+            nextPage: nextPage,
         ]
     }

@@ -102,50 +102,64 @@ BindingBuilder setPageBuildContext(PageBuildContext pageBuildContext) {
         this
     }

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

         def previousTocNode = pageBuildContext.toc.findTocNode(sourcePath)?.previous
         if (!previousTocNode) {
-            return EMPTY_STRING
+            return [
+                path : EMPTY_STRING,
+                title: EMPTY_STRING,
+            ]
         }

         if (!previousTocNode.pageSource) {
-            return escapeHtml4("<< $previousTocNode.title")
+            return [
+                path : EMPTY_STRING,
+                title: escapeHtml4(previousTocNode.title),
+            ]
         }

         def relativePath = outputPath.parent.relativize(previousTocNode.pageSource.outputPath)
-
-        def sb = new StringBuilder()
-        sb << "<a href='${relativePath.toString() + previousTocNode.pageReference.hash}' class='prev'>"
-        sb << escapeHtml4("<< $previousTocNode.title")
-        sb << "</a>"
-        return sb.toString()
+        return [
+            path : relativePath.toString() + previousTocNode.pageReference.hash,
+            title: escapeHtml4(previousTocNode.title)
+        ]
     }

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

         def nextTocNode = pageBuildContext.toc.findTocNode(sourcePath)?.next
         if (!nextTocNode) {
-            return EMPTY_STRING
+            return [
+                path : EMPTY_STRING,
+                title: EMPTY_STRING,
+            ]
         }

         if (!nextTocNode.pageSource) {
-            return escapeHtml4("$nextTocNode.title >>")
+            return [
+                path : EMPTY_STRING,
+                title: escapeHtml4(nextTocNode.title),
+            ]
         }

         def relativePath = outputPath.parent.relativize(nextTocNode.pageSource.outputPath)
-
-        def sb = new StringBuilder()
-        sb << "<a href='${relativePath.toString() + nextTocNode.pageReference.hash}' class='next'>"
-        sb << escapeHtml4("$nextTocNode.title >>")
-        sb << "</a>"
-        return sb.toString()
+        return [
+            path : relativePath.toString() + nextTocNode.pageReference.hash,
+            title: escapeHtml4(nextTocNode.title)
+        ]
     }

     private Closure getResourceMethod() {
[from now] 2014/08/03 16:04:45

diff --git a/src/dist/template/templates/layout.html b/src/dist/template/templates/layout.html
index 4904a83..13af16b 100644
--- a/src/dist/template/templates/layout.html
+++ b/src/dist/template/templates/layout.html
@@ -1,45 +1,53 @@
 <html>
-<head>
-    <meta charset="UTF-8">
-    <title>$title</title>
-    <meta name="viewport" content="width=device-width, initial-scale=1.0">
-    <link rel="stylesheet" href="${resource('/css/bootstrap.css')}">
-    <link rel="stylesheet" href="${resource('/css/prettify.css')}">
-    <link rel="stylesheet" href="${resource('/css/style.css')}">
-</head>
-<body>
-    <div class="navbar navbar-static-top">
-        <div class="container">
-            <a class="navbar-brand" href="$tocPath">$title</a>
-            <a class="toc btn btn-primary btn-lg" href="$tocPath"><span class="glyphicon glyphicon-list"></span></a>
+    <head>
+        <meta charset="UTF-8">
+        <title>$title</title>
+        <meta name="viewport" content="width=device-width, initial-scale=1.0">
+        <link rel="stylesheet" href="${resource('/css/bootstrap.css')}">
+        <link rel="stylesheet" href="${resource('/css/prettify.css')}">
+        <link rel="stylesheet" href="${resource('/css/style.css')}">
+    </head>
+    <body>
+        <div class="navbar navbar-static-top">
+            <div class="container">
+                <a class="navbar-brand" href="$tocPath">$title</a>
+                <a class="toc btn btn-primary btn-lg" href="$tocPath"><span class="glyphicon glyphicon-list"></span></a>
+            </div>
         </div>
-    </div>
-    <div class="container">
-        <nav class="page-nav">
-            <ul class="nav">
-                <li class="prev">$prevLink</li>
-                <li class="next">$nextLink</li>
-            </ul>
-        </nav>
-        <section class="page-content">
-            $content
-        </section>
-        <nav class="page-nav">
-            <ul class="nav">
-                <li class="prev">$prevLink</li>
-                <li class="next">$nextLink</li>
-            </ul>
-        </nav>
-    </div>
-    <footer id="footer" class="footer">
         <div class="container">
-            <p class="credit text-muted">Powered by <a href="https://github.com/kobo/gaiden">Gaiden</a>.</p>
+            <nav class="page-nav">
+                <ul class="nav">
+                    <li class="prev">
+                        <a href="${prevPage.path}">${prevPage.title}</a>
+                    </li>
+                    <li class="next">
+                        <a href="${nextPage.path}">${nextPage.title}</a>
+                    </li>
+                </ul>
+            </nav>
+            <section class="page-content">
+                $content
+            </section>
+            <nav class="page-nav">
+                <ul class="nav">
+                    <li class="prev">
+                        <a href="${prevPage.path}">${prevPage.title}</a>
+                    </li>
+                    <li class="next">
+                        <a href="${nextPage.path}">${nextPage.title}</a>
+                    </li>
+                </ul>
+            </nav>
         </div>
-    </footer>
+        <footer id="footer" class="footer">
+            <div class="container">
+                <p class="credit text-muted">Powered by <a href="https://github.com/kobo/gaiden">Gaiden</a>.</p>
+            </div>
+        </footer>

-    <script src="${resource('/js/jquery-1.10.2.min.js')}"></script>
-    <script src="${resource('/js/bootstrap.js')}"></script>
-    <script src="${resource('/js/prettify.js')}"></script>
-    <script src="${resource('/js/application.js')}"></script>
-</body>
+        <script src="${resource('/js/jquery-1.10.2.min.js')}"></script>
+        <script src="${resource('/js/bootstrap.js')}"></script>
+        <script src="${resource('/js/prettify.js')}"></script>
+        <script src="${resource('/js/application.js')}"></script>
+    </body>
 </html>
[from now] 2014/08/03 16:05:18

diff --git a/src/dist/template/templates/layout.html b/src/dist/template/templates/layout.html
index 13af16b..b235187 100644
--- a/src/dist/template/templates/layout.html
+++ b/src/dist/template/templates/layout.html
@@ -1,53 +1,53 @@
 <html>
-    <head>
-        <meta charset="UTF-8">
-        <title>$title</title>
-        <meta name="viewport" content="width=device-width, initial-scale=1.0">
-        <link rel="stylesheet" href="${resource('/css/bootstrap.css')}">
-        <link rel="stylesheet" href="${resource('/css/prettify.css')}">
-        <link rel="stylesheet" href="${resource('/css/style.css')}">
-    </head>
-    <body>
-        <div class="navbar navbar-static-top">
-            <div class="container">
-                <a class="navbar-brand" href="$tocPath">$title</a>
-                <a class="toc btn btn-primary btn-lg" href="$tocPath"><span class="glyphicon glyphicon-list"></span></a>
-            </div>
-        </div>
-        <div class="container">
-            <nav class="page-nav">
-                <ul class="nav">
-                    <li class="prev">
-                        <a href="${prevPage.path}">${prevPage.title}</a>
-                    </li>
-                    <li class="next">
-                        <a href="${nextPage.path}">${nextPage.title}</a>
-                    </li>
-                </ul>
-            </nav>
-            <section class="page-content">
-                $content
-            </section>
-            <nav class="page-nav">
-                <ul class="nav">
-                    <li class="prev">
-                        <a href="${prevPage.path}">${prevPage.title}</a>
-                    </li>
-                    <li class="next">
-                        <a href="${nextPage.path}">${nextPage.title}</a>
-                    </li>
-                </ul>
-            </nav>
-        </div>
-        <footer id="footer" class="footer">
-            <div class="container">
-                <p class="credit text-muted">Powered by <a href="https://github.com/kobo/gaiden">Gaiden</a>.</p>
-            </div>
-        </footer>
+  <head>
+    <meta charset="UTF-8">
+    <title>$title</title>
+    <meta name="viewport" content="width=device-width, initial-scale=1.0">
+    <link rel="stylesheet" href="${resource('/css/bootstrap.css')}">
+    <link rel="stylesheet" href="${resource('/css/prettify.css')}">
+    <link rel="stylesheet" href="${resource('/css/style.css')}">
+  </head>
+  <body>
+    <div class="navbar navbar-static-top">
+      <div class="container">
+        <a class="navbar-brand" href="$tocPath">$title</a>
+        <a class="toc btn btn-primary btn-lg" href="$tocPath"><span class="glyphicon glyphicon-list"></span></a>
+      </div>
+    </div>
+    <div class="container">
+      <nav class="page-nav">
+        <ul class="nav">
+          <li class="prev">
+            <a href="${prevPage.path}">${prevPage.title}</a>
+          </li>
+          <li class="next">
+            <a href="${nextPage.path}">${nextPage.title}</a>
+          </li>
+        </ul>
+      </nav>
+      <section class="page-content">
+        $content
+      </section>
+      <nav class="page-nav">
+        <ul class="nav">
+          <li class="prev">
+            <a href="${prevPage.path}">${prevPage.title}</a>
+          </li>
+          <li class="next">
+            <a href="${nextPage.path}">${nextPage.title}</a>
+          </li>
+        </ul>
+      </nav>
+    </div>
+    <footer id="footer" class="footer">
+      <div class="container">
+        <p class="credit text-muted">Powered by <a href="https://github.com/kobo/gaiden">Gaiden</a>.</p>
+      </div>
+    </footer>

-        <script src="${resource('/js/jquery-1.10.2.min.js')}"></script>
-        <script src="${resource('/js/bootstrap.js')}"></script>
-        <script src="${resource('/js/prettify.js')}"></script>
-        <script src="${resource('/js/application.js')}"></script>
-    </body>
+    <script src="${resource('/js/jquery-1.10.2.min.js')}"></script>
+    <script src="${resource('/js/bootstrap.js')}"></script>
+    <script src="${resource('/js/prettify.js')}"></script>
+    <script src="${resource('/js/application.js')}"></script>
+  </body>
 </html>
[from now] 2014/08/03 17:15:06

diff --git a/src/main/groovy/gaiden/PageReferenceFactory.groovy b/src/main/groovy/gaiden/PageReferenceFactory.groovy
index 1491f60..3a61b9f 100644
--- a/src/main/groovy/gaiden/PageReferenceFactory.groovy
+++ b/src/main/groovy/gaiden/PageReferenceFactory.groovy
@@ -27,7 +27,7 @@ private Path getPath(Path original, PageSource basePageSource) {
         def path = removeHash(original)

         if (PathUtils.getExtension(path) && !(PathUtils.getExtension(path) in SourceCollector.PAGE_SOURCE_EXTENSIONS)) {
-            return gaidenConfig.outputDirectory.resolve(path.absolute ? path.toString().substring(1) : path.toString())
+            return path.absolute ? Paths.get(path.toString().substring(1)) : path
         }

         if (path.absolute) {
@@ -38,19 +38,23 @@ private Path getPath(Path original, PageSource basePageSource) {
             return getCanonicalPath(gaidenConfig.pagesDirectory.resolve(path))
         }

-        return getCanonicalPath(basePageSource.path.resolveSibling(path).toAbsolutePath())
+        return getCanonicalPath(basePageSource.path.resolveSibling(path))
     }

     private static Path getCanonicalPath(Path path) {
         if (Files.exists(path)) {
-            return path
+            return path.toRealPath()
         }

-        return SourceCollector.PAGE_SOURCE_EXTENSIONS.collect { String extension ->
+        def found = SourceCollector.PAGE_SOURCE_EXTENSIONS.collect { String extension ->
             Paths.get("${path.toString()}.${extension}")
         }.find { Path pathWithExtension ->
             Files.exists(pathWithExtension)
-        } ?: path
+        }
+        if (found) {
+            return found.toRealPath()
+        }
+        return path
     }

     private static String getHash(Path path) {
diff --git a/src/main/groovy/gaiden/TocBuilder.groovy b/src/main/groovy/gaiden/TocBuilder.groovy
index 1ae6cfd..49b40f7 100644
--- a/src/main/groovy/gaiden/TocBuilder.groovy
+++ b/src/main/groovy/gaiden/TocBuilder.groovy
@@ -134,8 +134,9 @@ private String resolveOutputPath(BuildContext context, Path path) {
         if (!pageSource) {
             if (PathUtils.getExtension(pageReference.path) in SourceCollector.PAGE_SOURCE_EXTENSIONS) {
                 System.err.println("WARNING: " + messageSource.getMessage("toc.page.reference.not.exists.message", [path]))
+                return null
             }
-            return null
+            return path.toString()
         }
         return gaidenConfig.tocOutputFile.parent.relativize(pageSource.outputPath).toString() + pageReference.hash
     }
diff --git a/src/main/groovy/gaiden/markdown/GaidenLinkRenderer.groovy b/src/main/groovy/gaiden/markdown/GaidenLinkRenderer.groovy
index 1965f94..1f1ed3c 100644
--- a/src/main/groovy/gaiden/markdown/GaidenLinkRenderer.groovy
+++ b/src/main/groovy/gaiden/markdown/GaidenLinkRenderer.groovy
@@ -78,8 +78,7 @@
                 System.err.println("WARNING: " + messageSource.getMessage("output.page.reference.not.exists.message", [url, pageSource.path]))
             }

-            def relativePath = pageSource.outputPath.parent.relativize(pageReference.path).toString()
-            return createRendering(relativePath, title, text)
+            return createRendering(url, title, text)
         }

         def relativePath = pageSource.outputPath.parent.relativize(targetPageSource.outputPath).toString()
[from now] 2014/08/03 19:18:08

diff --git a/src/main/groovy/gaiden/GaidenConfig.groovy b/src/main/groovy/gaiden/GaidenConfig.groovy
index bacad5c..bb5e314 100644
--- a/src/main/groovy/gaiden/GaidenConfig.groovy
+++ b/src/main/groovy/gaiden/GaidenConfig.groovy
@@ -76,46 +76,46 @@
     String appHomePath = System.properties["app.home"] as String

     /** The path of user directory */
-    String userDirectoryPath = System.properties["user.dir"] as String
+    String projectDirectoryPath = System.properties["user.dir"] as String

     /** Returns the {@link #templateFilePath} as {@link Path} */
     Path getTemplateFile() {
-        userDirectory.resolve(templateFilePath)
+        projectDirectory.resolve(templateFilePath)
     }

     /** Returns the {@link #tocFilePath} as {@link Path} */
     Path getTocFile() {
-        userDirectory.resolve(tocFilePath)
+        projectDirectory.resolve(tocFilePath)
     }

     /** Returns the {@link #pagesDirectoryPath} as {@link Path} */
     Path getPagesDirectory() {
-        userDirectory.resolve(pagesDirectoryPath)
+        projectDirectory.resolve(pagesDirectoryPath)
     }

     /** Returns the {@link #staticDirectoryPath} as {@link Path} */
     Path getStaticDirectory() {
-        userDirectory.resolve(staticDirectoryPath)
+        projectDirectory.resolve(staticDirectoryPath)
     }

     /** Returns the {@link #outputDirectoryPath} as {@link Path} */
     Path getOutputDirectory() {
-        userDirectory.resolve(outputDirectoryPath)
+        projectDirectory.resolve(outputDirectoryPath)
     }

     /** Returns the application home directory as {@link Path} */
-    Path getProjectDirectory() {
+    Path getAppHomeDirectory() {
         Paths.get(appHomePath)
     }

     /** Returns the user directory as {@link Path} */
-    Path getUserDirectory() {
-        Paths.get(userDirectoryPath)
+    Path getProjectDirectory() {
+        Paths.get(projectDirectoryPath)
     }

     /** Returns the default template directory as {@link Path} */
     Path getDefaultTemplateDirectory() {
-        projectDirectory.resolve("template")
+        appHomeDirectory.resolve("template")
     }

     Path getTocOutputFile() {
@@ -123,7 +123,7 @@ Path getTocOutputFile() {
     }

     Path getGaidenConfigFile() {
-        userDirectory.resolve(CONFIG_PATH)
+        projectDirectory.resolve(CONFIG_PATH)
     }

     @PostConstruct
diff --git a/src/main/groovy/gaiden/command/CleanCommand.groovy b/src/main/groovy/gaiden/command/CleanCommand.groovy
index ffd0826..a085b4c 100644
--- a/src/main/groovy/gaiden/command/CleanCommand.groovy
+++ b/src/main/groovy/gaiden/command/CleanCommand.groovy
@@ -45,7 +45,7 @@
     @CompileStatic(TypeCheckingMode.SKIP)
     @Override
     void execute(List<String> arguments, OptionAccessor optionAccessor) {
-        new AntBuilder().delete(dir: gaidenConfig.userDirectory.resolve(gaidenConfig.outputDirectoryPath))
+        new AntBuilder().delete(dir: gaidenConfig.projectDirectory.resolve(gaidenConfig.outputDirectoryPath))
         println messageSource.getMessage("command.clean.success.message")
     }
 }
diff --git a/src/main/groovy/gaiden/command/CreateProjectCommand.groovy b/src/main/groovy/gaiden/command/CreateProjectCommand.groovy
index bdfb633..9c1d85b 100644
--- a/src/main/groovy/gaiden/command/CreateProjectCommand.groovy
+++ b/src/main/groovy/gaiden/command/CreateProjectCommand.groovy
@@ -48,7 +48,7 @@ void execute(List<String> arguments, OptionAccessor optionAccessor) {
             throw new GaidenException("command.create.project.name.required.error")
         }

-        def projectDirectory = gaidenConfig.userDirectory.resolve(arguments.first())
+        def projectDirectory = gaidenConfig.projectDirectory.resolve(arguments.first())
         if (Files.exists(projectDirectory)) {
             throw new GaidenException("command.create.project.already.exists.error", [projectDirectory])
         }
diff --git a/src/main/groovy/gaiden/markdown/ImageRenderer.groovy b/src/main/groovy/gaiden/markdown/ImageRenderer.groovy
index 131855a..fa1e253 100644
--- a/src/main/groovy/gaiden/markdown/ImageRenderer.groovy
+++ b/src/main/groovy/gaiden/markdown/ImageRenderer.groovy
@@ -42,6 +42,7 @@
     ImageRenderer(PageSource pageSource, Path staticDirectory, Path outputDirectory, MessageSource messageSource) {
         this.pageSource = pageSource
         this.staticDirectory = staticDirectory
+        this.outputDirectory = outputDirectory
         this.messageSource = messageSource
     }

@@ -75,17 +76,22 @@ ImageElement render(String imagePath, String alt) {

     private String getRelativeImagePath(String imagePath) {
         def imageFile = getImageFile(imagePath)
-        checkFileExists(imageFile, imagePath)

-        pageSource.outputPath.relativize(outputDirectory.resolve(staticDirectory.relativize(imageFile))).toString()
+        if (Files.notExists(imageFile)) {
+            // It is required to normalize path such as '/path/to/../images/sample.png'.
+            System.err.println("WARNING: " + messageSource.getMessage("image.reference.not.exists.message", [imagePath, pageSource.path]))
+            return imagePath
+        }
+
+        pageSource.outputPath.parent.relativize(outputDirectory.resolve(staticDirectory.toRealPath().relativize(imageFile.toRealPath()))).toString()
     }

     private Path getImageFile(String imagePath) {
         def path = Paths.get(imagePath)
         if (path.absolute) {
-            return staticDirectory.resolve(path)
+            return staticDirectory.resolve(imagePath.substring(1))
         } else {
-            return pageSource.path.resolve(imagePath)
+            return staticDirectory.resolve(outputDirectory.relativize(pageSource.outputPath.parent).resolve(imagePath))
         }
     }

diff --git a/src/test/groovy/gaiden/GaidenSpec.groovy b/src/test/groovy/gaiden/GaidenSpec.groovy
index c31a6a1..514af79 100644
--- a/src/test/groovy/gaiden/GaidenSpec.groovy
+++ b/src/test/groovy/gaiden/GaidenSpec.groovy
@@ -18,10 +18,12 @@

 import gaiden.context.BuildContext
 import gaiden.message.MessageSource
-import spock.lang.AutoCleanup
+import org.apache.commons.io.FilenameUtils
 import spock.lang.Shared
 import spock.lang.Specification

+import java.nio.file.Files
+
 abstract class GaidenSpec extends Specification {

     def savedSystemOut
@@ -31,14 +33,15 @@
     @Shared
     MessageSource messageSource = new MessageSource()

-    @AutoCleanup("deleteDir")
     def pagesDirectory = File.createTempDir()

     def setup() {
         saveSystemProperties()
+        setupGaidenConfig()
     }

     def cleanup() {
+        cleanupGaidenConfig()
         restoreSystemProperties()
     }

@@ -48,6 +51,32 @@ BuildContext createBuildContext(List<Map> pageSourceMaps) {
         new BuildContext(documentSource: documentSource)
     }

+    PageSource createPageSource(String path) {
+        def pageSource = new PageSource()
+
+        pageSource.path = gaidenConfig.pagesDirectory.resolve(path)
+        if (Files.notExists(pageSource.path.parent)) {
+            Files.createDirectories(pageSource.path.parent)
+
+        }
+        Files.createFile(pageSource.path)
+
+        pageSource.outputPath = gaidenConfig.outputDirectory.resolve(FilenameUtils.removeExtension(path) + ".html")
+
+        pageSource
+    }
+
+    GaidenConfig gaidenConfig
+
+    private void setupGaidenConfig() {
+        gaidenConfig = new GaidenConfig()
+        gaidenConfig.projectDirectoryPath = Files.createTempDirectory("gaiden-project")
+    }
+
+    private void cleanupGaidenConfig() {
+        gaidenConfig.projectDirectory.deleteDir()
+    }
+
     private saveSystemProperties() {
         savedSystemOut = System.out
         savedSystemErr = System.err
diff --git a/src/test/groovy/gaiden/command/CleanCommandSpec.groovy b/src/test/groovy/gaiden/command/CleanCommandSpec.groovy
index 4695c07..c619d4c 100644
--- a/src/test/groovy/gaiden/command/CleanCommandSpec.groovy
+++ b/src/test/groovy/gaiden/command/CleanCommandSpec.groovy
@@ -35,7 +35,7 @@ def setup() {
         gaidenConfig = gaidenApplication.applicationContext.getBean(GaidenConfig)

         gaidenProjectDirectory = Files.createTempDirectory("project-dir")
-        gaidenConfig.userDirectoryPath = gaidenProjectDirectory.toString()
+        gaidenConfig.projectDirectoryPath = gaidenProjectDirectory.toString()
         Files.createFile(gaidenConfig.gaidenConfigFile)

         assert Files.exists(gaidenConfig.gaidenConfigFile)
@@ -49,7 +49,7 @@ def cleanup() {

     def "'execute' should clean the build directory"() {
         given:
-            def buildDirectory = gaidenConfig.userDirectory.resolve(gaidenConfig.outputDirectoryPath)
+            def buildDirectory = gaidenConfig.projectDirectory.resolve(gaidenConfig.outputDirectoryPath)
             Files.createDirectory(buildDirectory)
             assert Files.exists(buildDirectory)

diff --git a/src/test/groovy/gaiden/command/CreateProjectCommandSpec.groovy b/src/test/groovy/gaiden/command/CreateProjectCommandSpec.groovy
index 42c74c5..deebde4 100644
--- a/src/test/groovy/gaiden/command/CreateProjectCommandSpec.groovy
+++ b/src/test/groovy/gaiden/command/CreateProjectCommandSpec.groovy
@@ -38,7 +38,7 @@ def setup() {

         gaidenConfig = gaidenApplication.applicationContext.getBean(GaidenConfig)
         gaidenConfig.appHomePath = "src/dist"
-        gaidenConfig.userDirectoryPath = outputDirectory.canonicalPath
+        gaidenConfig.projectDirectoryPath = outputDirectory.canonicalPath

         newProjectName = "newProject"
         newProjectDirectory = new File(outputDirectory, newProjectName)
diff --git a/src/test/groovy/gaiden/markdown/ImageRendererSpec.groovy b/src/test/groovy/gaiden/markdown/ImageRendererSpec.groovy
index 259329a..b46df1f 100644
--- a/src/test/groovy/gaiden/markdown/ImageRendererSpec.groovy
+++ b/src/test/groovy/gaiden/markdown/ImageRendererSpec.groovy
@@ -19,6 +19,9 @@
 import gaiden.GaidenSpec
 import gaiden.PageSource

+import java.nio.file.Files
+import java.nio.file.Paths
+
 class ImageRendererSpec extends GaidenSpec {

     def savedSystemErr
@@ -31,10 +34,23 @@ def cleanup() {
         System.err = savedSystemErr
     }

+    private void createStaticResources(String... paths) {
+        paths.each { String path ->
+            def file = gaidenConfig.staticDirectory.resolve(path)
+            if (Files.notExists(file.parent)) {
+                Files.createDirectories(file.parent)
+            }
+            Files.createFile(file)
+        }
+    }
+
     def "'render' should return a rendering object which has replaced image path"() {
         setup:
-            def pageSource = new PageSource(path: pageSourcePath)
-            def renderer = new ImageRenderer(pageSource, new File("non-existent-static"), messageSource)
+            def pageSource = createPageSource(pageSourcePath)
+            def renderer = new ImageRenderer(pageSource, gaidenConfig.staticDirectory, gaidenConfig.outputDirectory, messageSource)
+
+        and:
+            createStaticResources("test.png", "img/test.png", "path/test.png")

         and:
             def printStream = Mock(PrintStream)
@@ -47,33 +63,38 @@ def cleanup() {
             rendering.src == renderingSrc
             rendering.alt == "TEST_ALT"

-        and: "output a warning message when a image file doesn't exist"
-            1 * printStream.println("WARNING: '${imagePath}' in the '${pageSource.path}' refers to non-existent file")
+//        and: "output a warning message when a image file doesn't exist"
+//            1 * printStream.println("WARNING: '${imagePath}' in the '${pageSource.path}' refers to non-existent file")

         where:
-            pageSourcePath | imagePath        | renderingSrc
-            "test.md"      | "/img/test.png"  | "img/test.png"
-            "test.md"      | "/test.png"      | "test.png"
-            "path/test.md" | "/img/test.png"  | "../img/test.png"
-            "path/test.md" | "/test.png"      | "../test.png"
-            "path/test.md" | "/path/test.png" | "test.png"
+            pageSourcePath | imagePath          | renderingSrc
+            "test.md"      | "/img/test.png"    | "img/test.png"
+            "test.md"      | "/test.png"        | "test.png"
+            "path/test.md" | "/img/test.png"    | "../img/test.png"
+            "path/test.md" | "/test.png"        | "../test.png"
+            "path/test.md" | "/path/test.png"   | "test.png"
+            "test.md"      | "img/test.png"     | "img/test.png"
+            "test.md"      | "test.png"         | "test.png"
+            "path/test.md" | "../img/test.png"  | "../img/test.png"
+            "path/test.md" | "../test.png"      | "../test.png"
+            "path/test.md" | "../path/test.png" | "test.png"
     }

-    def "'render' should not replace an image path if doesn't start with a slash"() {
-        setup:
-            def renderer = new ImageRenderer(new PageSource(path: "test.md"), new File("test/resources/static-files"), messageSource)
-
-        when:
-            def rendering = renderer.render("img/test.png", "TEST_ALT")
-
-        then:
-            rendering.src == "img/test.png"
-            rendering.alt == "TEST_ALT"
-    }
+//    def "'render' should not replace an image path if doesn't start with a slash"() {
+//        setup:
+//            def renderer = new ImageRenderer(createPageSource("test.md"), gaidenConfig.staticDirectory, gaidenConfig.outputDirectory, messageSource)
+//
+//        when:
+//            def rendering = renderer.render("img/test.png", "TEST_ALT")
+//
+//        then:
+//            rendering.src == "img/test.png"
+//            rendering.alt == "TEST_ALT"
+//    }

     def "'render' should not output a warning message when a image file exists"() {
         setup:
-            def renderer = new ImageRenderer(new PageSource(path: pageSourcePath), new File("src/test/resources/static-files"), messageSource)
+            def renderer = new ImageRenderer(new PageSource(path: Paths.get(pageSourcePath)), Paths.get("src/test/resources/static-files"), messageSource)

         and:
             def printStream = Mock(PrintStream)
@@ -99,7 +120,7 @@ def cleanup() {

     def "'render' should not output a warning message when a image path is URL"() {
         setup:
-            def renderer = new ImageRenderer(new PageSource(path: "test.md"), new File("src/test/resources/static-files"), messageSource)
+            def renderer = new ImageRenderer(new PageSource(path: Paths.get("test.md")), Paths.get("src/test/resources/static-files"), messageSource)

         and:
             def printStream = Mock(PrintStream)
[from now] 2014/08/03 19:24:16

diff --git a/src/test/groovy/gaiden/markdown/ImageRendererSpec.groovy b/src/test/groovy/gaiden/markdown/ImageRendererSpec.groovy
index b46df1f..6070efb 100644
--- a/src/test/groovy/gaiden/markdown/ImageRendererSpec.groovy
+++ b/src/test/groovy/gaiden/markdown/ImageRendererSpec.groovy
@@ -17,10 +17,8 @@
 package gaiden.markdown

 import gaiden.GaidenSpec
-import gaiden.PageSource

 import java.nio.file.Files
-import java.nio.file.Paths

 class ImageRendererSpec extends GaidenSpec {

@@ -34,16 +32,6 @@ def cleanup() {
         System.err = savedSystemErr
     }

-    private void createStaticResources(String... paths) {
-        paths.each { String path ->
-            def file = gaidenConfig.staticDirectory.resolve(path)
-            if (Files.notExists(file.parent)) {
-                Files.createDirectories(file.parent)
-            }
-            Files.createFile(file)
-        }
-    }
-
     def "'render' should return a rendering object which has replaced image path"() {
         setup:
             def pageSource = createPageSource(pageSourcePath)
@@ -63,9 +51,6 @@ private void createStaticResources(String... paths) {
             rendering.src == renderingSrc
             rendering.alt == "TEST_ALT"

-//        and: "output a warning message when a image file doesn't exist"
-//            1 * printStream.println("WARNING: '${imagePath}' in the '${pageSource.path}' refers to non-existent file")
-
         where:
             pageSourcePath | imagePath          | renderingSrc
             "test.md"      | "/img/test.png"    | "img/test.png"
@@ -80,21 +65,10 @@ private void createStaticResources(String... paths) {
             "path/test.md" | "../path/test.png" | "test.png"
     }

-//    def "'render' should not replace an image path if doesn't start with a slash"() {
-//        setup:
-//            def renderer = new ImageRenderer(createPageSource("test.md"), gaidenConfig.staticDirectory, gaidenConfig.outputDirectory, messageSource)
-//
-//        when:
-//            def rendering = renderer.render("img/test.png", "TEST_ALT")
-//
-//        then:
-//            rendering.src == "img/test.png"
-//            rendering.alt == "TEST_ALT"
-//    }
-
-    def "'render' should not output a warning message when a image file exists"() {
+    def "'render' should output a warning message when an image file is not found"() {
         setup:
-            def renderer = new ImageRenderer(new PageSource(path: Paths.get(pageSourcePath)), Paths.get("src/test/resources/static-files"), messageSource)
+            def pageSource = createPageSource(pageSourcePath)
+            def renderer = new ImageRenderer(pageSource, gaidenConfig.staticDirectory, gaidenConfig.outputDirectory, messageSource)

         and:
             def printStream = Mock(PrintStream)
@@ -107,20 +81,20 @@ private void createStaticResources(String... paths) {
             rendering.src == renderingSrc
             rendering.alt == "TEST_ALT"

-        and:
-            0 * printStream.println(_)
+        and: "output a warning message when a image file doesn't exist"
+            1 * printStream.println("WARNING: '${imagePath}' in the '${pageSource.path}' refers to non-existent file")

         where:
             pageSourcePath | imagePath             | renderingSrc
-            "test.md"      | "/images/dummy.png"   | "images/dummy.png"
+            "test.md"      | "/images/dummy.png"   | "/images/dummy.png"
             "test.md"      | "images/dummy.png"    | "images/dummy.png"
-            "path/test.md" | "/images/dummy.png"   | "../images/dummy.png"
+            "path/test.md" | "/images/dummy.png"   | "/images/dummy.png"
             "path/test.md" | "../images/dummy.png" | "../images/dummy.png"
     }

-    def "'render' should not output a warning message when a image path is URL"() {
+    def "'render' should not output a warning message when an image path is URL"() {
         setup:
-            def renderer = new ImageRenderer(new PageSource(path: Paths.get("test.md")), Paths.get("src/test/resources/static-files"), messageSource)
+            def renderer = new ImageRenderer(createPageSource("test.md"), gaidenConfig.staticDirectory, gaidenConfig.outputDirectory, messageSource)

         and:
             def printStream = Mock(PrintStream)
@@ -144,4 +118,14 @@ private void createStaticResources(String... paths) {
                 "file:///path/to/test.png",
             ]
     }
+
+    private void createStaticResources(String... paths) {
+        paths.each { String path ->
+            def file = gaidenConfig.staticDirectory.resolve(path)
+            if (Files.notExists(file.parent)) {
+                Files.createDirectories(file.parent)
+            }
+            Files.createFile(file)
+        }
+    }
 }