[from now] 2014/08/02 20:52:29

diff --git a/src/main/groovy/gaiden/PageReferenceFactory.groovy b/src/main/groovy/gaiden/PageReferenceFactory.groovy
index 9cf5e6b..1491f60 100644
--- a/src/main/groovy/gaiden/PageReferenceFactory.groovy
+++ b/src/main/groovy/gaiden/PageReferenceFactory.groovy
@@ -1,5 +1,6 @@
 package gaiden

+import gaiden.util.PathUtils
 import groovy.transform.CompileStatic
 import org.springframework.beans.factory.annotation.Autowired
 import org.springframework.stereotype.Component
@@ -25,6 +26,10 @@ PageReference get(Path path, PageSource basePageSource = null) {
     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())
+        }
+
         if (path.absolute) {
             return getCanonicalPath(gaidenConfig.pagesDirectory.resolve(path.toString().substring(1)))
         }
@@ -41,16 +46,11 @@ private static Path getCanonicalPath(Path path) {
             return path
         }

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

     private static String getHash(Path path) {
diff --git a/src/main/groovy/gaiden/TocBuilder.groovy b/src/main/groovy/gaiden/TocBuilder.groovy
index e5322aa..4f155c6 100644
--- a/src/main/groovy/gaiden/TocBuilder.groovy
+++ b/src/main/groovy/gaiden/TocBuilder.groovy
@@ -129,18 +129,14 @@ private String buildContent(BuildContext context, List<TocNode> tocNodes) {
     }

     private String resolveOutputPath(BuildContext context, Path path) {
-        def pageReference = pageReferenceFactory.get(path, null)
-        def extension = PathUtils.getExtension(pageReference.path)
-        if (!(extension in SourceCollector.PAGE_SOURCE_EXTENSIONS)) {
-            return path
-        }
-
+        def pageReference = pageReferenceFactory.get(path)
         def pageSource = context.documentSource.findPageSource(pageReference)
         if (!pageSource) {
-            System.err.println("WARNING: " + messageSource.getMessage("toc.page.reference.not.exists.message", [path]))
+            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 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 4a92ecf..1965f94 100644
--- a/src/main/groovy/gaiden/markdown/GaidenLinkRenderer.groovy
+++ b/src/main/groovy/gaiden/markdown/GaidenLinkRenderer.groovy
@@ -77,7 +77,9 @@
             if (isPageSourceExtension(url)) {
                 System.err.println("WARNING: " + messageSource.getMessage("output.page.reference.not.exists.message", [url, pageSource.path]))
             }
-            return createRendering(url, title, text)
+
+            def relativePath = pageSource.outputPath.parent.relativize(pageReference.path).toString()
+            return createRendering(relativePath, title, text)
         }

         def relativePath = pageSource.outputPath.parent.relativize(targetPageSource.outputPath).toString()