shawl.qiu Javascript 代码格式化类/Styler, StyleEngine v1.0
说明:
码代码这事, 开始的时候没啥经验, 就乱码...
等到积累一定的知识后, 发现代码风格实在是太重要了...
不过先前有可能已码了一堆风格不好的代码...
那怎么办? 逐个手工转换风格吗?
不用.. 鄙人写的这个JS 工具就是对付这类问题的...
本类主要规划四大类的代码格式化功能.
1. 类 C 语言(v1.0 实现, 已实现)
可完美格式化JS代码,
也可良好格式化脚本类类C语言, 如: Php
目前未针对强语言进行设计, 但也可用于格式化类C强语言, 不过因其复杂度而言, 某些代码格式化会不理想, 如C++之类那样庞大复杂的代码.
2. CSS 样式格式化(v1.1 实现, 未实现)
3. Tag 格式化(v1.2 实现, 未实现)
主要针对标签类语言, 如 xml, (x)html
4. 类 Basic 语言(v1.3 实现, 未实现)
主要规划针对 VBscript 进行设计,
但也可作用于 Pascal, VB 之类.
欢迎提供反馈: shawl.qiu@gmail.com
目录:
1. 调用(Styler_v1-0.htm)
2. 类 Styler(Styler_v1-0.js)
3. 类 StyleEngine(StyleEngine_v1-0.js)
shawl.qiu
2007-08-06
http://blog.csdn.net/btbtd
演示:
http://files.myopera.com/btbtd/javascript/class/Styler/Styler_v1-0.htm
http://shawl.qiu.googlepages.com/Styler_v1-0.htm
下载:
http://files.myopera.com/btbtd/javascript/class/Styler/Styler_v1-0.7z
http://shawl.qiu.googlepages.com/Styler_v1-0.7z
内容:
1. 调用(Styler_v1-0.htm)
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
- <title>shawl.qiu template</title>
- <style type="text/css">
- /* <![CDATA[ */
- hr
- {
- border-top: 1px dotted #FFFFFF!important;
- border-right: 1px dotted #FFFFFF!important;
- border-bottom:1px dotted black!important;
- border-left: 1px dotted #FFFFFF!important;
- margin:2px 0px;
- }
- /* ]]> */
- </style>
- <script type="text/javascript">
- //<![CDATA[
- if (navigator.appName=="Microsoft Internet Explorer")
- {
- //最大化窗口
- self.moveTo(-5,-5)
- self.resizeTo(screen.availWidth +8,screen.availHeight+8)
- //这个脚本定义的宽度其实比原窗口还要大那么一点.
- }
- //]]>
- </script>
- </head>
- <body>
- <form id="StylerId"></form>
- <script type="text/javascript" src="./Styler_v1-0.js"></script>
- <script type="text/javascript" src="./StyleEngine_v1-0.js"></script>
- <script type="text/javascript">
- //<![CDATA[
- var Ser = new Styler();
- Ser.Go(Initer);
- var Au = Ser.Get.Au();
- document.title = Au.Subject + " " + Au.Version;
- Ser = null;
- function Initer(Param)
- {
- Param.Main.Id = "StylerId";
- if(Param.Engine==null)
- {
- Param.Engine = new StyleEngine;
- }
- Param.Container.OrderAr = [3, 0, 2, 4, 1];
- Param.GenreSelect.Default = "CLike"; // Enum{BasicLike|CLike|Tag|Css}
- Param.InputTextarea.DefaultValue =
- [
- "/n"
- ,"{//shawl.qiu code/n"
- ,"/*/n"
- ," xxxxxxxxxxx/n"
- ," xxxxxxxxxxx/n"
- ," xxxxxxxxxxx/n"
- ," xxxxxxxxxxx/n"
- ,"*//n"
- ,"a/n"
- ,"{/n"
- ,"b/n"
- ,"{"
- ,"/* xxxxxxxxxxx/n"
- ," xxxxxxxxxxx/n"
- ," xxxxxxxxxxx/n"
- ," xxxxxxxxxxx/n"
- ,"*//n"
- ,"b0.1/n"
- ,"{/n"
- ,"b1/n"
- ,"}/n"
- ,"c/n"
- ,"}/n"
- ,"d/n"
- ,"}/n"
- ,"d1/n"
- ,"{/n"
- ,"a1/n"
- ,"}/n"
- ,"{/n"
- ,"a2/n"
- ,"}/n"
- ,"a0.1/n"
- ,"}/n"
- ,"/n"
- ,"/n"
- ,"/n"
- ,"{/n"
- ,"a/n"
- ,"{/n"
- ,"b/n"
- ,"{/n"
- ,"c/n"
- ,"{/n"
- ,"d/n"
- ,"}/n"
- ,"}/n"
- ,"}/n"
- ,"}/n"
- ].join("");
- };
- //]]>
- </script>
- </body>
- </html>
- // shawl.qiu JavaScript Document
- /*-----------------------------------------------------------------------------------*/
- * shawl.qiu Javascript Styler class v1.0
- /*-----------------------------------------------------------------------------------*/
- //---------------------------------begin class Styler()-------------------------------//
- function Styler()
- { // shawl.qiu code
- //------------------------------------begin public variable
- //------------------------------------end public variable
- //------------------------------------begin private variable
- //---------------begin about
- var Au = {}
- Au.Subject = "shawl.qiu Javascript Styler class";
- Au.Version = "v1.0";
- Au.Name = "shawl.qiu";
- Au.Email = "shawl.qiu@gmail.com";
- Au.Blog = "http://blog.csdn.net/btbtd/";
- Au.CreatedDate = "2007-7-31";
- Au.Update = {};
- Au.Update["1"] = "";
- //---------------end about
- var This = this;
- var Param = {};
- Param.Main = {};
- Param.Main.Id = "StylerId";
- Param.Main.Element = null;
- Param.Engine = null;
- Param.Inti = false;
- Param.GenreSelect = {};
- Param.GenreSelect.Default = "CLike"; //CLike|BasicLike
- Param.GenreSelect.Label= "<b>流派:</b> ";
- fExtendElementBSE(Param.GenreSelect, true, "GenreSelectId", null);
- Param.Container = {};
- Param.Container.OrderAr = [0, 1, 2, 3, 4];
- Param.Container.Init = false;
- Param.Container.Set = [];
- Param.Container.Len = 5;
- Param.Container.PrefixId = function(){return Param.Main.Id+"Container";};
- Param.InputTextarea = {};
- fExtendTextareaIESSII
- (Param.InputTextarea, "TextareaId", null, "", "输入: <br/>", 15, 100);
- Param.ResultTextarea = {};
- fExtendTextareaIESSII
- (Param.ResultTextarea, "ResultTextarea", null, "", "结果: <br/>", 15, 100);
- Param.Button = {};
- Param.Button.Label = "操作: ";
- Param.Button.Submit = {};
- fExtendButtonSES(Param.Button.Submit, "SubmitId", null, "执行");
- Param.Button.Reset = {};
- fExtendButtonSES(Param.Button.Reset, "ResetId", null, "重置");
- Param.Genre = {};
- Param.Genre.CLike = {};
- Param.Genre.CLike.Indent = {};
- Param.Genre.CLike.Indent.Default = 1;
- Param.Genre.CLike.Indent.Min = 1;
- Param.Genre.CLike.Indent.Max = 8;
- fExtendElementBSE(Param.Genre.CLike.Indent, true, "CLikeIndentId", null);
- Param.Genre.CLike.Space = {};
- Param.Genre.CLike.Space.Default = "Space"; // Enum{Space|Tab}
- fExtendElementBSE(Param.Genre.CLike.Space, true, "CLikeSpaceId", null);
- Param.Genre.CLike.Paren = {};
- fExtendElementBSE(Param.Genre.CLike.Paren, true, "CLikeParenIndent", null);
- Param.Genre.CLike.BreakEmptyBrace = {};
- fExtendElementBSE(Param.Genre.CLike.BreakEmptyBrace, true, "CLikeBreakEmptyBrace", null);
- Param.Genre.CLike.LeftBraceBreakLeft = {};
- fExtendElementBSE
- (Param.Genre.CLike.LeftBraceBreakLeft, true, "CLikeLeftBraceBreakLeft", null);
- Param.Genre.CLike.LeftBraceBreakRight = {};
- fExtendElementBSE
- (Param.Genre.CLike.LeftBraceBreakRight, false, "CLikeLeftBraceBreakRight", null);
- Param.Genre.CLike.RightBraceBreakLeft = {};
- fExtendElementBSE
- (Param.Genre.CLike.RightBraceBreakLeft, true, "CLikeRightBraceBreakLeft", null);
- Param.Genre.CLike.RightBraceBreakRight = {};
- fExtendElementBSE
- (Param.Genre.CLike.RightBraceBreakRight, false, "CLikeRightBraceBreakRight", null);
- Param.Genre.CLike.BreakSuperBrace = {};
- fExtendElementBSE(Param.Genre.CLike.BreakSuperBrace, true, "CLikeBreakSuperBrace", null);
- Param.Genre.CLike.BreakFunc = {};
- fExtendElementBSE(Param.Genre.CLike.BreakFunc, true, "CLikeBreakFunc", null);
- Param.Genre.CLike.BreakAnnoFunc = {};
- fExtendElementBSE(Param.Genre.CLike.BreakAnnoFunc, true, "CLikeBreakAnnoFunc", null);
- Param.Genre.CLike.FixOperator = {};
- fExtendElementBSE(Param.Genre.CLike.FixOperator, true, "CLikeFixOperator", null);
- Param.Genre.CLike.Custom = {};
- Param.Genre.CLike.Custom.Before = {};
- fExtendElementBSE(Param.Genre.CLike.Custom.Before, true, "CLikeCustomBeforeId", null);
- Param.Genre.CLike.Custom.Before.Paren = {};
- fExtendElementBSE
- (Param.Genre.CLike.Custom.Before.Paren, true, "CLikeCustomBeforeParenId", null);
- Param.Genre.CLike.Custom.Before.Brace = {};
- fExtendElementBSE
- (Param.Genre.CLike.Custom.Before.Brace, true, "CLikeCustomBeforeBraceId", null);
- Param.Genre.CLike.Custom.After = {};
- fExtendElementBSE(Param.Genre.CLike.Custom.After, true, "CLikeCustomAfterId", null);
- Param.Genre.CLike.Custom.After.General = {};
- fExtendElementBSE
- (Param.Genre.CLike.Custom.After.General, true, "CLikeCustomAfterGeneralId", null);
- Param.Genre.CLike.DefaultSpace = {};
- Param.Genre.CLike.DefaultSpace.Num = 1;
- Param.Genre.CLike.DefaultSpace.Max = 80;
- Param.Genre.CLike.DefaultSpace.Min = 0;
- fExtendElementBSE(Param.Genre.CLike.DefaultSpace, true, "CLikeDefaultSpace", null);
- //------------------------------------end private variable
- //------------------------------------begin public method
- this.Get = {};
- this.Get.Au = fGetAu;
- this.Go = fGo;
- this.SetContainer = fSetContainer;
- //------------------------------------end public method
- //------------------------------------begin private method
- function fGo(ConfigFunc)
- {
- if(ConfigFunc&&ConfigFunc.constructor==Function)
- {
- ConfigFunc(Param);
- }
- if(!Param.Inti)
- {
- Param.Main.Element = fSetObj(Param.Main.Id);
- fSetContainer();
- fGenreSelect();
- fSetButton();
- fSetTextarea();
- fSetResultTextarea();
- fSetOptions();
- }
- Param.Inti = true;
- } // end function fGo(ConfigFunc)
- function fSetOptions()
- {
- var Sle = Param.GenreSelect.Element;
- var sGenre = Sle.options[Sle.selectedIndex].value;
- switch(sGenre)
- {
- case "Tag":
- fSetTagOptions();
- break;
- case "Css":
- fSetCssOptions();
- break;
- case "BasicLike":
- fSetBasicLikeOptions()
- break;
- default:
- fSetCLikeOptions();
- break;
- }
- } // end function fSetOptions
- function fGenreSelect(sDefaultSelect)
- {
- var GenreSelect = Param.GenreSelect;
- var sId = GenreSelect.Id();
- var sLbl = GenreSelect.Label;
- var Sle = document.createElement("select");
- Sle.id = sId;
- switch(GenreSelect.Default)
- {
- case "Tag":
- fAddOption(Sle, "Tag", "Tag", true, true);
- fAddOption(Sle, "Css", "Css", false, false);
- fAddOption(Sle, "类C", "CLike", false, false);
- fAddOption(Sle, "类Basic", "BasicLike", false, false);
- break;
- case "Css":
- fAddOption(Sle, "Css", "Css", true, true);
- fAddOption(Sle, "Tag", "Tag", false, false);
- fAddOption(Sle, "类C", "CLike", false, false);
- fAddOption(Sle, "类Basic", "BasicLike", false, false);
- break;
- case "BasicLike":
- fAddOption(Sle, "类Basic", "BasicLike", true, true);
- fAddOption(Sle, "类C", "CLike", false, false);
- fAddOption(Sle, "Tag", "Tag", false, false);
- fAddOption(Sle, "Css", "Css", false, false);
- break;
- default:
- fAddOption(Sle, "类C", "CLike", true, true);
- fAddOption(Sle, "类Basic", "BasicLike", false, false);
- fAddOption(Sle, "Tag", "Tag", false, false);
- fAddOption(Sle, "Css", "Css", false, false);
- break;
- }
- Param.Container.Set[0].innerHTML = sLbl;
- Param.Container.Set[0].appendChild(Sle);
- Param.GenreSelect.Element = document.getElementById(sId);
- Param.GenreSelect.Element.onchange =
- function()
- {
- fSetOptions();
- }
- Param.Container.Set[0].appendChild(document.createElement("hr"));
- } // end function fGenreSelect
- function fSetCssOptions()
- {
- var Container = Param.Container;
- var CLike = Param.Genre.CLike;
- var EgCLike = Param.Engine.Get.CLike();
- Container.Set[1].innerHTML = "<div><b>选项:</b> (打勾表示真, 不打表示假)</div>";
- Container.Set[1].innerHTML += "<p style='color:red;'>本功能暂未实现!</p>";
- }
- function fSetTagOptions()
- {
- var Container = Param.Container;
- var CLike = Param.Genre.CLike;
- var EgCLike = Param.Engine.Get.CLike();
- Container.Set[1].innerHTML = "<div><b>选项:</b> (打勾表示真, 不打表示假)</div>";
- Container.Set[1].innerHTML += "<p style='color:red;'>本功能暂未实现!</p>";
- }
- function fSetBasicLikeOptions()
- {
- var Container = Param.Container;
- var CLike = Param.Genre.CLike;
- var EgCLike = Param.Engine.Get.CLike();
- Container.Set[1].innerHTML = "<div><b>选项:</b> (打勾表示真, 不打表示假)</div>";
- Container.Set[1].innerHTML += "<p style='color:red;'>本功能暂未实现!</p>";
- }
- function fSetTextarea()
- {
- var Textarea = Param.InputTextarea;
- var Container = Param.Container;
- if(Textarea.Element!=null) return;
- var sLbl = Textarea.Label;
- var sId = Textarea.Id();
- var iRows = Textarea.Rows;
- var iCols = Textarea.Cols;
- var Txa = document.createElement("textarea");
- Txa.id = sId;
- Txa.rows = iRows;
- Txa.cols = iCols;
- Txa.value = Textarea.DefaultValue;
- Container.Set[3].innerHTML = sLbl;
- Container.Set[3].appendChild(Txa);
- Textarea.Element = document.getElementById(sId);
- Param.Container.Set[3].appendChild(document.createElement("hr"));
- }
- function fSetResultTextarea()
- {
- var Textarea = Param.ResultTextarea;
- var Container = Param.Container;
- if(Textarea.Element!=null) return;
- var sLbl = Textarea.Label;
- var sId = Textarea.Id();
- var iRows = Textarea.Rows;
- var iCols = Textarea.Cols;
- var Txa = document.createElement("textarea");
- Txa.id = sId;
- Txa.rows = iRows;
- Txa.cols = iCols;
- Txa.value = Textarea.DefaultValue;
- Container.Set[4].innerHTML = sLbl;
- Container.Set[4].appendChild(Txa);
- Textarea.Element = document.getElementById(sId);
- Param.Container.Set[4].appendChild(document.createElement("hr"));
- }
- function fSetButton()
- {
- var Button = Param.Button;
- var Textarea = Param.InputTextarea;
- var ResultTextarea = Param.ResultTextarea;
- var Container = Param.Container;
- if(Button.Submit.Element==null)
- {
- var SubmitText = Button.Submit.Text;
- var ResetText = Button.Reset.Text;
- Container.Set[2].innerHTML = Button.Label;
- var ExecBtn = document.createElement("input");
- ExecBtn.type="button"
- ExecBtn.id = Button.Submit.Id();
- ExecBtn.value = SubmitText;
- ExecBtn.onclick =
- function ()
- {
- var Sle = Param.GenreSelect.Element;
- var sGenre = Sle.options[Sle.selectedIndex].value;
- var TempFunc =
- function(CLike)
- {
- switch(sGenre)
- {
- case "Tag":
- break;
- case "Css":
- break;
- case "BasicLike":
- break;
- default:
- //BreakEmptyBrace
- CLike.Options.Space = Param.Genre.CLike.Space.Element.value;
- CLike.Options.Indent = Param.Genre.CLike.Indent.Element.value-0;
- CLike.Options.Parenthesis.Indent = Param.Genre.CLike.Paren.Element.checked;
- CLike.Options.Brace.Inline.Break =
- !Param.Genre.CLike.BreakEmptyBrace.Element.checked;
- CLike.Options.Brace.Left.BreakLeft =
- Param.Genre.CLike.LeftBraceBreakLeft.Element.checked;
- CLike.Options.Brace.Left.BreakRight =
- Param.Genre.CLike.LeftBraceBreakRight.Element.checked;
- CLike.Options.Brace.Right.BreakLeft =
- Param.Genre.CLike.RightBraceBreakLeft.Element.checked;
- CLike.Options.Brace.Right.BreakRight =
- Param.Genre.CLike.RightBraceBreakRight.Element.checked;
- CLike.Options.Brace.Super.Separate =
- Param.Genre.CLike.BreakSuperBrace.Element.checked;
- CLike.Options.Func.Break =
- Param.Genre.CLike.BreakFunc.Element.checked;
- CLike.Options.AnonFunc.Break =
- Param.Genre.CLike.BreakAnnoFunc.Element.checked;
- CLike.Options.Operator.Fix =
- Param.Genre.CLike.FixOperator.Element.checked;
- CLike.BreakLine.Before.Bool =
- Param.Genre.CLike.Custom.Before.Element.checked;
- CLike.BreakLine.Before.ParenList =
- Param.Genre.CLike.Custom.Before.Paren.Element.value;
- CLike.BreakLine.Before.BraceList =
- Param.Genre.CLike.Custom.Before.Brace.Element.value;
- CLike.BreakLine.After.Bool =
- Param.Genre.CLike.Custom.After.Element.checked;
- CLike.BreakLine.After.General =
- Param.Genre.CLike.Custom.After.General.Element.value;
- var TempNum = parseInt(Param.Genre.CLike.DefaultSpace.Element.value);
- if(isNaN(TempNum)) TempNum= Param.Genre.CLike.DefaultSpace.Num;
- if(TempNum<Param.Genre.CLike.DefaultSpace.Min)
- TempNum = Param.Genre.CLike.DefaultSpace.Min;
- if(TempNum>Param.Genre.CLike.DefaultSpace.Max)
- TempNum = Param.Genre.CLike.DefaultSpace.Max;
- CLike.Options.DefaultSpace = TempNum;
- break;
- }
- };
- Param.Engine.Config = TempFunc;
- ResultTextarea.Element.value = Param.Engine.Go(Textarea.Element.value, sGenre);
- }
- }
- if(Button.Reset.Element==null)
- {
- var ResetIpt = document.createElement("input");
- ResetIpt.id = Button.Reset.Id();
- ResetIpt.type = "reset";
- ResetIpt.value = ResetText;
- ResetIpt.onclick =
- function()
- {
- Textarea.Element.value = Textarea.DefaultValue;
- fSetOptions();
- return false;
- }
- Container.Set[2].appendChild(ExecBtn);
- Container.Set[2].appendChild(document.createTextNode(" "));
- Container.Set[2].appendChild(ResetIpt);
- Button.Submit.Element = document.getElementById(Button.Submit.Id);
- Button.Reset.Element = document.getElementById(Button.Reset.Id);
- }
- Param.Container.Set[2].appendChild(document.createElement("hr"));
- } // end function fSetButton
- function fAddOption(sle, text, value, defaultSelected, selected)
- {// shawl.qiu script
- if(!defaultSelected) defaultSelected = false;
- if(!selected) selected = false;
- sle.options[sle.options.length] = new Option(text, value, defaultSelected, selected);
- } // end function fAddOption
- function fSetContainer()
- {
- if(!Param.Container.Init)
- {
- var Count = 0;
- var TempId = "";
- while(Count<Param.Container.Len)
- {
- TempId = Param.Container.PrefixId()+Count;
- var Div = document.createElement("div");
- Div.id = TempId;
- Param.Main.Element.appendChild(Div);
- Param.Container.Set[Count] = document.getElementById(TempId);
- Count++;
- }
- Param.Container.Init = true;
- }
- if
- (
- Param.Container.OrderAr.constructor==Array
- &&Param.Container.OrderAr.length==Param.Container.Len
- )
- {
- var Len = Param.Container.OrderAr.length;
- var Index = 0;
- Param.Container.OrderAr.reverse();
- while(--Len>-1)
- {
- Index = Param.Container.OrderAr[Len];
- Param.Main.Element.removeChild(Param.Container.Set[Index]);
- Param.Main.Element.appendChild(Param.Container.Set[Index]);
- }
- } // end if
- } // end function fSetContainer
- function fSetObj(Obj, sMsg)
- {// shawl.qiu code : void return
- if(!sMsg) sMsg = "无法获得对象!";
- switch(Obj.constructor)
- {
- case String:
- Obj = document.getElementById(Obj);
- break;
- case Array:
- Obj = Obj[0];
- break;
- }
- if(Obj==null)
- {
- throw new Error(sMsg);
- }
- return Obj;
- } // end function fSetObj(OldObj, NewObj, sMsg)
- function fGetAu()
- {
- return Au;
- }
- function fExtendTextareaIESSII(Obj, sId, Ele, sDefault, sLabel, iRows, iCols)
- {
- Obj.Id = function(){return Param.Main.Id+sId;};
- Obj.Element = Ele;
- Obj.DefaultValue = sDefault;
- Obj.Label = sLabel;
- Obj.Rows = iRows;
- Obj.Cols = iCols;
- }
- function fExtendButtonSES(Obj, sId, Ele, sText)
- {
- Obj.Id = function(){return Param.Main.Id+sId;};
- Obj.Element = Ele;
- Obj.Text = sText;
- }
- function fExtendElementBSE(Obj, bBool, sId, Ele)
- {
- Obj.Bool = bBool;
- Obj.Id = function(){return Param.Main.Id+sId;};
- Obj.Element = Ele;
- }
- function fSetCLikeOptions()
- {
- var Container = Param.Container;
- var CLike = Param.Genre.CLike;
- var EgCLike = Param.Engine.Get.CLike();
- Container.Set[1].innerHTML = "<div><b>选项:</b> (打勾表示真, 不打表示假)</div>";
- // list 1
- var TempEle = document.createElement("div");
- var TempTNode = document.createTextNode("缩进次数: ");
- TempEle.appendChild(TempTNode);
- var TempSelect = document.createElement("select");
- TempSelect.id = CLike.Indent.Id();
- TempEle.appendChild(TempSelect);
- var TempTNode = document.createTextNode(" 空白字符: ");
- TempEle.appendChild(TempTNode);
- var TempSelect = document.createElement("select");
- TempSelect.id = CLike.Space.Id();
- TempEle.appendChild(TempSelect);
- Container.Set[1].appendChild(TempEle);
- CLike.Indent.Element = document.getElementById(CLike.Indent.Id());
- fAddOption(CLike.Indent.Element, CLike.Indent.Default, CLike.Indent.Default);
- var TempCount = CLike.Indent.Max - CLike.Indent.Min - 1;
- for(var i=0; i<=TempCount; i++)
- {
- var Val = i + CLike.Indent.Max - TempCount;
- if(Val === CLike.Indent.Default) continue;
- fAddOption(CLike.Indent.Element, Val, Val);
- } // end for
- CLike.Space.Element = document.getElementById(CLike.Space.Id());
- switch(Param.Genre.CLike.Space.Default)
- {
- case "Tab":
- fAddOption(CLike.Space.Element, "Tab", "/t");
- fAddOption(CLike.Space.Element, "Space", " ");
- break;
- default:
- fAddOption(CLike.Space.Element, "Space", " ");
- fAddOption(CLike.Space.Element, "Tab", "/t");
- break;
- }
- // list 2
- var TempEle = document.createElement("div");
- var TempTNode = document.createTextNode("缩进多行括号()中的内容: ");
- TempEle.appendChild(TempTNode);
- var TempCbx = document.createElement("input");
- TempCbx.type = "checkbox";
- TempCbx.id = Param.Genre.CLike.Paren.Id();
- TempEle.appendChild(TempCbx);
- Container.Set[1].appendChild(TempEle);
- CLike.Paren.Element = document.getElementById(Param.Genre.CLike.Paren.Id());
- CLike.Paren.Element.checked = Param.Genre.CLike.Paren.Bool;
- // list 3
- var TempEle = document.createElement("div");
- var TempTNode = document.createTextNode("不换行空{}/域对象:");
- TempEle.appendChild(TempTNode);
- var TempCbx = document.createElement("input");
- TempCbx.type = "checkbox";
- TempCbx.id = Param.Genre.CLike.BreakEmptyBrace.Id();
- TempEle.appendChild(TempCbx);
- Container.Set[1].appendChild(TempEle);
- CLike.BreakEmptyBrace.Element =
- document.getElementById(Param.Genre.CLike.BreakEmptyBrace.Id());
- CLike.BreakEmptyBrace.Element.checked = Param.Genre.CLike.BreakEmptyBrace.Bool;
- // list 4
- var TempEle = document.createElement("div");
- var TempTNode = document.createTextNode("左花括号{: 左边添加换行符: ");
- TempEle.appendChild(TempTNode);
- var TempCbx = document.createElement("input");
- TempCbx.type = "checkbox";
- TempCbx.id = Param.Genre.CLike.LeftBraceBreakLeft.Id();
- TempEle.appendChild(TempCbx);
- var TempTNode = document.createTextNode(", 右边添加换行符: ");
- TempEle.appendChild(TempTNode);
- var TempCbx = document.createElement("input");
- TempCbx.type = "checkbox";
- TempCbx.id = Param.Genre.CLike.LeftBraceBreakRight.Id();
- TempEle.appendChild(TempCbx);
- Container.Set[1].appendChild(TempEle);
- CLike.LeftBraceBreakLeft.Element = document.getElementById(CLike.LeftBraceBreakLeft.Id());
- CLike.LeftBraceBreakLeft.Element.checked = Param.Genre.CLike.LeftBraceBreakLeft.Bool;
- CLike.LeftBraceBreakRight.Element = document.getElementById(CLike.LeftBraceBreakRight.Id());
- CLike.LeftBraceBreakRight.Element.checked = Param.Genre.CLike.LeftBraceBreakRight.Bool;
- // list 5
- var TempEle = document.createElement("div");
- var TempTNode = document.createTextNode("右花括号}: 左边添加换行符: ");
- TempEle.appendChild(TempTNode);
- var TempCbx = document.createElement("input");
- TempCbx.type = "checkbox";
- TempCbx.id = Param.Genre.CLike.RightBraceBreakLeft.Id();
- TempEle.appendChild(TempCbx);
- var TempTNode = document.createTextNode(", 右边添加换行符: ");
- TempEle.appendChild(TempTNode);
- var TempCbx = document.createElement("input");
- TempCbx.type = "checkbox";
- TempCbx.id = Param.Genre.CLike.RightBraceBreakRight.Id();
- TempEle.appendChild(TempCbx);
- Container.Set[1].appendChild(TempEle);
- CLike.RightBraceBreakLeft.Element = document.getElementById(CLike.RightBraceBreakLeft.Id());
- CLike.RightBraceBreakLeft.Element.checked = Param.Genre.CLike.RightBraceBreakLeft.Bool;
- CLike.RightBraceBreakRight.Element = document.getElementById(CLike.RightBraceBreakRight.Id());
- CLike.RightBraceBreakRight.Element.checked = Param.Genre.CLike.RightBraceBreakRight.Bool;
- // list 6
- var TempEle = document.createElement("div");
- var TempTNode = document.createTextNode("断行一级右花括号}: ");
- TempEle.appendChild(TempTNode);
- var TempCbx = document.createElement("input");
- TempCbx.type = "checkbox";
- TempCbx.id = Param.Genre.CLike.BreakSuperBrace.Id();
- TempEle.appendChild(TempCbx);
- Container.Set[1].appendChild(TempEle);
- CLike.BreakSuperBrace.Element =
- document.getElementById(Param.Genre.CLike.BreakSuperBrace.Id());
- CLike.BreakSuperBrace.Element.checked = Param.Genre.CLike.BreakSuperBrace.Bool;
- // list 7
- var TempEle = document.createElement("div");
- var TempTNode = document.createTextNode("断行函数: ");
- TempEle.appendChild(TempTNode);
- var TempCbx = document.createElement("input");
- TempCbx.type = "checkbox";
- TempCbx.id = Param.Genre.CLike.BreakFunc.Id();
- TempEle.appendChild(TempCbx);
- Container.Set[1].appendChild(TempEle);
- CLike.BreakFunc.Element =
- document.getElementById(Param.Genre.CLike.BreakFunc.Id());
- CLike.BreakFunc.Element.checked = Param.Genre.CLike.BreakFunc.Bool;
- // list 8
- var TempEle = document.createElement("div");
- var TempTNode = document.createTextNode("断行匿名函数: ");
- TempEle.appendChild(TempTNode);
- var TempCbx = document.createElement("input");
- TempCbx.type = "checkbox";
- TempCbx.id = Param.Genre.CLike.BreakAnnoFunc.Id();
- TempEle.appendChild(TempCbx);
- Container.Set[1].appendChild(TempEle);
- CLike.BreakAnnoFunc.Element =
- document.getElementById(Param.Genre.CLike.BreakAnnoFunc.Id());
- CLike.BreakAnnoFunc.Element.checked = Param.Genre.CLike.BreakAnnoFunc.Bool;
- // list 9
- var TempEle = document.createElement("div");
- var TempTNode = document.createTextNode("修整运算符对齐: ");
- TempEle.appendChild(TempTNode);
- var TempCbx = document.createElement("input");
- TempCbx.type = "checkbox";
- TempCbx.id = Param.Genre.CLike.FixOperator.Id();
- TempEle.appendChild(TempCbx);
- Container.Set[1].appendChild(TempEle);
- CLike.FixOperator.Element =
- document.getElementById(Param.Genre.CLike.FixOperator.Id());
- CLike.FixOperator.Element.checked = Param.Genre.CLike.FixOperator.Bool;
- // list 9-1-10
- var TempEle = document.createElement("div");
- var TempTNode = document.createTextNode("行默认空白: ");
- TempEle.appendChild(TempTNode);
- var TempCbx = document.createElement("input");
- TempCbx.type = "text";
- TempCbx.size = 2;
- TempCbx.id = CLike.DefaultSpace.Id();
- TempEle.appendChild(TempCbx);
- Container.Set[1].appendChild(TempEle);
- CLike.DefaultSpace.Element =
- document.getElementById(CLike.DefaultSpace.Id());
- CLike.DefaultSpace.Element.value =
- CLike.DefaultSpace.Num;
- var TempTNode =
- document.createTextNode
- ([" (<=", CLike.DefaultSpace.Max, ", >="+CLike.DefaultSpace.Min, ")"].join(""));
- TempEle.appendChild(TempTNode);
- // list 10
- var TempEle = document.createElement("div");
- TempEle.appendChild(document.createElement("p"));
- var TempTNode = document.createTextNode("自定义前断行: ");
- TempEle.appendChild(TempTNode);
- var TempCbx = document.createElement("input");
- TempCbx.type = "checkbox";
- TempCbx.id = Param.Genre.CLike.Custom.Before.Id();
- TempEle.appendChild(TempCbx);
- Container.Set[1].appendChild(TempEle);
- CLike.Custom.Before.Element =
- document.getElementById(Param.Genre.CLike.Custom.Before.Id());
- CLike.Custom.Before.Element.checked = Param.Genre.CLike.Custom.Before.Bool;
- // list 11
- var TempEle = document.createElement("div");
- var TempTNode = document.createTextNode("括号()邻边: ");
- TempEle.appendChild(TempTNode);
- var TempCbx = document.createElement("input");
- TempCbx.type = "text";
- TempCbx.size = 80;
- TempCbx.id = Param.Genre.CLike.Custom.Before.Paren.Id();
- TempEle.appendChild(TempCbx);
- Container.Set[1].appendChild(TempEle);
- CLike.Custom.Before.Paren.Element =
- document.getElementById(Param.Genre.CLike.Custom.Before.Paren.Id());
- Param.Genre.CLike.Custom.Before.Paren.Element.value =
- EgCLike.BreakLine.Before.ParenList;
- var TempTNode = document.createTextNode(" (以|分隔多项)");
- TempEle.appendChild(TempTNode);
- // list 12
- var TempEle = document.createElement("div");
- var TempTNode = document.createTextNode("花括号{}邻边: ");
- TempEle.appendChild(TempTNode);
- var TempCbx = document.createElement("input");
- TempCbx.type = "text";
- TempCbx.size = 80;
- TempCbx.id = Param.Genre.CLike.Custom.Before.Brace.Id();
- TempEle.appendChild(TempCbx);
- Container.Set[1].appendChild(TempEle);
- CLike.Custom.Before.Brace.Element =
- document.getElementById(Param.Genre.CLike.Custom.Before.Brace.Id());
- Param.Genre.CLike.Custom.Before.Brace.Element.value =
- EgCLike.BreakLine.Before.BraceList;
- var TempTNode = document.createTextNode(" (以|分隔多项)");
- TempEle.appendChild(TempTNode);
- // list 13
- var TempEle = document.createElement("div");
- TempEle.appendChild(document.createElement("p"));
- var TempTNode = document.createTextNode("自定义后断行: ");
- TempEle.appendChild(TempTNode);
- var TempCbx = document.createElement("input");
- TempCbx.type = "checkbox";
- TempCbx.id = Param.Genre.CLike.Custom.After.Id();
- TempEle.appendChild(TempCbx);
- Container.Set[1].appendChild(TempEle);
- CLike.Custom.After.Element =
- document.getElementById(Param.Genre.CLike.Custom.After.Id());
- CLike.Custom.After.Element.checked = Param.Genre.CLike.Custom.After.Bool;
- // list 14
- var TempEle = document.createElement("div");
- var TempTNode = document.createTextNode("普通后断行语句: ");
- TempEle.appendChild(TempTNode);
- var TempCbx = document.createElement("input");
- TempCbx.type = "text";
- TempCbx.size = 80;
- TempCbx.id = CLike.Custom.After.General.Id();
- TempEle.appendChild(TempCbx);
- Container.Set[1].appendChild(TempEle);
- CLike.Custom.After.General.Element =
- document.getElementById(CLike.Custom.After.General.Id());
- CLike.Custom.After.General.Element.value =
- EgCLike.BreakLine.After.General;
- var TempTNode = document.createTextNode(" (以|分隔多项)");
- TempEle.appendChild(TempTNode);
- // hr bar
- Param.Container.Set[1].appendChild(document.createElement("hr"));
- } // end function fSetCLikeOptions
- //------------------------------------end private method
- } // shawl.qiu code
- //---------------------------------end class Styler()---------------------------------//
- // shawl.qiu JavaScript Document
- /*-----------------------------------------------------------------------------------*/
- * shawl.qiu StyleEngine class v1.0
- /*-----------------------------------------------------------------------------------*/
- //---------------------------------begin class StyleEngine()-------------------------------//
- function StyleEngine()
- { // shawl.qiu code
- //------------------------------------begin public variable
- //------------------------------------end public variable
- //------------------------------------begin private variable
- //---------------begin about
- var Au = {};
- Au.Subject = "shawl.qiu StyleEngine class";
- Au.Version = "v1.0";
- Au.Name = "shawl.qiu";
- Au.Email = "shawl.qiu@gmail.com";
- Au.Blog = "http://blog.csdn.net/btbtd/";
- Au.CreatedDate = "2007-7-31";
- Au.Update = {};
- Au.Update["1"] = "";
- //---------------end about
- var This = this;
- var Genre ={};
- Genre.CLike = {};
- var CLike = Genre.CLike;
- CLike.Escape = {};
- fExtendObjASI(CLike.Escape, false, "SQCLIKEESCAPEMARKER", false);
- CLike.String = {};
- fExtendObjASI(CLike.String, false, "SQCLIKESTRINGMARKER", false);
- CLike.CommentMultiline = {};
- fExtendObjASI(CLike.CommentMultiline, false, "SQCLIKECOMMENTMULTILINEMARKER", false);
- CLike.CommentSingleline = {};
- fExtendObjASI(CLike.CommentSingleline, false, "SQCLIKECOMMENTSINGLELINEMARKER", false);
- CLike.JsRegExp = {};
- fExtendObjASI(CLike.JsRegExp, false, "SQCLIKEJSREGEXPMARKER", false);
- CLike.Options = {};
- CLike.Options.Indent = 1;
- CLike.Options.Space = " ";
- CLike.Options.DefaultSpace = 1;
- CLike.Options.LevelCount = 0;
- CLike.Options.Parenthesis = {};
- CLike.Options.Parenthesis.Indent = true;
- fExtendObjSSSA
- (
- CLike.Options.Parenthesis
- , "SQCLIKEPARENTHESISBGEINMARKER"
- , "SQCLIKEPARENTHESISENDMARKER"
- , "x"
- , [0]
- );
- CLike.Options.Brace = {};
- CLike.Options.Brace.Inline = {};
- CLike.Options.Brace.Inline.Break = false;
- fExtendObjASI(CLike.Options.Brace.Inline, false, "SQCLIKEBRACEINLINEMARKER", false);
- CLike.Options.Brace.Left = {};
- CLike.Options.Brace.Left.BreakLeft = true;
- CLike.Options.Brace.Left.BreakRight = false;
- CLike.Options.Brace.Right = {};
- CLike.Options.Brace.Right.BreakLeft = true;
- CLike.Options.Brace.Right.BreakRight = false;
- fExtendObjSSSA
- (
- CLike.Options.Brace
- , "SQCLIKEBRACEBGEINMARKER"
- , "SQCLIKEBRACEENDMARKER"
- , "x"
- , [0]
- );
- CLike.Options.Brace.Super = {};
- CLike.Options.Brace.Super.Separate = true;
- CLike.Options.Brace.Super.Separater = "/n";
- CLike.Options.Brace.Super.Marker = "SQCLIKEBRACESUPERMARKER";
- CLike.Options.Operator = {};
- CLike.Options.Operator.Fix = true;
- CLike.Options.Operator.List = "//=//=//=|//>//>//>//=|//>//>//=|//<//<//=|//!//=//=|//>//>//=|//>//>//>|//+//+|//-//-|//<//<|//>//>|//<//=|//>//=|//=//=|//!//=|//&//&|//|//||//*//=|/=|//%//=|//+//=|//-//=|//&//=|//|//+|//^//=|//~|//=|//<|//>|//^|//?|//:|//-|//+|//||//%|//*|///";
- CLike.Options.Func = {};
- CLike.Options.Func.Break = true;
- CLike.Options.Func.Marker = "SQCLIKEFUNCMARKER";
- CLike.Options.AnonFunc = {};
- CLike.Options.AnonFunc.Break = true;
- CLike.Options.AnonFunc.Marker = "SQCLIKEANONFUNCMARKER";
- CLike.BreakLine = {};
- CLike.BreakLine.Before = {};
- CLike.BreakLine.Before.Bool = true;
- CLike.BreakLine.Before.Marker = "SQCLIKEBREAKLINEBEFOREMARKER";
- CLike.BreakLine.Before.ParenList = "for|do|while|else if|switch|with|catch|if"; // x|y
- CLike.BreakLine.Before.BraceList = "try|else"; // x|y
- CLike.BreakLine.After = {};
- CLike.BreakLine.After.Bool = true;
- CLike.BreakLine.After.Marker = "SQCLIKEBREAKLINEAFTERMARKER";
- CLike.BreakLine.After.ParenList = ""; // x|y
- CLike.BreakLine.After.BraceList = ""; // x|y
- CLike.BreakLine.After.General = "break"; // x|y
- //-----------------------end Clike
- Genre.BasicLike = null;
- Genre.HtmlTag = null;
- Genre.XmlTag = null;
- var Source = [""];
- var Formated = "";
- //------------------------------------end private variable
- //------------------------------------begin public method
- //-------------------Begin this.Get
- this.Get = {};
- this.Get.Au = fGetAu;
- this.GetAu = this.Get.Au;
- this.Get.Genre = function(){return Genre;};
- this.Get.CLike = function(){return Genre.CLike;};
- //-------------------End this.Get
- //-------------------Begin this.Set
- //-------------------End this.Set
- this.Go = fGo;
- this.Genre = {};
- this.Config = null;
- //------------------------------------end public method
- //------------------------------------begin private method
- function fGo(SourceText, sGenre)
- {// return string
- var bIsString = false;
- if(SourceText.constructor==String)
- {
- Source[0] = SourceText;
- bIsString = true;
- }
- else
- {
- Source = SourceText;
- }
- if(This.Config.constructor==Function)
- {
- This.Config(CLike);
- }
- switch(sGenre)
- {
- case "BasicLike":
- break;
- case "Tag":
- break;
- case "Css":
- break;
- default:
- fGettingCLike(Source)
- break;
- };
- if(bIsString)
- {
- return Source[0];
- }
- } // end function fGo
- function fGettingCLike(Source)
- {
- var CLike = Genre.CLike;
- var TempSpace = fStringSpace(CLike.Options.Space, CLike.Options.Indent);
- var DefaultSpace = fStringSpace(CLike.Options.Space, CLike.Options.DefaultSpace);
- fEncodeCode(Source, CLike.Escape, "(.)");
- fEncodeCode(Source, CLike.String, "(///"[^//n]*?///")");
- fEncodeCode(Source, CLike.String, "(//'[^//n]*?//')");
- fEncodeCode(Source, CLike.CommentMultiline, "(/*[//s//S]*?//*///)");
- fEncodeCode(Source, CLike.CommentSingleline, "(//.*)");
- fEncodeCode(Source, CLike.JsRegExp, "(///[^///].*?///)");
- if(!CLike.Options.Brace.Left.BreakLeft)
- {
- Source[0] = Source[0].replace(//n([^/n/S]*?{)/, "$1");
- }
- if(CLike.Options.Operator.Fix)
- {
- var TempRe =
- new RegExp(["[^//n//S]*(",CLike.Options.Operator.List, ")[^//n//S]*"].join(""), "g");
- Source[0] = Source[0].replace(TempRe, TempSpace+"$1"+TempSpace);
- Source[0] = Source[0].replace(/[^/n/S]*(,|/+/+|/-/-|/;)[^/n/S]*/g, "$1"+TempSpace);
- Source[0] = Source[0].replace(//s*/-/s*(/d+)/g, TempSpace+"-$1");
- }
- if(!CLike.Options.Brace.Inline.Break)
- {
- fEncodeCode(Source, CLike.Options.Brace.Inline, "(//=[^//n//S]*?{[^{//n]*?})");
- }
- if(CLike.Options.AnonFunc.Break)
- {
- //SQCLIKEANONFUNCMARKER
- Source[0] = Source[0].replace
- (
- /((?:/=|/:|,)[^/n/S]*?)(function[^/S]*?/()/gi
- ,"$1/n$2"
- );
- }
- // 是否断行左{
- if(CLike.Options.Brace.Left.BreakLeft)
- {
- Source[0] = Source[0].replace(//{/g, "/n{");
- }
- if(CLike.Options.Brace.Left.BreakRight)
- {
- Source[0] = Source[0].replace(//{/g, "{/n");
- }
- // 是否断行右}
- if(CLike.Options.Brace.Right.BreakLeft)
- {
- Source[0] = Source[0].replace(//}/g, "/n}");
- }
- if(CLike.Options.Brace.Right.BreakRight)
- {
- Source[0] = Source[0].replace(//}/g, "}/n");
- }
- // 清除空白
- fClearSpace(Source);
- if(CLike.Options.Func.Break)
- {
- Source[0] = Source[0].replace(/((function|void)[/s]*?[a-z0-9_$]+)/gi
- , CLike.Options.Func.Marker+"/n$1");
- }
- if(CLike.BreakLine.Before.Bool)
- {
- if(CLike.BreakLine.Before.ParenList!="")
- {
- var TempStr = fGetPtnBySepItem(CLike.BreakLine.Before.ParenList, "|", "|", "//b");
- var TempAr = ["((?:", TempStr, ")[//s]*?//()"];
- var TempRe = new RegExp(TempAr.join(""), "gi");
- Source[0] = Source[0].replace(TempRe
- , CLike.BreakLine.Before.Marker+"/n$1");
- }
- if(CLike.BreakLine.Before.BraceList!="")
- {
- var TempStr = fGetPtnBySepItem(CLike.BreakLine.Before.BraceList, "|", "|", "//b");
- var TempAr = ["((?:", TempStr, ")[//s]*?//{)"];
- var TempRe = new RegExp(TempAr.join(""), "gi");
- Source[0] = Source[0].replace(TempRe
- , CLike.BreakLine.Before.Marker+"/n$1");
- }
- }
- if(CLike.BreakLine.After.Bool)
- {
- if(CLike.BreakLine.After.General!="")
- {
- var TempStr = fGetPtnBySepItem(CLike.BreakLine.After.General, "|", "|", "//b");
- var TempAr = ["((?:", TempStr, ").*)"];
- var TempRe = new RegExp(TempAr.join(""), "gi");
- Source[0] = Source[0].replace(TempRe
- , "$1/n"+CLike.BreakLine.After.Marker);
- }
- }
- if(CLike.Options.Parenthesis.Indent)
- {
- // 括号缩进
- fMakeMarker
- (
- Source
- , CLike.Options.Parenthesis.BeginMarker
- , CLike.Options.Parenthesis.EndMarker
- , CLike.Options.Parenthesis.LevelAr
- , CLike.Options.Parenthesis.MarkerPostfix
- , "/("
- , "/)"
- , false
- );
- for(var i=0, j=CLike.Options.Parenthesis.LevelAr[0]; i<j; i++)
- {
- var PatternPostfix = i+CLike.Options.Parenthesis.MarkerPostfix;
- var sBegin = CLike.Options.Parenthesis.BeginMarker + PatternPostfix;
- var sEnd = CLike.Options.Parenthesis.EndMarker + PatternPostfix;
- var TempAr = ["(",sBegin,")","([//s//S]*?)","(",sEnd,")"];
- //alert(TempAr.join(""));
- var Re = new RegExp(TempAr.join(""), "g");
- var NoSpaceRe =
- new RegExp
- (
- [
- "([^//n//S]*("
- ,CLike.CommentSingleline.Marker
- ,"//d+|"
- ,CLike.CommentMultiline.Marker
- ,"//d+"
- ,")*//s*)"].join("")
- );
- Source[0] =
- Source[0].replace
- (
- Re
- ,function($0, $1, $2, $3)
- {
- //alert($0);
- var iLine = $2.split("/n").length;
- var Ar = [];
- Ar.push("(");
- var sSpaceBegin="";
- var sSpaceEnd="";
- $2 = $2.replace(NoSpaceRe, function($0, $1){sSpaceBegin=$1; return "";});
- $2 = $2.replace(/(/s*$)/, function($0, $1){sSpaceEnd=$1; return "";});
- if(iLine>2)
- {
- $2 = fUnshiftString($2, TempSpace);
- }
- Ar.push(sSpaceBegin);
- Ar.push($2);
- Ar.push(sSpaceEnd);
- Ar.push(")");
- return Ar.join("");
- }
- );
- } // end for
- }
- // 大括号块缩进
- fMakeMarker
- (
- Source
- , CLike.Options.Brace.BeginMarker
- , CLike.Options.Brace.EndMarker
- , CLike.Options.Brace.LevelAr
- , CLike.Options.Brace.MarkerPostfix
- , "{"
- , "}"
- , false
- );
- // 断行一级 }
- if(CLike.Options.Brace.Super.Separate)
- {
- var TempReStr =
- ["(",CLike.Options.Brace.EndMarker, 0, CLike.Options.Brace.MarkerPostfix,")","(.*)"];
- var TempRe = new RegExp(TempReStr.join(""), "g");
- Source[0] =
- Source[0].replace
- (
- TempRe
- , "$1$2"
- +CLike.Options.Brace.Super.Separater
- +CLike.Options.Brace.Super.Marker
- );
- }
- if(CLike.Options.AnonFunc.Break)
- {
- var TempAr =
- [
- "([^//n//S]*?(?:function|arguments//.callee)[//s]*?//([//s//S]*?"
- , CLike.Options.Brace.BeginMarker
- //, "[//d]+"
- , "(//d+)[//s//S]*?"
- , CLike.Options.Brace.EndMarker
- , "//2)"
- ];
- var TempRe = new RegExp(TempAr.join(""), "g");
- Source[0] = Source[0].replace
- (
- TempRe,
- function($0, $1)
- {
- return fUnshiftString($1, TempSpace);
- }
- )
- var TempAr =
- [
- "(function[^//S]*?//([//s//S]*?"
- ,CLike.Options.Brace.BeginMarker
- ,"(//d+)[//s//S]*?"
- ,CLike.Options.Brace.EndMarker
- ,"//2.*)"
- ];
- var TempRe = new RegExp(TempAr.join(""), "g");
- Source[0] = Source[0].replace
- (
- TempRe
- ,"$1/n"+CLike.Options.AnonFunc.Marker
- );
- }
- // 还原并缩进 {}
- for(var i=0, j=CLike.Options.Brace.LevelAr[0]; i<j; i++)
- {
- var PatternPostfix = i+CLike.Options.Brace.MarkerPostfix;
- var sBegin = CLike.Options.Brace.BeginMarker + PatternPostfix;
- var sEnd = CLike.Options.Brace.EndMarker + PatternPostfix;
- var TempAr = ["(",sBegin,")","([//s//S]*?)","(",sEnd,")"];
- //alert(TempAr.join(""));
- var Re = new RegExp(TempAr.join(""), "g");
- var NoSpaceRe =
- new RegExp
- (
- [
- "([^//n//S]*("
- ,CLike.CommentSingleline.Marker
- ,"//d+|"
- ,CLike.CommentMultiline.Marker
- ,"//d+)*//s*)"].join("")
- );
- Source[0] =
- Source[0].replace
- (
- Re
- ,function($0, $1, $2, $3)
- {
- //alert($0);
- var Ar = [];
- Ar.push("{");
- var sSpaceBegin="";
- var sSpaceEnd="";
- //alert($2)
- $2 = $2.replace(NoSpaceRe, function($0, $1){sSpaceBegin=$1; return "";});
- $2 = $2.replace(/(/s*$)/, function($0, $1){sSpaceEnd=$1; return "";});
- $2 = fUnshiftString($2, TempSpace);
- Ar.push(sSpaceBegin);
- Ar.push($2);
- Ar.push(sSpaceEnd);
- Ar.push("}");
- return Ar.join("");
- }
- );
- } // end for
- // 行默认空白字符
- fUnshiftString(Source, DefaultSpace);
- var TempRe, TempAr;
- fRemoveMarker(Source, CLike.Options.Brace.Super.Marker, "g");
- fRemoveMarker(Source, CLike.Options.Func.Marker, "g");
- fRemoveMarker(Source, CLike.BreakLine.Before.Marker, "g");
- fRemoveMarker(Source, CLike.BreakLine.After.Marker, "g");
- fRemoveMarker(Source, CLike.Options.AnonFunc.Marker, "g");
- fRemoveMarker(Source, "{[^//n//S]*", "g", "{");
- fRemoveMarker(Source, "//n[^//n//S]*//n([^//n//S]*})", "g", "/n$1");
- fRemoveMarker
- (
- Source, "({.*?//n)[^//n//S]*//n(.*?(//bfor//b|//bif//b|//bswitch//b)//s*?//()"
- , "g"
- , "$1$2"
- );
- // 清除 不必要的 断行
- TempAr =
- [
- "(//n[//s]*("
- ,CLike.CommentSingleline.Marker
- ,"|"
- ,CLike.CommentMultiline.Marker
- ,").*?//n)[^//n//S]*//n(.*?(//bfor//b|//bif//b|//bfunction//b)//s*[//w$]*?//()"
- ];
- fRemoveMarker(Source, TempAr.join(""), "g", "$1$3");
- fRemoveMarker(Source, "(}.*?//n)[^//n//S]*//n(.*?(?://bcatch//b|//belse if//b)//s*?//()", "g", "$1$2");
- fRemoveMarker(Source, "(}.*?//n)[^//n//S]*//n(.*?(?://belse//b)//s*?{)", "g", "$1$2");
- fRemoveMarker(Source, "({.*?//n)[^//n//S]*//n(.*?(//btry//b)//s*?{)", "g", "$1$2");
- fRemoveMarker(Source, "//n[^//n//S]*//n//s*//n", "g", "/n/n");
- TempAr =
- [
- "(}.*//n)"
- ,"[^//n//S]*?//n"
- ,"([//s]*?"
- ,CLike.CommentSingleline.Marker
- ,"|"
- ,CLike.CommentMultiline.Marker
- ,")"
- ];
- fRemoveMarker(Source, TempAr.join(""), "g", "$1$2");
- fRemoveMarker(Source, "//((?:[^//n//S]+)", "g", "(");
- //CLike.Options.Brace.Inline.Break
- fDecodeCode(Source, CLike.Options.Brace.Inline);
- fDecodeCode(Source, CLike.JsRegExp);
- fDecodeCode(Source, CLike.CommentSingleline);
- // 修正多行注释
- var iTempLen = CLike.CommentMultiline.Set.length;
- while(--iTempLen>-1)
- {
- var TempAr = ["//n([^//n//S]*)",CLike.CommentMultiline.Marker, iTempLen];
- var TempRe =
- new RegExp
- (TempAr.join(""), "g");
- //alert(TempAr.join(""))
- Source[0].replace
- (
- TempRe
- , function($0, $1, $2)
- {
- //alert(CLike.CommentMultiline.Set[iTempLen])
- var Ar = CLike.CommentMultiline.Set[iTempLen].split("/n");
- var Len = Ar.length;
- var ArReplace = [];
- ArReplace.push(Ar[0]);
- if(Len>=2)
- {
- var Str2 = Ar.slice(1, Len-1).join("/n");
- Str2 = fClearSpace(Str2);
- Str2 = fUnshiftString(Str2, $1+CLike.Options.Space);
- //alert(Str2)
- ArReplace.push(Str2);
- }
- if(Len>1)
- ArReplace.push("/n"+fUnshiftString(fClearSpace(Ar[Len-1]), $1));
- CLike.CommentMultiline.Set[iTempLen] = ArReplace.join("");
- }
- );
- } // end while
- fDecodeCode(Source, CLike.CommentMultiline);
- fDecodeCode(Source, CLike.String);
- fDecodeCode(Source, CLike.Escape);
- fStringTrim(Source, true);
- } // end function fGettingCLike(Source)
- function fGetPtnBySepItem(sSource, sSplit, sJoin, sAddiItem)
- {
- var Ar = sSource.split(sSplit);
- var iTemp = Ar.length;
- while(--iTemp>-1)
- {
- Ar[iTemp] = sAddiItem+Ar[iTemp]+sAddiItem;
- }
- return Ar.join(sJoin);
- }
- function fRemoveMarker(Source, Re, sFlag, sReplace)
- {
- if(Re.constructor == String)
- {
- Re = new RegExp(Re, sFlag);
- }
- if(!sFlag) sFlag = "g";
- if(!sReplace) sReplace = "";
- Source[0] = Source[0].replace(Re, sReplace);
- }
- function fExtendObjASI(Obj, Set, sPrefix, iCount)
- {// shawl.qiu code : void return
- if(!Set) Set = [];
- if(!iCount) iCount = 0;
- if(!sPrefix) throw new Error("参数出错!");
- Obj.Set = Set;
- Obj.Marker = sPrefix;
- Obj.Count = iCount;
- } // end function fExtendObjASI(Obj, Set, sPrefix, iCount)
- function fExtendObjSSSA(Obj, BeginMarker, EndMarker, MarkerPostfix, LevelAr)
- {// shawl.qiu code : void return
- Obj.BeginMarker = BeginMarker;
- Obj.EndMarker = EndMarker;
- Obj.MarkerPostfix = MarkerPostfix;
- Obj.LevelAr = LevelAr;
- } // end function fExtendObjSSSA(Obj, BeginMarker, EndMarker, MarkerPostfix, LevelAr)
- function fEncodeCode(Source, Obj, sPattern)
- {
- var Re = new RegExp(sPattern, "g");
- Source[0] = Source[0]
- .replace
- (
- Re
- ,function($0, $1)
- {
- Obj.Set.push($1);
- //alert($1);
- return Obj.Marker+(Obj.Count++);
- }
- );
- } // end function fEncodeCode(Source, Obj, sPattern)
- function fDecodeCode(Source, Obj)
- {
- var iTemp = Obj.Set.length;
- while(--iTemp>-1)
- {
- Source[0] =
- Source[0].replace
- (Obj.Marker+iTemp, Obj.Set[iTemp]);
- }
- } // end function fDecode
- function fMakeMarker
- (
- Nodes
- , PATTERNBEGIN
- , PATTERNEND
- , LevelAr
- , sIdPostfix
- , sLeft
- , sRight
- , bDebug
- )
- { // shawl.qiu code : return string
- if(Nodes[0].indexOf(sLeft)<0||Nodes[0].indexOf(sRight)<0) return Nodes[0];
- var Debug = bDebug;
- var Pos = [];
- var iStart = 0;
- var iOver = Nodes[0].length;
- var TempPostfix = LevelAr[0]+sIdPostfix;
- var TEMPPATTERNBEGIN = PATTERNBEGIN+TempPostfix;
- var TEMPPATTERNEND = PATTERNEND+TempPostfix;
- if(Debug)
- {
- document.write("Nodes: ", Nodes, "<p/>");
- document.write
- (
- "Nodes[0].indexOf(sLeft, iStart): "
- , Nodes[0].indexOf(sLeft, iStart)
- , "<br/>"
- );
- }
- while(iStart<iOver)
- {
- var LeftPos = Nodes[0].indexOf(sLeft, iStart);
- if(LeftPos<0) break;
- var RightPos = Nodes[0].indexOf(sRight, LeftPos);
- var TempStr = Nodes[0].substring(LeftPos, RightPos+1);
- var iLeftOccur = fStringGetTimes(TempStr, sLeft, true);
- var iRightOccur = fStringGetTimes(TempStr, sRight, true);
- if(iLeftOccur-iRightOccur!==0)
- {
- var Times = iLeftOccur - iRightOccur;
- RightPos = fGetNextPos(Nodes, Times, LeftPos, RightPos+1, sLeft, sRight);
- }
- if(RightPos<0) break;
- var TempNodes = [];
- TempNodes.push(Nodes[0].substring(0, LeftPos));
- TempNodes.push(TEMPPATTERNBEGIN);
- TempNodes.push(Nodes[0].substring(LeftPos+1, RightPos));
- TempNodes.push(TEMPPATTERNEND);
- TempNodes.push(Nodes[0].slice(RightPos+1));
- Nodes[0] = TempNodes.join("");
- var Span = TEMPPATTERNBEGIN.length + TEMPPATTERNBEGIN.length-2;
- iOver+=Span;
- RightPos+=Span-2;
- TempStr = Nodes[0].substring(LeftPos, RightPos+1);
- iLeftOccur = fStringGetTimes(TempStr, sLeft, true);
- iRightOccur = fStringGetTimes(TempStr, sRight, true);
- if(Debug)
- {
- document.write("TempNodes: ", TempNodes, "<br/>");
- document.write("LeftPos: ", LeftPos, "<br/>");
- document.write("RightPos: ", RightPos, "<br/>");
- document.write("TempStr: ", TempStr, "<br/>");
- document.write("iLeftOccur: ", iLeftOccur, "<br/>");
- document.write("iRightOccur: ", iRightOccur, "<br/>");
- document.write("<p/>");
- }
- iStart = RightPos+1;
- }
- LevelAr[0] = LevelAr[0]+1;
- if(Nodes[0].indexOf(sLeft)>-1&&Nodes[0].indexOf(sRight)>-1)
- {
- arguments.callee
- (
- Nodes
- , PATTERNBEGIN
- , PATTERNEND
- , LevelAr
- , sIdPostfix
- , sLeft
- , sRight
- , bDebug
- );
- }
- } // end function fMakeMarker
- function fGetNextPos(Nodes, Times, LeftPos, RightPos, sLeft, sRight)
- {
- if(Times<1) return RightPos;
- for(var i=0; i<Times; i++)
- {
- RightPos = Nodes[0].indexOf(sRight, RightPos);
- }
- var TempStr = Nodes[0].substring(LeftPos, RightPos+1);
- var iLeftOccur = fStringGetTimes(TempStr, sLeft, true);
- var iRightOccur = fStringGetTimes(TempStr, sRight, true);
- Times = iLeftOccur-iRightOccur;
- if(Times>0)
- {
- RightPos = arguments.callee(Nodes, Times, LeftPos, RightPos+1, sLeft, sRight);
- }
- return RightPos;
- }
- function fStringGetTimes(Source, sMatch, bCharacter)
- {// shawl.qiu code : return integer
- var iTime=0;
- var Re = null;
- if(Source.constructor==String)
- {
- Source = [Source];
- }
- if(!bCharacter)
- {
- Re = new RegExp('//b('+sMatch+')//b', 'gi');
- }
- else
- {
- sMatch = "//"+sMatch;
- Re = new RegExp('('+sMatch+')', 'gi');
- }
- Source[0].replace
- (
- Re
- , function($0,$1)
- {
- if($1!='')iTime+=1;
- }
- );
- return iTime;
- } // end function fStringGetTimes
- function fUnshiftString(Source, sForUnshift)
- {// shawl.qiu code : return string
- var bString = false;
- if(Source.constructor==String)
- {
- Source = [Source];
- bString = true;
- }
- Source[0] = Source[0]
- .replace
- (
- /^([/s/S])/g
- ,sForUnshift+"$1"
- );
- Source[0] = Source[0]
- .replace
- (
- //n([/s/S])/g
- ,"/n"+sForUnshift+"$1"
- );
- if(bString)
- {
- return Source[0];
- }
- } // end function fUnshiftString(Source, sForUnshift)
- function fClearSpace(Source)
- {// shawl.qiu code : return string
- var bString = false;
- if(Source.constructor==String)
- {
- Source = [Source];
- bString = true;
- }
- Source[0] = Source[0]
- .replace
- (
- /^/s+|/s+$/g
- ,""
- );
- Source[0] = Source[0]
- .replace
- (
- //n/s+/g
- ,"/n"
- );
- Source[0] = Source[0]
- .replace
- (
- //s*?/n/g
- ,"/n"
- );
- if(bString)
- {
- return Source[0];
- }
- } // end function fClearSpace
- function fStringSpace(nStr, nTime)
- {
- var nLen=arguments.length;
- if(nLen===0)
- {
- nStr=' '
- nTime=2;
- }
- if(nLen==1)
- {
- nStr+=' ';
- nTime=2;
- }
- return new Array(nTime+1).join(nStr);
- } // end function fStringSpace
- function fGetAu()
- {
- return Au;
- }
- function fClearRepeatLine(sSource)
- {// shawl.qiu code : return string
- return sSource.replace(//n[/s]*/n/g, "/n");
- } // end function fClearRepeatLine
- function fStringTrim(Source, bLine)
- {// shawl.qiu code : return string
- var bString = false;
- if(Source.constructor==String)
- {
- Source = [Source];
- bString = true;
- }
- if(!bLine)
- {
- Source[0] = Source[0].replace(/^/s+|/s+$/g, '');
- }
- else
- {
- Source[0] = Source[0].replace(/^[^/n/S]*?/n|/s+$/g, '');
- }
- if(bString)
- {
- return Source[0];
- }
- } // end function fStringTrim(Source)
- //------------------------------------end private method
- } // shawl.qiu code
- //---------------------------------end class StyleEngine()---------------------------------//