GithubHelp home page GithubHelp logo

csstransform's Introduction

#csstransform

Automated css transformations.

Build Status NPM version

#Installation

npm install csstransform --save-dev

#Example Usage csstransform can be used to transform existing css and transform for example it's selectors.

var css = __dirname + '/input/bootstrap.css';
var target = __dirname + '/output/bootstrap-match-string.css';

var csstransform = require('../index');
var csst = csstransform(css);
csst.transformSelectorText({
  prepend: '.bootstrap-admin',
  exclude: /(.modal-backdrop|.fade)/g
});
csst.toString(target);

We use it for post processing the compiled bootstrap.css files. The transformation is done like this:

var csstransform = require("csstransform");
var csst = csstransform(src);
csst.transformSelectorText({
  prepend: '.bootstrap-admin',
  exclude: /(.modal-backdrop|.fade)/g
}).transformSelectorText({
  replace: ['.bootstrap-admin body', 'body .bootstrap-admin']
}).transformSelectorText({
  replace: ['.bootstrap-admin html', 'html .bootstrap-admin']
});
csst.toString(target);

With this you can easily create a Grunt task like this:

module.exports = function(grunt) {
  grunt.initConfig({
    less: {
      all: {
        files: {
          "public/css/application.css": "app/styles/application.less",
          "public/css/responsive.css": "app/styles/responsive.less",
          "public/css/admin/bootstrap.css": "app/styles/admin/bootstrap.less",
          "public/css/admin/responsive.css": "app/styles/admin/responsive.less"
        }
      }
    },
    csstransform: {
      all: {
        files: {
          "public/css/admin/bootstrap-admin.css": "public/css/admin/bootstrap.css",
          "public/css/admin/responsive-admin.css": "public/css/admin/responsive.css"
        }
      }
    }
  });

  grunt.loadNpmTasks("grunt-contrib-less");

  grunt.registerMultiTask('csstransform', 'Admin Bootstrap build post processing...', function() {
    //grunt.log.writeln(this.target);
    if (!this.data || !this.data.files) return;

    var files = this.data.files;
    for (target in files) {
      var src = files[target];
      var csstransform = require("csstransform");
      var csst = csstransform(src);
      csst.transformSelectorText({
        prepend: '.bootstrap-admin',
        exclude: /(.modal-backdrop|.fade)/g
      }).transformSelectorText({
        replace: ['.bootstrap-admin body', 'body .bootstrap-admin']
      }).transformSelectorText({
        replace: ['.bootstrap-admin html', 'html .bootstrap-admin']
      });
      csst.toString(target);
      grunt.log.writeln("File " + src + " -> " + target + " created.");
    }
  });

  grunt.registerTask("default", ["less", "csstransform"]);
};

#Usage for twitter bootstrap Note: The Gruntfile above is how we post process twitter bootstrap in order to allow different bootstrap builds on the same page. Just wrap the elements that use the custom bootstrap build in a div like this:

<div class="bootstrap-admin">
  ... inline editing that uses the post processed twitter bootstrap build.
</div>

With this, the custom bootstrap does not interfere with your custom styles. The custom bootstrap styles are only applied within the elements inside of the elements with the class bootstrap-admin.

#How it works csstransform uses cssom (a full fledged css parser) to parse the css into an internal css dom. When calling toString(target), the css is formatted with cssbeautify.

#API

##csstransform(css) Creates a new CSST Object and parses the css file into a cssom Object.

Parameters css: String, css Path to the css file to read

Returns Object, the new created CCST Object that holds the parsed css dom for further transformations.

##transformSelectorText(op) Transforms the css selectors. transformSelectorText is chainable. Therefore several Transformation can take place.

Parameters op: Object, {[match,] [exclude,] [append,] [prepend,] [replace]}

Returns Object, this CSST object for function chanining.

op Object

{	
	prepend: String,
	append: String,
	replace: [String, String],
	match: String or Regex,
	exclude: String or Regex
}

Example

csst.transformSelectorText({
	prepend: '.bootstrap-admin',
	exclude: /(.modal-backdrop|.fade)/g
}).transformSelectorText({
	replace: ['.bootstrap-admin ', ''],
	match: ".tooltip"
});

op Object Description ####prepend prepend will prepend the given string to the selector.

####append append will append the given string to the selector.

####replace replace is an Array where the first element is the String that should be replaced and the second element is the String that will be used as replacement.

####match If match is missing, it matches every selector. If match is provided with a String value, it will be used as contains(). If match is a Regex, it will directly be used in the .match(regex) function.

####exclude Opposite of match. The found selectors will be excluded from the transfomration.

##toString(target) writes the the formatted css to the target file.

Parameters target: String, Path to the target file.

Returns String, The formatted css.

#Tests Run tests with mocha:

mocha

csstransform's People

Contributors

andineck avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

isabella232

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.