Skip to content

Commit

Permalink
Readme and options
Browse files Browse the repository at this point in the history
  • Loading branch information
lukeapage committed Feb 18, 2015
1 parent fd1cf38 commit 259adba
Show file tree
Hide file tree
Showing 6 changed files with 124 additions and 10 deletions.
37 changes: 36 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,39 @@
less-plugin-rtl
===============

TODO
Reverses less from ltr to rtl

```less
.reverse {
float: left;
margin-left: 5px;
margin: 1px 2px 3px 4px;
& when (@rtl) {
color: green;
}
}
```

Becomes...

```css
.reverse {
float: right;
margin-right: 5px;
margin: 1px 4px 3px 2px;
color: green;
}
```

To use with lessc

```bash
$ npm install -g less-plugin-rtl
$ lessc --rtl file.less out.css
```

and to run in LTR mode..

```bash
$ lessc --rtl="dir=LTR" file.less out.css
```
39 changes: 34 additions & 5 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,39 @@
var getRTLPlugin = require("./rtl-plugin"),
RTLVariablePlugin = require("./rtl-variable-plugin");

module.exports = {
function LessPluginRTL(options) {
this.setOptions(options);
}

LessPluginRTL.prototype = {
install: function(less, pluginManager) {
var RTLPlugin = getRTLPlugin(less);
pluginManager.addVisitor(new RTLPlugin());
pluginManager.addPreProcessor(new RTLVariablePlugin());
}
if (this.options.dir === "RTL") {
var RTLPlugin = getRTLPlugin(less);
pluginManager.addVisitor(new RTLPlugin());
}
pluginManager.addPreProcessor(new RTLVariablePlugin(this.options.dir === "RTL"));
},
printUsage: function () {
console.log("RTL Plugin");
console.log("use dir=RTL or dir=LTR")
},
setOptions: function(options) {
this.options = this.parseOptions(options);
},
parseOptions: function(options) {
if (!options) {
options = {dir: "RTL"};
}
if (typeof options === "string") {
var args = options.match(/dir=(RTL|LTR)/);
if (!args) {
throw new Error("Invalid options for ");
}
options = {dir: args[1]};
}
return options;
},
minVersion: [2, 4, 0]
};

module.exports = LessPluginRTL
5 changes: 3 additions & 2 deletions lib/rtl-variable-plugin.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
function RTLVariablePlugin() {
function RTLVariablePlugin(isRTL) {
this.isRTL = Boolean(isRTL);
}

RTLVariablePlugin.prototype.process = function(src, extra) {
var variable = "@rtl: true; @ltr: false;\n"
var variable = "@rtl: " + String(this.isRTL) + "; @ltr: " + String(!this.isRTL) + ";\n";
var contentsIgnoredChars = extra.imports.contentsIgnoredChars;
var filename = extra.fileInfo.filename;
contentsIgnoredChars[filename] = contentsIgnoredChars[filename] || 0;
Expand Down
16 changes: 16 additions & 0 deletions test/css/ltr/test.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
.reverse {
float: left;
float: right /*comment*/;
margin-left: 5px;
padding-right: 3px;
font-size: 12px;
/* unchanged*/
left: 3px;
border-left-style: dashed;
}
.shorthands {
margin: 5px;
margin: 6px 7px;
margin: 1px 7px 3px;
margin: 1px 2px 3px 4px;
}
9 changes: 7 additions & 2 deletions test/index.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
var less = require("less"),
lessTest = require("less/test/less-test"),
lessTester = lessTest(),
plugin = require('../lib'),
Plugin = require('../lib'),
rtlPlugin = new Plugin(),
ltrPlugin = new Plugin("dir=LTR"),
stylize = less.lesscHelper.stylize;

console.log("\n" + stylize("LESS - RTL", 'underline') + "\n");

lessTester.runTestSet(
{strictMath: true, relativeUrls: true, silent: true, plugins: [plugin] },
{strictMath: true, relativeUrls: true, silent: true, plugins: [rtlPlugin] },
"rtl/");
lessTester.runTestSet(
{strictMath: true, relativeUrls: true, silent: true, plugins: [ltrPlugin] },
"ltr/")

if (lessTester.finish) {
lessTester.finish();
Expand Down
28 changes: 28 additions & 0 deletions test/less/ltr/test.less
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
.reverse {
float: left;
float: right/*comment*/;
margin-left: 5px;
padding-right: 3px;
font-size: 12px; /* unchanged*/
left: 3px;
border-left-style: dashed;
}
.shorthands {
@top: 1px;
@right: 2px;
@bottom: 3px;
@left: 4px;
@all: 5px;
@vertical: 6px;
@horizontal: 7px;

margin: @all;
margin: @vertical @horizontal;
margin: @top @horizontal @bottom;
margin: @top @right @bottom @left;
}
& when (@rtl) {
.rtl-only {
margin: 3px;
}
}

0 comments on commit 259adba

Please sign in to comment.