Skip to content

jnorthr/Doctors

Repository files navigation

Doctors

Status

Travis C/I Project Health - Build Status (Travis CI)


Notes About Doctor

A Groovy Template Engine to produce html stream from text files written in the Asciidoctor syntax.


Using Groovlets - a nice explanation HOW-TO

Web.xml
<!-- setup the GroovyServlet in your web.xml
          and bind the script file extension -->

  <servlet>
      <servlet-name>groovlets</servlet-name>
      <servlet-class>groovy.servlet.GroovyServlet</servlet-class>
  </servlet>
  <servlet-mapping>
      <servlet-name>groovlets</servlet-name>
      <url-pattern>*.groovy</url-pattern>
  </servlet-mapping>
Build.gradle script
 apply plugin: 'groovy' // java is ok too
  apply plugin: 'war'

  repositories {
    mavenCentral()
    mavenLocal()
  }

  dependencies {
    providedCompile 'javax.servlet:servlet-api:2.5'
    compile 'org.codehaus.groovy:groovy-all:2.4.5'
  }

Handle a GET

Output HTML on GET request is pretty easy using the built-in MarkupBuilder

Template script
// if present use name parameter from
// query string (eg. ?name=Francesco)
// else use "World"

def name = params.name ?: "World"

// output some html

html.html {
    head {
        title "Hello from Groovlet!"
    }
    body {
        h1 "Hello, ${name}!"
    }
}

In the example above, you see also Groovy native template engine: (G)String concatenation.

Handle a POST

With built-in variables bound from the Servlet request via the GrailsServlet, you can also perform more complicated operations like handling some JSON in POST request.

Groovy Request Handler script
response.contentType = 'application/json'

// Access headers, with context bound variable
if (headers['Content-Type'] != "application/json") {
    throw new RuntimeException("Please use 'application/json' header")
}

// Get content from POST body
def jsonContent = request.reader.text

// Parse JSON to Map
def content = null
if (jsonContent) {
    content = new JsonSlurper()
                .parseText(jsonContent)
}

// build JSON output
json."echo" {
    "original" content
}

Groovy Template Servlet

It delegates work to a groovy.text.TemplateEngine implementation processing HTTP requests.

Usage

helloworld.html is a headless HTML-like template

Groovy Template script
  <html>
    <body>
      <% 3.times { %>
        Hello World!
      <% } %>
      <br>
    </body>
  </html>

Minimal web.xml example serving HTML-like templates

Web.xml
 <web-app>
   <servlet>
     <servlet-name>template</servlet-name>
     <servlet-class>groovy.servlet.TemplateServlet</servlet-class>
   </servlet>
   <servlet-mapping>
     <servlet-name>template</servlet-name>
     <url-pattern>*.html</url-pattern>
   </servlet-mapping>
 </web-app>

Template Engine Configuration

By default, the TemplateServlet uses the SimpleTemplateEngine which interprets JSP-like templates. The init parameter template.engine defines the fully qualified class name of the template to use:

  1. template.engine = [empty] - equals groovy.text.SimpleTemplateEngine

  2. template.engine = groovy.text.SimpleTemplateEngine

  3. template.engine = groovy.text.GStringTemplateEngine

  4. template.engine = groovy.text.XmlTemplateEngine

  5. template.engine = groovy.text.DoctorTemplateEngine

Additional Web.xml configuration when serving Asciidcotor templates

web.xml
        <servlet>
		<servlet-name>DoctorTemplateServlet</servlet-name>
		<servlet-class>groovyx.caelyf.CaelyfTemplateServlet</servlet-class>
 		<init-param>
			<param-name>template.engine</param-name>
			<param-value>groovy.text.DoctorTemplateEngine</param-value>
		</init-param>
        <init-param>
            <!-- Remove the default "generated by" messages from the templates -->
            <param-name>generated.by</param-name>
            <param-value>true</param-value>
        </init-param>
        <init-param>
            <param-name>verbose</param-name>
            <!-- Output generation time in the HTML, see source page -->
            <param-value>true</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
	</servlet>

    <servlet-mapping>
        <servlet-name>DoctorTemplateServlet</servlet-name>
        <url-pattern>*.adoc</url-pattern>
    </servlet-mapping>
Tip
Set <load-on-startup> parameter to zero to improve initial performance or if asciidoctor files are not used immediately

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published