├── .gitignore ├── .npmignore ├── History.md ├── test ├── tip.html └── index.js ├── Makefile ├── index.js ├── package.json └── Readme.md /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | -------------------------------------------------------------------------------- /.npmignore: -------------------------------------------------------------------------------- 1 | support 2 | test 3 | examples 4 | *.sock 5 | -------------------------------------------------------------------------------- /History.md: -------------------------------------------------------------------------------- 1 | 2 | 0.0.1 / 2010-01-03 3 | ================== 4 | 5 | * Initial release 6 | -------------------------------------------------------------------------------- /test/tip.html: -------------------------------------------------------------------------------- 1 |
2 |
'Message here'
3 |
-------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | 2 | test: 3 | @./node_modules/.bin/mocha \ 4 | --require should \ 5 | --reporter spec 6 | 7 | .PHONY: test -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | 2 | /** 3 | * Convert `str` to a module.exports string. 4 | * 5 | * @param {String} str 6 | * @return {String} 7 | * @api public 8 | */ 9 | 10 | module.exports = function(str){ 11 | return "module.exports = '" 12 | + str 13 | .replace(/'/g, "\\'") 14 | .replace(/\r\n|\r|\n/g, "\\n") 15 | + "';"; 16 | }; -------------------------------------------------------------------------------- /test/index.js: -------------------------------------------------------------------------------- 1 | 2 | /** 3 | * Module dependencies. 4 | */ 5 | 6 | var fs = require('fs') 7 | , str2js = require('..') 8 | , read = fs.readFileSync 9 | , vm = require('vm') 10 | 11 | describe('str2js(str)', function(){ 12 | it('should return js', function(){ 13 | var html = read('test/tip.html', 'utf8'); 14 | var js = str2js(html); 15 | var mod = { exports: {} }; 16 | vm.runInNewContext(js, { module: mod }); 17 | mod.exports.should.equal(html); 18 | }) 19 | }) -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "string-to-js", 3 | "version": "0.0.1", 4 | "description": "Make plain text (HTML, CSS, JSON, etc) require()-able", 5 | "keywords": ["html", "css", "json", "require"], 6 | "author": "TJ Holowaychuk ", 7 | "dependencies": {}, 8 | "devDependencies": { 9 | "mocha": "*", 10 | "should": "*" 11 | }, 12 | "main": "index", 13 | "repository": { 14 | "type" : "git", 15 | "url" : "http://github.com/visionmedia/node-string-to-js.git" 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /Readme.md: -------------------------------------------------------------------------------- 1 | 2 | # string-to-js 3 | 4 | Make plain text (HTML, CSS, JSON, etc) require()-able. 5 | 6 | ## Installation 7 | 8 | $ npm install string-to-js 9 | 10 | ## Example 11 | 12 | tip.html: 13 | 14 | ```html 15 |
16 |
17 |
18 | ``` 19 | 20 | js: 21 | 22 | ```js 23 | 24 | /** 25 | * Module dependencies. 26 | */ 27 | 28 | var fs = require('fs') 29 | , str2js = require('string-to-js') 30 | , read = fs.readFileSync; 31 | 32 | var html = read('tip.html', 'utf8'); 33 | var js = str2js(html); 34 | console.log(js); 35 | ``` 36 | 37 | output js string: 38 | 39 | ```js 40 | module.exports = '
\n
\'Message here\'
\n
'; 41 | ``` 42 | 43 | ## License 44 | 45 | (The MIT License) 46 | 47 | Copyright (c) 2012 TJ Holowaychuk <tj@vision-media.ca> 48 | 49 | Permission is hereby granted, free of charge, to any person obtaining 50 | a copy of this software and associated documentation files (the 51 | 'Software'), to deal in the Software without restriction, including 52 | without limitation the rights to use, copy, modify, merge, publish, 53 | distribute, sublicense, and/or sell copies of the Software, and to 54 | permit persons to whom the Software is furnished to do so, subject to 55 | the following conditions: 56 | 57 | The above copyright notice and this permission notice shall be 58 | included in all copies or substantial portions of the Software. 59 | 60 | THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, 61 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 62 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 63 | IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 64 | CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 65 | TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 66 | SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. --------------------------------------------------------------------------------