#{ Author: Limin Fu Email: phoolimin@gmail.com Date: 2008-12-20 #} load sdml_parser; load sdml_codehl; class Sdml2Html : SdmlParser { routine Sdml2Html(); routine New() => SdmlParser { return Sdml2Html(); } routine Warn( info : string ); routine Error( info : string ); routine AddPrefix(); routine Replace( src = '' )=>string; routine OpenHeader( dtype = '' ); #routine CloseHeader(){ output += '\n'; } routine MakeTitle( title = '' ); routine MakeAuthor( name = '' ); routine MakeTOC( sect : TextSection ); routine OpenBody(); routine MakeImage( name = '' ); routine MakeLink( name='', tok1='', tok2='' )=>string; routine MakeTable( headers : list, cells : list >, param='' ); routine OpenSection( level=0, numbering='' ); routine CloseSection( level=0, numbering='' ); } routine Sdml2Html::Sdml2Html() { codeparser[ 'dao' ] = DaoCodeHL(); codeparser[ 'cpp' ] = CppCodeHL(); codeparser[ 'bnf' ] = BnfCodeHL(); codeparser[ 'sdml' ] = SdmlCodeHL2(); makesuffix = 1; linebreak = '
'; orderlist = ( '
    ', '\n
\n' ); unorderlist = ( '\n' ); listitem = ( '\n
  • ', '
  • \n' ); empty_table = '\n
     
    '; quotebegin = "
    "; quoteend = "
    "; quotename1 = ""; quotename2 = ":

    "; BasicTagMap = { "<*>" => ( "", "", "") , "<%>" => ( "", "", "" ) , "<->" => ( "", "", "" ) , "<_>" => ( "", "", "" ) , "" => ( "", "", "" ) , "" => ( "", "", "" ) , "" => ( "", "", "" ) , "" => ( "", "", "" ) , "" => ( "", "", "" ) }; #io.writeln( codeparser ); } routine Sdml2Html::Warn( info : string ) { output += '

    WARNING: ' + info + '!
    \n'; std::error( info ) } routine Sdml2Html::Error( info : string ) { output += '
    ERROR: ' + info + '!
    \n'; std::error( info ) } routine Sdml2Html::OpenHeader( dtype = '' ) { # output += '\n'; } routine Sdml2Html::AddPrefix() { if( makeheader ==0 ) return; output += '
    '; if( output.find( suffix ) >=0 ){ preface = "\n" + preface; output = "\n" + output; } } routine Sdml2Html::MakeTitle( title = '' ) { output += '

    ' + title + '

    \n'; } routine Sdml2Html::MakeAuthor( name = '' ) { name = name.change( '( %w[%w%.]* ) %s* @ %s* (%w+) %s* %. %s* (%w+)', '%1%2%3' ); output += '

    ' + name; output += '

    '; #output += '

    ' + std.ctimef( 0, '%Y-%M-%D' ) + '

    '; } routine Sdml2Html::OpenBody() { } routine Sdml2Html::MakeTOC( sect : TextSection ) { if( sect.level > top_level + 1 ) return; if( sect.subsections.size() ==0 ) return; tpl = '
  • $(name)
  • \n' toc = '\n
      '; for( sc in sect.subsections ) toc += tpl.expand( (index = sc.numbering, name = sc.sectname) ); output = output.insert( toc + '
    ', sect.bodyStart ); } routine Sdml2Html::MakeImage( name = '' ) { const html_image = '
    @(tooltip_$(suffix))
    '; if( name.match( '^ %w+ $' ) != none ){ expand = { 'align' => 'right', 'suffix' => suffix, 'name' => '' }; expand[ 'name' ] = name; output += html_image.expand( expand ); }else{ output += ''; } } routine Sdml2Html::MakeLink( name='', tok1='', tok2='' )=>string { code = "" + name + ""; return code; } routine Sdml2Html::MakeTable( headers :list, cells :list >, param='' ) { parser = Sdml2Html(); parser.suffix = suffix; parser.makesuffix = 0; parser.makeheader = 0; rows = cells.size(); colm = headers.size(); if( colm ==0 ) colm = cells[0].size(); table = ""; hsize = headers.size(); if( hsize ) table += ""; for( j = 0 : hsize-1 ) table += "" + parser.Parse( headers[j] ) + ""; if( hsize ) table += "\n"; for( i = 0 : rows-1 ){ if( cells[i].size() ==0 ){ table += ''; for( j = 0 : colm-1 ) table += ''; table += '\n'; skip; } table += ""; for( j = 0 : colm-1 ) table += "" + parser.Parse( cells[i][j] ) + ""; table += "\n"; } output += '\n\n'; output += table; output += "
    \n"; } routine Sdml2Html::OpenSection( level=0, numbering='' ) { tag = "H" + (string) (level+1); output += ""; output += "<" + tag + ">" + numbering + " "; } routine Sdml2Html::CloseSection( level=0, numbering='' ) { tag = "H" + (string) (level+1); output += "\n"; output += "@(edit_" + suffix + ") "; output += "@(top_" + suffix + ")\n"; #io.writeln( subsect.numbering, subsect.sectname ); } routine Sdml2Html::Replace( src = '' ) => string { src = src.replace( '<', '<' ); src = src.replace( '>', '>' ); src = src.change( '[\n] %s* [\n] %s* [\n]', '

    \n' ); src = src.change( '[\n] %s* [\n]', '
    \n' ); return src; } src = @[sdml] abc class A { var i } -- item; -- item; -- item; -- item; -- item; -- item; -- item; -- item; @[sdml] #src = io.read( 'test2.sdml' ) parser = Sdml2Html(); fout = io.open( 'test.html', 'w' ); html = parser.Parse( src ) io.writeln( html ) fout.writeln( html ); fout.close(); #{ #} #{ routine main( input : string, output = "" ) { src = io.read( input ); if( output.size() ==0 ){ dot = input.find( ".", -1, 1 ); if( dot >=0 ){ output = input[:dot] + "html"; }else{ output = input + ".html"; } } fout = io.open( output, "w" ); parser = Sdml2Html(); fout.writeln( parser.Parse( src ) ); fout.close(); } #}