黑马程序员技术交流社区

标题: JS实现的MarkDwon解释器 [打印本页]

作者: yxz    时间: 2013-8-27 17:30
标题: JS实现的MarkDwon解释器
几天前做的MD解释器拿出来和大家交流一下吧。现在已经实现了标题、段落和换行、强调、分隔线
MarkDown的内容请看这里http://www.oschina.net/question/100267_75314
目前还很不完善,不过重要功能都已经实现了,其他功能也就不在话下了。
最近没时间搞这个了,我也是云六的,自荐信只拿了7分,真愁人。
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4.     <title>MarkWown</title>
  5. </head>
  6. <body>
  7. <textarea style="width: 400px;height: 300px" id="rawtext"></textarea>     <p>
  8. <input type="button" value="submit" onclick="Compiler=new compiler('rawtext','OutputHTML');
  9. Compiler.encode()">
  10. <div id="OutputHTML"></div>

  11. <script>
  12.     function compiler(mid,pid){
  13.         var rawlist=document.getElementById(mid);
  14.         var raws="";
  15.         var rawi=0;
  16.         var regexlst;
  17.         var mdbox=document.getElementById(pid);
  18.         var passline=[];

  19.         dict=[
  20.             {"match":/^#{1,6}/,"value":"_ishead()"},
  21.             {"match":/^\*{3,}$/,"value":"_outkit('<hr/>')"},
  22.             {"match":/^\-{3,}$/,"value":"_outkit('<hr/>')"},
  23.             {"match":/^[\* ]*$/,"value":"_outkit('<hr/>')"},
  24.             {"match":/((^(\*|\_){1,3})|((\*|\_){1,3}$))/g,"value":"_textstype()"},
  25.             {"match":/^\- \- \-/,"value":"_outkit('<hr/>')"}
  26.         ];

  27.         function getrawstring(){
  28.             if(rawlist.value==""){
  29.                 return 0;
  30.             }
  31.             mdbox.innerText="";
  32.             rawlist=rawlist.value.split("\n")
  33.             _encode();
  34.         }

  35.         //处理文字样式
  36.         function _textstype(){
  37.             if(regexlst.length<2||regexlst[0].length==0||regexlst[1].length==0){
  38.                 _outkit(raws);
  39.             }else{
  40.                 lst=Min(regexlst[0].length,regexlst[1].length);
  41.                 raws=raws.substring(lst[0],raws.length-lst[0])
  42.                 switch (lst[0]){
  43.                     case 1:
  44.                         _outkit("<em>"+raws+"</em>");break;
  45.                     case 2:
  46.                         _outkit("<strong>"+raws+"</strong>");break;
  47.                     case 3:
  48.                         _outkit("<em><strong>"+raws+"</strong></em>");
  49.                 }
  50.             }
  51.         }


  52.         //图片处理
  53.         function _isimage(){

  54.         }

  55.         //处理开头
  56.         function _ishead(){
  57.             mf=regexlst[0].length;
  58.             _outkit("<h"+mf+">"+raws.substr(mf)+"</h"+mf+">");
  59.         }

  60.         function _outkit(html){
  61.             mdbox.innerHTML+=html;
  62.         }

  63.         //处理-和=的情况
  64.         function _ismaintitle(){
  65.             for(raw in rawlist){
  66.                 if(rawlist[raw-1]!=undefined){
  67.                     if(rawlist[raw].match(/^\-{1,}$/)!=null){
  68.                         passline.push(raw-1);
  69.                         rawlist[raw-1]="<h2>"+rawlist[raw-1]+"</h2>"
  70.                         rawlist.splice(raw,1);
  71.                     }else if(rawlist[raw].match(/^\={1,}$/)!=null){
  72.                         passline.push(raw-1);
  73.                         rawlist[raw-1]="<h1>"+rawlist[raw-1]+"</h1>"
  74.                         rawlist.splice(raw,1);
  75.                     }
  76.                 }
  77.             }
  78.         }

  79.         //最小的数
  80.         function _sort(a, b){
  81.             return a - b
  82.         }
  83.         function Min(){
  84.             var ilist=[]
  85.             for(i in arguments)
  86.                 ilist.push(arguments[i])
  87.             return ilist.sort(_sort);
  88.         }

  89.         //判断array是否存在某个item
  90.         function isExist(arr,item){
  91.             var sum=0;
  92.             for(i in arr){
  93.                 if(arr[i]===item)
  94.                     sum++;
  95.             }
  96.             return sum;
  97.         }

  98.         function _encode(){
  99.             _ismaintitle();
  100.             for(rawi in rawlist){
  101.                 raws=rawlist[rawi];
  102.                 if(raws==""){
  103.                     _outkit("<p>");
  104.                     continue;
  105.                 }
  106.                 if(isExist(passline,rawi)){
  107.                     _outkit(raws);
  108.                     continue;
  109.                 }
  110.                 for(j in dict){
  111.                     if((regexlst=raws.match(dict[j].match))!=null){
  112.                         eval(dict[j].value);
  113.                         break;
  114.                     }
  115.                     if(j==(dict.length-1))
  116.                         _outkit(raws);
  117.                 }
  118.             }
  119.         }
  120.         this.encode=getrawstring;
  121.     }
  122. </script>
  123. </body>
  124. </html>
复制代码

作者: yxz    时间: 2013-8-27 17:31
版主看看能不能加点技术分。




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2