A Text to HTML Converter in Batch

This batch reads a text file and converts it into a form understood by web browsers, HTML. You'll still have to edit the file but it's a lot easier than adding all of the tags manually. It also converts all "&" and "<" characters to "&amp;" and "&lt;" to avoid confusing browsers. QBasic must be on the path for this to work.

Syntax...

Unless /pre is specified, <P> tags are added to blank lines and lines beginning with at least 5 spaces. If only the input and output files are specified with no other parameters then original line-lengths will be lost, possibly creating a mess.

If the third parm is '/pre' the P tags are left out and pre tags added around the document. If the third parm is a number any line shorter will have a BR tag added to the end. 50 to 60 works well but varies depending on the document contents. The third parameter must be either /pre or a valid number or error will result!

References to http:// and ftp:// are converted to hypertext links unless the fourth parameter is /nolinks (very cool). Parms past the third (or /nolinks) are added to the title field. Now handles periods and parenthesis after links.

As is often the case, new situations arise that cause me to change this batch. The last one (4/29/97) was to handle urls enclosed with < and >. Since the lines that handled all the possible chars a link might end with were getting rather long I restructured them to set a flag (var 'fc') then used that to trigger processing. Make that 10/26/97 for a tiny bugfix.

This code has been HTML-converted for proper display. To recover the code, copy/paste from the browser screen or get the UTILBATS collection.


-------- 2HTML.BAT --------

 
:: Text-to-HTML Converter Batch (10/26/97)
:: (c)1996,1997 Terry Newton - Requires QBASIC
@echo off
if '%2==' echo Syntax: %0 infile outfile [breaklength or /pre] [/nolinks] [title]...
if '%2==' echo with just infile and outfile, P tags are added and links converted
if '%2==' echo if breaklength specified BR tags added to lines less than breaklength
if '%2==' echo if /pre specified no tags added except headers and converted links
if '%2==' echo third parameter (if used) MUST be a number (50 works) or /pre
if '%2==' echo if fourth parm is /nolinks hypertext links are not converted
if '%2==' echo always converts "&" to "&amp;" and "<" to "&lt;" 
if '%2==' goto end
if not exist %1 echo Input file %1 not found
if not exist %1 goto end
echo.>%2
if not exist %2 echo Output file %2 is not a valid filename
if not exist %2 goto end
del %2
echo Processing...
::====== set errortrap and open files ======
>process$.bas echo :on error goto closeout
>>process$.bas echo open "%1" for input as #1:open "%2" for output as #2
::====== add headers ======
if '%4=='/nolinks >>process$.bas echo print #2, "<html><head><title>%5 %6 %7 %8 %9</title></head><body>"
if '%4=='/nolinks goto head1
>>process$.bas echo print #2, "<html><head><title>%4 %5 %6 %7 %8 %9</title></head><body>"
:head1
if '%3=='/pre >>process$.bas echo print #2, "<pre>" 
::====== main loop - get input line ======
>>process$.bas echo floop:line input #1, a$
::====== replace blank lines with P tags (if not /pre) ======
if not '%3=='/pre >>process$.bas echo if space$(len(a$))=a$ then print #2, "<p>":goto floop
::====== replace "&" characters with "&amp;" ======
>>process$.bas echo a=1
>>process$.bas echo aloop:p=instr(a,a$,"&"):if p=0 then goto aloop1
>>process$.bas echo le=len(a$):if p=1 then b$="" else b$=left$(a$,p-1)
>>process$.bas echo if p=le then c$="" else c$=right$(a$,le-p)
>>process$.bas echo a$=b$+"&amp;"+c$:a=p+4:goto aloop
>>process$.bas echo aloop1:a=1
::====== replace "<" characters with "&lt;" ======
>>process$.bas echo qloop:p=instr(a,a$,"<"):if p=0 then goto qloop1
>>process$.bas echo le=len(a$):if p=1 then b$="" else b$=left$(a$,p-1)
>>process$.bas echo if p=le then c$="" else c$=right$(a$,le-p)
>>process$.bas echo a$=b$+"&lt;"+c$:a=p+3:goto qloop
>>process$.bas echo qloop1:a=1
::==== branch to nobreak if /pre specified ======
if '%3=='/pre goto nobreaks
::====== insert P tags in lines beginning with 5 spaces
>>process$.bas echo if left$(a$,5)="     " then a$="<p>"+a$
::====== if breakpoint specified add BR tags ======
if '%3==' goto nobreaks
>>process$.bas echo if int(len(a$)/%3)=0 then a$=a$+" <br>"
>>process$.bas echo if left$(ltrim$(a$),5)=right$(rtrim$(a$),5) then a$=a$+" <br>"
:nobreaks
if '%4=='/nolinks goto nolinks
::==== convert http links ======
>>process$.bas echo hloop:p=instr(a,lcase$(a$),"http://"):if p=0 then goto hloop1
>>process$.bas echo le=len(a$):q=instr(p,a$," "):if q=0 then q=le else q=q-1
>>process$.bas echo if p=1 then b$="" else b$=left$(a$,p-1)
>>process$.bas echo if q=le then c$="" else c$=right$(a$,le-q)
>>process$.bas echo d$=mid$(a$,p,q-p+1):if instr(d$,".")=0 then goto hloop1
>>process$.bas echo htrim:t$=right$(d$,1):fc=t$="." or t$=")" or t$=">"
>>process$.bas echo if fc then c$=t$+c$:d$=left$(d$,len(d$)-1):goto htrim
>>process$.bas echo a$=b$+"<a href="+chr$(34)+d$+chr$(34)+">"+d$+"</a>"+c$
>>process$.bas echo a=q+len(d$)+15:if sgn(len(a$)-a)=1 then goto hloop
>>process$.bas echo hloop1:a=1
::==== convert ftp links ======
>>process$.bas echo tloop:p=instr(a,lcase$(a$),"ftp://"):if p=0 then goto tloop1
>>process$.bas echo le=len(a$):q=instr(p,a$," "):if q=0 then q=le else q=q-1
>>process$.bas echo if p=1 then b$="" else b$=left$(a$,p-1)
>>process$.bas echo if q=le then c$="" else c$=right$(a$,le-q)
>>process$.bas echo d$=mid$(a$,p,q-p+1):if instr(d$,".")=0 then goto tloop1
>>process$.bas echo ttrim:t$=right$(d$,1):fc=t$="." or t$=")" or t$=">"
>>process$.bas echo if fc then c$=t$+c$:d$=left$(d$,len(d$)-1):goto ttrim
>>process$.bas echo a$=b$+"<a href="+chr$(34)+d$+chr$(34)+">"+d$+"</a>"+c$
>>process$.bas echo a=q+len(d$)+15:if sgn(len(a$)-a)=1 then goto tloop
>>process$.bas echo tloop1:
:nolinks
::====== write new line and loop ======
>>process$.bas echo print #2,a$:goto floop
::====== tidy up output file and close files
>>process$.bas echo closeout:close #1
if '%3=='/pre >>process$.bas echo print #2,"</pre>"
>>process$.bas echo print #2,"</body></html>":close #2:system
::====== run the temp basic program then delete it ======
qbasic /run process$.bas
del process$.bas
if exist %2 echo %2 HTML file created
if not exist %2 echo Something didn't work...
if not exist %2 pause
:end