Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 218 lines (182 sloc) 7.21 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217
import org.radeox.engine.BaseRenderEngine;
import org.radeox.api.engine.*;
import org.radeox.engine.context.BaseInitialRenderContext
import org.grails.doc.DocEngine;

def ant = new AntBuilder()

GRAILS_HOME = "../grails"
CONTEXT_PATH = DocEngine.CONTEXT_PATH
SOURCE_FILE = DocEngine.SOURCE_FILE

props = new Properties()
new File("./resources/doc.properties").withInputStream {input ->
    props.load(input)
}
new File("${GRAILS_HOME}/build.properties").withInputStream {input ->
    props.load(input)
}

title = props.title
version = props."grails.version"
authors = props.author

def compare = [compare: {o1, o2 ->
    def idx1 = o1.name[0..o1.name.indexOf(' ') - 1]
    def idx2 = o2.name[0..o2.name.indexOf(' ') - 1]
    def nums1 = idx1.split(/\./).findAll { it.trim() != ''}*.toInteger()
    def nums2 = idx2.split(/\./).findAll { it.trim() != ''}*.toInteger()
    // pad out with zeros to ensure accurate comparison
    while (nums1.size() < nums2.size()) {
        nums1 << 0
    }
    while (nums2.size() < nums1.size()) {
        nums2 << 0
    }
    def result = 0
    for (i in 0..<nums1.size()) {
        result = nums1[i].compareTo(nums2[i])
        if (result != 0) break
    }
    result
},
        equals: { false }] as Comparator

files = new File("./src/guide").listFiles().findAll { it.name.endsWith(".gdoc") }.sort(compare)
context = new BaseInitialRenderContext();
context.set(CONTEXT_PATH, "..")

ant = new AntBuilder()
cache = [:]

engine = new DocEngine(context)
templateEngine = new groovy.text.SimpleTemplateEngine()
context.setRenderEngine(engine)

book = [:]
for (f in files) {
    def chapter = f.name[0..-6]
    book[chapter] = f
}

toc = new StringBuffer()
soloToc = new StringBuffer()
fullContents = new StringBuffer()
chapterContents = new StringBuffer()
chapterTitle = null

void writeChapter(String title, StringBuffer content) {
    new File("output/guide/${title}.html").withWriter {
        template.make(title: title, content: content.toString()).writeTo(it)
    }
    content.delete(0, content.size()) // clear buffer
}

ant.mkdir(dir: "output/guide")
ant.mkdir(dir: "output/guide/pages")
new File("resources/style/guideItem.html").withReader {reader ->
    template = templateEngine.createTemplate(reader)

    for (entry in book) {
        //println "Generating documentation for $entry.key"
        def title = entry.key
        def level = 0
        def matcher = (title =~ /^(\S+?)\.? /) // drops last '.' of "xx.yy. "
        if (matcher.find()) {
            level = matcher.group(1).split(/\./).size() - 1
        }
        def margin = level * 10

        if (level == 0) {
            if (chapterTitle) // initially null, to collect sections
                writeChapter(chapterTitle, chapterContents)

            chapterTitle = title // after previous used to write prev chapter

            soloToc << "<div class=\"tocItem\" style=\"margin-left:${margin}px\"><a href=\"${chapterTitle}.html\">${chapterTitle}</a></div>"
        }
        else {
            soloToc << "<div class=\"tocItem\" style=\"margin-left:${margin}px\"><a href=\"${chapterTitle}.html#${entry.key}\">${entry.key}</a></div>"
        } // level 0=h1, (1..n)=h2


        def hLevel = level == 0 ? 1 : 2
        def header = "<h$hLevel><a name=\"${title}\">${title}</a></h$hLevel>"

        context.set(SOURCE_FILE, entry.value)
        context.set(CONTEXT_PATH, "..")
        def body = engine.render(entry.value.text, context)

        toc << "<div class=\"tocItem\" style=\"margin-left:${margin}px\"><a href=\"#${title}\">${title}</a></div>"
        fullContents << header << body
        chapterContents << header << body

        new File("output/guide/pages/${title}.html").withWriter {
            template.make(title: title, content: body).writeTo(it)
        }
    }
}
if (chapterTitle) // write final chapter collected (if any seen)
    writeChapter(chapterTitle, chapterContents)


ant.mkdir(dir: "output")
ant.mkdir(dir: "output/img")
ant.mkdir(dir: "output/css")
ant.mkdir(dir: "output/ref")

ant.copy(file: "resources/style/index.html", todir: "output")
ant.copy(todir: "output/img") {
    fileset(dir: "resources/img")
}
ant.copy(todir: "output/css") {
    fileset(dir: "resources/css")
}
ant.copy(todir: "output/ref") {
    fileset(dir: "resources/style/ref")
}

vars = [
        title: props.title,
        subtitle: props.subtitle,
        footer: props.footer,
        authors: props.authors,
        version: props."grails.version",
        copyright: props.copyright,

        toc: toc.toString(),
        body: fullContents.toString()
]

new File("./resources/style/layout.html").withReader {reader ->
    template = templateEngine.createTemplate(reader)
    new File("output/guide/single.html").withWriter {out ->
        template.make(vars).writeTo(out)
    }
    vars.toc = soloToc
    vars.body = ""
    new File("output/guide/index.html").withWriter {out ->
        template.make(vars).writeTo(out)
    }
}

menu = new StringBuffer()
files = new File("src/ref").listFiles().toList().sort()
reference = [:]
new File("resources/style/referenceItem.html").withReader {reader ->
    template = templateEngine.createTemplate(reader)
    for (f in files) {
        if (f.directory && !f.name.startsWith(".")) {
            def section = f.name
            reference."${section}" = [:]
            menu << "<h1 class=\"menuTitle\">${f.name}</h1>"
            new File("output/ref/${f.name}").mkdirs()
            def textiles = f.listFiles().findAll { it.name.endsWith(".gdoc")}.sort()
            def usageFile = new File("src/ref/${f.name}.gdoc")
            if (usageFile.exists()) {
                def data = usageFile.text
                reference."${section}".usage = data
                context.set(SOURCE_FILE, usageFile.name)
                context.set(CONTEXT_PATH, "../..")
                def contents = engine.render(data, context)
                new File("output/ref/${f.name}/Usage.html").withWriter {out ->
                    template.make(content: contents).writeTo(out)
                }
                menu << "<div class=\"menuUsageItem\"><a href=\"${f.name}/Usage.html\" target=\"mainFrame\">Usage</a></div>"
            }
            for (txt in textiles) {
                def name = txt.name[0..-6]
                menu << "<div class=\"menuItem\"><a href=\"${f.name}/${name}.html\" target=\"mainFrame\">${name}</a></div>"
                def data = txt.text
                reference."${section}"."$name" = data
                context.set(SOURCE_FILE, txt.name)
                context.set(CONTEXT_PATH, "../..")
                def contents = engine.render(data, context)
                //println "Generating reference item: ${name}"
                new File("output/ref/${f.name}/${name}.html").withWriter {out ->
                    template.make(content: contents).writeTo(out)
                }
            }
        }
    }

}
vars.menu = menu
new File("./resources/style/menu.html").withReader {reader ->
    template = templateEngine.createTemplate(reader)
    new File("output/ref/menu.html").withWriter {out ->
        template.make(vars).writeTo(out)
    }
}




println "Done. Look at output/index.html"


Something went wrong with that request. Please try again.