Subj : TYPEHTML.SRC
To   : All
From : Digital Man
Date : Mon Oct 09 2000 11:41 am

I'm converting my "working documents" for Synchronet (to-do list, FAQs, etc) to
HTML and needed an easy way to "share" these files with the users on the BBS,
without requiring them to download them and view them in a web browser.

So, I spent "some time" today creating this little "diddy" in Baja and just
thought I'd post the source here since it's probably the most intense Baja
coding I've done since QNET.SRC (QWK networking module for SBBS v2). I'll be
putting this on Vertrauen for download too.

It only handles BASIC HTML right now, so don't be too shocked if it doesn't
print your more elaborate HTML files very nicely. Obviously, things like
graphics and fonts can't be well represented on a typical telnet terminal.
(HTML to RIP anyone?)

Well, here it is:

# TypeHTML

# Synchronet Baja Modules to Display HTML files to TTY/ANSI users

# Created October 09, 2000 by Rob Swindell

!include file_io.inc
!include sbbsdefs.inc

# Color Attributes for HTML type styles
!define NORMAL          "\1N\1H"
!define HEADING1        "\1H\1Y"
!define HEADING2        "\1H\1C"
!define HEADING3        "\1H\1M"
!define HEADING4        "\1H\1G"
!define HEADING5        "\1H\1B"
!define HEADING6        "\1H\1R"
!define BOLD            "\1H\1C\x014"
!define ITALIC          "\1H\1G\x012"
!define UNDERLINE       "\1H\1W\x016"
!define STRIKE_THROUGH  "\1N\1K\x017"
!define LIST_ITEM       "    \1H\1Wo \1G"

str     filename
str     href
str     tmp
int     i
int     file
int     hlevel                  # header level
int     col                     # current cursor column
int     last_line_len           # length of last line printed
int     lines                   # total lines printed
int     printing                # printing on
int     newline                 # new line read from html file

copy filename str

compare_str ""
if_true
       print "usage: *typehtml <filename>"
       return
       end_if

fopen file O_RDONLY|O_DENYNONE filename
if_false
       printf "!Error %d opening %s\r\n" _ERRNO filename
       return
       end_if

set col 0               # reset column counter
set last_line_len 0     # reset last line length
set lines 0             # reset line counter
set printing 1          # printing by default
cls                     # clear screen
print NORMAL            # normal color

:next_line

       # Check abort flag
       copy i _sys_status
       and i SS_ABORT
       compare i 0
       if_not_equal    # user aborting listing
               return
               end_if

       # Read a line
       fread_line file str
       if_false # end of file
               crlf
               pause
               return
               end_if

       set newline 1

       # truncate whitespace
       strip_ctrl str
       :skip_whitespace
               compare_word " "
               if_false
                       compare_word "\t"
                       end_if
               if_true
                       shift_str 1
                       goto skip_whitespace
                       end_if

       :next_char
               compare_str ""
               if_true
                       goto next_line
                       end_if
               sprintf tmp "%.1s" str
               shift_str 1
               compare tmp "<"
               if_false
                       compare printing 1
                       if_true
                               # handle word wrap
                               compare newline 1
                               if_equal
                                       compare col 0
                                       if_not_equal
                                               print " "
                                               add col 1
                                               end_if
                                       end_if
                               set newline 0

                               # don't start lines with white-space
                               compare col 0
                               if_true
                                       compare tmp " "
                                       end_if
                               if_false
                                       print tmp
                                       add col 1
                                       compare col 79
                                       if_greater_or_equal
                                               call crlf
                                               end_if
                                       end_if
                               end_if
                       goto next_char
                       end_if

               # Title
               compare_word "TITLE>"
               if_true
                       set printing 0
                       call find_close_bracket
                       goto next_char
                       end_if
               compare_word "/TITLE>"
               if_true
                       set printing 1
                       call find_close_bracket
                       goto next_char
                       end_if


               # List item
               compare_word "LI>"
               if_false
                       compare_word "LI "
                       end_if
               if_true
                       compare col 0
                       if_not_equal
                               call crlf
                               end_if
                       printf LIST_ITEM
                       set col 6
                       call find_close_bracket
                       goto next_char
                       end_if

               # Strike-through
               compare_word "S>"
               if_false
                       compare_word "STRIKE>"
                       end_if
               if_true
                       printf STRIKE_THROUGH
                       call find_close_bracket
                       goto next_char
                       end_if

               # Bold
               compare_word "B>"
               if_false
                       compare_word "STONG>"
                       end_if
               if_true
                       printf BOLD
                       call find_close_bracket
                       goto next_char
                       end_if

               # Italics
               compare_word "I>"
               if_false
                       compare_word "EM>"
                       end_if
               if_true
                       printf ITALIC
                       call find_close_bracket
                       goto next_char
                       end_if

               # Underline
               compare_word "U>"
               if_false
                       compare_word "U>"
                       end_if
               if_true
                       printf UNDERLINE
                       call find_close_bracket
                       goto next_char
                       end_if


               # Attribute off
               compare_word "/S>"
               if_false
                       compare_word "/STRIKE>"
                       end_if
               if_false
                       compare_word "/B>"
                       end_if
               if_false
                       compare_word "/STONG>"
                       end_if
               if_false
                       compare_word "/I>"
                       end_if
               if_false
                       compare_word "/EM>"
                       end_if
               if_false
                       compare_word "/U>"
                       end_if
               if_true
                       print NORMAL
                       call find_close_bracket
                       goto next_char
                       end_if

               # Table cell
               compare_word "TD>"
               if_false
                       compare_word "TD "
                       end_if
               if_true
                       # visually separate cells
                       compare col 0
                       if_greater
                               print " "
                               add col 1
                               end_if
                       call find_close_bracket
                       goto next_char
                       end_if


               # Dumb CR/LF block
               compare_word "BR>"
               if_false
                       compare_word "BR "
                       end_if
               if_false
                       compare_word "P>"
                       end_if
               if_false
                       compare_word "P "
                       end_if
               if_false
                       compare_word "TR>"
                       end_if
               if_false
                       compare_word "TR "
                       end_if
               if_true
                       call crlf
                       call find_close_bracket
                       goto next_char
                       end_if


               # Intelligent CR/LF block (force single blank line)
               compare_word "/UL>"
               if_false
                       compare_word "/OL>"
                       end_if
               if_false
                       compare_word "/DIR>"
                       end_if
               if_false
                       compare_word "/MENU>"
                       end_if
               if_true
                       print NORMAL
                       end_if
               if_false
                       compare_word "H"
                       if_true
                               shift_str 1
                               copy hlevel str
                               compare hlevel 0
                               if_greater
                                       switch hlevel
                                               case 1
                                                       print HEADING1
                                                       end_case
                                               case 2
                                                       print HEADING2
                                                       end_case
                                               case 3
                                                       print HEADING3
                                                       end_case
                                               case 4
                                                       print HEADING4
                                                       end_case
                                               case 5
                                                       print HEADING5
                                                       end_case
                                               default
                                                       print HEADING6
                                                       end_case
                                               end_switch
                                       setlogic TRUE
                               else
                                       setlogic FALSE
                                       end_if
                               end_if
                       end_if
               if_false
                       compare_word "/H"
                       if_true
                               shift_str 2
                               copy hlevel str
                               compare hlevel 0
                               if_greater
                                       print NORMAL
                                       setlogic TRUE
                               else
                                       setlogic FALSE
                                       end_if
                               end_if
                       end_if
               if_true
                       compare lines 0
                       if_equal
                               compare col 0
                               end_if
                       if_greater
                               compare last_line_len 0
                               if_equal
                                       compare col 0
                                       end_if
                               if_not_equal
                                       call crlf
                                       call crlf
                                       end_if
                               end_if
                       call find_close_bracket
                       goto next_char
                       end_if

               # Hyper-link
               compare_word "A HREF="
               if_true
                       shift_str 7
                       set href ""
                       :copy_href
                               sprintf tmp "%.1s" str
                               compare tmp ">"
                               if_false
                                       shift_str 1
                                       strcat href tmp
                                       goto copy_href
                                       end_if
                       end_if

               # Show hyper-link
               compare_word "/A>"
               if_true
                       compare href ""
                       if_false
                               strlen i href
                               add i 3
                               add col i
                               compare col 79
                               if_equal_or_greater
                                       call crlf
                                       sub i 1
                                       copy col i
                               else
                                       print " "
                                       end_if
                               printf "<%s>" href
                               end_if
                       set href ""
                       end_if

               call find_close_bracket
               goto next_char

return

# Carriage-return/Line-feed with column reset
:crlf
copy last_line_len col
crlf
set col 0
add lines 1
return

# Shift str past first close bracket
:find_close_bracket
compare_str ""
if_true
       return
       end_if

sprintf tmp "%.1s" str
shift_str 1
compare tmp ">"
if_false
       goto find_close_bracket
       end_if
return

---
� Synchronet � Vertrauen � Home of Synchronet � telnet://vert.synchro.net