diff --git a/bin/panini.js b/bin/panini.js index 932a894..e2c707a 100755 --- a/bin/panini.js +++ b/bin/panini.js @@ -12,6 +12,7 @@ var options = { "root": String, "layouts": String, "partials": String, + "inlinelayout": String, "data": String, "helpers": String, "output": String, @@ -23,6 +24,7 @@ var shorthands = { "r": "--root", "l": "--layouts", "p": "--partials", + "i": "--inlinelayout", "d": "--data", "h": "--helpers", "o": "--output", diff --git a/lib/helpMessage.js b/lib/helpMessage.js index e00285b..8816573 100644 --- a/lib/helpMessage.js +++ b/lib/helpMessage.js @@ -7,6 +7,7 @@ module.exports = function() { ' --root (required) path to the root folder all pages live in\n' + ' --output (required) path to the folder compiled pages should get sent to\n' + ' --partials path to root folder for partials \n' + + ' --inlinelayout prefix of inline partial to look for \n' + ' --helpers path to folder for additional helpers \n' + ' --data path to folder for additional data \n' + '\n' + diff --git a/lib/render.js b/lib/render.js index e5fb2cb..661c06b 100644 --- a/lib/render.js +++ b/lib/render.js @@ -38,6 +38,36 @@ function render(file, enc, cb) { } } + // Determine which inline partial layout prefix to use + var inlineLayout = typeof page.attributes.inlinelayout !== "undefined" ? page.attributes.inlinelayout : typeof this.options.inlinelayout !== "undefined" ? this.options.inlinelayout : 'layout-'; + //remove Handlebars.partials starting with inlineLayout prefix + var inlineLayoutPartialList = Object.keys(this.Handlebars.partials).filter( + function (partial) { + return (partial.indexOf(inlineLayout) === 0); + } + ); + inlineLayoutPartialList.forEach(partial => delete this.Handlebars.partials[partial]); + //add to Handlebars.partials any inline partials starting with "layout-" + if (page.body.indexOf('{{#*inline') !== -1 || page.body.indexOf('{{~#*inline') !== -1) { + // var inlinePartialAndCommentRegex = /{{!--(?[.\s\S]*?)--}}|(?{{#\*inline[\s]+\"(?layout-[0-9a-zA-Z_-]*)\"}}(?[.\s\S]*?){{\/inline}}[\s]*)/gm; + var inlinePartialAndCommentRegex = new RegExp(String.raw`{{!--(?[.\s\S]*?)--}}|(?{{#\*inline[\s]+\"(?${inlineLayout}[0-9a-zA-Z_-]*)\"}}(?[.\s\S]*?){{\/inline}}[\s]*)`, "gm"); + var replaceWith = ''; + var bodyText = page.body; + var inlinePartialMatch; + while (inlinePartialMatch = inlinePartialAndCommentRegex.exec(page.body)) { + // Add inline partials from page.body starting with "layout-" + if (typeof inlinePartialMatch.groups.partial !== 'undefined') {//hasOwnProperty not working? + this.Handlebars.registerPartial( + inlinePartialMatch.groups.name, + this.Handlebars.compile(inlinePartialMatch.groups.body + '\n') + ); + bodyText = bodyText.replace(inlinePartialMatch.groups.partial, replaceWith); + } + } + page.body = bodyText;//amended body - removed inline partials starting with "layout-" + //note: would not harm to use body with removed handlebars comments + } + // Now create Handlebars templates out of them var pageTemplate = this.Handlebars.compile(page.body + '\n'); diff --git a/readme.md b/readme.md index 83dd9b8..b173183 100644 --- a/readme.md +++ b/readme.md @@ -97,6 +97,59 @@ Path to a folder containing HTML partials. Partial files can have the extension {{> header}} ``` +### `inlinelayout` + +**Type:** `String` + +Inline partial name prefix, if not set - the default prefix will be `layout-`. Page Inline Partials to be used within layout pages. + +```html +{{#*inline "layout-inline-partial-bot"}} + +{{/inline}} +{{#*inline "layout-inline-partial-top"}} + +{{/inline}} + +``` + +The page inline partials with `inlinelayout` prefix can be used within the layouts. If there is not a corresponding inline partial on the page, then the default content will be displayed. Note: the default content can be made empty. + +```html +{{#> layout-inline-partial-top}} + +{{/layout-inline-partial-top}} +{{> body}} +{{#> layout-inline-partial-bot}} + +{{/layout-inline-partial-bot}} +``` + +To use an `inlinelayout` other than the default `layout-` prefix or `panini.options.inlinelayout` prefix on a specific page, override it in the Front Matter on that page. + +```html +--- +inlinelayout: alt-inlinelayout- +--- +{{#*inline "alt-inlinelayout-inline-partial-bot"}} + +{{/inline}} +{{#*inline "alt-inlinelayout-inline-partial-top"}} + +{{/inline}} + +``` +layout +```html +{{#> alt-inlinelayout-inline-partial-top}} + +{{/alt-inlinelayout-inline-partial-top}} +{{> body}} +{{#> alt-inlinelayout-inline-partial-bot}} + +{{/alt-inlinelayout-inline-partial-bot}} +``` + ### `helpers` **Type:** `String` diff --git a/test/fixtures/partials-inline/build/comment-multiple-inline-partial-top-bot.html b/test/fixtures/partials-inline/build/comment-multiple-inline-partial-top-bot.html new file mode 100644 index 0000000..f6170c8 --- /dev/null +++ b/test/fixtures/partials-inline/build/comment-multiple-inline-partial-top-bot.html @@ -0,0 +1,11 @@ + + + +

layout-inline-partial-top content

+ +

Page Body

+ +

layout-inline-partial-bot content

+ + + diff --git a/test/fixtures/partials-inline/build/index.html b/test/fixtures/partials-inline/build/index.html new file mode 100644 index 0000000..f6170c8 --- /dev/null +++ b/test/fixtures/partials-inline/build/index.html @@ -0,0 +1,11 @@ + + + +

layout-inline-partial-top content

+ +

Page Body

+ +

layout-inline-partial-bot content

+ + + diff --git a/test/fixtures/partials-inline/build/inline-partial-bot-comment-top.html b/test/fixtures/partials-inline/build/inline-partial-bot-comment-top.html new file mode 100644 index 0000000..756b137 --- /dev/null +++ b/test/fixtures/partials-inline/build/inline-partial-bot-comment-top.html @@ -0,0 +1,9 @@ + + +

No layout-inline-partial-top default content

+

Page Body

+ +

layout-inline-partial-bot content - commented

+ + + diff --git a/test/fixtures/partials-inline/build/inline-partial-bot.html b/test/fixtures/partials-inline/build/inline-partial-bot.html new file mode 100644 index 0000000..7739539 --- /dev/null +++ b/test/fixtures/partials-inline/build/inline-partial-bot.html @@ -0,0 +1,9 @@ + + +

No layout-inline-partial-top default content

+

Page Body

+ +

layout-inline-partial-bot content

+ + + diff --git a/test/fixtures/partials-inline/build/inline-partial-top-bot-comment-top-bot.html b/test/fixtures/partials-inline/build/inline-partial-top-bot-comment-top-bot.html new file mode 100644 index 0000000..784499c --- /dev/null +++ b/test/fixtures/partials-inline/build/inline-partial-top-bot-comment-top-bot.html @@ -0,0 +1,7 @@ + + +

No layout-inline-partial-top default content

+

Page Body

+

No layout-inline-partial-bot default content

+ + diff --git a/test/fixtures/partials-inline/build/inline-partial-top-comment-bot.html b/test/fixtures/partials-inline/build/inline-partial-top-comment-bot.html new file mode 100644 index 0000000..9e3e041 --- /dev/null +++ b/test/fixtures/partials-inline/build/inline-partial-top-comment-bot.html @@ -0,0 +1,9 @@ + + + +

layout-inline-partial-top content

+ +

Page Body

+

No layout-inline-partial-bot default content

+ + diff --git a/test/fixtures/partials-inline/build/inline-partial-top.html b/test/fixtures/partials-inline/build/inline-partial-top.html new file mode 100644 index 0000000..9e3e041 --- /dev/null +++ b/test/fixtures/partials-inline/build/inline-partial-top.html @@ -0,0 +1,9 @@ + + + +

layout-inline-partial-top content

+ +

Page Body

+

No layout-inline-partial-bot default content

+ + diff --git a/test/fixtures/partials-inline/build/no-inline-partial.html b/test/fixtures/partials-inline/build/no-inline-partial.html new file mode 100644 index 0000000..784499c --- /dev/null +++ b/test/fixtures/partials-inline/build/no-inline-partial.html @@ -0,0 +1,7 @@ + + +

No layout-inline-partial-top default content

+

Page Body

+

No layout-inline-partial-bot default content

+ + diff --git a/test/fixtures/partials-inline/expected/comment-multiple-inline-partial-top-bot.html b/test/fixtures/partials-inline/expected/comment-multiple-inline-partial-top-bot.html new file mode 100644 index 0000000..f6170c8 --- /dev/null +++ b/test/fixtures/partials-inline/expected/comment-multiple-inline-partial-top-bot.html @@ -0,0 +1,11 @@ + + + +

layout-inline-partial-top content

+ +

Page Body

+ +

layout-inline-partial-bot content

+ + + diff --git a/test/fixtures/partials-inline/expected/index.html b/test/fixtures/partials-inline/expected/index.html new file mode 100644 index 0000000..f6170c8 --- /dev/null +++ b/test/fixtures/partials-inline/expected/index.html @@ -0,0 +1,11 @@ + + + +

layout-inline-partial-top content

+ +

Page Body

+ +

layout-inline-partial-bot content

+ + + diff --git a/test/fixtures/partials-inline/expected/inline-partial-bot-comment-top.html b/test/fixtures/partials-inline/expected/inline-partial-bot-comment-top.html new file mode 100644 index 0000000..756b137 --- /dev/null +++ b/test/fixtures/partials-inline/expected/inline-partial-bot-comment-top.html @@ -0,0 +1,9 @@ + + +

No layout-inline-partial-top default content

+

Page Body

+ +

layout-inline-partial-bot content - commented

+ + + diff --git a/test/fixtures/partials-inline/expected/inline-partial-bot.html b/test/fixtures/partials-inline/expected/inline-partial-bot.html new file mode 100644 index 0000000..7739539 --- /dev/null +++ b/test/fixtures/partials-inline/expected/inline-partial-bot.html @@ -0,0 +1,9 @@ + + +

No layout-inline-partial-top default content

+

Page Body

+ +

layout-inline-partial-bot content

+ + + diff --git a/test/fixtures/partials-inline/expected/inline-partial-top-bot-comment-top-bot.html b/test/fixtures/partials-inline/expected/inline-partial-top-bot-comment-top-bot.html new file mode 100644 index 0000000..784499c --- /dev/null +++ b/test/fixtures/partials-inline/expected/inline-partial-top-bot-comment-top-bot.html @@ -0,0 +1,7 @@ + + +

No layout-inline-partial-top default content

+

Page Body

+

No layout-inline-partial-bot default content

+ + diff --git a/test/fixtures/partials-inline/expected/inline-partial-top-comment-bot.html b/test/fixtures/partials-inline/expected/inline-partial-top-comment-bot.html new file mode 100644 index 0000000..9e3e041 --- /dev/null +++ b/test/fixtures/partials-inline/expected/inline-partial-top-comment-bot.html @@ -0,0 +1,9 @@ + + + +

layout-inline-partial-top content

+ +

Page Body

+

No layout-inline-partial-bot default content

+ + diff --git a/test/fixtures/partials-inline/expected/inline-partial-top.html b/test/fixtures/partials-inline/expected/inline-partial-top.html new file mode 100644 index 0000000..9e3e041 --- /dev/null +++ b/test/fixtures/partials-inline/expected/inline-partial-top.html @@ -0,0 +1,9 @@ + + + +

layout-inline-partial-top content

+ +

Page Body

+

No layout-inline-partial-bot default content

+ + diff --git a/test/fixtures/partials-inline/expected/no-inline-partial.html b/test/fixtures/partials-inline/expected/no-inline-partial.html new file mode 100644 index 0000000..784499c --- /dev/null +++ b/test/fixtures/partials-inline/expected/no-inline-partial.html @@ -0,0 +1,7 @@ + + +

No layout-inline-partial-top default content

+

Page Body

+

No layout-inline-partial-bot default content

+ + diff --git a/test/fixtures/partials-inline/layouts/default.html b/test/fixtures/partials-inline/layouts/default.html new file mode 100644 index 0000000..033de70 --- /dev/null +++ b/test/fixtures/partials-inline/layouts/default.html @@ -0,0 +1,11 @@ + + + {{#> layout-inline-partial-top}} +

No layout-inline-partial-top default content

+ {{/layout-inline-partial-top}} + {{> body}} + {{#> layout-inline-partial-bot}} +

No layout-inline-partial-bot default content

+ {{/layout-inline-partial-bot}} + + diff --git a/test/fixtures/partials-inline/pages/comment-multiple-inline-partial-top-bot.html b/test/fixtures/partials-inline/pages/comment-multiple-inline-partial-top-bot.html new file mode 100644 index 0000000..9672564 --- /dev/null +++ b/test/fixtures/partials-inline/pages/comment-multiple-inline-partial-top-bot.html @@ -0,0 +1,23 @@ +{{!-- +{{#*inline "layout-inline-partial-bot"}} +

layout-inline-partial-bot content in comment

+{{/inline}} +{{#*inline "layout-inline-partial-top"}} +

layout-inline-partial-top content in comment

+{{/inline}} +--}} +{{#*inline "layout-inline-partial-bot"}} +

layout-inline-partial-bot content

+{{/inline}} +{{#*inline "layout-inline-partial-top"}} +

layout-inline-partial-top content

+{{/inline}} +

Page Body

+{{!-- +{{#*inline "layout-inline-partial-bot"}} +

layout-inline-partial-bot content in comment bottom page

+{{/inline}} +{{#*inline "layout-inline-partial-top"}} +

layout-inline-partial-top content in comment bottom page

+{{/inline}} +--}} \ No newline at end of file diff --git a/test/fixtures/partials-inline/pages/index.html b/test/fixtures/partials-inline/pages/index.html new file mode 100644 index 0000000..e9b6978 --- /dev/null +++ b/test/fixtures/partials-inline/pages/index.html @@ -0,0 +1,7 @@ +{{#*inline "layout-inline-partial-bot"}} +

layout-inline-partial-bot content

+{{/inline}} +{{#*inline "layout-inline-partial-top"}} +

layout-inline-partial-top content

+{{/inline}} +

Page Body

\ No newline at end of file diff --git a/test/fixtures/partials-inline/pages/inline-partial-bot-comment-top.html b/test/fixtures/partials-inline/pages/inline-partial-bot-comment-top.html new file mode 100644 index 0000000..dff4046 --- /dev/null +++ b/test/fixtures/partials-inline/pages/inline-partial-bot-comment-top.html @@ -0,0 +1,9 @@ +{{!-- +{{#*inline "layout-inline-partial-top"}} +

layout-inline-partial-top content

+{{/inline}} +--}} +{{#*inline "layout-inline-partial-bot"}} +

layout-inline-partial-bot content - commented

+{{/inline}} +

Page Body

\ No newline at end of file diff --git a/test/fixtures/partials-inline/pages/inline-partial-bot.html b/test/fixtures/partials-inline/pages/inline-partial-bot.html new file mode 100644 index 0000000..8fbc36a --- /dev/null +++ b/test/fixtures/partials-inline/pages/inline-partial-bot.html @@ -0,0 +1,4 @@ +{{#*inline "layout-inline-partial-bot"}} +

layout-inline-partial-bot content

+{{/inline}} +

Page Body

\ No newline at end of file diff --git a/test/fixtures/partials-inline/pages/inline-partial-top-bot-comment-top-bot.html b/test/fixtures/partials-inline/pages/inline-partial-top-bot-comment-top-bot.html new file mode 100644 index 0000000..46ae289 --- /dev/null +++ b/test/fixtures/partials-inline/pages/inline-partial-top-bot-comment-top-bot.html @@ -0,0 +1,9 @@ +{{!-- +{{#*inline "layout-inline-partial-bot"}} +

layout-inline-partial-bot content in comment

+{{/inline}} +{{#*inline "layout-inline-partial-top"}} +

layout-inline-partial-top content in comment

+{{/inline}} +--}} +

Page Body

\ No newline at end of file diff --git a/test/fixtures/partials-inline/pages/inline-partial-top-comment-bot.html b/test/fixtures/partials-inline/pages/inline-partial-top-comment-bot.html new file mode 100644 index 0000000..eca62c8 --- /dev/null +++ b/test/fixtures/partials-inline/pages/inline-partial-top-comment-bot.html @@ -0,0 +1,9 @@ +{{#*inline "layout-inline-partial-top"}} +

layout-inline-partial-top content

+{{/inline}} +{{!-- +{{#*inline "layout-inline-partial-bot"}} +

layout-inline-partial-bot content - commented

+{{/inline}} +--}} +

Page Body

\ No newline at end of file diff --git a/test/fixtures/partials-inline/pages/inline-partial-top.html b/test/fixtures/partials-inline/pages/inline-partial-top.html new file mode 100644 index 0000000..f428507 --- /dev/null +++ b/test/fixtures/partials-inline/pages/inline-partial-top.html @@ -0,0 +1,4 @@ +{{#*inline "layout-inline-partial-top"}} +

layout-inline-partial-top content

+{{/inline}} +

Page Body

\ No newline at end of file diff --git a/test/fixtures/partials-inline/pages/no-inline-partial.html b/test/fixtures/partials-inline/pages/no-inline-partial.html new file mode 100644 index 0000000..87b7e72 --- /dev/null +++ b/test/fixtures/partials-inline/pages/no-inline-partial.html @@ -0,0 +1 @@ +

Page Body

\ No newline at end of file diff --git a/test/test.js b/test/test.js index 8506373..a375ecd 100644 --- a/test/test.js +++ b/test/test.js @@ -78,6 +78,24 @@ describe('Panini', () => { }); }); + it('builds a page with inline partials used within layout', done => { + var p = new Panini({ + root: FIXTURES + 'partials-inline/pages/', + layouts: FIXTURES + 'partials-inline/layouts/', + inlinelayout: 'layout-' + }); + + p.refresh(); + + src(FIXTURES + 'partials-inline/pages/*') + .pipe(p.render()) + .pipe(dest(FIXTURES + 'partials-inline/build')) + .on('finish', () => { + equal(FIXTURES + 'partials-inline/expected', FIXTURES + 'partials-inline/build'); + done(); + }); + }); + it('builds a page with custom data', done => { var p = new Panini({ root: FIXTURES + 'data-page/pages/',