Skip to content

Commit

Permalink
Merge pull request #3 from sneas/custom-layout
Browse files Browse the repository at this point in the history
Custom layout
  • Loading branch information
sneas authored Apr 4, 2017
2 parents 14044fc + 98098c2 commit c03d30f
Show file tree
Hide file tree
Showing 73 changed files with 186 additions and 132 deletions.
46 changes: 45 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ npm install component-library

Create an empty folder in your projects's file system and fill it with HTML files (components). One HTML file per one component.

See [demo/templates](demo/templates) as an example.
See [demo/src/templates](demo/src/templates) as an example.

### Integration with build process

Expand Down Expand Up @@ -75,6 +75,50 @@ gulp.task('cl:watch', ['cl'], function() {
});
```

## FAQ
### What if my project has a custom initialization logic in layout?
Create you own layout file by copying minimum HTML required:

```html
<!DOCTYPE html>
<html>
<head></head>
<body>
{{ content | safe }}
</body>
</html>
```

Add your custom code to it. **NB:** You can add only the required code. All the CSS links and JS scripts will be added automatically.
Add the `layout` param to the configuration object. Example:
```javascript
var componentLibrary = require('component-library');

componentLibrary(
'/project/component-library/templates/dir',
'/project/public/component-library',
{
layout: '/project/component-library/your-layout.html',
baseUrl: '/component-library/',
favicon: {
href: '/project/favicon.ico',
rel: 'shortcut icon',
type: 'image/x-icon'
},
js: [
//List of your project's JS files goes here
'/project/js/file.js'
],
css: [
//List of your project's CSS files goes here
'/project/css/file.css'
]
}
);
```

See [demo/src/layout.html](demo/src/layout.html) as an example.

## License

(MIT License)
Expand Down
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>
3 changes: 3 additions & 0 deletions demo/src/templates/0.intro.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<p>
Demo library based on <a href="https://getbootstrap.com/">Bootstrap</a> CSS framework.
</p>
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.
3 changes: 0 additions & 3 deletions demo/templates/0.intro.html

This file was deleted.

5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@
"publish-demo": "gh-pages -d demo/public/component-library"
},
"dependencies": {
"bootstrap-sass-namespace": "3.3.7-alpha.3",
"bootstrap-sass-namespace": "3.3.7-alpha.5",
"cheerio": "^0.22.0",
"directory-tree": "^1.0.0",
"font-awesome": "^4.6.3",
"gulp": "^3.9.1",
Expand All @@ -39,7 +40,7 @@
"node-modules-resolve": "^1.3.0",
"npm-sass": "^1.3.0",
"nunjucks": "3.0.0",
"smartmenus-namespace": "1.0.1-alpha.4",
"smartmenus-namespace": "1.0.1-alpha.5",
"writefile": "^0.2.8"
},
"devDependencies": {
Expand Down
19 changes: 0 additions & 19 deletions sass/_layout.scss

This file was deleted.

14 changes: 9 additions & 5 deletions sass/bootstrap/_theme.scss
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
background-image: none;
}

.#{$class-prefix}-dropdown-menu > .#{$class-prefix}-tag-li > .#{$class-prefix}-tag-a:hover,
.#{$class-prefix}-dropdown-menu > .#{$class-prefix}-tag-li > .#{$class-prefix}-tag-a:focus {
.#{$class-prefix}-dropdown-menu > .#{$class-prefix}-html-li > .#{$class-prefix}-html-a:hover,
.#{$class-prefix}-dropdown-menu > .#{$class-prefix}-html-li > .#{$class-prefix}-html-a:focus {
background-image: none;
outline: none;
}
Expand All @@ -24,11 +24,15 @@
padding-right: 0px;
}

.#{$class-prefix}-nav .#{$class-prefix}-open > .#{$class-prefix}-tag-a,
.#{$class-prefix}-nav .#{$class-prefix}-open > .#{$class-prefix}-tag-a:hover,
.#{$class-prefix}-nav .#{$class-prefix}-open > .#{$class-prefix}-tag-a:focus {
.#{$class-prefix}-nav .#{$class-prefix}-open > .#{$class-prefix}-html-a,
.#{$class-prefix}-nav .#{$class-prefix}-open > .#{$class-prefix}-html-a:hover,
.#{$class-prefix}-nav .#{$class-prefix}-open > .#{$class-prefix}-html-a:focus {
@media (max-width: $grid-float-breakpoint) {
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";
2 changes: 1 addition & 1 deletion src/assemblers/__tests__/templates.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ describe('templates assembler', function() {
const $ = cheerio.load(fs.readFileSync(overviewFile, 'utf8'), {
normalizeWhitespace: true
});
expect($('.cl-node-header.cl-bs-tag-h1').text().trim()).to.be.equal('Overview');
expect($('.cl-node-header.cl-bs-html-h1').text().trim()).to.be.equal('Overview');
done();
});
});
Expand Down
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>
14 changes: 7 additions & 7 deletions src/assemblers/views/macro/element-tree.njk
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{% macro elementTree(element, depth, baseUrl) %}
<div class="cl-node cl-node-level-{{ depth }}{{ 'cl-node-template' if not element.children }}" id="{{ baseUrl + element.relativePath }}">
<div class="cl-bs-tag-html">
<div class="cl-bs-tag-body">
<div class="cl-node-header cl-bs-tag-h{{ depth }}">
<div class="cl-bs-html-html">
<div class="cl-bs-html-body">
<div class="cl-node-header cl-bs-html-h{{ depth }}">
{{ element.name }}
<a href="{{ baseUrl + element.relativePath }}" class="cl-node-header-link">
<i class="cl-fa cl-fa-paragraph"></i>
Expand All @@ -20,13 +20,13 @@
<div class="cl-node-template-preview">
{{ element.path | template | safe }}
</div>
<div class="cl cl-bs-tag-html">
<div class="cl-bs-tag-body">
<div class="cl cl-bs-html-html">
<div class="cl-bs-html-body">
<div class="cl-node-temaplate-code cl-js-template-code">
<button class="cl-bs-tag-button cl-bs-btn cl-bs-btn-default cl-bs-btn-xs cl-js-template-code-toggle">
<button class="cl-bs-html-button cl-bs-btn cl-bs-btn-default cl-bs-btn-xs cl-js-template-code-toggle">
<i class="cl-fa cl-fa-code"></i>
</button>
<pre class="cl-bs-tag-pre cl-node-template-code-content cl-js-template-code-content"><code class="html">{{ element.path | template | highlight | safe }}</code></pre>
<pre class="cl-bs-html-pre cl-node-template-code-content cl-js-template-code-content"><code class="html">{{ element.path | template | highlight | safe }}</code></pre>
</div>
</div>
</div>
Expand Down
6 changes: 3 additions & 3 deletions src/assemblers/views/macro/navigation.njk
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
{% macro navigation(tree, baseUrl, current) %}
<li class="cl-bs-tag-li">
<a class="cl-bs-tag-a" href="{{ baseUrl + 'index.html#' + baseUrl + tree.relativePath }}">
<li class="cl-bs-html-li">
<a class="cl-bs-html-a" href="{{ baseUrl + 'index.html#' + baseUrl + tree.relativePath }}">
{{ tree.name }}

{% if tree.children %}
<span class="cl-bs-caret"></span>
{% endif %}
</a>
{% if tree.children %}
<ul class="cl-bs-tag-ul cl-bs-dropdown-menu">
<ul class="cl-bs-html-ul cl-bs-dropdown-menu">
{% for child in tree.children %}
{{ navigation(child, baseUrl, current) }}
{% endfor %}
Expand Down
4 changes: 2 additions & 2 deletions src/assemblers/views/macro/search-tree.njk
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{% macro searchTree(tree, baseUrl) %}
<li class="cl-bs-tag-li">
<a class="cl-bs-tag-a" href="{{ baseUrl + 'index.html#' + baseUrl + tree.relativePath }}">
<li class="cl-bs-html-li">
<a class="cl-bs-html-a" href="{{ baseUrl + 'index.html#' + baseUrl + tree.relativePath }}">
{{ tree.name }}
</a>
</li>
Expand Down
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-html-html">
<div class="cl-bs-html-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-html-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-html-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-html-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>
Loading

0 comments on commit c03d30f

Please sign in to comment.