Skip to content

Commit

Permalink
Tweaks to get PSH loaded via NPM
Browse files Browse the repository at this point in the history
  • Loading branch information
bensyverson committed Jul 10, 2015
1 parent d21fc29 commit f4413bf
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 14 deletions.
9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ Put Stuff Here will only expand a phrase if it’s in one contiguous line. The l
Put Stuff Here can be used in Node, the browser, or via Browserify.

```javascript
var psh = require('./putstuffhere.js').shared();
var psh = require('putstuffhere').shared();

// Fetch template.html from this directory.
psh.getTemplateFunction('template.html', function(err, func) {
Expand All @@ -103,7 +103,7 @@ psh.getTemplateFunction('template.html', function(err, func) {


```javascript
var psh = require('./putstuffhere.js');
var psh = require('putstuffhere');

// Compile a string literal directly
var func = psh.compileText('<p>Put title here</p>');
Expand All @@ -113,8 +113,9 @@ console.log( func(locals) );
```

```html
<script src="queue.js" type="text/javascript" charset="utf-8"></script>
<script src="putstuffhere.js" type="text/javascript" charset="utf-8"></script>
<script src="lib/ajax.js" type="text/javascript" charset="utf-8"></script>
<script src="lib/queue.js" type="text/javascript" charset="utf-8"></script>
<script src="lib/putstuffhere.js" type="text/javascript" charset="utf-8"></script>
<script type="text/javascript">
var psh = PutStuffHere.shared();
var func = psh.compileText('<p>Put title here</p>');
Expand Down
2 changes: 2 additions & 0 deletions generatedocs.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#!/bin/sh

# This script uses autodoc and docco to generate documentation in two flavors

autodoc -t --javascripts lib/ajax.js,lib/queue.js lib/putstuffhere.js
autodoc --template autodoc.html.mustache --javascripts lib/ajax.js,lib/queue.js lib/putstuffhere.js
docco --output annotated/ lib/*.js
5 changes: 3 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,14 @@

if (typeof(require) === typeof(undefined)) window.require = function(){return null;};
var PutStuffHerePrivate = PutStuffHerePrivate || require('./lib/putstuffhere.js');
var OrgStuffHereQueue = OrgStuffHereQueue || require('./lib/queue.js');

if (typeof(module) === typeof(undefined)) window.module = {};

var _sharedPutStuffHereInstance = new PutStuffHerePrivate();

var PutStuffHere = {
'shared': function(){ return _sharedPutStuffHereInstance; }
'shared': function(){ return _sharedPutStuffHereInstance; },
'queue': OrgStuffHereQueue,
};


Expand Down
21 changes: 14 additions & 7 deletions lib/putstuffhere.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,11 @@ if (typeof(require) === typeof(undefined)) window.require = function(){return n
var println = println || function(arg) { console.log(arg); };

var isBrowser = (typeof window !== 'undefined');
var fs = require('fs');
var fs = isBrowser ? null : require('fs');

var OrgStuffHereQueue = OrgStuffHereQueue || require('./queue.js');
var PutStuffHereAjax = PutStuffHereAjax || require('./ajax.js');

// var orgstuffhereNull = '___•••NULL•••___';


// Test

/**
* Extend String with an escape function
* @method orgstuffhereEscape
Expand Down Expand Up @@ -83,11 +78,13 @@ var PutStuffHerePrivate = function() {

this.shouldExtractBody = true;

// The main regex. This can and should be extended with other syntaxes.
var regex = /([\s\W]|^)(?:(?:put|insert)\s+(.+?\S)(?:\s*\(([^)]+)\))?\s+here)([\W\s]|$)/gi;
var cache = {};

var rendered = '';


/**
* Wrap variables
* @private
Expand All @@ -114,6 +111,14 @@ var PutStuffHerePrivate = function() {
return '' + p1 + "\" + (typeof ctx." + p2 + " !== typeof(undefined) ? ctx." + varName + " : '') + \"" + p4;
};

var _templateRoot = '';
this.setTemplateRoot = function(newValue) {
_templateRoot = newValue;
};

this.templateRoot = function() {
return _templateRoot;
};


this.rendered = function() {
Expand Down Expand Up @@ -273,6 +278,7 @@ PutStuffHerePrivate.prototype.setDefaultHTML = function(aString) {
self.html[self.orgstuffhereNull] = aString;
}


PutStuffHerePrivate.prototype.extractBody = function(html) {
var self = this;
if (html.match(/<body[^>]*>/i)) {
Expand Down Expand Up @@ -384,8 +390,9 @@ PutStuffHerePrivate.prototype.readHTML = function(src, isMock) {
});
}
} else if (typeof fs !== typeof(undefined)) {
var path = require('path');
// Or in Node.js, or any context capable of loading fs
fs.readFile(__dirname + '/' + src, function(err, data){
fs.readFile(path.resolve(self.templateRoot(), src), function(err, data){
self.html[src] = '';
if (err) {
println("Warning: " + src);
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "putstuffhere",
"version": "0.9.0",
"version": "0.9.2",
"description": "Put Stuff Here is a minimalist, human-first templating system.",
"main": "index.js",
"scripts": {
Expand Down

0 comments on commit f4413bf

Please sign in to comment.