From 1a2477c6a21fce77841d4d897bcb6fa7aa218523 Mon Sep 17 00:00:00 2001 From: synthresin Date: Sun, 1 May 2016 15:55:51 +0900 Subject: [PATCH 1/3] =?UTF-8?q?=ED=8C=A8=ED=82=A4=EC=A7=80=20=EB=AA=85?= =?UTF-8?q?=EC=97=90=20.js=20=EC=A0=9C=EA=B1=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 1583789..2a68c27 100644 --- a/package.json +++ b/package.json @@ -1,5 +1,5 @@ { - "name": "multilingual.js", + "name": "multilingual", "version": "0.0.1", "description": "Micro screen typography for CJK typesetting", "main": "index.js", From acbcc84eae9cc062e797212907ef4d33f584b676 Mon Sep 17 00:00:00 2001 From: synthresin Date: Sun, 1 May 2016 16:26:53 +0900 Subject: [PATCH 2/3] =?UTF-8?q?=ED=98=84=EC=9E=AC=20npm=20=ED=8C=A8?= =?UTF-8?q?=ED=82=A4=EC=A7=95=EC=97=90=20=ED=95=84=EC=9A=94=EC=97=86?= =?UTF-8?q?=EB=8A=94=20=EC=96=B4=ED=8A=B8=EB=A6=AC=EB=B7=B0=ED=8A=B8=20?= =?UTF-8?q?=EC=A0=9C=EA=B1=B0=20=EB=82=98=EC=A4=91=EC=97=90=20=ED=95=84?= =?UTF-8?q?=EC=9A=94=ED=95=98=EB=A9=B4=20=EA=B7=B8=EB=95=8C=20=EB=84=A3?= =?UTF-8?q?=EB=8A=94=20=EA=B1=B8=EB=A1=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- package.json | 14 -------------- 1 file changed, 14 deletions(-) diff --git a/package.json b/package.json index 2a68c27..74c84a5 100644 --- a/package.json +++ b/package.json @@ -3,9 +3,6 @@ "version": "0.0.1", "description": "Micro screen typography for CJK typesetting", "main": "index.js", - "scripts": { - "test": "webpack-dev-server" - }, "repository": { "type": "git", "url": "git+ssh://git@github.com/wonyoungso/multilingual.js.git" @@ -17,17 +14,6 @@ "composite", "fonts" ], - "devDependencies": { - "webpack": "^1.9.11", - "expose-loader": "~0.7.1", - "mocha-loader": "~0.7.1", - "mocha": "~2.0.1", - "chai": "~3.5.0", - "webpack-dev-server": "^1.9.0", - "lodash": "~4.2.0", - "jquery": "~2.1.4", - "backbone-events-standalone": "~0.2.7" - }, "author": "Wonyoung So (http://wonyoung.so/), E Roon Kang ", "license": "MIT", "bugs": { From e0eb550f68003a17f881eb9e1350131c202da551 Mon Sep 17 00:00:00 2001 From: synthresin Date: Sun, 1 May 2016 16:49:08 +0900 Subject: [PATCH 3/3] =?UTF-8?q?npm=20=EB=AA=A8=EB=93=88=EB=A1=9C=20?= =?UTF-8?q?=EC=82=AC=EC=9A=A9=ED=95=A0=20=EC=88=98=20=EC=9E=88=EB=8F=84?= =?UTF-8?q?=EB=A1=9D=20Multilingual=20=EA=B0=9D=EC=B2=B4=20=EB=8B=A4?= =?UTF-8?q?=EC=8B=9C=20=EC=9E=91=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- index.js | 107 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 107 insertions(+) create mode 100644 index.js diff --git a/index.js b/index.js new file mode 100644 index 0000000..98bd7f5 --- /dev/null +++ b/index.js @@ -0,0 +1,107 @@ +'use strict'; + +var regexs = { + en: "[A-Za-z]+", + ko: "[가-힣]+", + jp: "[\u3040-\u309F\u30A0-\u30FF]+", + cn: "[\u4E00-\u9FBF]+", + num: "[0-9]+", + punct: "[\(\).,“”\-]|&|<|>+" +} + +var Multilingual = function(params) { + this.containers = params.containers; + this.configuration = params.configuration; + + this.init(); +} + +Multilingual.prototype.init = function() { + var finalRegex = this.composeRegex(); + var configuration = this.configuration; + + for (var i = 0, len = this.containers.length; i < len; i++){ + var container = this.containers[i]; + container.innerHTML = container.innerHTML.replace(finalRegex, function(){ + for (var i = 1; i < arguments.length; i++) { + if (arguments[i] != undefined) { + var config = configuration[i - 1]; + var className; + + if (typeof config == "string"){ + className = "ml-" + config; + } else { + className = config.className; + } + + return "" + arguments[i] + ""; + } + } + }); + } +}; + +Multilingual.prototype.escapeRegexStr = function(str) { + return str.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, "\\$&"); +} + +Multilingual.prototype.computeCustomRegex = function(charset) { + charset = this.escapeRegexStr(charset); + + var htmlEscapedChars = []; + var finalStr = ""; + + if (charset.match(/\&/) != null || charset.match(/\&/) != null){ + htmlEscapedChars.push("&"); + charset = charset.replace(/\&/, ""); + charset = charset.replace(/\&/, ""); + } + + if (charset.match(//) != null || charset.match(/\>/) != null){ + htmlEscapedChars.push(">"); + charset = charset.replace(/>/, ""); + charset = charset.replace(/\>/, ""); + } + + if (charset.match(/>/) != null || charset.match(/\&emdash;/) != null){ + htmlEscapedChars.push("&emdash;"); + charset = charset.replace(/>/, ""); + charset = charset.replace(/\&emdash;/, ""); + } + + if (htmlEscapedChars.length > 0) { + finalStr = "([" + charset + "]|" + htmlEscapedChars.join("|") + "+)"; + } else { + finalStr = "([" + charset + "]+)"; + } + return finalStr; +} + + +Multilingual.prototype.composeRegex = function(){ + var finalRegexStr = "(\?![^<>&]*>)"; + + for (var i = 0, len = this.configuration.length; i < len; i++){ + var config = this.configuration[i]; + + if (typeof config == "string"){ + finalRegexStr += "(" + regexs[config] + ")"; + } else { + finalRegexStr += this.computeCustomRegex(config.charset); + } + + if (i < this.configuration.length - 1) { + finalRegexStr += "|"; + } + } + + return new RegExp(finalRegexStr, "gm"); +} + +module.exports = Multilingual;