'use strict'
const path = require('path')
const fs = require('fs')
const utils = require('loader-utils')
const yaml = require('js-yaml')

const matter = require('gray-matter')

module.exports = function (source) {
    this.cacheable && this.cacheable(true)
    
    // get which config options to load
    let loader = utils.getOptions(this)

    // lets load the shared locals object
    let locals = loader.locals 

    const page = matter(source)
    
    if (!page.data.content) {
        page.data.content = page.content
    }
        
    // compile content through markdown
    // divide into individual page section key/values if needed
    if (page.data.content) {
        // trim based on Content Sections `%% key %%` (see README)
        const content = page.data.content.trim().split("%%");
        
        if (content[0].length == 0) {
            content.shift()
        }

        if (content.length == 1) {
            page.data.content = content[0]
        } 
        else {
            let i = 0
            do {
                // this is a variable assignment
                if (content[i].trim().indexOf(" ") == -1){
                    page.data[content[i].trim()] = locals.markdown( content[i + 1] )
                    i++
                    i++
                } 
                else {
                    page.data.content = content[i]
                    i++
                }                
            } while (i < content.length)
        }

        page.data.content = locals.markdown( page.data.content )
        page.content = ""
    }

    // set up the appropriate filename
    if (page.data.slug) {
        page.resourcePath = `${page.data.slug}`
    }
    else {
        page.resourcePath = `[path][name]`
    }

    // get the associated template
    if (!page.data.template) {
        page.data.template = loader.config.default_template
    }

    page.data.template_url = `${loader.config.template_dir}/${page.data.template}.pug`
    this.addDependency( path.resolve( page.data.template_url ) )
    page.data.template_file = fs.readFileSync(page.data.template_url, 'utf8')
    return page
}

@@ -1782,29 +1782,46 @@
 emplate%0A    
-if
+page.data.template =
  (
-!
 page.data.te
@@ -1827,26 +1827,24 @@
 emplate)
- %7B
 %0A       
  page.da
@@ -1827,32 +1827,34 @@
 emplate)%0A       
+ ?
  page.data.templ
@@ -1856,18 +1856,26 @@
 template
- =
+%0A        :
  loader.
@@ -1898,21 +1898,144 @@
 emplate%0A
+%0A
     
-%7D
+page.data.template = (page.data.postprocess.template)%0A        ? page.data.postprocess.template%0A        : page.data.template
 %0A%0A    pa

add postprocessing template override