Skip to content

Instantly share code, notes, and snippets.

@g-pechorin
Created August 16, 2014 12:40
Show Gist options
  • Save g-pechorin/63afe0a225d6883d2224 to your computer and use it in GitHub Desktop.
Save g-pechorin/63afe0a225d6883d2224 to your computer and use it in GitHub Desktop.
uses gradle to drive MarkDown
/*
This gradle file will produce a site in build/marksite from MarkDown files in src/site
markFolder - makes folders. is separate because or $reasons.
markSite - translates the .md files in src/site to HTML in build/site
markClean - deletes build/site
*/
String markDown = "src/site"
String markHTML = "$buildDir/site"
// this makes PegDown available during gradle execution
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'org.pegdown:pegdown:1.4.2'
}
}
import org.pegdown.PegDownProcessor // IDEA will say there's an error here - IDEA is mistaken
import groovy.io.FileType
task markFolder << {
mkdir(new File(markHTML))
}
task markSite(dependsOn: [markFolder]) << {
PegDownProcessor processor = new PegDownProcessor() // IDEA will say there's an error here - IDEA is mistaken
new File(markDown).eachFileRecurse(FileType.FILES) { sourceFile ->
// we only care about .md files
if (sourceFile.name.matches("\\w+\\.md")) {
// get the source relative filename, with forward slashes only
def sourceName = sourceFile.absolutePath.substring(new File(markDown).absolutePath.length() + 1).replace('\\', '/')
// compute the output name and filepath
String outputName = sourceName.replaceAll('\\.md$', '.html')
def outputFile = new File(markHTML, outputName)
// perform the actual conversion
outputFile.text = processor.markdownToHtml(sourceFile.text)
// print a status message
logger.info("Wrote `$outputName`")
}
}
}
markSite.dependsOn markFolder
task markClean << {
delete(new File(markHTML))
}
markClean
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment