; HTMINDEX
;
; Program for Proteus
;
; (C) 1998-2004 Simone Zanella Productions
;
; Create a new file including all bookmarks found inside HTML files
; matching specifications; this program was used to create
; an early version of the index ad the end of the Proteus Manual.
; Implicit parameters: input and output NULL
;!proteus -z
FUNCTION StripExtra(s)
; Remove any extra tag between < and >
nl = RESTFROM(s, 2)
nl = LEFT(nl, SUB(STRLEN(nl), 4))
k = STRSTR(nl, "<")
WHILE k
nk = INC(k)
WHILE STRNEQ(SUBSTR(nl, nk, 1), ">")
INC(@nk)
LOOP
INC(@nk)
nl = DELETE(nl, k, SUB(nk, k))
k = STRSTR(nl, "<")
LOOP
RETURN "<" nl RIGHT(s, 4)
FUNCTION WriteAvl(label, s)
; Transcribe the contents of the AVL
FWRITELN(_HD, s "<br>")
RETURN 0
FUNCTION AddAvl(s)
; Add to the AVL tree the string corresponding to bookmark s
p = STRSTR(s, ">")
s2 = RESTFROM(s, INC(p))
p = STRSTR(s2, "<")
s2 = LEFT(s2, DEC(p))
AVLSET(_A, ALLTRIM(UPPER(s2), " "), s)
RETURN
IF OR(ISEMPTY(ARGV(5)), ISEMPTY(ARGV(6)))
CONSOLELN "Syntax: " ARGV(1) " " ARGV(2) " \"pathname\" destination"
CONSOLELN ""
CONSOLELN "Purpose: write hyperlink tags for every bookmark found in each"
CONSOLELN "page matching pathname (which can include wildcards)"
ABORT 0
FI
!ifdef UNIX
DirSep = "/"
!else
DirSep = "\\"
!endif
SourceDir = STRIPQUOTES(ARGV(5))
; Isolate base directory and file specification
X = STRRSTR(SourceDir, DirSep)
IF X
BaseDir = LEFT(SourceDir, X)
Spec = RESTFROM(SourceDir, INC(X))
ELSE
BaseDir = ""
Spec = SourceDir
FI
H = DIROPEN(SourceDir, 1)
F = IIF(NEQ(H, -1), 1, 0)
HD = FOPEN(ARGV(6), 26)
IF EQ(HD, -1)
CONSOLELN "Could not open \"" ARGV(6) "\"."
ABORT 1
FI
RL = ""
A = AVLNEW()
WHILE GT(F, 0)
; File found - open for reading
FH = FOPEN(BaseDir IIF(ISNOTEMPTY(BaseDir), DirSep, "") \
DIRLAST(H, 1), 1)
IF EQ(FH, -1)
CONSOLELN "Could not open \"" DIRLAST(H, 1) "\"."
ELSE
; Open succesfull
PL = ""
; Until the end of file..
WHILE NOT(FEOF(FH))
FLine = LTRIM(FREADLN(FH), " ")
IF STRNEQ(RL, "")
FLine = RL FLine
RL = ""
FI
; Bookmark started on the previous line?
IF AND(STRLEN(FLine), STRLEN(PL))
IF REXMATCH(FLine, "</a>")
ADD(@R_START, 4)
CL = PL SUBSTR(FLine, 1, SUB(R_START, 1))
CL = "<a href=\"" DIRLAST(H, 1) "#" RESTFROM(CL, 10)
FLine = RESTFROM(FLine, R_START)
AddAvl(StripExtra(CL))
PL = ""
ELSE
PL = PL FLine
CONTINUE
FI
FI
WHILE STRLEN(FLine)
PBook = STRISTR(FLine, "<a name=")
IF PBook
IF REXMATCH(FLine, "</a>")
Found = 1
WHILE AND(LT(R_START, PBook), Found)
FLine = RESTFROM(FLine, ADD(R_START, 4))
PBook = STRISTR(FLine, "<a name=")
Found = REXMATCH(FLine, "</a>")
LOOP
IF Found
ADD(@R_START, 4)
CL = SUBSTR(FLine, PBook, SUB(R_START, PBook))
CL = "<a href=\"" DIRLAST(H, 1) "#" RESTFROM(CL, 10)
FLine = RESTFROM(FLine, R_START)
AddAvl(StripExtra(CL))
ELSE
PL = RESTFROM(FLine, PBook) " "
BREAK
FI
ELSE
PL = RESTFROM(FLine, PBook) " "
BREAK
FI
ELSE
FLine = RIGHT(RTRIM(FLine, " "), 2)
IF STREQ(FLine, "<a")
RL = FLine " "
FI
BREAK
FI
LOOP
LOOP
FCLOSE(FH)
FI
F = DIRNEXT(H)
LOOP
DIRCLOSE(H)
AVLTRAVERSE(A, WriteAvl)
FCLOSE(HD)
ABORT 0