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; diff --git a/package.json b/package.json index 1583789..74c84a5 100644 --- a/package.json +++ b/package.json @@ -1,11 +1,8 @@ { - "name": "multilingual.js", + "name": "multilingual", "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": {