diff --git a/src/main/groovy/markdown2book/Generator.groovy b/src/main/groovy/markdown2book/Generator.groovy index 0d6af64..70f309a 100644 --- a/src/main/groovy/markdown2book/Generator.groovy +++ b/src/main/groovy/markdown2book/Generator.groovy @@ -15,7 +15,6 @@ class Generator { private static MARKDOWN_EXTENSIONS = ['.markdown', '.md', '.mdown'] private static HEADING_PATTERN = Pattern.compile("^(#+)\\s+(.*)\$") private static FILENAME_SUBSTITUTE_PATTERN = Pattern.compile("^(?:\\d+-)?(.*?)\\.(?:markdown|md|mdown)\$") - private static MARKDOWN_PROCESSOR = new PegDownProcessor(Extensions.ALL) private static TEMPLATE_ENGINE = new SimpleTemplateEngine() private static TEMPLATE_EXTENSION = "html" private static ALL_TEMPLATE = "all." + TEMPLATE_EXTENSION @@ -25,16 +24,18 @@ class Generator { private static CHAPTERS_DIR_NAME = 'chapters' private static REFERENCES_FILE_NAME = "references.markdown" private static DONT_COPY_TO_DESTINATION = [TEMPLATES_DIR_NAME, CHAPTERS_DIR_NAME, REFERENCES_FILE_NAME] - + private toc = [] private chapters = [:] private references - + + final PegDownProcessor markdownProcessor final File source final File destination final String charset - Generator(File source, File destination, String charset = Charset.defaultCharset().name()) { + Generator(File source, File destination, String charset = Charset.defaultCharset().name(), int extensions = Extensions.ALL) { + markdownProcessor = new PegDownProcessor(extensions) if (source == null) { fail("The 'source' argument can not be null") } @@ -258,7 +259,7 @@ class Generator { } private markdown(input) { - MARKDOWN_PROCESSOR.markdownToHtml(input) + markdownProcessor.markdownToHtml(input) } private getChaptersSourceDir() { diff --git a/src/test/groovy/markdown2book/BaseSpec.groovy b/src/test/groovy/markdown2book/BaseSpec.groovy index a770d1b..7b73acc 100644 --- a/src/test/groovy/markdown2book/BaseSpec.groovy +++ b/src/test/groovy/markdown2book/BaseSpec.groovy @@ -1,12 +1,19 @@ package markdown2book +import org.junit.Rule +import org.junit.rules.TemporaryFolder import spock.lang.* +import java.nio.charset.Charset + abstract class BaseSpec extends Specification { - + + @Rule + TemporaryFolder destRoot; + def createDestination(book) { // This is no good, but unsure of a way to safely get to the target dir - def destination = new File("target/books", book) + def destination = destRoot.newFolder(book) if (destination.exists()) { assert destination.deleteDir() } @@ -14,17 +21,17 @@ abstract class BaseSpec extends Specification { destination } - def generate(book) { + def generate(book, extensions = null) { def destination = createDestination(book) - def generator = createGenerator(book, destination) + def generator = createGenerator(book, destination, extensions) generator.generate() destination } - def createGenerator(book, destination) { + def createGenerator(book, destination, extensions) { def source = new File(this.class.classLoader.getResource("books/$book").toURI()) assert source.exists() - new Generator(source, destination) + extensions == null ? new Generator(source, destination) : new Generator(source, destination, Charset.defaultCharset().name(), extensions) } } \ No newline at end of file diff --git a/src/test/groovy/markdown2book/SelectExtensionsSpec.groovy b/src/test/groovy/markdown2book/SelectExtensionsSpec.groovy new file mode 100644 index 0000000..a1ed9fd --- /dev/null +++ b/src/test/groovy/markdown2book/SelectExtensionsSpec.groovy @@ -0,0 +1,29 @@ +package markdown2book + +import org.pegdown.Extensions + +class SelectExtensionsSpec extends BaseSpec { + + final EXPECTED_LINES = [ + 'The intended purpose of the documentation is to give an understanding', + 'of the main features offered by TextMate and should provide you with', + 'those details which are not easily found alone by trial-and error. The', + 'documentation is not exhaustive.' + ] + + def "all extensions created by default"() { + when: 'do not specify extensions' + def dest = generate("original-example") + + then: 'get content with e.g. hard line breaks' + new File(dest, 'preface.html').text.contains(EXPECTED_LINES.join('
')) + } + + def "only extensions specified are used"() { + when: 'do not specify extensions' + def dest = generate("original-example", Extensions.NONE) + + then: 'get content without hard line breaks' + new File(dest, 'preface.html').text.contains(EXPECTED_LINES.join(' ')) + } +} diff --git a/src/test/resources/books/original-example/chapters/010-preface.markdown b/src/test/resources/books/original-example/chapters/010-preface.markdown index d43b486..6a8459c 100644 --- a/src/test/resources/books/original-example/chapters/010-preface.markdown +++ b/src/test/resources/books/original-example/chapters/010-preface.markdown @@ -2,7 +2,10 @@ ## About the Documentation -The intended purpose of the documentation is to give an understanding of the main features offered by TextMate and should provide you with those details which are not easily found alone by trial-and error. The documentation is not exhaustive. +The intended purpose of the documentation is to give an understanding +of the main features offered by TextMate and should provide you with +those details which are not easily found alone by trial-and error. The +documentation is not exhaustive. You are supposed to already know what a text editor is, in particular have some experience with Cocoa's text editor control (used e.g. in TextEdit, Mail, and Xcode). While TextMate does not use that control, it does for the most part mimic its behavior.