Issues (117)

lib/patternlibrary.js (2 issues)

1 1
var fs          = require('fs');
2 1
var path        = require('path');
3 1
var fm          = require('front-matter');
4 1
var resolvePath = require('./util/module-or-process-path');
5
6
class Patternlibrary {
7
	
8 92
	constructor (opts) {
9
		
10 92
	    this.options         = {};
11 92
	    this.adapters        = {};
12 92
	    this.handlebars      = null;
13 92
	    this.markdown        = null;
14 92
	    this.template        = null;
15 92
		this._layoutTemplate = null;
16 92
	    this.time            = null;
17
	    
18 92
	    this.searchOptions   = {
19
		    extra     : [],
20
		    sort      : [],
21
		    pageTypes : {}
22
	    };
23
	    
24 92
	    this.reset();
25 92
	    this.init(opts);
26
	}
27
	
28
	/**
29
	 * resets/clears all (pattern) data
30
	 */
31
	reset () {
32 113
	    this.data = {
33
	    	patterns  : {},
34
	    	categories: {}
35
	    };
36 113
	    return (this);
37
	}
38
	
39
	/**
40
	 * Retrieves (handlebars) layout template
41
	 * 
42
	 * if no layout is set yet, returns the default layout template
43
	 * 
44
	 * @var layoutname
45
	 * @returns {string}
46
	 */
47
    get layoutname () {
48 28
    	return this._layoutName;
49
	}
50
	
51
	/**
52
	 * Retrieves (handlebars) layout template
53
	 * 
54
	 * if no layout is set yet, returns the default layout template
55
	 * 
56
     * @var {function} layout - a precompiled (handlebars) template
57
	 * @returns {string}
58
	 */
59
    get layout () {
60 35
    	if (!this._layoutTemplate) {
61 1
    		this.layout = this.options.gui.layout;
62
    	}
63 35
    	return this._layoutTemplate;
64
    }
65
	
66
    /**
67
     * Sets and compiles (handlebars) layout by the file basename of the layoutfile
68
     * 
69
     * if no name is given an 'empty' layout ('{{> body}}') is set
70
     * 
71
	 * @function layout
72
     * @param {string} layoutname - the file basename of the layoutfile
73
     */
74
    set layout ( layoutname ) {
75 80
    	var layoutSource = '{{> body}}';
76 80
    	this._layoutName = 'none';
77 80
    	if ( (typeof layoutname != 'undefined') && (layoutname != '') && (layoutname != 'none') && (layoutname != false) ) {
0 ignored issues
show
Comparing layoutname to false using the != operator is not safe. Consider using !== instead.
Loading history...
78 71
    		var layoutfile = path.join(this.options.layouts, layoutname+'.html');
79 71
    		var layoutgui  = resolvePath(path.join(this.options.gui.layouts, layoutname+'.html'));
80
    		
81 71
        	try {
82 71
        		if (fs.existsSync(layoutgui) && !fs.existsSync(layoutfile)) {
83
            		// gui layout
84 43
        			layoutfile = layoutgui;
85
        		}
86 71
        		layoutSource = fs.readFileSync(layoutfile).toString();
87 70
            	this._layoutName = layoutname;
88
        	} catch (e) {
89 1
        		this.log.warn('Error loading Patternlibrary layoutfile "'+layoutname+'"');
90 1
        		throw new Error(e.message);
91
        	}
92
    	}
93 79
    	this._layoutTemplate = this.handlebars.compile(layoutSource, {noEscape: true});
94
    }
95
    
96
	/**
97
	 * Retrieves (handlebars) page template
98
	 * 
99
	 * if no page template is set yet, returns the default 
100
	 * gui doc-page templateto be used in a series of similar 
101
	 * pages with iterating data
102
	 * 
103
	 * it retrieves a compiled Handlebars template and
104
     * returns the rendred page content when invoked
105
     * ```
106
     * var rendered = P.pagetemplate(pagevars);
107
     * ```
108
     * 
109
     * @function pagetemplate
110
	 * @param {object} data - template data
111
	 * @returns {string}
112
	 */
113
    get pagetemplate () {
114 39
    	if (!this._pageTemplate) {
115 1
    		this.pagetemplate = this.options.gui.docpage;
116
    	}
117 39
    	return this._pageTemplate;
118
    }
119
	
120
    /**
121
     * Sets and compiles (handlebars) page by page filename
122
     * for a *Patternlibrary* page template to be used in a 
123
     * series of similar pages with iterating data
124
     * 
125
     * if `page` is a '.md' or '.markdown' file, it is also 
126
     * passed through the markdown renderer
127
     * 
128
     * the content is compiled into a Handlebars template and
129
     * retrieves the rendred page content when invoked
130
     * ```
131
     * var rendered = P.pagetemplate(pagevars);
132
     * ```
133
     * 
134
     * if the parameter does not correspond to a readable file
135
     * the parameter's string value is assigned as page template 
136
     * 
137
     * look-up order:
138
     * - project's doc pages dir (pages src dir + basepath)
139
     * - project's `gui` pages dir
140
     * - module's `gui` pages dir
141
     * - file path
142
     * 
143
     * @param {string} page - the file basename of the page-file
144
     * @var {function} pagetemplate - a precompiled (handlebars) template
145
     */
146
    set pagetemplate ( page ) {
147
148
		//markdown = isMD(pageSource);
149
		
150 72
    	var pageSource = page;
151 72
    	var markdown   = isMD(pageSource);
152 72
    	if ( (typeof page != 'undefined') && (page != '') && (page != 'none') && (page != false) ) {
0 ignored issues
show
Comparing page to false using the != operator is not safe. Consider using !== instead.
Loading history...
153 72
    		var pagefile = path.join(this.options.root, this.options.basepath, page);
154 72
    		var guipage  = resolvePath(path.join(this.options.gui.pages, this.options.basepath, page));
155
    		
156 72
    		if ( !fs.existsSync(pagefile) ) {
157 71
        		if ( fs.existsSync(guipage) ) {
158
        			// file from gui pages
159 43
            		pageSource = fs.readFileSync(guipage).toString();
160
        		} else {
161 28
            		if ( fs.existsSync(pageSource) ) {
162
            			// file from `pageSource` as a file-path by it self
163 27
                		pageSource = fs.readFileSync(pageSource).toString();
164
            		} 
165
        		}
166
    		} else {
167
    			// file from pages
168 1
        		pageSource = fs.readFileSync(pagefile).toString();
169
    		}
170
    	}
171
    	// strip yml data in the beginning
172 72
    	pageSource = fm(pageSource).body;
173
    		
174
		// finally compile Markdown content, if we have a markdown file here...
175 72
		if (markdown) {
176
            
177 1
    		this.log.info('Rendering Markdown content...');
178
    		// interestingly, just rendering with MarkdownIt seems to do too much escaping
179
    		// and renders some special characters useless for to be interpreted by 
180
    		// Handlebars, like `{{> ...`, so we wrap it with Handlebars and let its
181
    		// MarkdownIt helper there do it...
182 1
    		pageSource = '{{#markdown}}'+pageSource+'{{/markdown}}'; 
183
            
184
		}
185
186 72
    	this._pageTemplate = this.handlebars.compile(pageSource, {noEscape: true});
187
    }
188
    
189
}
190
191 1
Patternlibrary.prototype.init            = require('./patternlibrary/init');
192 1
Patternlibrary.prototype.config          = require('./patternlibrary/config');
193
194 1
Patternlibrary.prototype.loaddata        = require('./patternlibrary/load-data');
195 1
Patternlibrary.prototype.loadhelpers     = require('./patternlibrary/load-helpers');
196
197
198 1
Patternlibrary.prototype.run             = require('./patternlibrary/run');
199 1
Patternlibrary.prototype.parsepatterns   = require('./patternlibrary/parse-patterns');
200 1
Patternlibrary.prototype.parsecategories = require('./patternlibrary/parse-categories');
201 1
Patternlibrary.prototype.parsedocs       = require('./patternlibrary/parse-docs');
202
203 1
Patternlibrary.prototype.buildpages      = require('./patternlibrary/build-pages');
204 1
Patternlibrary.prototype.buildgui        = require('./patternlibrary/build-gui');
205 1
Patternlibrary.prototype.builddocs       = require('./patternlibrary/build-docs');
206 1
Patternlibrary.prototype.adapter         = require('./patternlibrary/adapter');
207
208 1
Patternlibrary.prototype.renderdata      = require('./patternlibrary/render-data');
209 1
Patternlibrary.prototype.renderdocs      = require('./patternlibrary/render-docs');
210 1
Patternlibrary.prototype.renderpattern   = require('./patternlibrary/render-pattern');
211 1
Patternlibrary.prototype.renderpage      = require('./patternlibrary/render-page');
212
213 1
Patternlibrary.prototype.listpatterns    = require('./patternlibrary/list-patterns');
214 1
Patternlibrary.prototype.listcategories  = require('./patternlibrary/list-categories');
215 1
Patternlibrary.prototype.statistics      = require('./patternlibrary/statistics');
216
217 1
Patternlibrary.prototype.searchconfig    = require('./patternlibrary/search-config');
218 1
Patternlibrary.prototype.buildsearch     = require('./patternlibrary/build-search');
219 1
Patternlibrary.prototype.updatedatafiles = require('./patternlibrary/update-datafiles');
220
221 1
module.exports = Patternlibrary;
222
223
224
function isMD ( str ) { 
225
	return ( (path.extname( str ) == '.md') || (path.extname( str ) == '.markdown') ); 
226
}