Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Separate wrapper markup generator to its own module #163

Merged
merged 1 commit into from
Nov 7, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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