Skip to content

Commit

Permalink
一行中出现变量替换「#{...}」表达式即认为是输出行
Browse files Browse the repository at this point in the history
  • Loading branch information
zswang committed Jun 18, 2015
1 parent 2c3ee54 commit 071c80a
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 24 deletions.
2 changes: 1 addition & 1 deletion bower.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "jhtmls",
"version": "0.1.3",
"version": "0.1.4",
"homepage": "https://github.com/zswang/jhtmls",
"authors": "Zswang <[email protected]>",
"description": "JS and HTML alternate javascript template",
Expand Down
24 changes: 14 additions & 10 deletions jhtmls.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
(function(exportName) {
(function (exportName) {
'use strict';
var exports = exports || {};
/**
* jhtmls
* @file jhtmls
*
* 一套基于HTML和JS语法自由穿插的模板系统
* @author 王集鹄(wangjihu,http://weibo.com/zswang) 鲁亚然(luyaran,http://weibo.com/zinkey)
* @version 2014-05-21
Expand All @@ -19,13 +20,16 @@
* @param {String} text 文本
*/
function encodeHTML(text) {
return String(text).replace(/["<>& ]/g, function(all) {
return String(text).replace(/["<>& ]/g, function (all) {
return '&' + htmlEncodeDict[all] + ';';
});
}
/**
* 构造模板的处理函数
* 不是 JS 行的正则判断
* 碰见替换表达式
* 示例:title: #{title}
* 正则:/^.*#\{[^}]*\}.*$/mg
* 特殊字符开通
* 示例:&、=、:、|
* 正则:/^[ \t]*[&=:|].*$/mg
Expand All @@ -42,22 +46,22 @@
var body = [];
body.push('with(this){');
body.push(template
.replace(/<(script|style)[^>]*>[\s\S]*?<\/\1>/g, function(all) { // 处理<script>和<style>原样输出
.replace(/<(script|style)[^>]*>[\s\S]*?<\/\1>/g, function (all) { // 处理<script>和<style>原样输出
return ['!#{unescape("', escape(all), '")}'].join('');
})
.replace(/[\r\n]+/g, '\n') // 去掉多余的换行,并且去掉IE中困扰人的\r
.replace(/^\n+|\s+$/mg, '') // 去掉空行,首部空行,尾部空白
.replace(
/^([ \t]*[&=:|].*|[ \w\t_$]*([^&\^?|\n\w\/'"{}\[\]+\-():;, \t=\.$_]|:\/\/).*$|^(?!\s*(else|do|try|finally|void|typeof\s[\w$_]*)\s*$)[^'":;{}()\[\],\n|=&\/^?]+$)\s?/mg,
function(expression) { // 输出原文
/^(.*#\{[^}]*\}.*|[ \t]*[&=:|].*|[ \w\t_$]*([^&\^?|\n\w\/'"{}\[\]+\-():;, \t=\.$_]|:\/\/).*$|(?!\s*(else|do|try|finally|void|typeof\s[\w$_]*)\s*$)[^'":;{}()\[\],\n|=&\/^?]+$)\s?/mg,
function (expression) { // 输出原文
// 处理空白字符
expression = expression
.replace(/&none;/g, '') // 空字符
.replace(/["'\\]/g, '\\$&') // 处理转义符
.replace(/\n/g, '\\n') // 处理回车转义符
.replace( // #{expression} | $name
/(!?#)\{(.*?)\}|(!?\$)([a-z_]+\w*(?:\.[a-z_]+\w*)*)/g,
function(all, flag, value, flag2, value2) { // 变量替换
function (all, flag, value, flag2, value2) { // 变量替换
if (flag2) { // 匹配 $name
flag = flag2;
value = value2;
Expand Down Expand Up @@ -109,11 +113,11 @@
* @param{Object} d 数据
* @param{Object} h 辅助对象 helper
*/
var format = function(d, h) {
var format = function (d, h) {
// h = h || fn;
var output = [];
if (typeof h === 'undefined') {
h = function(d) {
h = function (d) {
fn.call(d, output, encodeHTML, h, exports);
};
}
Expand All @@ -128,7 +132,7 @@
exports.render = render;
if (typeof define === 'function') {
if (define.amd || define.cmd) {
define(function() {
define(function () {
return exports;
});
}
Expand Down
2 changes: 1 addition & 1 deletion jhtmls.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 package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "jhtmls",
"title": "jhtmls",
"description": "JS and HTML alternate javascript template",
"version": "0.1.3",
"version": "0.1.4",
"homepage": "http://jhtmls.com/",
"main": "src/jhtmls.js",
"author": {
Expand All @@ -27,7 +27,7 @@
"mocha": "2.0.1",
"should": "4.1.0",
"uglify-js": "2.4.15",
"jdists": "0.1.10",
"jdists": "0.7.7",
"jshint": "2.5.8"
},
"scripts": {
Expand Down
24 changes: 14 additions & 10 deletions src/jhtmls.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
(function(exportName) {
(function (exportName) {

'use strict';

var exports = exports || {};

/**
* jhtmls
* @file jhtmls
*
* 一套基于HTML和JS语法自由穿插的模板系统
* @author 王集鹄(wangjihu,http://weibo.com/zswang) 鲁亚然(luyaran,http://weibo.com/zinkey)
* @version 2014-05-21
Expand All @@ -24,14 +25,17 @@
* @param {String} text 文本
*/
function encodeHTML(text) {
return String(text).replace(/["<>& ]/g, function(all) {
return String(text).replace(/["<>& ]/g, function (all) {
return '&' + htmlEncodeDict[all] + ';';
});
}

/**
* 构造模板的处理函数
* 不是 JS 行的正则判断
* 碰见替换表达式
* 示例:title: #{title}
* 正则:/^.*#\{[^}]*\}.*$/mg
* 特殊字符开通
* 示例:&、=、:、|
* 正则:/^[ \t]*[&=:|].*$/mg
Expand All @@ -48,14 +52,14 @@
var body = [];
body.push('with(this){');
body.push(template
.replace(/<(script|style)[^>]*>[\s\S]*?<\/\1>/g, function(all) { // 处理<script>和<style>原样输出
.replace(/<(script|style)[^>]*>[\s\S]*?<\/\1>/g, function (all) { // 处理<script>和<style>原样输出
return ['!#{unescape("', escape(all), '")}'].join('');
})
.replace(/[\r\n]+/g, '\n') // 去掉多余的换行,并且去掉IE中困扰人的\r
.replace(/^\n+|\s+$/mg, '') // 去掉空行,首部空行,尾部空白
.replace(
/^([ \t]*[&=:|].*|[ \w\t_$]*([^&\^?|\n\w\/'"{}\[\]+\-():;, \t=\.$_]|:\/\/).*$|^(?!\s*(else|do|try|finally|void|typeof\s[\w$_]*)\s*$)[^'":;{}()\[\],\n|=&\/^?]+$)\s?/mg,
function(expression) { // 输出原文
/^(.*#\{[^}]*\}.*|[ \t]*[&=:|].*|[ \w\t_$]*([^&\^?|\n\w\/'"{}\[\]+\-():;, \t=\.$_]|:\/\/).*$|(?!\s*(else|do|try|finally|void|typeof\s[\w$_]*)\s*$)[^'":;{}()\[\],\n|=&\/^?]+$)\s?/mg,
function (expression) { // 输出原文

// 处理空白字符
expression = expression
Expand All @@ -64,7 +68,7 @@
.replace(/\n/g, '\\n') // 处理回车转义符
.replace( // #{expression} | $name
/(!?#)\{(.*?)\}|(!?\$)([a-z_]+\w*(?:\.[a-z_]+\w*)*)/g,
function(all, flag, value, flag2, value2) { // 变量替换
function (all, flag, value, flag2, value2) { // 变量替换
if (flag2) { // 匹配 $name
flag = flag2;
value = value2;
Expand Down Expand Up @@ -129,11 +133,11 @@
* @param{Object} d 数据
* @param{Object} h 辅助对象 helper
*/
var format = function(d, h) {
var format = function (d, h) {
// h = h || fn;
var output = [];
if (typeof h === 'undefined') {
h = function(d) {
h = function (d) {
fn.call(d, output, encodeHTML, h, exports);
};
}
Expand All @@ -152,7 +156,7 @@

if (typeof define === 'function') {
if (define.amd || define.cmd) {
define(function() {
define(function () {
return exports;
});
}
Expand Down
1 change: 1 addition & 0 deletions test/fixtures/base.html
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
title:
none!
|hello|
[email protected]
Expand Down
1 change: 1 addition & 0 deletions test/fixtures/base.jhtmls
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
title:#{title}
&none;none!
|hello|
a = 1;
Expand Down

0 comments on commit 071c80a

Please sign in to comment.