From eb5c9fbf5daf5e01fbf23d4928dca9abe103f6ed Mon Sep 17 00:00:00 2001 From: Luke Page Date: Fri, 28 Dec 2012 15:25:15 +0000 Subject: [PATCH] Relative url's option for node lessc --- bin/lessc | 32 ++++++++++++++++++++++++-------- lib/less/browser.js | 13 ++++++++++--- lib/less/index.js | 3 ++- lib/less/lessc_helper.js | 4 +++- lib/less/parser.js | 11 +++++++++-- test/less-test.js | 2 +- 6 files changed, 49 insertions(+), 16 deletions(-) diff --git a/bin/lessc b/bin/lessc index 2fbd87310..31a6700b7 100755 --- a/bin/lessc +++ b/bin/lessc @@ -15,7 +15,8 @@ var options = { paths: [], color: true, strictImports: false, - rootpath: '' + rootpath: '', + relativeUrls: false }; var continueProcessing = true, currentErrorcode; @@ -65,12 +66,17 @@ args = args.filter(function (arg) { options.color = false; break; case 'include-path': - options.paths = match[2].split(os.type().match(/Windows/) ? ';' : ':') - .map(function(p) { - if (p) { - return path.resolve(process.cwd(), p); - } - }); + if (!match[2]) { + sys.puts("include-path option requires a parameter"); + continueProcessing = false; + } else { + options.paths = match[2].split(os.type().match(/Windows/) ? ';' : ':') + .map(function(p) { + if (p) { + return path.resolve(process.cwd(), p); + } + }); + } break; case 'O0': options.optimization = 0; break; case 'O1': options.optimization = 1; break; @@ -80,7 +86,16 @@ args = args.filter(function (arg) { break; case 'rp': case 'rootpath': - options.rootpath = path.normalize(match[2] + '/'); + if (!match[2]) { + sys.puts("rootpath option requires a parameter"); + continueProcessing = false; + } else { + options.rootpath = path.normalize(match[2] + '/').replace('\\', '/'); + } + break; + case "ru": + case "relative-urls": + options.relativeUrls = true; break; } }); @@ -126,6 +141,7 @@ var parseLessFile = function (e, data) { optimization: options.optimization, filename: input, rootpath: options.rootpath, + relativeUrls: options.relativeUrls, strictImports: options.strictImports, dumpLineNumbers: options.dumpLineNumbers }).parse(data, function (err, tree) { diff --git a/lib/less/browser.js b/lib/less/browser.js index d60c6e9b8..9aa036025 100644 --- a/lib/less/browser.js +++ b/lib/less/browser.js @@ -211,11 +211,17 @@ function loadStyleSheet(sheet, callback, reload, remaining) { var css = cache && cache.getItem(href); var timestamp = cache && cache.getItem(href + ':timestamp'); var styles = { css: css, timestamp: timestamp }; - var rootpath = hrefParts.path; + //TODO - sheet.rootpath might be a unique path that needs using + // or it might be the url of the last sheet + // hrefParts.path is the full url path in the current sheet + // we need to take sheet.rootpath and if that is set, combine it + // somehow with the relative part of the href? + // also need to take into account sheet.relativeUrls + var rootpath = hrefParts.path; xhr(href, sheet.type, function (data, lastModified) { - // Store data this session - session_cache += data.replace(/@import .+?;/ig, ''); + // Store data this session + session_cache += data.replace(/@import .+?;/ig, ''); if (!reload && styles && lastModified && (new(Date)(lastModified).valueOf() === @@ -233,6 +239,7 @@ function loadStyleSheet(sheet, callback, reload, remaining) { mime: sheet.type, filename: href, rootpath: rootpath, + relativeUrls: sheet.relativeUrls, contents: contents, // Passing top importing parser content cache ref down. files: files, dumpLineNumbers: less.dumpLineNumbers diff --git a/lib/less/index.js b/lib/less/index.js index 56f1e4236..0efaf91ad 100644 --- a/lib/less/index.js +++ b/lib/less/index.js @@ -116,7 +116,7 @@ less.Parser.importer = function (file, paths, callback, env) { // then rootpath should become 'less/module/nav/' // - If path of imported file is '../mixins.less' and rootpath is 'less/', // then rootpath should become 'less/../' - if(!/^(?:[a-z-]+:|\/)/.test(file) && j != -1) { + if(env.relativeUrls && !/^(?:[a-z-]+:|\/)/.test(file) && j != -1) { rootpath = rootpath + file.slice(0, j+1); // append (sub|sup) directory path of imported file } @@ -127,6 +127,7 @@ less.Parser.importer = function (file, paths, callback, env) { contents: env.contents, files: env.files, syncImport: env.syncImport, + relativeUrls: env.relativeUrls, rootpath: rootpath, dumpLineNumbers: env.dumpLineNumbers }).parse(data, function (e, root) { diff --git a/lib/less/lessc_helper.js b/lib/less/lessc_helper.js index 8d98404ab..1a82ef9a3 100644 --- a/lib/less/lessc_helper.js +++ b/lib/less/lessc_helper.js @@ -23,7 +23,7 @@ var lessc_helper = { //Print command line options printUsage: function() { - sys.puts("usage: lessc [options] [destination]"); + sys.puts("usage: lessc [option option=parameter ...] [destination]"); sys.puts(""); sys.puts("If source is set to `-' (dash or hyphen-minus), input is read from stdin."); sys.puts(""); @@ -48,6 +48,8 @@ var lessc_helper = { sys.puts(" media query which is compatible with the SASS"); sys.puts(" format, and 'all' which will do both."); sys.puts(" -rp, --rootpath Set rootpath for url rewriting in relative imports and urls."); + sys.puts(" Works with or withour the relative-urls option."); + sys.puts(" -ru, --relative-urls re-write relative urls to the base less file."); sys.puts(""); sys.puts("Report bugs to: http://github.com/cloudhead/less.js/issues"); sys.puts("Home page: "); diff --git a/lib/less/parser.js b/lib/less/parser.js index dc745f713..768376552 100644 --- a/lib/less/parser.js +++ b/lib/less/parser.js @@ -1500,8 +1500,15 @@ if (less.mode === 'browser' || less.mode === 'rhino') { // We pass `true` as 3rd argument, to force the reload of the import. // This is so we can get the syntax tree as opposed to just the CSS output, // as we need this to evaluate the current stylesheet. - // __ Now using the hack of passing a ref to top parser's content cache in the 1st arg. __ - loadStyleSheet({ href: path, title: path, type: env.mime, contents: env.contents, files: env.files, rootpath: env.rootpath }, function (e, root, data, sheet, _, path) { + loadStyleSheet({ + href: path, + title: path, + type: env.mime, + contents: env.contents, + files: env.files, + rootpath: env.rootpath, + relativeUrls: env.relativeUrls }, + function (e, root, data, sheet, _, path) { if (e && typeof(env.errback) === "function") { env.errback.call(null, path, paths, callback, env); } else { diff --git a/test/less-test.js b/test/less-test.js index 47abc12cc..29ae7d8a9 100644 --- a/test/less-test.js +++ b/test/less-test.js @@ -23,7 +23,7 @@ less.tree.functions._color = function (str) { sys.puts("\n" + stylize("LESS", 'underline') + "\n"); -runTestSet(); +runTestSet({relativeUrls: true}); runTestSet(null, "errors/", function(name, err, compiledLess, doReplacements) { fs.readFile(path.join('test/less/', name) + '.txt', 'utf8', function (e, expectedErr) {