Просмотр исходного кода

added ua-parser free version to allow using custom parsers

Signed-off-by: Gerald <gera2ld@163.com>
Gerald 10 лет назад
Родитель
Сommit
a8c22678a6

+ 13 - 2
README.md

@@ -15,6 +15,15 @@
 $ bower install duoshuo-ua
 ```
 
+dist中各文件的说明:
+
+1. **duoshuo-ua-core.min.js**是不集成ua-parser和CSS精简版,便于自行扩展。
+1. **duoshuo-ua-parser.min.js**集成轻量级的ua-parser,但是不集成CSS。
+1. **duoshuo-ua-parser-css.min.js**集成轻量级的ua-parser和CSS,提供默认的显示方案。
+1. **duoshuo-ua.min.css**为默认的样式文件。
+
+默认将使用**duoshuo-ua-parser.min.js**和**duoshuo-ua.min.css**。
+
 文档
 ---
 duoshuoQuery中可以定义`myIds`和`getUAString`函数:
@@ -36,7 +45,7 @@ duoshuoQuery中可以定义`myIds`和`getUAString`函数:
 
 <!-- 第1部分:在embed.js之前定义duoshuoQuery和加载duoshuo-ua
 两者顺序可以交换-->
-<script src="dist/duoshuo-ua-with-css.min.js"></script>
+<script src="dist/duoshuo-ua-parser-css.min.js"></script>
 <script>var duoshuoQuery={short_name:'test',myIds:[1234567]};</script>
 
 <!-- 第2部分:可写成异步加载,必须写在第1部分后面 -->
@@ -53,7 +62,9 @@ duoshuoQuery中可以定义`myIds`和`getUAString`函数:
 </html>
 ```
 
-更高级的例子见examples。[点此](http://gerald.top/code/duoshuo-ua)查看更多说明。
+更高级的例子见examples。
+
+更多介绍:<http://gerald.top/code/duoshuo-ua>
 
 案例
 ---

+ 2 - 2
bower.json

@@ -1,12 +1,12 @@
 {
   "name": "duoshuo-ua",
-  "version": "1.0.3",
+  "version": "1.0.4",
   "authors": [
     "Gerald <gera2ld@163.com>"
   ],
   "description": "Plugin of duoshuo to show operating systems and browsers for duoshuo comments.",
   "main": [
-	  "dist/duoshuo-ua.min.js",
+	  "dist/duoshuo-ua-parser.min.js",
 	  "dist/duoshuo-ua.min.css"
   ],
   "keywords": [

Разница между файлами не показана из-за своего большого размера
+ 8 - 0
dist/duoshuo-ua-core.min.js


Разница между файлами не показана из-за своего большого размера
+ 8 - 0
dist/duoshuo-ua-parser-css.min.js


Разница между файлами не показана из-за своего большого размера
+ 8 - 0
dist/duoshuo-ua-parser.min.js


Разница между файлами не показана из-за своего большого размера
+ 0 - 1
dist/duoshuo-ua-with-css.min.js


Разница между файлами не показана из-за своего большого размера
+ 0 - 1
dist/duoshuo-ua.min.js


+ 3 - 2
examples/example.html

@@ -9,8 +9,9 @@
 				myIds:[1234567],
 			};
 		</script>
-		<script src=../dist/duoshuo-ua-with-css.min.js></script>
-		<script src=http://static.duoshuo.com/embed.js></script>
+		<!-- 使用集成ua-parser和默认CSS的版本 -->
+		<script src="../dist/duoshuo-ua-parser-css.min.js"></script>
+		<script src="http://static.duoshuo.com/embed.js"></script>
 	</head>
 	<body>
 		<div class=ds-thread data-thread-key="5174f0a16a04594d4800590e"></div>

+ 4 - 2
examples/faisalman.html

@@ -30,8 +30,10 @@
 				},
 			};
 		</script>
-		<script src=../dist/duoshuo-ua-with-css.min.js></script>
-		<script src=http://static.duoshuo.com/embed.unstable.js></script>
+		<!-- 选用不集成ua-parser和CSS的版本,并引入默认CSS -->
+		<link rel="stylesheet" href="../dist/duoshuo-ua.min.css">
+		<script src="../dist/duoshuo-ua-core.min.js"></script>
+		<script src="http://static.duoshuo.com/embed.js"></script>
 	</head>
 	<body>
 		<div class=ds-thread data-thread-key="5174f0a16a04594d4800590e"></div>

+ 15 - 2
examples/zsxsoft.html

@@ -26,10 +26,23 @@
 		},
 	};
 	</script>
-	<script src="../dist/duoshuo-ua-with-css.min.js"></script>
+	<!-- 选用不集成ua-parser的版本 -->
+	<script src="../dist/duoshuo-ua-core.min.js"></script>
+	<!-- 自定义样式 -->
+	<style>
+		#ds-reset .ds-os,
+		#ds-reset .ds-br,
+		#ds-reset .ds-webmaster {
+			display: inline-block;
+			border-radius: .3em;
+			margin-left: .5em;
+			padding: .3em;
+			border: 1px solid gray;
+		}
+	</style>
 	<script src="http://static.duoshuo.com/embed.js"></script>
 </head>
 <body>
 	<div class="ds-thread" data-thread-key="5174f0a16a04594d4800590e"></div>
 </body>
-</html>
+</html>

+ 65 - 11
gulpfile.js

@@ -6,22 +6,76 @@ var merge = require('gulp-merge');
 var uglify = require('gulp-uglify');
 var minifycss = require('gulp-minify-css');
 var css2js = require('gulp-css2js');
+var header = require('gulp-header');
+var clone = require('gulp-clone');
+var order = require('gulp-order');
+var pkg = require('./package.json');
+var func_wrap = '!function () {\n<%=contents%>\n}.call({});';
+var banner = [
+	'/**',
+	' * <%= pkg.title %> - <%= subtitle %>',
+	' * <%= description %>',
+	' * @version v<%= pkg.version %>',
+	' * @license <%= pkg.license %>',
+	' * @author <%= pkg.author %>',
+	' */',
+	'',
+].join('\n');
 
-gulp.task('default', function () {
-	var js = gulp.src(['./src/ua-parser.js', './src/duoshuo-ua.js'])
-		.pipe(concat('duoshuo-ua.js'))
-		.pipe(wrap('(function(){\n<%=contents%>\n}).call({});'))
+gulp.task('build-core', function () {
+	return gulp.src(['./src/core.js'])
+		.pipe(wrap(func_wrap))
 		.pipe(uglify())
-		.pipe(rename({suffix: '.min'}))
+		.pipe(header(banner, {
+			pkg: pkg,
+			subtitle: '精简版',
+			description: '此版本不集成ua-parser,需要自行设置getUAString',
+		}))
+		.pipe(rename('duoshuo-ua-core.min.js'))
 		.pipe(gulp.dest('./dist'));
-	var css = gulp.src('./src/duoshuo-ua.css')
-		.pipe(minifycss())
+});
+
+var embed_js, embed_css;
+
+gulp.task('build-with-ua-parser', function () {
+	var stream = gulp.src('./src/*.js')
+		.pipe(order([
+			'!**/default.js',
+		]))
+		.pipe(concat('duoshuo-ua-parser.js'))
+		.pipe(wrap(func_wrap))
+		.pipe(uglify())
+		.pipe(header(banner, {
+			pkg: pkg,
+			subtitle: '集成版',
+			description: '此版本集成轻量级的ua-parser但不集成CSS,提供默认的UA显示方案',
+		}));
+	embed_js = stream.pipe(clone());
+	return stream
 		.pipe(rename({suffix: '.min'}))
-		.pipe(gulp.dest('./dist'))
-		.pipe(css2js());
-	return merge(js, css)
-		.pipe(concat('duoshuo-ua-with-css.js'))
+		.pipe(gulp.dest('./dist'));
+});
+
+gulp.task('build-css', function () {
+	var stream = gulp.src('./src/*.css')
+		.pipe(concat('duoshuo-ua.min.css'))
+		.pipe(minifycss());
+	embed_css = stream.pipe(clone());
+	return stream
+		.pipe(gulp.dest('./dist'));
+});
+
+gulp.task('build-with-ua-parser-css', ['build-with-ua-parser', 'build-css'], function () {
+	return merge(embed_js, embed_css.pipe(css2js()))
+		.pipe(concat('duoshuo-ua-parser-css.js'))
 		.pipe(uglify())
+		.pipe(header(banner, {
+			pkg: pkg,
+			subtitle: 'CSS集成版',
+			description: '此版本集成ua-parser和默认CSS,提供默认的UA显示方案',
+		}))
 		.pipe(rename({suffix: '.min'}))
 		.pipe(gulp.dest('./dist'));
 });
+
+gulp.task('default', ['build-core', 'build-with-ua-parser-css']);

+ 5 - 1
package.json

@@ -1,9 +1,10 @@
 {
   "name": "duoshuo-ua",
-  "version": "1.0.3",
+  "version": "1.0.4",
   "title": "Duoshuo UA Plugin",
   "description": "Plugin of duoshuo to show operating systems and browsers for duoshuo comments.",
   "author": "Gerald <gera2ld@163.com>",
+  "license": "MIT",
   "repository": {
     "type": "git",
     "url": "git@github.com:gera2ld/duoshuo-ua.git"
@@ -13,10 +14,13 @@
   },
   "devDependencies": {
     "gulp": "^3.8.11",
+    "gulp-clone": "^1.0.0",
     "gulp-concat": "^2.5.2",
     "gulp-css2js": "^1.0.2",
+    "gulp-header": "^1.2.2",
     "gulp-merge": "^0.1.0",
     "gulp-minify-css": "^0.4.4",
+    "gulp-order": "^1.1.1",
     "gulp-rename": "^1.2.0",
     "gulp-uglify": "^1.1.0",
     "gulp-wrap": "^0.10.1"

+ 12 - 11
src/duoshuo-ua.js

@@ -1,14 +1,16 @@
-/* 多说User-Agent插件
- * 作者:Gerald <gera2ld@163.com>
- * @require ua-parser.js
+/**
+ * 多说User-Agent插件核心脚本
+ * 用于监听DUOSHUO及评论加载过程并执行注入
+ *
+ * @author Gerald <gera2ld@163.com>
+ *
+ * Optional requirements:
+ *   ./default.js
  */
-var UAParser = this.UAParser;
 
-function getUAString(local) {
-	var agent = UAParser.parse(local.agent);
-	return '<div class="ds-os">' + UAParser.getString(agent.os) + '</div>' +
-		'<div class="ds-br">' + UAParser.getString(agent.browser) + '</div>' +
-		(local.webmaster ? '<div class=ds-webmaster>站长</div>' : '');
+var _this = this;
+function emptyString(local) {
+	return '';
 }
 
 function callBefore(local, args) {
@@ -26,7 +28,7 @@ function callAfter(local, args) {
 	var res = local.result;
 	var i = res.indexOf('<div class="ds-comment-header">');
 	var j = res.indexOf('</div>', i);
-	var func = duoshuoQuery.getUAString || getUAString;
+	var func = duoshuoQuery.getUAString || _this.getUAString || emptyString;
 	local.result = res.slice(0, j) + func(local) + res.slice(j);
 }
 
@@ -40,7 +42,6 @@ function init() {
 		callAfter.call(this, local, args);
 		return local.result;
 	}
-	DUOSHUO.UAParser = UAParser;
 }
 
 function observeProperty(item, key, callback) {

+ 17 - 0
src/default.js

@@ -0,0 +1,17 @@
+/**
+ * 多说User-Agent插件默认显示方案
+ * 使用轻量级的ua-parser.js解析User-Agent
+ *
+ * @author Gerald <gera2ld@163.com>
+ */
+
+!function (window, undefined) {
+	var UAParser = window.UAParser;
+
+	window.getUAString = function (local) {
+		var agent = UAParser.parse(local.agent);
+		return '<div class="ds-os">' + UAParser.getString(agent.os) + '</div>' +
+			'<div class="ds-br">' + UAParser.getString(agent.browser) + '</div>' +
+			(local.webmaster ? '<div class=ds-webmaster>站长</div>' : '');
+	};
+}(this);

+ 1 - 0
src/ua-parser.js

@@ -2,6 +2,7 @@
 * User-Agent parser
 * @author Gerald <gera2ld@163.com>
 */
+
 !function (window, undefined) {
 	var mapper = {
 		reg: function (str, maps) {