Skip to content

Commit

Permalink
Merge pull request #163 from hannu/wrapper-module
Browse files Browse the repository at this point in the history
Separate wrapper markup generator to its own module
  • Loading branch information
Juuso Backman committed Nov 7, 2014
2 parents 21de858 + 2bd4fbd commit cfc2054
Show file tree
Hide file tree
Showing 8 changed files with 282 additions and 449 deletions.
2 changes: 1 addition & 1 deletion lib/modules/io.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
var fs = require('fs'),
path = require('path'),
parser = require('./parser')();
parser = require('./parser');

module.exports = function(ioServer, options) {

Expand Down
96 changes: 4 additions & 92 deletions lib/modules/kss.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@

'use strict';
var kss = require('kss'),
sanitizeHtml = require('sanitize-html');
sanitizeHtml = require('sanitize-html'),
wrapperMarkup = require('./wrapper-markup');

// Parses kss.KssSection to JSON
function jsonSections(sections) {
Expand Down Expand Up @@ -39,106 +40,17 @@ module.exports = {

// Parse node-kss object ( {'file.path': 'file.contents.toString('utf8'}' )
parseKSS: function(files, options, success) {
var json = {},

/* Stores links to sections representing
* styleguide nested structure
*/
nested = {
sections: []
};

var json = {};
kss.parse(files, options, function(err, styleguide) {
if (err) {
new PluginError(PLUGIN_NAME, 'Error parsing', err);
return false;
} else {
json.sections = jsonSections(styleguide.section());

var prevSections = [],
index = {},
cursor,

ownWrapper;

json.sections.map(function(section) {
/* Check if the parent section was already mentioned
* in the tree */
var ref = section.reference;
/* Put cursor to the root for the root elements */
if (ref.indexOf('.') == -1) {
cursor = nested.sections;
}
while (ref.indexOf('.') != -1) {
ref = ref.substring(0, ref.lastIndexOf('.'));

/* Parent section */
var par = prevSections[prevSections.indexOf(ref)];
if (par) {
section.parentReference = par;
/* Create nested container if it does not exists yet */
index[par].sections = index[par].sections || [];
cursor = index[par].sections;
break;
}
}
prevSections.push(section.reference);
/* Remember where we put current section */
var length = cursor.push(section);
index[section.reference] = cursor[length - 1];
});

/* Calculates a warpper for a section */
var ownWrapper = function(section) {
var re, match,
wrapper = '<sg:wrapper-content/>';

re = /^([\s\S]*)<sg\:wrapper>([\s\S]*)<\/sg\:wrapper>([\s\S]*)$/;
match = re.exec(section.markup);
if (match) {
wrapper = match[2];
}
return wrapper;
}

/* Make inherited wrappers */
json.sections.map(function(section) {

/* Calculate own wrapper */
var wrapper = ownWrapper(section);
/* Wrap a wrapper with a parent wrapper */
if (section.parentReference) {
var parentWrapper = index[section.parentReference].wrapper;
wrapper = parentWrapper.replace('<sg:wrapper-content/>', wrapper);
}
section.wrapper = wrapper;

if (section.markup) {
/* Clean markup */
if (section.markup.indexOf('<sg:wrapper>') != -1) {
section.markup = section.markup.substring(0, section.markup.indexOf('<sg:wrapper'));
}
/* Wrap markup */
section.wrappedMarkup = section.markup;
section.wrappedMarkup = section.wrapper.replace('<sg:wrapper-content/>', section.markup);
}

/* Wrap modifiers */
section.modifiers.forEach(function(modifier) {
/* Clean modifier markup */
if (modifier.markup.indexOf('<sg:wrapper>') != -1) {
modifier.markup = modifier.markup.substring(0, modifier.markup.indexOf('<sg:wrapper'));
}
/* Wrap modifier markup */
modifier.wrappedMarkup = modifier.markup;
modifier.wrappedMarkup = section.wrapper.replace('<sg:wrapper-content/>', modifier.markup);
});

/* Clean nested sections */
delete section.sections;
return section;
json.sections = wrapperMarkup.generateSectionWrapperMarkup(json.sections);

});
if (typeof success === 'function') {
success(json);
}
Expand Down
Loading

0 comments on commit cfc2054

Please sign in to comment.