Created
October 21, 2012 15:28
-
-
Save danveloper/3927274 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import com.danveloper.markdown.compiler.ProjectMarkdownCompiler | |
eventCompileEnd = { | |
def markdownCompiler = new ProjectMarkdownCompiler(projectCompiler, grailsAppName) | |
markdownCompiler.compile() | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.danveloper.markdown.compiler | |
import grails.util.GrailsNameUtils | |
import org.codehaus.groovy.grails.compiler.GrailsProjectCompiler | |
/** | |
* Adapted from GrailsProjectCompiler | |
* | |
* @author dwoods | |
* @date 10/21/12 | |
*/ | |
class ProjectMarkdownCompiler { | |
AntBuilder ant | |
GrailsProjectCompiler grailsProjectCompiler | |
def basedir, projectWorkDir, grailsAppName, classesDir | |
static gspMap | |
ProjectMarkdownCompiler(final GrailsProjectCompiler grailsProjectCompiler, String grailsAppName) { | |
this.grailsProjectCompiler = grailsProjectCompiler | |
this.basedir = grailsProjectCompiler?.buildSettings?.baseDir?.absolutePath | |
this.projectWorkDir = grailsProjectCompiler?.buildSettings?.projectWorkDir | |
this.classesDir = grailsProjectCompiler?.buildSettings?.classesDir | |
this.grailsAppName = grailsAppName | |
this.ant = new AntBuilder() | |
this.ant.taskdef(name: "mdc", classname: "com.danveloper.ant.tasks.MarkdownPageCompilerTask") | |
this.ant.path(id: "grails.compile.classpath", grailsProjectCompiler.compileClasspath) | |
grailsProjectCompiler.configureClasspath() | |
} | |
void compile() { | |
// compile MD files in grails-app/views directory | |
File mdTmpDir = new File(projectWorkDir, "mdcompile") | |
ant.echo(message: "Compiling markdown files...") | |
ant.mdc(destdir:classesDir, | |
srcdir:"${basedir}/grails-app/views", | |
packagename:GrailsNameUtils.getPropertyNameForLowerCaseHyphenSeparatedName(grailsAppName), | |
serverpath:"/WEB-INF/grails-app/views/", | |
classpathref:"grails.compile.classpath", | |
tmpdir:mdTmpDir) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.danveloper.ant.tasks | |
import grails.build.logging.GrailsConsole | |
import org.apache.tools.ant.BuildException | |
import org.apache.tools.ant.DirectoryScanner | |
import org.apache.tools.ant.taskdefs.MatchingTask | |
import org.apache.tools.ant.types.Path | |
import org.apache.tools.ant.types.Reference | |
import org.codehaus.groovy.control.CompilerConfiguration | |
import org.codehaus.groovy.grails.web.pages.GroovyPageCompiler | |
import com.danveloper.markdown.compiler.ProjectMarkdownCompiler | |
/** | |
* Adapted from GroovyPageCompilerTask | |
* | |
* @author dwoods | |
* @date 10/21/12 | |
*/ | |
class MarkdownPageCompilerTask extends MatchingTask { | |
File destdir | |
Path classpath | |
File srcdir | |
File tmpdir | |
String packagename = 'default' | |
String serverpath | |
String encoding | |
boolean verbose | |
/** | |
* Adds a path to the classpath. | |
* | |
* @return a class path to be configured | |
*/ | |
Path createClasspath() { | |
if (classpath == null) { | |
classpath = new Path(getProject()) | |
} | |
return classpath.createPath() | |
} | |
/** | |
* Adds a reference to a classpath defined elsewhere. | |
* | |
* @param r a reference to a classpath | |
*/ | |
void setClasspathRef(Reference r) { | |
createClasspath().setRefid(r) | |
} | |
void execute() { | |
def compiler = new GroovyPageCompiler() | |
if (classpath) { | |
CompilerConfiguration config = new CompilerConfiguration() | |
config.classpath = classpath.toString() | |
compiler.compilerConfig = config | |
} | |
if (!destdir || !destdir.exists()) { | |
throw new BuildException("destination [${destdir}] directory doesn't exist or is not set!", getLocation()) | |
} | |
compiler.targetDir = destdir | |
if (!srcdir || !srcdir.exists()) { | |
throw new BuildException("source [${srcdir}] directory doesn't exist or is not set!", getLocation()) | |
} | |
compiler.viewsDir = srcdir | |
DirectoryScanner scanner = super.getDirectoryScanner(srcdir) | |
scanner.includes = ["**/*.md"] as String[] | |
scanner.scan() | |
List mdFiles = scanner.includedFiles.collect { new File("${srcdir}/$it") } | |
compiler.srcFiles = mdFiles | |
int mdCount = mdFiles?.size() | |
if (mdCount) { | |
GrailsConsole.getInstance().updateStatus("Compiling ${mdCount} MD file${mdCount>1?'s':''} for package [${packagename}]") | |
} | |
if (tmpdir) { | |
compiler.generatedGroovyPagesDirectory = tmpdir | |
} | |
if (packagename) { | |
compiler.packagePrefix = packagename | |
} | |
if (serverpath) { | |
compiler.viewPrefix=serverpath | |
} | |
if (encoding) { | |
compiler.encoding = encoding | |
} | |
ProjectMarkdownCompiler.gspMap = compiler.compile() | |
compiler = null | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment