<%

'MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
sub compile_source(pnow)  
'mmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmm

dim WName 'KK.07.17.01
dim c1,c2,c3
dim gosml, godown
dim WSCut, WSCutLen 
dim j,k
dim wi,ws


'set_debug
source = source & ve & ve
'set_debug
if deb then printl " source=" & source
i = parse_s_ml_header(1,source,false)
'"false" to not redefine pars. if not found;


TLen = len(source)
t  = ""  ' great beginning, empty page
tt = ""

d = 0
makei(d)
p(0)  = get_i("#start")
tag_state = false
set_state_attr
SelfNamedFlag = "" 'KK.07.17.01





do while i <= Tlen - 2

c1 = mid(source, i,   1)
c2 = mid(source, i+1, 1)
c3 = mid(source, i+2, 1)
c  = c1
 
if deb then printl "curr state(" & pcurrent & ")=`" & elemcur & "`  current characters=c1,c2,c3 =`"  & _
c1 & "`  `" & c2 & "`  `" & c3 & "` tag=" & tag_state & " was established by encountering `" & pscut(d) & "` " & " i=" & i


godown = false         ' assume that we will meet some special chars ...
gosml  = true          ' assume not in the foreign script area ...




'====================================================================================== 
'  FOREIGH SCRIPT AREA 
'--------------------------------------------------------------------------------------
if ForeignScriptFlag then 
     'we are inside of Foreign script
     gosml = false                    'if current element is 
                                      'foreign script, then do not shor-ml it ...
     if lcase(c1) = contrcurr and _
        c2 = xescape and _
        isNeutral(c3) then            '\ is not Neutral; \\ - won't be caught;
                                      'triggering from foreign script;
        gosml = true                  'set gosml flag to swith to sml-mode:
                                      'pass c1,c2,c3 to handle in sml code;
     else
                                      'byte by byte output is a horrible menthod:
        add c1                        'make normal output inside of foreign script;
     end if
end if
'--------------------------------------------------------------------------------------
'ef FOREIGH SCRIPT AREA 
'====================================================================================== 






if gosml then
'====================================================================================== 
' SHORT ML AREA 
'-------------------------------------------------------------------------------------- 
' SO "\" CAN NOT BE INPUT IN TAG AREA ITSELF; INLY IN FORM "\."
' EXCEPTIONS: TAG PARAMENTERS STRING AREAS ...

if c1 = xescape then                  '=A; IF FIRST CHAR IS AN ESCAPE
                                      'first escape is handled only in this =A block;
     if c2=xself then
        addi xescape                  'add xescape char itself; 
                                      '\. has maximum precedence for \.x-triple;
        i=i+1                         'this results in making c3-position current; 

     elseif c2=SML_E_key then         ' ^-mode
       
        'EXTENTION OF SML-SCRIPT: ADDING SHORTCUTS 
        'LONGER THAN 2 CHARS E=E=E=E=E=E=E=E=E=E=E=E=E=E=E=E=E=E=E
        'if SML_E_long(i_next_el) then
        'so, at position i+1 found the first char of the long-scut;
        for SML_E_i = 1 to SML_E_L
            WSCut = SML_E_scut(SML_E_i)
            WSCutLen = len(WSCut)
            if mid( source, i+1, WSCutLen) = WScut then
               'no need to terminate longscut;
               add SML_E_value(SML_E_i)
               i = i + WSCutLen      '+1=> swallows terminator char, not good;
               exit for
            end if
         next    
         'E=E=E=E=E=E=E=E=E=E=E=E=E=E=E=E=E=E=E=E=E=E=E=E=E=E=E=E=E


    elseif tag_state then       ' THIS IS A TAG STATE, CLOSE THE TAG ...
                                '// tag extends; escape IS PRESERVED (DEFFERED) FOR the next scan
                                '// and will CLOSE AN ELEMENT if neutral char will be encountered: 
                                '// \D moo \space   - will do <td></td>
           extend_e           
                                'TO RESET SYNTAX:
                                'useless closing-chain-reaction cause by \" "
                                'can be avoided if to put:
                                'if c2=inNeutral then i=i+1

           i=i-1                ' keep escape conditions for next step handling ...        
    elseif isNeutral(c2) or _
        c2 = xescape then       ' to the beginning of loop
        close_e
        'preserve escaper, cr, lf; swallow others:
        select case c2
           case xescape
           case " ", chr(9)
              i=i+1
           case else
              add c2
              i=i+1
        end select
    else                        
        get_i_el c2
        if i_next_el > 0 then                   ' opening normally ...
           if i_next_el = p(d) then  close_e    ' this turns on option  \D   ..... \D which clolses previous \D 
                                                ' first and then opens a new \D
                                                ' !!!! important turn: <td> ..</td><td> ...
                                          	'                           ..</tr><tr> ...
                                                ' make option in header to toggle this turn
                                                ' SEEMS nested <font> is not allowed.
           open_e                               ' open el
           tt = tt & t
           if pnow then response.write t 
           t = ""
           i = i + 1
        else
           add "<br>" & ve & "Unknown control """ & _
               c2 & """ in shortcut: """ & _
               c1 & c2 & """"  
        end if
    end if
    ''''''''	END FIRST CHAR IS AN ESCAPE   ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    ''''''''	END FIRST CHAR IS AN ESCAPE   ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''


elseif tag_state then  '=A; FIRST CHAR IS NORMAL
       godown = true     
else                   '    FIRST CHAR IS NORMAL
      godown = true    
     '==========================================================
     ' HANDLE CLOSING SHORTCUTS LIKE    x\
     '----------------------------------------------------------
     if c2 = xescape and _
        ( isNeutral(c3) or _
          ( c3=xescape and (xescape <> xself))) then 		
        'case: x\'spacer'
                              'if tag_state then extend_e 
        if isNeutral(c1) then 
          wi = d 
	else  
	  wi = index_closest_shortcut( lcase(c1) )			    
         'predecessor might be found ...
          if wi <1 then 
            add "Unspecified control " & c1 & _
               " in closing shortcut """ & c1 & c2 & _
               """" & " at position in text " & i & _
               ve & "By default, closing all open elements."
             wi = d
          end if
        end if 
        k = d
        for j=k to wi step - 1       'close all elements up to
                                     'level wi;
            close_e
        next

        if c3 = " " or _
           c3 = chr(9) _
        then 
           i=i+2                'swallow others;
        else
           i=i+1                'preserve c3=escaper,cr,lf
        end if

      godown = false                 ' go to the next loop step
   '----------------------------------------------------------
   end if ' HANDLE CLOSING SHORTCUTS
   '==========================================================
end if 	'=A; ef IF FIRST CHAR IS AN ESCAPE or NORMAL 






'DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD
if godown then	    '  N O   S H O R T C U T S   B E L O W;   M A Y   B E    O N L Y   P A R C U T S
                    '  no else for this if.
'ddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd


'TTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTT
if tag_state then   		'=1  INSIDE OF TAG 
'ttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttt

if  isNeutral(c1) then				'=2
    extend_e					'spacer ultimately extends an element;
elseif c1 = tag_div and c2 = tag_div then	'=1b
    ' escape tag divider char
    add c1
    i=i+1
elseif c1 = tag_div then			'"," --> " "; for ex: \f.moo,boo x \ -->
                                                '<font moo boo >x</font>
    add " "
elseif no_more_scuts then			'=2
    add c1					'normal chars addition
elseif c1 = xtend then                          'triggering to unshortcutted tag state
    no_more_scuts = true
    add " "
else


  'PPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPP
  ' PARCUTS
  '==========================================================================
  tagelem = get_i_sub_el(pcurrent,c) 
  if tagelem = 0 then  stope "parcut `" & c1 & "` does not not exists ... "

  '=================================================================
  if subelement(pcurrent,tagelem) <> "" then 	' PARTYPE 
  '-----------------------------------------------------------------
     if right(t,1) <> " " then add " "  		' part of syntax
     'then print subelement itself 
     add subelement(pcurrent, tagelem) 
     'handle tag element parameters	
     select case subeltype(pcurrent,tagelem)    '==partype
            case "#" :  fill_numbers
            case "#space" 
                  do : ws = get_next
                       add ws
                  loop until ws = " "
           case "1","2","3","4","5","6","7","8","9"
                 '"type is a digit"
                 j = cint(subeltype(pcurrent,tagelem))
                 'add as many chars as type is:
                 for k=1 to j
                    add get_next
                 next
            'POSSIBLE MODIFICATIONS. 
            '  cases can be addes to handle
            '  additional types
            case """"   'assume that area inside of " .... "
                 WName = ""
                 do 'until ws = """" 
                    ws = get_next  'get next char
                    select case ws
                       case """":  add WName & """"
                       'POSSIBLE MODIFICATIONS. 
                       '  cases can be addes to handle
                       '  additional functionality
                       case SelfNamed   'currently is "+"
                          SelfNamedFlag = ws	 
     			  add WName & """"
			  extend_e
			  add WName
			  close_e
			  ws = """"           ' to close do-loop
			  SelfNamedFlag = ""  ' they are + and !
                       case VarEsc
                          ws = get_next
                          if ws=VarEsc then
                             Wname = WName & VarEsc
                          else
                                'this is an access ServerScript Variables:
                                select case ws
                                   case path_info_esc
                                        WName = WName & path_info
                                   case this_page_esc
                                        WName = WName & this_page
                                end select
                          end if
                       case SelfClosed
                       case else:   WName = Wname & ws
                    end select                     
                 loop until ws = """"

       end select
       if right(t,1) <> " " then add " "  'part of syntax
  '=================================================================
  end if ' ef PARTYPE 
 '===========================================================================
 ' TRANSFORMING TAG 
 '---------------------------------------------------------------------------
       'if need to tansform the state like T-->RD
       i_next_el = enext(pcurrent, tagelem)
       if deb then printl "next el found=`" & element(i_next_el)
       if i_next_el <> 0 then 
          extend_e
          e_next_el = element(i_next_el)
          c_control = lcase(any_scut(i_next_el))
	    if i_next_el = p(d) then    ' this may be wrong improvement
			'add("&nbsp;")	' this string ... for example, to put something in cells; 
			close_e		' to treat like \DDDD\-
	    end if				' end this may be ...
          open_e
       end if
 '--------------------------------------------------------------------------
 'ef TRANSFORMING TAG 
 '===========================================================================

 '===========================================================================
 'ef PARAMETER SHORTCUTS 
 'PPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPP

 end if '=2 
else '=1  ef INSIDE OF TAG 
'TTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTT
    addi c1            'NORMAL TEXT OUTSIDE THE TAG
end if '=1 

'DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD
end if '=0; DDD - DOWN BLOCK=SEEMS two first characters are normal
'ddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd
' E N D   N O   T O P   L E V E L    S H O R T C U T S    B E L O W   
'--------------------------------------------------------------------------------------
end if	 ' ef SHORT ML AREA 
'====================================================================================== 



i = i + 1     ' this loop always advances pointer i ...
loop 
'mmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmm
end sub ' ef compile_source
'MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM














function index_closest_shortcut(c)
 dim i 
 index_closest_shortcut = 0
 for i=d to 1 step - 1
     if pscut(i) = c then 
	     index_closest_shortcut = i
           exit for
     end if
 next
end function




'=============================================
sub set_state_attr
  pcurrent           = p(d)
  elemcur            = element(pcurrent)
  low_elemcur        = lcase(elemcur)
  ilowcurr           = get_i(low_elemcur)
  contrcurr          = pscut(d)
  ForeignScriptFlag  = foreign(pcurrent)	
  'array foreign keeps flag for 
  'foreign script ...
  not_halfcurr  = not halftag(pcurrent)
end sub



%>