diff --git a/src/rt-style-support-data.js b/src/rt-style-support-data.js new file mode 100644 index 0000000..d1df6c9 --- /dev/null +++ b/src/rt-style-support-data.js @@ -0,0 +1,7 @@ +'use strict'; +var map = { + padding: 'number', + 'background-color': 'string' +}; + +module.exports = map; \ No newline at end of file diff --git a/src/rtStyle.js b/src/rtStyle.js new file mode 100644 index 0000000..771fa5f --- /dev/null +++ b/src/rtStyle.js @@ -0,0 +1,56 @@ +'use strict'; +var css = require('css'); +var _ = require('lodash'); +var stringUtils = require('./stringUtils'); +var rtnData = require('./rt-style-support-data.js'); + + +var templateCommonJSTemplate = _.template( + "'use strict';\n" + + 'var style = <%= body %>;\n' + + 'module.exports = style;\n' +); + +function convert(text) { + return templateCommonJSTemplate({body: convertBody(text)}); +} + +function convertBody(text) { + //source + var obj = css.parse(text, {silent: false}); + var result = _.reduce(obj.stylesheet.rules, processRule2, {}); + //var out = _.map(obj.stylesheet.rules, processRule).join(', '); + console.log(result); + return JSON.stringify(result, undefined, 2); +} + +function processRule2(result, rule) { + var name = rule.selectors[0].substring(1); + result[name] = _.reduce(rule.declarations, processDeclaration, {}); + return result; +} + +function processDeclaration(result, dec) { + result[stringUtils.convertToCamelCase(dec.property)] = convertValue(dec.property, dec.value); + return result; +} + +//function processRule(rule) { +// return rule.declarations.map(function (dec) { +// return stringUtils.convertToCamelCase(dec.property) + ': ' + convertValue(dec.property, dec.value); +// }).join(', '); +//} + +function convertValue(p, v) { + if (rtnData[p] === 'string') { + //return "'" + v + "'"; + return v; + } + // TODO remove units + return parseInt(v.match(/(\d+)/g)[0], 10); +} + +module.exports = { + convert: convert, + convertBody: convertBody +}; \ No newline at end of file