[from now] 2014/06/19 13:52:04

diff --git a/src/main/groovy/gaiden/kai/DocumentWriter.groovy b/src/main/groovy/gaiden/kai/DocumentWriter.groovy
index 107918c..75f4327 100644
--- a/src/main/groovy/gaiden/kai/DocumentWriter.groovy
+++ b/src/main/groovy/gaiden/kai/DocumentWriter.groovy
@@ -1,23 +1,27 @@
 package gaiden.kai

-import gaiden.kai.writer.MarkdownPageWriter
-import gaiden.kai.writer.MarkdownTocWriter
+import gaiden.kai.writer.TocWriter
 import groovy.transform.CompileStatic
+import groovy.util.logging.Slf4j
 import org.springframework.beans.factory.annotation.Autowired
 import org.springframework.stereotype.Component

+@Slf4j
 @Component
 @CompileStatic
 class DocumentWriter {

     @Autowired
-    MarkdownTocWriter markdownTocWriter
+    TocWriter tocWriter

     @Autowired
-    MarkdownPageWriter markdownPageWriter
+    PageWriterResolver pageWriterResolver

     void write(DocumentSource documentSource) {
-        markdownTocWriter.write(documentSource.tocSource)
-        documentSource.pageSources.each { markdownPageWriter.write(it) }
+        tocWriter.write(documentSource.tocSource)
+        documentSource.pageSources.each { PageSource pageSource ->
+            def pageWriter = pageWriterResolver.resolve(pageSource)
+            pageWriter.write(pageSource)
+        }
     }
 }
diff --git a/src/main/groovy/gaiden/kai/PageSource.groovy b/src/main/groovy/gaiden/kai/PageSource.groovy
index 465dcdd..3fb3e48 100644
--- a/src/main/groovy/gaiden/kai/PageSource.groovy
+++ b/src/main/groovy/gaiden/kai/PageSource.groovy
@@ -23,13 +23,11 @@ import java.nio.file.Path
 @CompileStatic
 class PageSource {

-    Path file
-    Path realFile
+    Path path
+    Path realPath
     String inputEncoding

     String getText() {
-        println realFile.toAbsolutePath()
-        println inputEncoding
-        realFile.getText(inputEncoding)
+        realPath.getText(inputEncoding)
     }
 }
diff --git a/src/main/groovy/gaiden/kai/SourceCollector.groovy b/src/main/groovy/gaiden/kai/SourceCollector.groovy
index 3ea97c4..7cfcae5 100644
--- a/src/main/groovy/gaiden/kai/SourceCollector.groovy
+++ b/src/main/groovy/gaiden/kai/SourceCollector.groovy
@@ -18,12 +18,10 @@ package gaiden.kai

 import groovy.io.FileType
 import groovy.transform.CompileStatic
-import org.apache.commons.io.FilenameUtils
 import org.springframework.beans.factory.annotation.Autowired
 import org.springframework.stereotype.Component

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

 @Component
 @CompileStatic
@@ -33,14 +31,14 @@ class SourceCollector {
     GaidenConfig gaidenConfig

     DocumentSource collect() {
-        def tocSource = collectTocSource()
         def pageSources = collectPageSources()
+        def tocSource = collectTocSource(pageSources)

         new DocumentSource(tocSource: tocSource, pageSources: pageSources)
     }

-    private TocSource collectTocSource() {
-        new TocSource(gaidenConfig.tocFile, gaidenConfig.inputEncoding)
+    private TocSource collectTocSource(List<PageSource> pageSources) {
+        new TocSource(gaidenConfig.tocFile, pageSources, gaidenConfig.inputEncoding)
     }

     private List<PageSource> collectPageSources() {
@@ -54,9 +52,9 @@ class SourceCollector {
     private PageSource createPageSource(Path path) {
         def pageSource = new PageSource()

-        pageSource.realFile = path
+        pageSource.realPath = path
         pageSource.inputEncoding = gaidenConfig.inputEncoding
-        pageSource.file = gaidenConfig.pagesDirectory.relativize(path)
+        pageSource.path = gaidenConfig.pagesDirectory.relativize(path)

         return pageSource
     }
diff --git a/src/main/groovy/gaiden/kai/TocNode.groovy b/src/main/groovy/gaiden/kai/TocNode.groovy
index 0abd523..e22cf78 100644
--- a/src/main/groovy/gaiden/kai/TocNode.groovy
+++ b/src/main/groovy/gaiden/kai/TocNode.groovy
@@ -19,4 +19,6 @@ class TocNode {
     List<TocNode> children
     TocNode previous
     TocNode next
+
+    PageSource pageSource
 }
diff --git a/src/main/groovy/gaiden/kai/TocSource.groovy b/src/main/groovy/gaiden/kai/TocSource.groovy
index 05aed94..7aa2009 100644
--- a/src/main/groovy/gaiden/kai/TocSource.groovy
+++ b/src/main/groovy/gaiden/kai/TocSource.groovy
@@ -12,8 +12,11 @@ class TocSource {

     List<TocNode> tocNodes

+    private List<PageSource> pageSources
+
     @CompileStatic(TypeCheckingMode.SKIP)
-    TocSource(Path tocFile, String inputEncoding) {
+    TocSource(Path tocFile, List<PageSource> pageSources, String inputEncoding) {
+        this.pageSources = pageSources
         def tocBuilder = new NodeBuilder()
         def rootNode = tocBuilder.toc(new GroovyShell().evaluate("{ -> ${tocFile.getText(inputEncoding)} }"))
         tocNodes = buildNodes(rootNode.children())
@@ -35,6 +38,7 @@ class TocSource {
                 tocNode.previous = previous
                 previous.next = tocNode
             }
+            tocNode.pageSource = getPageSourceOf(tocNode)
             tocNodes << tocNode
         }
         tocNodes
@@ -47,4 +51,8 @@ class TocSource {
         }
         getPreviousLastChild(tocNode.children.last())
     }
+
+    private PageSource getPageSourceOf(TocNode tocNode) {
+        pageSources.find { PageSource pageSource -> pageSource.path == tocNode.path }
+    }
 }
diff --git a/src/main/groovy/gaiden/kai/writer/MarkdownPageWriter.groovy b/src/main/groovy/gaiden/kai/writer/MarkdownPageWriter.groovy
index 9e84330..24d3215 100644
--- a/src/main/groovy/gaiden/kai/writer/MarkdownPageWriter.groovy
+++ b/src/main/groovy/gaiden/kai/writer/MarkdownPageWriter.groovy
@@ -2,6 +2,7 @@ package gaiden.kai.writer

 import gaiden.kai.GaidenConfig
 import gaiden.kai.PageSource
+import gaiden.kai.PageWriter
 import gaiden.kai.markdown.MarkdownProcessor
 import groovy.transform.CompileStatic
 import org.apache.commons.io.FilenameUtils
@@ -14,7 +15,7 @@ import java.nio.file.Paths

 @Component
 @CompileStatic
-class MarkdownPageWriter {
+class MarkdownPageWriter implements PageWriter {

     @Autowired
     GaidenConfig gaidenConfig
@@ -23,14 +24,20 @@ class MarkdownPageWriter {
     MarkdownProcessor markdownProcessor

     void write(PageSource pageSource) {
-        def out = gaidenConfig.outputDirectory.resolve(outputPathOf(pageSource.file))
+        def out = gaidenConfig.outputDirectory.resolve(getOutputPathOf(pageSource))
         if (Files.notExists(out.parent)) {
             Files.createDirectories(out.parent)
         }
         out.write(markdownProcessor.markdownToHtml(pageSource))
     }

-    static Path outputPathOf(Path path) {
-        Paths.get(FilenameUtils.removeExtension(path.toString()) + ".html")
+    @Override
+    boolean supports(PageSource pageSource) {
+        FilenameUtils.getExtension(pageSource.path.toString()) in ["markdown", "md"]
+    }
+
+    @Override
+    Path getOutputPathOf(PageSource pageSource) {
+        Paths.get(FilenameUtils.removeExtension(pageSource.path.toString()) + ".html")
     }
 }
diff --git a/src/main/groovy/gaiden/kai/writer/MarkdownTocWriter.groovy b/src/main/groovy/gaiden/kai/writer/MarkdownTocWriter.groovy
deleted file mode 100644
index 4dd39bc..0000000
--- a/src/main/groovy/gaiden/kai/writer/MarkdownTocWriter.groovy
+++ /dev/null
@@ -1,62 +0,0 @@
-package gaiden.kai.writer
-
-import gaiden.kai.GaidenConfig
-import gaiden.kai.TocNode
-import gaiden.kai.TocSource
-import groovy.json.JsonBuilder
-import groovy.transform.CompileStatic
-import org.springframework.beans.factory.annotation.Autowired
-import org.springframework.stereotype.Component
-
-import java.nio.file.Files
-
-@Component
-@CompileStatic
-class MarkdownTocWriter {
-
-    @Autowired
-    GaidenConfig gaidenConfig
-
-    void write(TocSource tocSource) {
-        def tocFile = gaidenConfig.outputDirectory.resolve("toc.js")
-        if (Files.notExists(tocFile.parent)) {
-            Files.createDirectories(tocFile.parent)
-        }
-        tocFile.write($/\
-var gaiden;
-(function(gaiden) {
-    gaiden.toc = ${toJson(tocSource)};
-})(gaiden || (gaiden = {}));
-/$)
-    }
-
-    private String toJson(TocSource tocSource) {
-        new JsonBuilder(toMapList(tocSource.tocNodes)).toPrettyString()
-    }
-
-    private List<Map> toMapList(List<TocNode> tocNodes) {
-        return tocNodes.collect { TocNode tocNode ->
-            def tocNodeMap = [:]
-            tocNodeMap["title"] = tocNode.title
-            tocNodeMap["path"] = outputPathOf(tocNode)
-            tocNodeMap["numbers"] = tocNode.numbers.join(".")
-            if (tocNode.parent) {
-                tocNodeMap["parent"] = outputPathOf(tocNode.parent)
-            }
-            if (tocNode.next) {
-                tocNodeMap["next"] = outputPathOf(tocNode.next)
-            }
-            if (tocNode.previous) {
-                tocNodeMap["previous"] = outputPathOf(tocNode.previous)
-            }
-            if (tocNode.children) {
-                tocNodeMap["children"] = toMapList(tocNode.children)
-            }
-            tocNodeMap
-        }
-    }
-
-    private static String outputPathOf(TocNode tocNode) {
-        MarkdownPageWriter.outputPathOf(tocNode.path).toString()
-    }
-}
diff --git a/src/main/groovy/gaiden/kai/writer/TocWriter.groovy b/src/main/groovy/gaiden/kai/writer/TocWriter.groovy
new file mode 100644
index 0000000..7c615b1
--- /dev/null
+++ b/src/main/groovy/gaiden/kai/writer/TocWriter.groovy
@@ -0,0 +1,67 @@
+package gaiden.kai.writer
+
+import gaiden.kai.GaidenConfig
+import gaiden.kai.PageWriterResolver
+import gaiden.kai.TocNode
+import gaiden.kai.TocSource
+import groovy.json.JsonBuilder
+import groovy.transform.CompileStatic
+import org.springframework.beans.factory.annotation.Autowired
+import org.springframework.stereotype.Component
+
+import java.nio.file.Files
+
+@Component
+@CompileStatic
+class TocWriter {
+
+    @Autowired
+    GaidenConfig gaidenConfig
+
+    @Autowired
+    PageWriterResolver pageWriterResolver
+
+    void write(TocSource tocSource) {
+        def tocFile = gaidenConfig.outputDirectory.resolve("toc.js")
+        if (Files.notExists(tocFile.parent)) {
+            Files.createDirectories(tocFile.parent)
+        }
+        tocFile.write($/\
+var gaiden;
+(function(gaiden) {
+    gaiden.toc = ${toJson(tocSource)};
+})(gaiden || (gaiden = {}));
+/$)
+    }
+
+    private String toJson(TocSource tocSource) {
+        new JsonBuilder(toMapList(tocSource.tocNodes)).toPrettyString()
+    }
+
+    private List<Map> toMapList(List<TocNode> tocNodes) {
+        return tocNodes.collect { TocNode tocNode ->
+            def tocNodeMap = [:]
+            tocNodeMap["title"] = tocNode.title
+            tocNodeMap["path"] = getOutputPathOf(tocNode)
+            tocNodeMap["numbers"] = tocNode.numbers.join(".")
+            if (tocNode.parent) {
+                tocNodeMap["parent"] = getOutputPathOf(tocNode.parent)
+            }
+            if (tocNode.next) {
+                tocNodeMap["next"] = getOutputPathOf(tocNode.next)
+            }
+            if (tocNode.previous) {
+                tocNodeMap["previous"] = getOutputPathOf(tocNode.previous)
+            }
+            if (tocNode.children) {
+                tocNodeMap["children"] = toMapList(tocNode.children)
+            }
+            tocNodeMap
+        }
+    }
+
+    private String getOutputPathOf(TocNode tocNode) {
+        def pageWriter = pageWriterResolver.resolve(tocNode.pageSource)
+        pageWriter.getOutputPathOf(tocNode.pageSource).toString()
+    }
+}
diff --git a/src/test/groovy/gaiden/kai/GaidenSpec.groovy b/src/test/groovy/gaiden/kai/GaidenSpec.groovy
index b199758..c447b75 100644
--- a/src/test/groovy/gaiden/kai/GaidenSpec.groovy
+++ b/src/test/groovy/gaiden/kai/GaidenSpec.groovy
@@ -44,8 +44,8 @@ abstract class GaidenSpec extends Specification {

         def pageSource = new PageSource()
         pageSource.inputEncoding = gaidenConfig.inputEncoding
-        pageSource.realFile = file
-        pageSource.file = Paths.get(path)
+        pageSource.realPath = file
+        pageSource.path = Paths.get(path)
         pageSource
     }

diff --git a/src/test/groovy/gaiden/kai/SourceCollectorSpec.groovy b/src/test/groovy/gaiden/kai/SourceCollectorSpec.groovy
index 4fb655d..567444a 100644
--- a/src/test/groovy/gaiden/kai/SourceCollectorSpec.groovy
+++ b/src/test/groovy/gaiden/kai/SourceCollectorSpec.groovy
@@ -23,8 +23,8 @@ class SourceCollectorSpec extends GaidenSpec {

         and:
             def pageSource = documentSource.pageSources.first()
-            pageSource.realFile == testPageSource
-            pageSource.file == Paths.get("a/b/c")
+            pageSource.realPath == testPageSource
+            pageSource.path == Paths.get("a/b/c")
             pageSource.inputEncoding == gaidenConfig.inputEncoding
             pageSource.text == "Test Markdown"
     }
@@ -42,6 +42,6 @@ class SourceCollectorSpec extends GaidenSpec {

         then:
             documentSource.pageSources.size() == 3
-            documentSource.pageSources*.realFile as Set == pageSources as Set
+            documentSource.pageSources*.realPath as Set == pageSources as Set
     }
 }
[from now] 2014/06/19 13:52:09

diff --git a/src/main/groovy/gaiden/kai/PageWriter.groovy b/src/main/groovy/gaiden/kai/PageWriter.groovy
new file mode 100644
index 0000000..921aad5
--- /dev/null
+++ b/src/main/groovy/gaiden/kai/PageWriter.groovy
@@ -0,0 +1,15 @@
+package gaiden.kai
+
+import groovy.transform.CompileStatic
+
+import java.nio.file.Path
+
+@CompileStatic
+interface PageWriter {
+
+    void write(PageSource pageSource)
+
+    boolean supports(PageSource pageSource)
+
+    Path getOutputPathOf(PageSource pageSource)
+}
diff --git a/src/main/groovy/gaiden/kai/PageWriterResolver.groovy b/src/main/groovy/gaiden/kai/PageWriterResolver.groovy
new file mode 100644
index 0000000..8e34dde
--- /dev/null
+++ b/src/main/groovy/gaiden/kai/PageWriterResolver.groovy
@@ -0,0 +1,36 @@
+package gaiden.kai
+
+import groovy.transform.CompileStatic
+import groovy.util.logging.Slf4j
+import org.springframework.beans.factory.annotation.Autowired
+import org.springframework.stereotype.Component
+
+import java.nio.file.Path
+
+@Slf4j
+@Component
+@CompileStatic
+class PageWriterResolver {
+
+    @Autowired
+    List<PageWriter> pageWriters
+
+    PageWriter resolve(PageSource pageSource) {
+        def pageWriter = pageWriters.find { it.supports(pageSource) }
+        pageWriter ?: UNKNOWN_PAGE_SOURCE_WRITER
+    }
+
+    private static final PageWriter UNKNOWN_PAGE_SOURCE_WRITER = new PageWriter() {
+
+        @Override
+        void write(PageSource pageSource) {
+            log.warn("Unknown page source: ${pageSource.realPath.toString()}")
+        }
+
+        @Override
+        boolean supports(PageSource pageSource) { true }
+
+        @Override
+        Path getOutputPathOf(PageSource pageSource) { null }
+    }
+}