#include #include #include "markdown.h" // void process_markdown() // { // MyTSchar& fileName; // // // Tcl_Obj* readObjPtr=Tcl_NewObj(); // Tcl_Channel channel=Tcl_OpenFileChannel(stateM.ip_,fileName.v(),"r",0); // if(!channel){ // return cu_print_error(stateM.ip_,"Could not open file '%s'",fileName.v()); // } // Tcl_SetChannelOption(stateM.ip_,channel,"-encoding","utf-8"); // Tcl_ReadChars(channel,readObjPtr,2000,0); // char* tbuf=(char*)Tcl_GetStringFromObj(readObjPtr,&length); // result=Tcl_Close(stateM.ip_,channel); // // Tcl_DecrRefCount(readObjPtr); // return 0; // } inline int is_chars(int mychar,const char* chars) { return strchr(chars,mychar)!=NULL; } inline int is_same_char_line(MyTSchar& buffer,int start,int end, int allow_spaces,int min_chars) { int num_chars=1; for(int i=start+1;i=min_chars) return 1; else return 0; } inline int num_same_char_line_prefix(MyTSchar& buffer,int start,int end, int allow_spaces) { int num_chars=1; for(int i=start+1;i1) return 0; } else if(smaller_than==0){ if(parentheses) return 0; return i; } else return 0; } else if(is_chars(buffer[i],"<")){ if(!is_open){ is_open=1; smaller_than=1; } else if(smaller_than>0){ return 0; } } else if(is_chars(buffer[i],">")){ if(smaller_than>0){ if(parentheses) return 0; return i+1; } is_open=1; } else if(is_chars(buffer[i],"(")){ parentheses++; is_open=1; } else if(is_chars(buffer[i],")")){ parentheses--; is_open=1; } else if(is_chars(buffer[i],"\\")){ is_open=1; i++; } else { is_open=1; } } if(is_open && smaller_than==0) return buffer.num(); return 0; } inline int spaces_num(MyTSchar& buffer,int start,int& num_spaces) { num_spaces=0; for(int i=start;i1) return 0; } else { if(num_enlinesA>0) return 0; num_enlinesA++; } } else if(is_chars(buffer[i],"\"'")){ if(!is_open){ openC=buffer[i]; is_open=1; } else if(buffer[i]==openC){ return i+1; } } else if(is_chars(buffer[i],"(")){ if(!is_open){ openC=buffer[i]; is_open=1; } else if(openC=='('){ return 0; } } else if(is_chars(buffer[i],")")){ if(!is_open) return 0; else if(openC=='('){ return i+1; } } else if(is_chars(buffer[i],"\\")){ if(!is_open) return 0; i++; } else { if(!is_open) return 0; is_open=1; } } return 0; } inline int link_url_num(MyTSchar& buffer,int start) { int is_open=0,num_chars=0; for(int i=start;igive_type(parent_level)!=MK::block_quote){ if(this->give_curr_type()==MK::fenced_code_block){ goto no_block_quote; } while(open_blocks_.num()>parent_level) this->curr_close(); this->append_block(MK::block_quote,SE_line_,SE_lineNoBlank_, blanks_line_); } SE_line_[0]=SE_lineNoBlank_[0]+1; if(is_chars(buffer[SE_line_[0]]," ")) SE_line_[0]++; SE_lineNoBlank_[0]=spaces_num(buffer,SE_line_[0],blanks_line_[0]); this->create_append_paragraph(buffer,parent_level+1); return; } if(parent_level>0 && this->give_type(parent_level)==MK::block_quote){ while(open_blocks_.num()>parent_level) this->curr_close(); } :no_block_quote; //################################################################################ // fenced_code_block //################################################################################ if(this->give_curr_type()==MK::fenced_code_block){ if(blanks_line_[0]<=3 && is_chars(buffer[SE_lineNoBlank_[0]],"`~")){ MKblocks& block=blocks_[open_blocks_[end_MTS]]; int valid=is_same_char_line(buffer,SE_lineNoBlank_[0], SE_lineNoBlank_[1],0,block.level_); if(buffer[SE_lineNoBlank_[0]]!=buffer[block.posNoBlank_[0]]) valid=0; if(valid){ block.pos_[1]=SE_line_[1]; block.posNoBlank_[1]=SE_lineNoBlank_[1]; block.blanks_[1]=blanks_line_[1]; this->curr_close(); return; } } MKblocks& block=blocks_[open_blocks_[end_MTS]]; block.pos_[1]=SE_line_[1]; block.posNoBlank_[1]=SE_lineNoBlank_[1]; block.blanks_[1]=blanks_line_[1]; return; } if(blanks_line_[0]<=3 && is_chars(buffer[SE_lineNoBlank_[0]],"`~")){ int level=num_same_char_line_prefix(buffer,SE_lineNoBlank_[0],SE_lineNoBlank_[1],0); if(level>=3){ if(blockquote_levelN!=blockquote_level) this->close_all(); else if(this->curr_is_open()) this->curr_close(); MKblocks& block=this->append_block(MK::fenced_code_block,SE_line_, SE_lineNoBlank_,blanks_line_); block.level_=level; return; } } //################################################################################ // indented_code_block //################################################################################ if(this->give_curr_type()==MK::indented_code_block){ if(blanks_line_[0]>=4){ MKblocks& block=blocks_[open_blocks_[end_MTS]]; block.pos_[1]=SE_line_[1]; block.posNoBlank_[1]=SE_lineNoBlank_[1]; block.blanks_[1]=blanks_line_[1]; return; } this->curr_close(); } if(!this->curr_is_open() && blanks_line_[0]>=4){ this->append_block(MK::indented_code_block,SE_line_,SE_lineNoBlank_, blanks_line_); return; } //################################################################################ // setext_heading //################################################################################ if(this->give_curr_type()==MK::paragraph && blanks_line_[0]<=3 && is_chars(buffer[SE_lineNoBlank_[0]],"-=")){ int valid=is_same_char_line(buffer,SE_lineNoBlank_[0],SE_lineNoBlank_[1],0,1); if(valid){ MKblocks& block=blocks_[open_blocks_[end_MTS]]; block.mtype_=MK::setext_heading; if(buffer[SE_lineNoBlank_[0]]=='=') block.level_=1; else block.level_=2; this->curr_close(); return; } } //################################################################################ // thematic_break //################################################################################ if(blanks_line_[0]<=3 && is_chars(buffer[SE_lineNoBlank_[0]],"-_*")){ int valid=is_same_char_line(buffer,SE_lineNoBlank_[0],SE_lineNoBlank_[1],1,3); if(valid){ if(blockquote_levelN!=blockquote_level) this->close_all(); else if(this->curr_is_open()) this->curr_close(); this->append_block(MK::thematic_break,SE_line_, SE_lineNoBlank_,blanks_line_); this->curr_close(); return; } } //################################################################################ // ATX_heading //################################################################################ if(blanks_line_[0]<=3 && is_chars(buffer[SE_lineNoBlank_[0]],"#")){ IntVector2D level,start_end,blank; level[0]=1; for(int i=SE_lineNoBlank_[0]+1;i0){ level[1]++; } else if(is_chars(buffer[i]," \t")){ if(start_end[0]==0) blank[0]++; else blank[1]++; } else { if(start_end[0]==0) start_end[0]=i; start_end[1]=i; blank[1]=0; } } if(level[0]>=6 && blank[0]>=1){ if(blockquote_levelN!=blockquote_level) this->close_all(); else if(this->curr_is_open()) this->curr_close(); start_end[1]++; MKblocks& block=this->append_block(MK::ATX_heading,SE_line_,start_end, blanks_line_); block.level_=level[0]; this->curr_close(); return; } } //################################################################################ // HTML_block //################################################################################ if(this->give_curr_type()==MK::HTML_block){ const char* rexsEnd[]={ "<(?in)\\A?$", "(?in)\\A.*-->", "(?in)\\A.*\\?>", "(?in)\\A.*>", "(?in)\\A.*\\]\\]>", "(?in)\\A\\s*$", "(?in)\\A\\s*$" }; MKblocks& block=blocks_[open_blocks_[end_MTS]]; block.pos_[1]=SE_line_[1]; block.posNoBlank_[1]=SE_lineNoBlank_[1]; block.blanks_[1]=blanks_line_[1]; if(buffer.regexp(SE_lineNoBlank_[0],rexsEnd[block.level_-1])){ this->curr_close(); } return; } if(blanks_line_[0]<=3 && is_chars(buffer[SE_lineNoBlank_[0]],"<")){ const char* rexsStart[]={ "(?in)\\A<(script|pre|style)\\s*>?$", "(?in)\\A