#{ Author: Limin Fu Email: phoolimin@gmail.com Date: 2007-01-22 License: GPL #} load aux; load sdml_parser; class DaoCodeHL { const Library = { "io"=>1, "std"=>1, "mt"=>1, "sys"=>1, "math"=>1 }; const Replace = { "<"=>"<", ">"=>">" }; const Block = { "#"=>"", "\'"=>"", "\""=>"" }; const HilitMap = { "var" => "", "const" => "", "global" => "", "static" => "", "interface" => "", "class" => "", "routine" => "", "operator" => "", "syntax" => "", "as" => "", "public" => "", "protected" => "", "private" => "", "do" => "", "if" => "", "else" => "", "while" => "", "for" => "", "in" => "", "switch" => "", "case" => "", "default" => "", "skip" => "", "break" => "", "return" => "", "yield" => "", "load" => "", "use" => "", "frame" => "", "defer" => "", "type" => "", "enum" => "", "int" => "", "long" => "", "float" => "", "double" => "", "complex" => "", "string" => "", "list" => "", "map" => "", "tuple" => "", "array" => "", "stream" => "", "cdata" => "", "read" => "", "write" => "", "writeln" => "", "size" => "", "clear" => "", "key" => "", "value" => "", "append" => "", "push" => "", "pop" => "", "insert" => "", "exit" => "", "error" => "", "warn" => "", "none" => "", "self" => "", "and" => "", "or" => "", "not" => "", "in" => "", "(" => "", ")" => "", "[" => "", "]" => "", "{" => "", "}" => "", "::" => "", ":=" => "", "=>" => "", "=~" => "", "!~" => "", "~~" => "", "<@>" => "", "..." => "" }; public routine Highlight( source : string, name = '' )=>string; } routine DaoCodeHL::Highlight( source : string, name = '' )=>string { tokens = std.tokenize( source ); output = '
\n';

	size = tokens.size();
	i = 0;
	while( i < size ){
		tok = tokens[i];
		if( Replace.find( tok ) != none ){
			repl = Replace[tok];
			output += repl;
		}else if( HilitMap.find( tok ) != none ){
			tag = HilitMap[tok];
			output += tag + tok + "";
		}else if( Library.find( tok ) != none ){
			output += "" + tok;
			if( i+2", ">" );
			output += tag + tmp + "";
		}else if( tok.match( '^ ( 0 (x|X) %x+ | %d[%d%.]+ | [%C_%d]+ ) $' ) != none ){
			output += "" + tok + "";
		}else if( tok.match( '^ %$ %w+ $' ) != none ){
			output += "" + tok + "";
		}else if( i+2 ", ">" );
			output += tmp;
		}else{
			output += tok;
		}
		i += 1;
	}
	output += "
\n"; return output; } class CppCodeHL { const Replace = { "<"=>"<", ">"=>">" }; const Block = { "\'"=>"", "\""=>"" }; const HilitMap = { "const" => "", "extern" => "", "static" => "", "class" => "", "struct" => "", "enum" => "", "public" => "", "protected" => "", "private" => "", "do" => "", "if" => "", "else" => "", "while" => "", "for" => "", "switch" => "", "case" => "", "default" => "", "continue" => "", "break" => "", "return" => "", "signed" => "", "unsigned" => "", "char" => "", "bool" => "", "int" => "", "short" => "", "float" => "", "double" => "", "void" => "", "sizeof" => "", "typedef" => "", "typeid" => "" }; public routine Highlight( source : string, name = '' )=>string; } routine CppCodeHL::Highlight( source : string, name = '' )=>string { tokens = tokenize( source, "#!%^&*()-=+|/{}[];:,.~<>? \t\n\r", "\'\"", 1 ); output = '
\n';

	size = tokens.size();
	i = 0;
	while( i < size ){
		tok = tokens[i];
		if( Replace.find( tok ) != none ){
			output += Replace[tok];
		}else if( HilitMap.find( tok ) != none ){
			output += HilitMap[tok] + tok + "";
		}else if( Block.find( tok[0:0] ) != none ){
			tmp = tok;
			tmp = tmp.replace( "<", "<" );
			tmp = tmp.replace( ">", ">" );
			output += Block[ tok[0:0] ] + tmp + "";
		}else if( tok.match( '^ ( 0 (x|X) %x+ | %d[%d%.]+ | [%C_%d]+ ) $' ) != none ){
			output += "" + tok + "";
		}else if( tok == "#" ){
			output += "" + tok + tokens[i+1] + "";
			i += 1;
		}else if( tok == "/" && i+1= size ) break;
				tok = tokens[i];
			}
			tmp = tmp.replace( "<", "<" );
			tmp = tmp.replace( ">", ">" );
			output += tmp;
			output += "";
		}else if( tok == "/" && i+1", ">" );
			output += tmp;
			output += "";
		}else{
			output += tok;
		}
		i += 1;
	}
	output += "
\n"; return output; } class BnfCodeHL { const Replace = { "<"=>"<", ">"=>">" }; const Block = { "\'"=>"", "\""=>"" }; const HilitMap = { "(" => "", ")" => "", "[" => "", "]" => "", "{" => "", "}" => "", "::=" => "", "|" => "", "&" => "", "+" => "", "*" => "" }; public routine Highlight( source : string, name = '' )=>string; } routine BnfCodeHL::Highlight( source : string, name = '' )=>string { tokens = tokenize( source, "|+*(){}[] \t\n\r", "\'\"", 1 ); output = '
\n';

	size = tokens.size();
	i = 0;
	while( i < size ){
		tok = tokens[i];
		if( Replace.find(tok) != none ){
			output += Replace[tok];
		}else if( HilitMap.find(tok) != none ){
			output += HilitMap[tok] + tok + "";
		}else if( Block.find( tok[0:0] ) != none ){
			tag = Block[ tok[0:0] ];
			tmp = tok;
			tmp = tmp.replace( "<", "<" );
			tmp = tmp.replace( ">", ">" );
			output += tag + tmp + "";
		}else{
			output += tok;
		}
		i += 1;
	}
	output += "
\n"; return output; } class SdmlCodeHL2 { const Replace = { "<"=>"<", ">"=>">" }; const Block = { "\'"=>"", "\""=>"" }; const HilitMap = { "(" => "", ")" => "", "[" => "", "]" => "", "{" => "", "}" => "", "::=" => "", "|" => "", "+" => "", "*" => "" }; public routine HL( source : string )=>string; routine Highlight( source : string, name = '' )=>string; } routine SdmlCodeHL2::HL( source : string )=>string { const pat1 = '%B{ %<%s* ([%w=]+ |%* |%% |_) [%s%w]* %> }{ %< %s* / %s* ([%w=]+ |%* |%% |_) %s*%> }'; const pat2 = '^( %<%s* ([%w=]+ |%* |%% |_) [%s%w]* %> ) (.*) ( %<%s* / %s* %2 %s*%> )$'; const kv = { 'title' => '', 'article' => '', 'report' => '', 'book' => '', "table" => "", "list" => "", "code" => "", "bf" => "", "it" => "", "*" => "", "%" => "", "_" => "", '' => '' }; const kv2 = { "list" => "", "table" => "", "code" => "", "bf" => "", "it" => "", "*" => "", "%" => "", "_" => "", '' => '' }; tokens = source.extract( pat1, $both ); output = ''; for( tok in tokens ){ tks = tok.capture( pat2 ); if( %tks ){ tag = tks[2]; tk1 = tks[1]; tk4 = tks[4]; Replace.iterate { tk1 = tk1.replace( X, Y ) } Replace.iterate { tk4 = tk4.replace( X, Y ) } html = ''; html2 = ''; html3 = ''; if( kv.find( tag ) != none ) html = kv[tag]; if( kv2.find( tag ) != none ){ html2 = kv2[tag]; html3 = ''; } output += html + tk1 + '' + html2; output += HL( tks[3] ); output += html3 + html + tk4 + ''; }else{ Replace.iterate { tok = tok.replace( X, Y ) } output += tok; } } return output; } routine SdmlCodeHL2::Highlight( source : string, name = '' )=>string { output = '
\n';
	output += HL( source );
	output += "
\n"; return output; }