Skip to content

Commit 2e03e77

Browse files
author
Juuso Backman
committed
Merge pull request #181 from hannu/integrate-splitter
Integrate KSS splitter. Show related CSS styles in UI
2 parents 24f315e + d08bcb4 commit 2e03e77

File tree

12 files changed

+263
-308
lines changed

12 files changed

+263
-308
lines changed

lib/app/js/directives/section.js

+15-1
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,10 @@ angular.module('sgApp')
88
templateUrl: 'views/partials/section.html',
99
link: function(scope, element, attrs) {
1010

11-
//init setting based on global 'showAllMarkup' value
11+
// Init setting based on global 'showAllMarkup' value
1212
scope.section.showMarkup = scope.markup.isVisible;
13+
// By default do not show CSS markup
14+
scope.section.showCSS = false;
1315

1416
scope.showSingleMarkupBox = function(index) {
1517
if (!scope.section.showMarkup) {
@@ -22,6 +24,18 @@ angular.module('sgApp')
2224
scope.section.showMarkup = false;
2325
}
2426
}
27+
28+
scope.showSingleCSSBox = function(index) {
29+
if (!scope.section.showCSS) {
30+
scope.section.showCSS = true;
31+
}
32+
}
33+
34+
scope.hideSingleCSSBox = function(index) {
35+
if (scope.section.showCSS) {
36+
scope.section.showCSS = false;
37+
}
38+
}
2539
}
2640
};
2741
}]);

lib/app/sass/app.scss

+5
Original file line numberDiff line numberDiff line change
@@ -692,6 +692,11 @@ $mobile: new-breakpoint(max-width 480px);
692692
padding: 1em;
693693
}
694694

695+
.hljs {
696+
max-height: 400px;
697+
overflow-y: auto;
698+
}
699+
695700
a.show-section {
696701
display: block;
697702
cursor: pointer;

lib/app/views/partials/section.html

+16-3
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,27 @@ <h4 ng-show="getLevel(section) === 'sub-sub-sub'" ng-show="" class="sg section-h
4343
ng-bind-html="section.wrappedMarkup | setVariables: variables | unsafe" dynamic-compile>
4444
</div>
4545
</div>
46+
47+
<div class="sg section-partial code-listing" ng-if="section.css">
48+
<div ng-if="section.showCSS" class="sg label">
49+
<a ng-click="hideSingleCSSBox($index)" target="_blank">
50+
<i class="fa fa-close"></i>
51+
</a>
52+
</div>
53+
<pre class="sg" ng-if="section.showCSS">
54+
<code hljs source="section.css" class="html"></code>
55+
</pre>
56+
<a class="sg show-section" ng-hide="section.showCSS" ng-click="showSingleCSSBox($index)">Show CSS</a>
57+
</div>
58+
4659
<div class="sg section-partial code-listing" ng-if="section.markup">
47-
<div ng-show="section.showMarkup" class="sg label">
60+
<div ng-if="section.showMarkup" class="sg label">
4861
<a ng-click="hideSingleMarkupBox($index)" target="_blank">
4962
<i class="fa fa-close"></i>
5063
</a>
5164
</div>
52-
<pre class="sg" ng-show="section.showMarkup">
53-
<code hljs source="section.markup" class="html"></code>
65+
<pre class="sg" ng-if="section.showMarkup">
66+
<code hljs source="section.markup" class="css"></code>
5467
</pre>
5568
<a class="sg show-section" ng-hide="section.showMarkup" ng-click="showSingleMarkupBox($index)">Show markup</a>
5669
</div>

lib/modules/kss.js

+67-12
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11

22
'use strict';
33
var kss = require('kss'),
4-
sanitizeHtml = require('sanitize-html'),
5-
wrapperMarkup = require('./wrapper-markup');
4+
Q = require('q'),
5+
kssSplitter = require('./kss-splitter'),
6+
sanitizeHtml = require('sanitize-html');
67

78
// Parses kss.KssSection to JSON
89
function jsonSections(sections) {
@@ -32,30 +33,84 @@ function jsonModifiers(modifiers) {
3233
});
3334
}
3435

36+
function trimLinebreaks(str) {
37+
// Remove leading and trailing whitespaces
38+
if (!str) {
39+
return str;
40+
}
41+
return str.replace(/^[\r\n]+|[\r\n]+$/g, '');
42+
}
43+
3544
function sanitize(string) {
3645
return sanitizeHtml(string, {allowedTags: [], allowedAttributes: []});
3746
}
3847

39-
module.exports = {
48+
function processBlock(block, options, json) {
49+
var blockPromise = Q.defer();
4050

41-
// Parse node-kss object ( {'file.path': 'file.contents.toString('utf8'}' )
42-
parseKSS: function(files, options, success) {
43-
var json = {};
44-
kss.parse(files, options, function(err, styleguide) {
51+
kss.parse(block.kss, options, function(err, styleguide) {
52+
var section,
53+
blockStyles;
4554
if (err) {
4655
new PluginError(PLUGIN_NAME, 'Error parsing', err);
56+
blockPromise.resolve();
4757
return false;
4858
} else {
49-
json.sections = jsonSections(styleguide.section());
59+
section = jsonSections(styleguide.section());
5060

51-
/* Make inherited wrappers */
52-
json.sections = wrapperMarkup.generateSectionWrapperMarkup(json.sections);
61+
if (section.length > 0) {
62+
if (section.length > 1) {
63+
console.warn('Warning: KSS splitter returned more than 1 KSS block. Styleguide might not be properly generated.');
64+
}
65+
blockStyles = trimLinebreaks(block.code);
5366

54-
if (typeof success === 'function') {
55-
success(json);
67+
// Add related CSS to section
68+
if (blockStyles && blockStyles !== '') {
69+
section[0].css = blockStyles;
70+
}
71+
json.sections = json.sections.concat(section);
5672
}
73+
blockPromise.resolve();
5774
}
5875
});
76+
return blockPromise;
5977
}
6078

79+
function processFile(contents, options, json) {
80+
var filePromise = Q.defer(),
81+
blockPromises = [],
82+
splittedFile = kssSplitter.getBlocks(contents);
83+
84+
// Process every block in the current file
85+
splittedFile.forEach(function(block) {
86+
blockPromises.push(processBlock(block, options, json));
87+
});
88+
89+
Q.all(blockPromises).then(function() {
90+
// All blocks are processed, resolve file promise
91+
filePromise.resolve();
92+
});
93+
94+
return filePromise;
95+
}
96+
97+
module.exports = {
98+
// Parse node-kss object ( {'file.path': 'file.contents.toString('utf8'}' )
99+
parseKSS: function(files, options, success) {
100+
var json = {
101+
sections: []
102+
},
103+
filePromises = [],
104+
fileKeys = Object.keys(files);
105+
106+
fileKeys.forEach(function(filePath) {
107+
var contents = files[filePath];
108+
filePromises.push(processFile(contents, options, json));
109+
});
110+
111+
Q.all(filePromises).then(function() {
112+
// All files are processed. Call success callback
113+
success(json);
114+
});
115+
}
61116
}

lib/styleguide.js

+5
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ var through = require('through2'),
2222
parseKSS = require(__dirname + '/modules/kss').parseKSS,
2323
pseudoSelectors = require(__dirname + '/modules/pseudo-selectors'),
2424
preprocess = require(__dirname + '/modules/preprocess'),
25+
wrapperMarkup = require(__dirname + '/modules/wrapper-markup'),
2526
path = require('path'),
2627
sgServer = require(__dirname + '/server'),
2728
File = require('vinyl'),
@@ -99,6 +100,7 @@ module.exports = function(opt) {
99100
// After reading all files, parse them and generate data for styleguide
100101
function(callback) {
101102
var _this = this;
103+
102104
parseKSS(filesBuffer, opt.kssOpt, function(styleguide) {
103105
var pushAllFiles = function() {
104106
return through.obj(function(file, enc, cb) {
@@ -109,6 +111,9 @@ module.exports = function(opt) {
109111
})
110112
};
111113

114+
// Make inherited wrappers
115+
styleguide.sections = wrapperMarkup.generateSectionWrapperMarkup(styleguide.sections);
116+
112117
// Store settings inside the styleguide JSON
113118
styleguide.config = opt;
114119

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
"node-neat": "^1.3.0",
5050
"node.extend": "^1.1.2",
5151
"ometajs": "^3.3.7",
52+
"q": "^1.1.1",
5253
"run-sequence": "^1.0.1",
5354
"sanitize-html": "^1.4.3",
5455
"serve-favicon": "^2.1.6",

test/data/wrapper.scss

-56
This file was deleted.

test/kss-parser.js

-52
This file was deleted.

0 commit comments

Comments
 (0)