kordamp / markdown-gradle-plugin

Markdown/HTML plugin for gradle
Apache License 2.0
71 stars 51 forks source link

How to use to generate HTML for an Android app #20

Closed dalewking closed 4 years ago

dalewking commented 8 years ago

Since I just spent a couple hours figuring out how to use your plugin to generate HTML to be used in an Android app, I thought I would pass it along so that you could document it in your readme.

The scheme I am detailing here will place the output as resource directories in the application. Some people will think that the generated HTML should go in the assets directory, but that is not a good practice because the assets directory cannot be translated. The proper place to store them on Android is as raw resources so you can provide different languages and possibly even different versions for different screen sizes.

To add markdown you create a markdown folder in the src directory of the source set. For example, src/main/markdown. Because this is for android resources you will want to put your markdown in a raw forlder inside that directory (src/main/markdown/raw)

Then you will need to add these lines to the build.gradle for the module:

import com.android.builder.model.SourceProvider
import org.kordamp.gradle.markdown.MarkdownToHtmlTask

android {
    sourceSets.all { sourceSet ->
        def src = new File("$projectDir/src/${sourceSet.name}/markdown")

        if(src.exists())
        {
            def task = task("${sourceSet.name}MarkdownToHtml", type:MarkdownToHtmlTask)

            task.sourceDir = src
            task.outputDir = new File("$buildDir/generated/html/${sourceSet.name}")
        }
    }

    applicationVariants.all { variant ->
        for(SourceProvider sourceSet : variant.sourceSets)
        {
            def task = tasks.findByPath("${sourceSet.name}MarkdownToHtml")

            if(task != null)
            {
                variant.registerResGeneratingTask(task, task.outputDir)
            }
        }
    }
}
dalewking commented 8 years ago

Since this generates multiple instances of the markdownToHtml task it is a little more work to configure them. You will likely want to add something like this to do configuration:

tasks.withType(MarkdownToHtmlTask).all { task ->
    task.inputEncoding = "utf8"
    task.configuration =
            [
                    hardWraps: true,
                    autoLinks: true,
            ]
}