基于CodeMirror&TernJS的汉语Javascript编辑器(和翻译器)

(41) 2024-02-27 18:01:01

http://www.ternjs.net/

TernJS.net提供了一个DEMO,正是支持语法高亮和智能感知的Javascript编辑器;于是我突发奇想,修改其中的Javascript parser,有了下面的汉语JS,名曰EJS。

主文件 doc/demo.html

代码检阅:https://github.com/xuhongxu96/EJS

基于CodeMirror&TernJS的汉语Javascript编辑器(和翻译器) (https://mushiming.com/)  第1张

主要特性:

1、关键词汉化

2、支持拼音输入的智能感知

改造过程:

请先从ternjs.net获取tern源代码。

一、文件 tern-master/lib/tern.js

1、修改关键词

 var jsKeywords = ("跳出 循环做 实例之于 类型之于 分支 否则 创建 变量 真 假 " +
        "捕获 善后 返回 无类型 继续 变量循环 判断 循环当 调试中断 " +
        "函数 我 语句组 默认 如果 抛出 删除 于 异常处理 空").split(" ");

2、增加拼音匹配引擎

/*
 * 基于本地数据拼音匹配引擎
 * Version 0.3 简体中文
 * Date: 2011-09-19
 * http://code.google.com/p/pinyin-engine/
 * (c) 2009-2010 TangBin, http://www.planeArt.cn
 *
 * This is licensed under the GNU LGPL, version 2.1 or later.
 * For details, see: http://creativecommons.org/licenses/LGPL/2.1/
 */
 
var pinyinEngine = function () {
	pinyinEngine.cache();
	return (this instanceof pinyinEngine) ? this.resetCache() : new pinyinEngine;
};



/**
 * 支持多音字的汉字转拼音算法
 * @version	2011-07-22
 * @see		https://github.com/hotoo/pinyin.js
 * @author	闲耘, 唐斌
 * @param	{String}		要转为拼音的目标字符串(汉字)
 * @param	{Boolean}		是否仅保留匹配的第一个拼音
 * @param	{String}		返回结果的分隔符,默认返回数组集合
 * @return	{String, Array} 如果 sp 为 null,则返回 Array
 *							否则,返回以 sp 分隔的字符串
 */
pinyinEngine.toPinyin = function () {
	
    // 笛卡尔乘积,返回两个数组的所有可能的组合
    function product (a, b, sp) {
        var r = [], val, str = [];
        for (var i = 0, l = a.length; i < l; i ++) {
            for (var j = 0, m = b.length; j < m; j ++) {
                val = r[r.length] = (a[i] instanceof Array) ? a[i].concat(b[j]) : [].concat(a[i],b[j]);
				str.push(val.join(""));
            };
        };
        return {
			array: r,
			string: str.join(sp || "")
		};
    };
	
    return function (keyword, single, sp) {
		var cache = pinyinEngine.cache();
        var len = keyword.length, pys, py, pyl, i, y;
		
        if (len === 0) {return single || sp ? "" : []};
        if (len === 1) {
            y = cache[keyword];
            if (single) {return y && y[0] ? y[0] : keyword};
						if (sp)
							if(y)
								return y.join(sp || "");
							else
								return keyword;
						else
            	return y || [keyword];
        } else {
            var py = [];
            for (i = 0; i < len; i ++) {
                y = cache[keyword.charAt(i)];
                if (y) {
                    py[py.length] = single ? y[0] : y;
                } else {
                    py[py.length] = single ? keyword.charAt(i) : [keyword.charAt(i)];
                };
            };
            if (single) {return sp == null ? py : py.join(sp || "")};

            pys = py[0];
			pyl = py.length;
			var prt;
            for (i = 1; i < pyl; i++) {
				prt = product(pys, py[i], sp);
                pys = prt.array;
            };
            return sp == null ? pys : prt.string;
        };
    };
	
}();

/**
 * 汉字拼音索引缓存
 * @return	{Object}	名为汉字值拼音的对象
 * @example	var pinyin = pinyinEngine.cache(); pinyin['乐']; // ["le", "yue"]
 */
pinyinEngine.cache = function () {
	var txt, w, k, l,
		data = pinyinEngine._data || [],
		word = data[0],
		key = data[1],
		cache = {},
		fromCharCode = String.fromCharCode;
	
	if (!data.length) return cache;
	
	// 对原始数据解压缩
	if (typeof key === 'string') key = key.split(',');
	for (var i = 0, arrow = 0, len = word.length; i < len; i ++, arrow ++) {
		k = word[i];
		
		// 处理占位符:负值与空值
		if (k < 0 || k === undefined) {
			arrow += Math.abs(k || 0);
			continue;
		};
		
		txt = fromCharCode(arrow + 19968);
		
		if (typeof k === 'number') {
			cache[txt] = [key[k]];
		// 多音字数据
		} else {
			cache[txt] = [];
			l = k.length;
			for (w = 0; w < l; w ++) {
				cache[txt].push(key[k[w]]);
			};
		};
	};
	
	pinyinEngine.cache = function () {
		return cache;
	};
	return cache;
};

// 拼音对照简体中文数据
pinyinEngine._data = [[350,65,,241,-2,324,372,271,280,332,,20,356,,87,38,,383,[134,246],233,285,,250,18,349,47,67,295,35,-2,66,,167,346,,272,287,,91,345,,378,-1,82,,41,,170,,62,,324,56,326,380,,164,134,236,-2,201,,133,,[321,374],186,350,,377,330,369,116,77,[161,358],,238,228,245,,99,,[35,284],350,-1,[193,213],133,241,349,331,334,-3,287,-1,124,-5,182,177,,264,-9,243,-6,[161,168],,356,376,,285,76,39,356,153,,359,116,241,330,131,-1,93,,345,336,-2,124,,325,141,,128,106,350,30,113,199,,334,131,314,167,-2,247,19,,336,-3,259,-3,350,[283,285],259,161,65,365,130,240,38,,372,130,129,,260,47,-1,179,,24,388,285,303,372,85,333,-2,315,259,243,,91,,55,171,350,-1,271,,350,199,188,-2,347,,378,233,,330,126,125,-2,259,,81,,79,,241,-6,141,350,-1,330,124,85,77,340,-4,378,355,123,[121,150],356,-1,271,326,[41,383],,345,,280,31,179,24,-1,326,380,-2,19,97,,209,,7,,171,,283,,295,,295,125,-4,62,-1,56,-5,326,61,380,395,355,,310,,110,,321,282,356,350,83,395,96,215,315,209,-1,354,322,243,,138,,230,-1,160,-1,347,6,-1,125,201,,124,-3,312,128,,350,285,-2,140,377,-2,36,149,,164,,285,,380,,355,-1,198,-1,67,-2,95,,350,-1,332,-1,176,,128,375,26,245,150,29,,217,,330,115,-4,247,-8,[14,234],-2,49,74,250,-3,137,-2,391,245,164,354,-4,298,85,,164,,238,9,[287,356],[241,295],,337,,356,-1,38,,346,[165,167],164,-1,126,340,85,,134,,226,-3,82,,2,-2,13,-12,100,10,,287,-1,58,,136,-2,306,115,350,,310,-1,129,,31,-2,146,135,,134,243,209,,387,329,180,-10,370,,377,,248,-3,346,-2,125,124,-2,268,,326,234,-4,336,-3,395,,314,-7,126,-5,389,-8,224,316,-2,[174,176],-1,81,31,153,-3,85,-1,164,-3,8,-20,55,,306,,17,39,222,-1,51,-4,4,-6,36,277,-18,334,-5,331,-2,168,-10,133,276,-3,128,126,384,-3,298,,127,-4,233,-9,131,342,-2,56,-5,264,-13,162,-28,76,330,359,,357,339,37,373,,333,101,,143,,191,-2,71,-1,317,295,346,-2,57,,68,-4,131,-1,264,-1,252,-1,5,95,172,331,,158,95,,100,338,18,241,134,62,388,,347,126,286,-1,124,30,-2,206,-1,89,255,-1,26,361,-3,185,-1,191,,262,,336,,137,217,-2,100,,378,,357,195,-5,67,-1,[82,238],18,116,37,136,,152,349,163,-2,67,333,169,-1,131,-2,241,,386,296,,167,,63,171,-1,126,,48,-8,170,,215,-1,124,78,-1,82,-5,85,,238,,139,120,-1,60,-1,339,,317,4,39,124,56,107,,364,58,63,,259,-1,81,246,350,,140,-1,39,327,-1,338,117,-2,358,169,172,365,89,42,,39,-1,279,-2,227,-2,229,164,,16,,131,98,,58,-1,148,-1,377,288,252,[28,277],46,143,,102,,102,139,73,124,310,-5,[245,335,343],,156,243,-1,98,126,-1,310,-6,324,,124,-1,[279,346],-2,[9,19],,134,,284,126,-3,85,-1,91,-9,235,,[32,128],,245,136,-4,233,-6,123,-1,350,-6,164,,252,7,95,125,330,182,169,-3,67,380,218,129,251,281,-2,164,130,160,-9,110,285,-2,19,-2,354,,191,,344,-3,189,,161,-2,341,,140,-5,199,-3,247,-10,336,-7,9,281,-2,96,330,359,-3,9,47,,339,-3,240,,229,85,-3,13,117,10,,[36,285],-2,360,-1,127,152,,332,-1,102,-2,80,-2,153,-9,233,251,350,-1,14,209,,285,,243,,269,,284,330,121,7,-2,117,336,,10,[49,391],387,,56,182,202,-1,19,,[19,20],,14,20,371,138,175,355,175,,98,329,-2,326,-1,377,185,351,326,,124,253,177,,135,336,,130,-3,248,-1,31,,74,314,164,-1,164,,345,346,282,-6,26,-1,164,,115,-1,53,,357,-1,334,346,,136,[277,332],,39,133,-3,295,-6,295,-3,251,-2,333,,271,[23,283],-4,355,28,124,355,291,78,-2,77,-1,287,,251,286,14,341,,227,-2,297,64,-1,147,97,134,171,,[58,307],147,377,128,373,5,65,143,304,36,285,355,,239,349,109,295,305,,161,63,124,-2,[341,356],,36,91,,348,,[91,110],124,63,,315,195,115,164,317,334,369,332,,176,0,181,-2,137,,170,320,351,80,13,,247,-1,[84,233],5,72,81,,107,314,[108,145],293,241,,377,351,,330,32,-1,331,43,,327,115,,330,,345,-1,74,-1,55,,35,,[90,97],85,-3,200,,85,350,55,224,164,10,357,104,,244,330,-4,205,,171,,355,,379,-7,98,388,326,,110,204,332,230,-1,283,116,195,-1,134,,360,,73,,229,-3,[360,365,369],[110,116,123],,133,354,85,,379,,138,97,138,-1,173,67,215,,295,-4,101,369,,350,169,388,193,190,377,348,124,,[91,138,180],,362,,143,121,-2,333,-1,340,,346,,350,1,237,283,,114,,73,322,105,361,-1,226,334,1,93,152,345,54,335,13,358,,117,,150,73,,124,217,198,353,-4,91,[74,223],36,281,164,200,-1,148,335,-2,374,369,-5,20,,113,94,,91,,346,-3,302,44,,1,-4,331,306,395,-1,330,-5,181,-3,160,,302,364,119,-1,124,-1,82,,116,,286,326,288,31,,164,-2,353,-3,277,,321,116,,379,,144,387,,280,-1,170,0,-4,51,-3,307,56,-4,[40,45],-3,80,-1,233,277,156,365,-1,225,,274,383,213,-5,19,65,159,335,-1,36,310,,133,,138,354,326,202,279,-1,156,129,115,107,64,-2,[222,258],,351,-1,329,-2,41,121,-1,331,110,,153,-6,342,-8,164,,[28,369],,192,,231,,153,,356,,174,148,-4,0,340,-2,298,-1,[1,350],302,-2,143,[54,303],272,34,,297,-4,285,91,,129,,328,,295,36,109,302,,106,,247,213,110,-1,75,-2,1,,315,,4,-4,297,297,,61,241,-5,25,125,-1,235,,86,97,-7,[285,341],-1,181,-1,162,69,-2,12,352,-1,190,-3,[40,395],232,-2,380,32,,393,,295,-1,168,,331,-2,111,-11,27,128,349,-4,60,-1,240,136,247,-1,175,-4,223,,130,-2,241,74,364,350,285,-3,[136,343],-3,86,-3,270,233,-7,264,109,-6,109,310,-2,[21,28],-14,335,-10,123,-6,256,-3,[128,136],-12,203,-8,203,-1,[54,147],-1,250,295,,[126,202],121,337,351,202,318,,[72,320],-5,116,,357,-1,154,47,-1,326,179,-2,171,97,-1,104,317,355,-2,240,356,,357,,252,356,248,-16,[119,357],-1,317,-2,284,-3,361,[326,341],91,,330,102,233,350,[59,61],-1,375,-4,152,31,241,-1,124,,377,,7,-3,137,-1,79,,11,305,140,118,395,145,-4,150,-1,126,305,164,5,330,81,385,239,-1,154,,305,,321,88,238,62,,209,,233,-2,4,-1,199,143,-2,61,33,-4,43,156,173,173,175,-3,338,67,-4,162,86,-6,73,-3,351,77,96,357,64,,144,280,,74,,62,,345,149,-2,139,,204,-2,357,-8,94,1,-6,182,-1,35,[279,346],-1,169,,[20,240],35,-1,285,344,104,-1,210,,356,20,-2,233,-7,55,,2,-3,377,-2,350,230,124,-1,273,-1,148,,306,-2,71,130,-2,232,,317,-2,243,-2,73,-2,351,-3,64,,115,9,-1,61,-4,140,-4,346,-3,69,-13,163,-6,303,35,-2,298,-1,303,-2,306,-4,[270,274],-5,91,-4,311,357,-16,287,,36,183,,131,,287,-2,354,-7,280,199,-4,244,167,-2,368,341,-7,197,72,-17,124,-3,13,-2,354,-10,110,-2,109,-13,256,-5,285,259,,384,,284,-1,[143,245],-1,116,-1,350,-7,[78,228,327],,39,-1,10,-4,85,,332,-3,153,331,323,-1,298,73,,349,-1,96,-3,351,123,,[54,304],,311,304,85,,348,347,[11,108],,285,-1,316,-1,350,149,[86,125,242],73,,152,-3,166,119,,346,-1,241,201,82,,81,-1,153,390,,241,-1,11,350,127,307,363,,331,-4,62,,282,-1,4,-12,219,218,,201,,126,303,-2,109,-2,294,264,80,325,,384,85,181,,259,-1,346,-3,69,124,-1,348,130,,192,-3,216,-3,13,356,321,-1,79,330,356,102,-1,209,379,-1,54,-5,187,,241,-1,246
THE END

发表回复