Skip to content

Commit

Permalink
feat(): Add custom layout support
Browse files Browse the repository at this point in the history
.cl-content CSS class is no longer supported by default
  • Loading branch information
sneas committed Apr 4, 2017
1 parent 14044fc commit f82a2e8
Show file tree
Hide file tree
Showing 67 changed files with 118 additions and 108 deletions.
5 changes: 3 additions & 2 deletions demo/gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,10 @@ gulp.task('assets', ['clean'], function() {
});

gulp.task('compile', ['assets'], function(cb) {
var templatesDir = path.join(__dirname, 'templates');
var templatesDir = path.join(__dirname, 'src/templates');
componentLibrary(templatesDir, publicDir, {
baseUrl: '/component-library/',
layout: path.join(__dirname, 'src/layout.html'),
js: [
'https://ajax.googleapis.com/ajax/libs/jquery/2.2.4/jquery.min.js',
'https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js',
Expand All @@ -41,5 +42,5 @@ gulp.task('compile', ['assets'], function(cb) {
});

gulp.task('watch', ['compile'], function() {
gulp.watch(['templates/**/*', 'assets/**/*', '../dist/assemblers/views/**/*', '../sass/**/*', '../assets/**/*'], ['compile']);
gulp.watch(['src/**/*', 'assets/**/*', '../dist/assemblers/views/**/*', '../sass/**/*', '../assets/**/*'], ['compile']);
});
9 changes: 9 additions & 0 deletions demo/src/layout.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<!DOCTYPE html>
<html>
<head></head>
<body>
<div class="container">
{{ content | safe }}
</div>
</body>
</html>
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
},
"dependencies": {
"bootstrap-sass-namespace": "3.3.7-alpha.3",
"cheerio": "^0.22.0",
"directory-tree": "^1.0.0",
"font-awesome": "^4.6.3",
"gulp": "^3.9.1",
Expand Down
19 changes: 0 additions & 19 deletions sass/_layout.scss

This file was deleted.

4 changes: 4 additions & 0 deletions sass/bootstrap/_theme.scss
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,8 @@
background-color: transparent;
border-color: transparent;
}
}

.cl-navbar-placeholder {
min-height: $navbar-height;
}
1 change: 0 additions & 1 deletion sass/cl.scss
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
@import "init";
@import "layout";
@import "tree";
@import "components";
63 changes: 46 additions & 17 deletions src/assemblers/templates.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import path from 'path';
import writefile from 'writefile';
import nunjucks from 'nunjucks';
import hljs from 'highlight.js';
import cheerio from 'cheerio';

function refineTree(item, rootDir) {
//Remove firsts numbers and extensions
Expand Down Expand Up @@ -34,22 +35,49 @@ function compileTree(item, outputDir, tree, options = {}) {

renderers.push(
new Promise(function(resolve, reject) {
nunjucks.render(
'page.njk',
_.extend({}, options, {
tree: tree,
current: item
}),
function(err, res) {
if (err) {
reject(err);
}

writefile(outputPath, res, function() {
resolve();
});
}
);
const params = _.extend({}, options, {
tree: tree,
current: item
});

const navigationHtml = nunjucks.render('navigation.njk', params);
const contentHtml = nunjucks.render('content.njk', params);
const layoutHtml = nunjucks.render(options.layout, {content: contentHtml});

const $ = cheerio.load(layoutHtml);
const head = $('head');
const body = $('body');

head.prepend(nunjucks.render('css.njk', params));

if (head.find('title').length === 0) {
head.prepend($('<title></title>').text(options.title));
}

_.forEach(options.css, function(cssLink) {
head.append($('<link rel="stylesheet">').attr('href', cssLink));
});

if (head.find('meta[charset]').length === 0) {
head.prepend('<meta charset="utf-8">');
}

if (head.find('link[rel=icon]').length === 0) {
head.append($('<link />').attr('href', options.favicon.href)
.attr('rel', options.favicon.rel)
.attr('type', options.favicon.type));
}

body.prepend(navigationHtml);
body.append(nunjucks.render('js.njk', params));

_.forEach(options.js, function(jsLink) {
body.append($('<script></script>').attr('src', jsLink));
});

writefile(outputPath, $.html(), function() {
resolve();
});
})
);

Expand All @@ -62,14 +90,15 @@ export default function(inputDir, outputDir, options = {}) {
title: 'Component Library',
js: [],
css: [],
layout: path.join(__dirname, 'views/layout.njk'),
favicon: {
href: options.baseUrl + 'assets_/assets/favicons/favicon-32x32.png',
rel: 'icon',
type: 'image/component-library/favicons/png'
}
});

(new nunjucks.configure(path.resolve(__dirname, 'views')))
(new nunjucks.configure([path.join(__dirname, 'views'), '']))
.addFilter('template', path => fs.readFileSync(path).toString())
.addFilter('highlight', code => hljs.highlight('htmlbars', code, true, false).value);

Expand Down
2 changes: 2 additions & 0 deletions src/assemblers/views/content.njk
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
{% from 'macro/element-tree.njk' import elementTree %}
{{ elementTree(current, 1, baseUrl) }}
1 change: 1 addition & 0 deletions src/assemblers/views/css.njk
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<link href="{{ baseUrl + 'assets_/css/cl.css' }}" rel="stylesheet">
9 changes: 9 additions & 0 deletions src/assemblers/views/js.njk
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<script>
BOOTSTRAP_NAMESPACE = 'cl-bs';
</script>

<script src="{{ baseUrl + 'assets_/jquery/jquery.js' }}"></script>
<script src="{{ baseUrl + 'assets_/smartmenus-namespace/jquery.smartmenus.js' }}"></script>
<script src="{{ baseUrl + 'assets_/smartmenus-namespace/addons/bootstrap/jquery.smartmenus.bootstrap.js' }}"></script>
<script src="{{ baseUrl + 'assets_/assets/js/cl.js' }}"></script>
<script src="{{ baseUrl + 'assets_/bootstrap-namespace/dropdown.js' }}"></script>
7 changes: 7 additions & 0 deletions src/assemblers/views/layout.njk
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<!DOCTYPE html>
<html>
<head></head>
<body>
{{ content | safe }}
</body>
</html>
36 changes: 36 additions & 0 deletions src/assemblers/views/navigation.njk
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
{% from 'macro/navigation.njk' import navigation %}
{% from 'macro/search-tree.njk' import searchTree %}
<div class="cl-bs-tag-html">
<div class="cl-bs-tag-body">
<div class="cl-bs-navbar cl-bs-navbar-inverse cl-bs-navbar-fixed-top" role="navigation">
<div class="cl-bs-container">
<div class="cl-bs-navbar-header">
<button type="button" class="cl-bs-navbar-toggle cl-bs-tag-button cl-navigation-toggle">
<span class="sr-only">Toggle navigation</span>
<span class="cl-bs-icon-bar"></span>
<span class="cl-bs-icon-bar"></span>
<span class="cl-bs-icon-bar"></span>
</button>
</div>
<div class="cl-bs-navbar-collapse cl-bs-collapse cl-navigation-collapse">
<ul class="cl-bs-tag-ul cl-bs-nav cl-bs-navbar-nav">
{% for child in tree.children %}
{{ navigation(child, baseUrl, current) }}
{% endfor %}
</ul>

<div class="cl-bs-navbar-form cl-bs-pull-right cl-bs-hidden-xs">
<div class="cl-bs-dropdown">
<input type="text" class="cl-bs-tag-input cl-bs-form-control cl-bs-form-control-sm" placeholder="Search" id="cl-search">
<ul class="cl-bs-dropdown-menu">
{{ searchTree(tree, baseUrl) }}
</ul>
</div>
</div>
</div>
</div>
</div>

<div class="cl-navbar-placeholder"></div>
</div>
</div>
69 changes: 0 additions & 69 deletions src/assemblers/views/page.njk

This file was deleted.

0 comments on commit f82a2e8

Please sign in to comment.