Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions src/main/groovy/markdown2book/Generator.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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")
}
Expand Down Expand Up @@ -258,7 +259,7 @@ class Generator {
}

private markdown(input) {
MARKDOWN_PROCESSOR.markdownToHtml(input)
markdownProcessor.markdownToHtml(input)
}

private getChaptersSourceDir() {
Expand Down
19 changes: 13 additions & 6 deletions src/test/groovy/markdown2book/BaseSpec.groovy
Original file line number Diff line number Diff line change
@@ -1,30 +1,37 @@
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()
}
assert destination.mkdirs()
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)
}

}
29 changes: 29 additions & 0 deletions src/test/groovy/markdown2book/SelectExtensionsSpec.groovy
Original file line number Diff line number Diff line change
@@ -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('<br/>'))
}

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(' '))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down