diff --git a/lib/patternlibrary.js b/lib/patternlibrary.js index 317155efae9ffa9ee77809988ee9932f5447438b..36ffccfa0ae5be8d97aae0315dc0061cebcb2845 100644 --- a/lib/patternlibrary.js +++ b/lib/patternlibrary.js @@ -97,7 +97,8 @@ class Patternlibrary { * Retrieves (handlebars) page template * * if no page template is set yet, returns the default - * gui doc-page template + * gui doc-page templateto be used in a series of similar + * pages with iterating data * * it retrieves a compiled Handlebars template and * returns the rendred page content when invoked @@ -118,6 +119,8 @@ class Patternlibrary { /** * Sets and compiles (handlebars) page by page filename + * for a *Patternlibrary* page template to be used in a + * series of similar pages with iterating data * * if `page` is a '.md' or '.markdown' file, it is also * passed through the markdown renderer @@ -128,6 +131,14 @@ class Patternlibrary { * var rendered = P.pagetemplate(pagevars); * ``` * + * if the parameter does not correspond to a readable file + * the parameter's string value is assigned as page template + * + * look-up order: + * - project's doc pages dir (pages src dir + basepath) + * - project's `gui` pages dir + * - module's `gui` pages dir + * - file path * * @param {string} page - the file basename of the page-file * @var {function} pagetemplate - a precompiled (handlebars) template @@ -142,47 +153,36 @@ class Patternlibrary { var pagefile = path.join(this.options.root, this.options.basepath, page); var guipage = resolvePath(path.join(this.options.gui.pages, this.options.basepath, page)); - try { - if ( !fs.existsSync(pagefile) ) { - if ( fs.existsSync(guipage) ) { - // file from gui pages - pageSource = fs.readFileSync(guipage).toString(); - } else { - if ( fs.existsSync(pageSource) ) { - // file from `pageSource` as a file-path by it self - pageSource = fs.readFileSync(pageSource).toString(); - } - } + if ( !fs.existsSync(pagefile) ) { + if ( fs.existsSync(guipage) ) { + // file from gui pages + pageSource = fs.readFileSync(guipage).toString(); } else { - // file from pages - pageSource = fs.readFileSync(pagefile).toString(); + if ( fs.existsSync(pageSource) ) { + // file from `pageSource` as a file-path by it self + pageSource = fs.readFileSync(pageSource).toString(); + } } - } catch (e) { - this.log.warn('Error loading Patternlibrary pagefile "'+pagefile+'"'); - throw new Error(e.message); - } + } else { + // file from pages + pageSource = fs.readFileSync(pagefile).toString(); + } } // strip yml data in the beginning pageSource = fm(pageSource).body; - - try { - - // finally compile Markdown content, if we have a markdown file here... - if (markdown) { - - this.log.info('Rendering Markdown content...'); - // interestingly, just rendering with MarkdownIt seems to do too much escaping - // and renders some special characters useless for to be interpreted by - // Handlebars, like `{{> ...`, so we wrap it with Handlebars and let its - // MarkdownIt helper there do it... - pageSource = '{{#md}}'+pageSource+'{{/md}}'; - - } - } catch (e) { - this.log.warn('Error rendering Markdown content'); - throw new Error(e.message); - } + // finally compile Markdown content, if we have a markdown file here... + if (markdown) { + + this.log.info('Rendering Markdown content...'); + // interestingly, just rendering with MarkdownIt seems to do too much escaping + // and renders some special characters useless for to be interpreted by + // Handlebars, like `{{> ...`, so we wrap it with Handlebars and let its + // MarkdownIt helper there do it... + pageSource = '{{#markdown}}'+pageSource+'{{/markdown}}'; + + } + this._pageTemplate = this.handlebars.compile(pageSource, {noEscape: true}); } diff --git a/test/fixtures.core/basic/layouts/default.html b/test/fixtures.templating/layout/layouts/default.html similarity index 100% rename from test/fixtures.core/basic/layouts/default.html rename to test/fixtures.templating/layout/layouts/default.html diff --git a/test/fixtures.core/basic/layouts/mylayout.html b/test/fixtures.templating/layout/layouts/mylayout.html similarity index 100% rename from test/fixtures.core/basic/layouts/mylayout.html rename to test/fixtures.templating/layout/layouts/mylayout.html diff --git a/test/fixtures.core/basic/pages/mypage.html b/test/fixtures.templating/layout/pages/mypage.html similarity index 100% rename from test/fixtures.core/basic/pages/mypage.html rename to test/fixtures.templating/layout/pages/mypage.html diff --git a/test/fixtures.core/basic/partials/.gitkeep b/test/fixtures.templating/layout/partials/.gitkeep similarity index 100% rename from test/fixtures.core/basic/partials/.gitkeep rename to test/fixtures.templating/layout/partials/.gitkeep diff --git a/test/fixtures.core/basic/partials/footer.html b/test/fixtures.templating/layout/partials/footer.html similarity index 100% rename from test/fixtures.core/basic/partials/footer.html rename to test/fixtures.templating/layout/partials/footer.html diff --git a/test/fixtures.templating/page-filename/layouts/default.html b/test/fixtures.templating/page-filename/layouts/default.html new file mode 100644 index 0000000000000000000000000000000000000000..1ac417499218e5d72ddace8d630fc45e1417a4f8 --- /dev/null +++ b/test/fixtures.templating/page-filename/layouts/default.html @@ -0,0 +1,5 @@ + + + {{> body}} + + diff --git a/test/fixtures.templating/page-filename/layouts/mylayout.html b/test/fixtures.templating/page-filename/layouts/mylayout.html new file mode 100644 index 0000000000000000000000000000000000000000..dd26c8119d87c47597b4f17205d998b08b0b7f17 --- /dev/null +++ b/test/fixtures.templating/page-filename/layouts/mylayout.html @@ -0,0 +1,6 @@ + + +

My Layout

+ {{> body}} + + diff --git a/test/fixtures.templating/page-filename/pages/pl/mypage.html b/test/fixtures.templating/page-filename/pages/pl/mypage.html new file mode 100644 index 0000000000000000000000000000000000000000..be01deea3e0e2a6487966c78dc8ba61531020fbb --- /dev/null +++ b/test/fixtures.templating/page-filename/pages/pl/mypage.html @@ -0,0 +1 @@ +

Body

diff --git a/test/fixtures.templating/page-filename/partials/.gitkeep b/test/fixtures.templating/page-filename/partials/.gitkeep new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/test/fixtures.templating/page-filename/partials/footer.html b/test/fixtures.templating/page-filename/partials/footer.html new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/test/fixtures.templating/page-guipages/gui/pages/pl/index.html b/test/fixtures.templating/page-guipages/gui/pages/pl/index.html new file mode 100644 index 0000000000000000000000000000000000000000..f98e76a3afc80e268b95a4eabe7e6c8a8aa111dc --- /dev/null +++ b/test/fixtures.templating/page-guipages/gui/pages/pl/index.html @@ -0,0 +1 @@ +

Patternlist

diff --git a/test/fixtures.templating/page-guipages/gui/pages/pl/mypage.html b/test/fixtures.templating/page-guipages/gui/pages/pl/mypage.html new file mode 100644 index 0000000000000000000000000000000000000000..be01deea3e0e2a6487966c78dc8ba61531020fbb --- /dev/null +++ b/test/fixtures.templating/page-guipages/gui/pages/pl/mypage.html @@ -0,0 +1 @@ +

Body

diff --git a/test/fixtures.templating/page-guipages/layouts/default.html b/test/fixtures.templating/page-guipages/layouts/default.html new file mode 100644 index 0000000000000000000000000000000000000000..1ac417499218e5d72ddace8d630fc45e1417a4f8 --- /dev/null +++ b/test/fixtures.templating/page-guipages/layouts/default.html @@ -0,0 +1,5 @@ + + + {{> body}} + + diff --git a/test/fixtures.templating/page-guipages/layouts/mylayout.html b/test/fixtures.templating/page-guipages/layouts/mylayout.html new file mode 100644 index 0000000000000000000000000000000000000000..dd26c8119d87c47597b4f17205d998b08b0b7f17 --- /dev/null +++ b/test/fixtures.templating/page-guipages/layouts/mylayout.html @@ -0,0 +1,6 @@ + + +

My Layout

+ {{> body}} + + diff --git a/test/fixtures.templating/page-guipages/pages/otherpage.html b/test/fixtures.templating/page-guipages/pages/otherpage.html new file mode 100644 index 0000000000000000000000000000000000000000..be01deea3e0e2a6487966c78dc8ba61531020fbb --- /dev/null +++ b/test/fixtures.templating/page-guipages/pages/otherpage.html @@ -0,0 +1 @@ +

Body

diff --git a/test/fixtures.templating/page-guipages/partials/.gitkeep b/test/fixtures.templating/page-guipages/partials/.gitkeep new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/test/fixtures.templating/page-guipages/partials/footer.html b/test/fixtures.templating/page-guipages/partials/footer.html new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/test/fixtures.templating/page-markdown/layouts/default.html b/test/fixtures.templating/page-markdown/layouts/default.html new file mode 100644 index 0000000000000000000000000000000000000000..1ac417499218e5d72ddace8d630fc45e1417a4f8 --- /dev/null +++ b/test/fixtures.templating/page-markdown/layouts/default.html @@ -0,0 +1,5 @@ + + + {{> body}} + + diff --git a/test/fixtures.templating/page-markdown/layouts/mylayout.html b/test/fixtures.templating/page-markdown/layouts/mylayout.html new file mode 100644 index 0000000000000000000000000000000000000000..dd26c8119d87c47597b4f17205d998b08b0b7f17 --- /dev/null +++ b/test/fixtures.templating/page-markdown/layouts/mylayout.html @@ -0,0 +1,6 @@ + + +

My Layout

+ {{> body}} + + diff --git a/test/fixtures.templating/page-markdown/pages/mypage.md b/test/fixtures.templating/page-markdown/pages/mypage.md new file mode 100644 index 0000000000000000000000000000000000000000..810031afd299a0ef7eb471304016c0c1a70da8de --- /dev/null +++ b/test/fixtures.templating/page-markdown/pages/mypage.md @@ -0,0 +1 @@ +# Body \ No newline at end of file diff --git a/test/fixtures.templating/page-markdown/partials/.gitkeep b/test/fixtures.templating/page-markdown/partials/.gitkeep new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/test/fixtures.templating/page-markdown/partials/footer.html b/test/fixtures.templating/page-markdown/partials/footer.html new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/test/test.10.basic-templating.js b/test/test.10.basic-templating.js index 0f18cbdf5f58c5efa5361b953595959143b6e307..fc7811ea7fc0d6f6e4a9b345b00a80058ab40ec5 100644 --- a/test/test.10.basic-templating.js +++ b/test/test.10.basic-templating.js @@ -1,60 +1,220 @@ import Patternlibrary from '..'; +import rimraf from 'rimraf'; +import mkdirp from 'mkdirp'; var extend = require('deep-extend'); var expect = require('chai').expect; +const CLEAN_UP = !true; + describe('Patternlibrary basic templating:', function() { - describe('Patternlibrary.layout', function() { - - it('assings a handlebars default layout "{{> body}}" from an empty template name', function() { - var p = new Patternlibrary.Patternlibrary(); - p.layout = ''; - expect(p.layout).to.be.a('function'); - - p.handlebars.registerPartial('body', 'I am here'); - var rendered = p.layout({}); - expect(rendered).to.equal('I am here') - }); - - it('assings a handlebars layout by template name from module dir', function() { - var p = new Patternlibrary.Patternlibrary(); - p.layout = 'ajax'; - expect(p.layout).to.be.a('function'); - - p.handlebars.registerPartial('body', 'I am here'); - var rendered = p.layout({}); - expect(rendered).to.equal('I am here') - }); - - it('assings a handlebars layout by template name from project dir', function() { - const FIXTURES = 'test/fixtures.core/basic/'; - - var patternlibraryOptions = { - verbose : false, - dest : FIXTURES + 'build', - root : FIXTURES + 'pages/', - layouts : FIXTURES + 'layouts', - partials: FIXTURES + 'partials' - } - - var p = new Patternlibrary.Patternlibrary(patternlibraryOptions); - p.layout = 'mylayout'; - expect(p.layout).to.be.a('function'); - - p.handlebars.registerPartial('body', 'I am here'); - var rendered = p.layout({}); - expect(rendered).to.equal("\n \n

My Layout

\n I am here \n\n") - }); - - it('throws an error if layout could not be found by template name', function() { - var p = new Patternlibrary.Patternlibrary(); - - expect(function() { - p.layout = 'some_unknown_templatefile'; - }).to.throw(Error); - }); - - }); - + describe('Patternlibrary.layout', function() { + + it('assings a handlebars default layout "{{> body}}" from an empty template name string', function() { + var p = new Patternlibrary.Patternlibrary(); + p.layout = ''; + expect(p.layout).to.be.a('function'); + + p.handlebars.registerPartial('body', 'I am here'); + var rendered = p.layout({}); + expect(rendered).to.equal('I am here') + }); + + it('assings a handlebars layout by template name from module dir', function() { + var p = new Patternlibrary.Patternlibrary(); + p.layout = 'ajax'; + expect(p.layout).to.be.a('function'); + + p.handlebars.registerPartial('body', 'I am here'); + var rendered = p.layout({}); + expect(rendered).to.equal('I am here') + }); + + it('assings a handlebars layout by template name from project dir', function() { + const FIXTURES = 'test/fixtures.templating/layout/'; + + var patternlibraryOptions = { + verbose : false, + dest : FIXTURES + 'build', + root : FIXTURES + 'pages/', + layouts : FIXTURES + 'layouts', + partials: FIXTURES + 'partials' + } + + var p = new Patternlibrary.Patternlibrary(patternlibraryOptions); + p.layout = 'mylayout'; + expect(p.layout).to.be.a('function'); + + p.handlebars.registerPartial('body', 'I am here'); + var rendered = p.layout({}); + expect(rendered).to.equal("\n \n

My Layout

\n I am here \n\n"); + }); + + it('throws an error if layout could not be found by template name', function() { + var p = new Patternlibrary.Patternlibrary(); + + expect(function() { + p.layout = 'some_unknown_templatefile'; + }).to.throw(Error); + }); + + it('layout name is empty if no layout is assigned', function() { + var p = new Patternlibrary.Patternlibrary(); + + expect(p.layoutname).to.be.undefined; + }); + + it('layout name is not empty if a layout is assigned', function() { + var p = new Patternlibrary.Patternlibrary(); + + p.layout = 'ajax'; + expect(p.layoutname).to.equal('ajax'); + }); + + it('layout name is "none" if an empty layout name is assigned', function() { + var p = new Patternlibrary.Patternlibrary(); + + p.layout = ''; + expect(p.layoutname).to.equal('none'); + }); + + }); + + describe('Patternlibrary.pagetemplate', function() { + + it('assigns the default GUI doc page template as default', function() { + var opts = { gui : { docpage : 'the page' } }; + var p = new Patternlibrary.Patternlibrary(opts); + + p.layout = ''; + expect(p.pagetemplate).to.be.a('function'); + expect(p.pagetemplate({})).to.equal('the page'); + }); + + it('assings a doc page template by filename from project\'s doc pages dir (pages src dir + basepath)', function() { + const FIXTURES = 'test/fixtures.templating/page-filename/'; + + var patternlibraryOptions = { + verbose : false, + dest : FIXTURES + 'build', + root : FIXTURES + 'pages', + layouts : FIXTURES + 'layouts', + partials: FIXTURES + 'partials' + } + + var p = new Patternlibrary.Patternlibrary(patternlibraryOptions); + p.layout = ''; + p.pagetemplate = 'mypage.html'; + expect(p.pagetemplate).to.be.a('function'); + expect(p.pagetemplate({})).to.equal("

Body

\n"); + }); + + + it('assings a doc page template by filename from modules\'s gui pages dir', function() { + const FIXTURES = 'test/fixtures.templating/page-guipages/'; + + var patternlibraryOptions = { + verbose : false, + dest : FIXTURES + 'build', + root : FIXTURES + 'pages', + layouts : FIXTURES + 'layouts', + partials: FIXTURES + 'partials' + } + + var p = new Patternlibrary.Patternlibrary(patternlibraryOptions); + p.layout = ''; + p.pagetemplate = 'dashboard.html'; + expect(p.pagetemplate).to.be.a('function'); + p.handlebars.registerPartial('docs-dashboard', 'the dashboard.html'); + expect(p.pagetemplate({})).to.equal("the dashboard.html "); + }); + + it('assings a doc page template by filename from project\'s gui pages dir', function() { + const FIXTURES = 'test/fixtures.templating/page-guipages/'; + + var patternlibraryOptions = { + verbose : false, + dest : FIXTURES + 'build', + root : FIXTURES + 'pages', + layouts : FIXTURES + 'layouts', + partials: FIXTURES + 'partials', + gui : { + pages: FIXTURES + 'gui/pages' + } + } + + var p = new Patternlibrary.Patternlibrary(patternlibraryOptions); + p.layout = ''; + p.pagetemplate = 'mypage.html'; + expect(p.pagetemplate).to.be.a('function'); + expect(p.pagetemplate({})).to.equal("

Body

\n"); + }); + + it('assings a doc page template by filename from project\'s gui pages dir over the same (filename) from modules\'s gui pages dir', function() { + const FIXTURES = 'test/fixtures.templating/page-guipages/'; + + var patternlibraryOptions = { + verbose : false, + dest : FIXTURES + 'build', + root : FIXTURES + 'pages', + layouts : FIXTURES + 'layouts', + partials: FIXTURES + 'partials', + gui : { + pages: FIXTURES + 'gui/pages' + } + } + + var p = new Patternlibrary.Patternlibrary(patternlibraryOptions); + p.layout = ''; + p.pagetemplate = 'index.html'; + expect(p.pagetemplate).to.be.a('function'); + expect(p.pagetemplate({})).to.equal("

Patternlist

\n"); + }); + + + it('assings a doc page template by file-path from project\'s pages dir', function() { + const FIXTURES = 'test/fixtures.templating/page-guipages/'; + + var patternlibraryOptions = { + verbose : false, + dest : FIXTURES + 'build', + root : FIXTURES + 'pages', + layouts : FIXTURES + 'layouts', + partials: FIXTURES + 'partials' + } + + var p = new Patternlibrary.Patternlibrary(patternlibraryOptions); + p.layout = ''; + p.pagetemplate = FIXTURES + 'pages/otherpage.html'; + expect(p.pagetemplate).to.be.a('function'); + expect(p.pagetemplate({})).to.equal("

Body

\n"); + }); + + + it('assings a markdown file as a doc page template', function() { + const FIXTURES = 'test/fixtures.templating/page-markdown/'; + + var patternlibraryOptions = { + verbose : false, + dest : FIXTURES + 'build', + root : FIXTURES + 'pages', + layouts : FIXTURES + 'layouts', + partials: FIXTURES + 'partials' + } + + var p = new Patternlibrary.Patternlibrary(patternlibraryOptions); + p.layout = ''; + p.pagetemplate = FIXTURES + 'pages/mypage.md'; + expect(p.pagetemplate).to.be.a('function'); + + expect(p.pagetemplate({})).to.equal('

Body

\n'); + }); + + + }); + + // rimraf.sync(FIXTURES + 'basic/build'); mkdirp(FIXTURES + 'basic/build'); + /// if (CLEAN_UP) rimraf.sync(FIXTURES + 'basic/build'); + }); diff --git a/test/test.90.handlebars.js b/test/test.90.handlebars.js index 3eaebef55a73d3711b65634b89a95d46c2e0c8bb..937436c854844fdced5b84a5668ed7b36473b8a7 100644 --- a/test/test.90.handlebars.js +++ b/test/test.90.handlebars.js @@ -204,7 +204,7 @@ describe('Patternlibrary built-in Handlebars helpers', () => { }); describe('{{md}}', () => { - it('converts Markdown to HTML', () => { + it('converts Markdown to HTML (helper with parameter)', () => { compare('{{md "**Bold**"}}', '

Bold

\n'); }); });