[from now] 2014/08/08 17:01:33

diff --git a/src/main/groovy/gaiden/DocumentWriter.groovy b/src/main/groovy/gaiden/DocumentWriter.groovy
index 89dbf77..e29d799 100644
--- a/src/main/groovy/gaiden/DocumentWriter.groovy
+++ b/src/main/groovy/gaiden/DocumentWriter.groovy
@@ -24,6 +24,7 @@ import org.springframework.beans.factory.annotation.Autowired
 import org.springframework.stereotype.Component

 import java.nio.file.Files
+import java.nio.file.Path

 /**
  * A document writer writes a {@link Document} to files.
@@ -58,7 +59,7 @@ class DocumentWriter {
         }

         writePages(document)
-        copyStaticFiles()
+        copyAssets()

         println "Built document at ${gaidenConfig.outputDirectory.toAbsolutePath()}"
     }
@@ -86,7 +87,16 @@ class DocumentWriter {
         Files.write(page.source.outputPath, content.getBytes(gaidenConfig.outputEncoding))
     }

-    private void copyStaticFiles() {
-        PathUtils.copyFiles(gaidenConfig.assetsDirectory, gaidenConfig.outputDirectory)
+    private void copyAssets() {
+        PathUtils.eachFileRecurse(gaidenConfig.pagesDirectory, [gaidenConfig.outputDirectory, gaidenConfig.templateFile.parent]) { Path src ->
+            if (isAsset(src)) {
+                def dest = gaidenConfig.outputDirectory.resolve(gaidenConfig.pagesDirectory.relativize(src))
+                PathUtils.copyFile(src, dest)
+            }
+        }
+    }
+
+    private boolean isAsset(Path path) {
+        PathUtils.getExtension(path)?.toLowerCase() in gaidenConfig.assetTypes
     }
 }
diff --git a/src/main/groovy/gaiden/GaidenConfig.groovy b/src/main/groovy/gaiden/GaidenConfig.groovy
index a64a760..3889877 100644
--- a/src/main/groovy/gaiden/GaidenConfig.groovy
+++ b/src/main/groovy/gaiden/GaidenConfig.groovy
@@ -36,7 +36,6 @@ import java.nio.file.Paths
 @CompileStatic
 class GaidenConfig {

-    static final String DEFAULT_ASSETS_DIRECTORY = "assets"
     static final String DEFAULT_BUILD_DIRECTORY = "build"
     static final String DEFAULT_TEMPLATE_FILE = "templates/layout.html"
     static final String DEFAULT_ENCODING = "UTF-8"
@@ -65,9 +64,6 @@ class GaidenConfig {
     /** The path of page source files directory */
     Path pagesDirectory = defaultProjectDirectory

-    /** The path of static files directory */
-    Path assetsDirectory = defaultProjectDirectory.resolve(DEFAULT_ASSETS_DIRECTORY)
-
     /** The path of directory to be outputted a document */
     Path outputDirectory = defaultProjectDirectory.resolve(DEFAULT_BUILD_DIRECTORY)

@@ -90,6 +86,8 @@ class GaidenConfig {

     boolean numbering = true

+    List<String> assetTypes = ["jpg", "jpeg", "png", "gif"]
+
     void setProjectDirectoryPath(String projectDirectory) {
         this.projectDirectory = Paths.get(projectDirectory)
     }
@@ -102,10 +100,6 @@ class GaidenConfig {
         this.pagesDirectory = projectDirectory.resolve(pagesDirectory)
     }

-    void setAssetsDirectoryPath(String assetsDirectory) {
-        this.assetsDirectory = projectDirectory.resolve(assetsDirectory)
-    }
-
     void setOutputDirectoryPath(String outputDirectory) {
         this.outputDirectory = projectDirectory.resolve(outputDirectory)
     }
diff --git a/src/main/groovy/gaiden/SourceCollector.groovy b/src/main/groovy/gaiden/SourceCollector.groovy
index 4dc1c26..c6f2963 100644
--- a/src/main/groovy/gaiden/SourceCollector.groovy
+++ b/src/main/groovy/gaiden/SourceCollector.groovy
@@ -51,7 +51,7 @@ class SourceCollector {

     private List<PageSource> collectPageSources() {
         def pageSources = []
-        PathUtils.eachFileRecurse(gaidenConfig.pagesDirectory, [gaidenConfig.assetsDirectory, gaidenConfig.outputDirectory, gaidenConfig.templateFile.parent]) { Path path ->
+        PathUtils.eachFileRecurse(gaidenConfig.pagesDirectory, [gaidenConfig.outputDirectory, gaidenConfig.templateFile.parent]) { Path path ->
             if (isPageSourceFile(path)) {
                 pageSources << createPageSource(path)
             }
diff --git a/src/main/groovy/gaiden/markdown/GaidenMarkdownProcessor.groovy b/src/main/groovy/gaiden/markdown/GaidenMarkdownProcessor.groovy
index 814435d..a62dc7e 100644
--- a/src/main/groovy/gaiden/markdown/GaidenMarkdownProcessor.groovy
+++ b/src/main/groovy/gaiden/markdown/GaidenMarkdownProcessor.groovy
@@ -69,7 +69,7 @@ class GaidenMarkdownProcessor extends PegDownProcessor {
      */
     String convertToHtml(Page page, Document document) throws ParsingTimeoutException {
         def linkRenderer = new GaidenLinkRenderer(page: page, document: document, messageSource: messageSource)
-        def imageRenderer = new ImageRenderer(page, gaidenConfig.assetsDirectory, gaidenConfig.outputDirectory, messageSource)
+        def imageRenderer = new ImageRenderer(page, gaidenConfig.outputDirectory, messageSource)
         new GaidenToHtmlSerializer(linkRenderer, imageRenderer).toHtml(page.contentNode)
     }
 }
diff --git a/src/main/groovy/gaiden/markdown/ImageRenderer.groovy b/src/main/groovy/gaiden/markdown/ImageRenderer.groovy
index 0a55236..3aabaf6 100644
--- a/src/main/groovy/gaiden/markdown/ImageRenderer.groovy
+++ b/src/main/groovy/gaiden/markdown/ImageRenderer.groovy
@@ -24,6 +24,7 @@ import groovy.transform.CompileStatic
 import java.nio.file.Files
 import java.nio.file.LinkOption
 import java.nio.file.Path
+import java.nio.file.Paths

 /**
  * A Renderer for image node.
@@ -35,13 +36,11 @@ import java.nio.file.Path
 class ImageRenderer {

     private Page page
-    private Path staticDirectory
     private Path outputDirectory
     private MessageSource messageSource

-    ImageRenderer(Page page, Path staticDirectory, Path outputDirectory, MessageSource messageSource) {
+    ImageRenderer(Page page, Path outputDirectory, MessageSource messageSource) {
         this.page = page
-        this.staticDirectory = staticDirectory
         this.outputDirectory = outputDirectory
         this.messageSource = messageSource
     }
@@ -66,7 +65,7 @@ class ImageRenderer {
      * @return {@link ImageElement}'s instance
      */
     ImageElement render(String imagePath, String alt) {
-        if (UrlUtils.isUrl(imagePath)) {  // URL
+        if (UrlUtils.isUrl(imagePath) || Paths.get(imagePath).absolute) {
             return new ImageElement(src: imagePath, alt: alt)
         }

@@ -76,8 +75,6 @@ class ImageRenderer {
             System.err.println("WARNING: " + messageSource.getMessage("image.reference.not.exists.message", [imagePath, page.source.path]))
             return new ImageElement(src: imagePath, alt: alt)
         }
-
-        def outputImage = outputDirectory.resolve(staticDirectory.relativize(image.toRealPath(LinkOption.NOFOLLOW_LINKS)))
-        return new ImageElement(src: page.relativize(outputImage), alt: alt)
+        return new ImageElement(src: page.source.path.parent.relativize(image.toRealPath(LinkOption.NOFOLLOW_LINKS)).toString(), alt: alt)
     }
 }
diff --git a/src/main/groovy/gaiden/util/PathUtils.groovy b/src/main/groovy/gaiden/util/PathUtils.groovy
index f65b89d..e2cfaa7 100644
--- a/src/main/groovy/gaiden/util/PathUtils.groovy
+++ b/src/main/groovy/gaiden/util/PathUtils.groovy
@@ -48,13 +48,17 @@ class PathUtils {
         Paths.get(FilenameUtils.removeExtension(path.toString()))
     }

+    static void copyFile(Path src, Path dest) {
+        if (Files.notExists(dest.parent)) {
+            Files.createDirectories(dest.parent)
+        }
+        Files.copy(src, dest, REPLACE_EXISTING)
+    }
+
     static void copyFiles(Path src, Path dest) {
         src.eachFileRecurse(FileType.FILES) { Path srcFile ->
             def destFile = dest.resolve(src.relativize(srcFile))
-            if (Files.notExists(destFile.parent)) {
-                Files.createDirectories(destFile.parent)
-            }
-            Files.copy(srcFile, destFile, REPLACE_EXISTING)
+            copyFile(srcFile, destFile)
         }
     }

diff --git a/src/test/groovy/gaiden/FunctionalSpec.groovy b/src/test/groovy/gaiden/FunctionalSpec.groovy
index d372376..aae8f05 100644
--- a/src/test/groovy/gaiden/FunctionalSpec.groovy
+++ b/src/test/groovy/gaiden/FunctionalSpec.groovy
@@ -69,7 +69,6 @@ abstract class FunctionalSpec extends GaidenSpec {
         def projectDirectory = Paths.get(path).toRealPath(LinkOption.NOFOLLOW_LINKS)
         gaidenConfig.projectDirectory = projectDirectory
         gaidenConfig.gaidenConfigFile = projectDirectory.resolve(GaidenConfig.GAIDEN_CONFIG_FILENAME)
-        gaidenConfig.assetsDirectory = projectDirectory.resolve(GaidenConfig.DEFAULT_ASSETS_DIRECTORY)
         gaidenConfig.pagesDirectory = projectDirectory
         gaidenConfig.templateFile = projectDirectory.resolve(GaidenConfig.DEFAULT_TEMPLATE_FILE)
         gaidenConfig.pagesFile = projectDirectory.resolve(GaidenConfig.PAGES_FILENAME)
diff --git a/src/test/resources/test-project/assets/image.jpg b/src/test/resources/test-project/assets/image.jpg
deleted file mode 100644
index e69de29..0000000
diff --git a/src/test/resources/test-project/assets/image.png b/src/test/resources/test-project/assets/image.png
new file mode 100644
index 0000000..e69de29
diff --git a/src/test/resources/test-project/build/image.jpg b/src/test/resources/test-project/build/image.jpg
deleted file mode 100644
index e69de29..0000000
diff --git a/src/test/resources/test-project/build/images/README.html b/src/test/resources/test-project/build/images/README.html
index 450ba3a..b5dc592 100644
--- a/src/test/resources/test-project/build/images/README.html
+++ b/src/test/resources/test-project/build/images/README.html
@@ -38,9 +38,13 @@
     <div>
       <h1>Image Test</h1>
 <ul>
-  <li><img src="../image.jpg" alt="Alt text"/></li>
-  <li><img src="../image.jpg" alt="Alt text" title="Optional title"/></li>
-  <li><img src="../image.jpg" alt="Alt text" title="Optional title attribute"/></li>
+  <li><img src="image.jpg" alt="Alt text"/></li>
+  <li><img src="image.jpg" alt="Alt text" title="Optional title"/></li>
+  <li><img src="image.jpg" alt="Alt text" title="Optional title attribute"/></li>
+  <li><img src="image.jpg" alt="Alt text" title="Optional title attribute"/></li>
+  <li><p><img src="../assets/image.png" alt="Alt text"/></p></li>
+  <li><img src="../assets/image.png" alt="Alt text" title="Optional title"/></li>
+  <li><img src="../assets/image.png" alt="Alt text" title="Optional title attribute"/></li>
 </ul>
     </div>
   </body>
diff --git a/src/test/resources/test-project/images/README.md b/src/test/resources/test-project/images/README.md
index 88d7e69..95770ac 100644
--- a/src/test/resources/test-project/images/README.md
+++ b/src/test/resources/test-project/images/README.md
@@ -1,10 +1,14 @@
 # Image Test

-* ![Alt text](../assets/image.jpg)
-* ![Alt text](../assets/image.jpg "Optional title")
-* ![Alt text][id]
-
-[id]: ../assets/image.jpg  "Optional title attribute"
-
-
-
+* ![Alt text](image.jpg)
+* ![Alt text](./image.jpg "Optional title")
+* ![Alt text][image1]
+* ![Alt text][image2]
+
+* ![Alt text](../assets/image.png)
+* ![Alt text](../assets/image.png "Optional title")
+* ![Alt text][image3]
+
+[image1]: image.jpg  "Optional title attribute"
+[image2]: ./image.jpg  "Optional title attribute"
+[image3]: ../assets/image.png  "Optional title attribute"
[from now] 2014/08/08 17:01:42

diff --git a/src/test/resources/test-project/build/assets/image.png b/src/test/resources/test-project/build/assets/image.png
new file mode 100644
index 0000000..e69de29
diff --git a/src/test/resources/test-project/build/images/image.jpg b/src/test/resources/test-project/build/images/image.jpg
new file mode 100644
index 0000000..e69de29
diff --git a/src/test/resources/test-project/images/image.jpg b/src/test/resources/test-project/images/image.jpg
new file mode 100644
index 0000000..e69de29