Skip to content

Commit

Permalink
refactor: add include tag plugin and parseFilters options
Browse files Browse the repository at this point in the history
  • Loading branch information
ipluser committed May 16, 2016
1 parent c888f48 commit ec25375
Show file tree
Hide file tree
Showing 10 changed files with 191 additions and 12 deletions.
4 changes: 2 additions & 2 deletions bower.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "magicbook",
"version": "0.7.1",
"version": "0.8.0",
"description": "An lightweight and scalable docs system for markdown, text or other.",
"homepage": "http://ipluser.github.io/magicbook/",
"keywords": [
Expand All @@ -16,4 +16,4 @@
"authors": [
"Plus <[email protected]>"
]
}
}
2 changes: 1 addition & 1 deletion dist/magicbook.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@
<script>
var book = Magicbook.markdown('awesome', {
homeUrl: 'public/doc/quick-start.md',
urlArgs: 'ver=0.7.1'
urlArgs: 'ver=0.8.0'
});

book.agilities();
book.show();
</script>
</body>
</html>
</html>
38 changes: 33 additions & 5 deletions lib/magicbook.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
baseUrl: '',
urlArgs: '',
debug: false,
parseFilters: [],
navigatorCallbackQueue: [],
routeCallbackQueue: []
};
Expand Down Expand Up @@ -193,8 +194,8 @@

requestGet(navigatorUrl, {
success: function drawNavigatorSuccess(data) {
var result = self.parser(data);
self.$navigator && self.$navigator.empty().append(self.parser(data));
var result = self.parse(data);
self.$navigator && self.$navigator.empty().append(result);

executeCallbackQueue(defaultCallbackQueue, 'success', self, [{
origin: data,
Expand All @@ -214,6 +215,7 @@
});
}

config.parseFilters = parseArray(config.parseFilters);
config.navigatorCallbackQueue = parseArray(config.navigatorCallbackQueue);
config.routeCallbackQueue = parseArray(config.routeCallbackQueue);

Expand Down Expand Up @@ -252,8 +254,34 @@

var proto = Magicbook.potion = Magicbook.prototype;

proto.parser = function parser(data) {
return data;
proto.parser = function parser(source) {
return source;
};

proto.parse = function parse(source) {
var self = this;
var filters = self.config.parseFilters;
var len = filters.length;
var index;

var data;
for (index = 0; index < len; index++) {
var filter = filters[index];
var beforeParse = filter.before;
var afterParse = filter.after;

if (isFunction(beforeParse)) {
data = beforeParse.call(self, source);
}

data = self.parser(data);

if (isFunction(filter.after)) {
data = afterParse.call(self, data);
}
}

return data && data || self.parser(source);
};

proto.route = function route(url, callback) {
Expand All @@ -275,7 +303,7 @@

requestGet(url, {
success: function renderCallbackSuccess(data) {
var result = self.parser(data);
var result = self.parse(data);
self.$content && self.$content.empty().append(result);

executeCallbackQueue(defaultCallbackQueue, 'success', self, [{
Expand Down
4 changes: 3 additions & 1 deletion navigator.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ License: [MIT](https://github.com/ipluser/magicbook/blob/gh-pages/LICENSE)
- [agilities](#public/doc/plugins/components/css/agilities.md)
- js
- [agilities](#public/doc/plugins/components/js/agilities.md)
- js
- [magicbook-plugin-include](#public/doc/plugins/components/js/magicbook-plugin-include.md)
- markdown
- css
- [awesome](#public/doc/plugins/markdown/css/awesome.md)
Expand All @@ -31,4 +33,4 @@ License: [MIT](https://github.com/ipluser/magicbook/blob/gh-pages/LICENSE)
- [Commit Message Format](https://github.com/angular/angular.js/blob/master/CONTRIBUTING.md#commit-message-format)

### Friend Links
- [speechless](http://ipluser.github.io/speechless/)
- [speechless](http://ipluser.github.io/speechless/)
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "magicbookjs",
"version": "0.7.1",
"version": "0.8.0",
"description": "An lightweight and scalable docs system for markdown, text or other.",
"author": "ipluser",
"main": "dist/magicbook.js",
Expand Down
80 changes: 80 additions & 0 deletions plugins/components/js/magicbook-plugin-include.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
;(function (global, $, Magicbook) { // eslint-disable-line

var TAG_NAME = 'include';
var defaults = {
openTag: '{%',
closeTag: '%}'
};

function tempate(cfg) {
var options = $.extend({}, defaults, cfg);
var openTag = options.openTag;
var closeTag = options.closeTag;

function parse(chunk) {
var tokens = chunk.trim().replace(/\s+/g, ' ').split(' ');
var tag = tokens[0];
var len = tokens.length;

if (len <= 1 || tag !== TAG_NAME) {
return openTag + chunk + closeTag; //
}

var content;
var url = tokens[1];
var _url = url.length > 2 ? url.substring(1, url.length - 1) : url;
_url = _url.search(/\:\/\//) !== -1 ? _url : this.relativeCurrentUrl(_url);

$.ajax({
url: _url,
async: false,
success: function templateIncludeTagSuccess(data) {
content = data;
},
error: function templateIncludeTagError() {
content = [TAG_NAME, ': \'', url, '\' not found'].join(' ');
}
});

return content;
}

return function render(source) {
var blocks = (source || '').split(openTag);
var len = blocks.length;
var index;

if (len <= 1) {
return source;
}

var out = '';
for (index = 0; index < len; index++) {
var block = blocks[index];
var chunks = block.split(closeTag);

if (chunks.length === 1) {
out += chunks[0];
continue;
}

var content = parse.call(this, chunks[0]);

out += content;
out += chunks[1];
}

return out;
};
}

Magicbook.potion.enableIncludeTag = function enableIncludeTag(cfg) {
var self = this;

self.config.parseFilters.push({
before: tempate(cfg)
});

self.templateIncludeTag = tempate(cfg);
};
})(window, jQuery, Magicbook); // eslint-disable-line
1 change: 1 addition & 0 deletions plugins/components/js/magicbook-plugin-include.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 18 additions & 0 deletions public/doc/options.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,24 @@ A path of navigator.
##### default `navigator.md`


### parseFilters
`parseFilters ` to execute before/after parser that consist of a series of `Filter` object. `Filter` object has `before` and `after`function properties. Example below:

```js
// It will be transformed into Array if it is a Filter object
parseFilters: [{
before: function (source) {
// todo
},
after: function (source) {
// todo
}
}]
```

##### default `[]`


### routeCallbackQueue
`routeCallbackQueue ` to execute after navigator have been rendered that consist of a series of `Callback` object. `Callback` object has `success`, `fail` and `finally` function properties. Example below:

Expand Down
50 changes: 50 additions & 0 deletions public/doc/plugins/components/js/magicbook-plugin-include.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
## magicbook-plugin-include.js
The **magicbook-plugin-include** supports `include` template tag in document.

### usage
#### Import Dependencies
To use magicbook-plugin-include, you’ll need to make sure `magicbook` are included.
```html
<script src="plugins/components/js/magicbook-plugin-include.min.js"></script>
```

#### To use include tag
```markup
{% include './test.md' %}
```

#### Initialize
```js
{magicbook instance}.enableIncludeTag({
openTag: '{%',
closeTag: '%}'
});

// or

{magicbook instance}.enableIncludeTag();
```

### configuration
#### openTag
Open controls for include tag.

##### default
`{%`


#### closeTag
Close controls for include tag.

##### default
`%}`


### methods
#### templateIncludeTag
Parse include tag.

##### parameters
| name | description |
|-----------|------------------|
| source | document content |

0 comments on commit ec25375

Please sign in to comment.