Commit 22873c21 authored by Björn Bartels's avatar Björn Bartels

PATLAB-7 add page-template tests

parent 6345db90
Pipeline #73 passed with stage
in 3 minutes and 43 seconds
......@@ -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});
}
......
<html>
<body>
<h1>My Layout</h1>
{{> body}}
</body>
</html>
<html>
<body>
<h1>My Layout</h1>
{{> body}}
</body>
</html>
<html>
<body>
<h1>My Layout</h1>
{{> body}}
</body>
</html>
This diff is collapsed.
......@@ -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**"}}', '<p><strong>Bold</strong></p>\n');
});
});
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment