REBOL [
    Title: "Build module list"
]

;; [---------------------------------------------------------------------------]
;; [ This is a program to build a list of our common modules.                  ]
;; [ The idea as that a list that shows file names and documentation           ]
;; [ summaries would be more helpful than looking at just file names           ]
;; [ and opening files to see what is in them.                                 ]
;; [                                                                           ]
;; [ The output of this program will be:                                       ]
;; [                                                                           ]
;; [ One html file that is a list of all the common modules in the             ]
;; [ common modules folder.                                                    ]
;; [                                                                           ]
;; [ One html file FOR EACH common module that contains the documentation      ]
;; [ extracted from comments in the module (the text after the key word        ]
;; [ DOCUMENTATION  in the text before the REBOL header).                      ]
;; [                                                                           ]
;; [ One html file FOR EACH common module that contain the module code         ]
;; [ run through a colorizing function found on the rebol.org website.         ]
;; [                                                                           ]
;; [ The main module index will contain links to the other files.              ]
;; [                                                                           ]
;; [ Whenever your add/change/delete a common module, run this script to       ]
;; [ rebuild the module index.                                                 ]
;; [---------------------------------------------------------------------------]

do %../modules/glb.r
do %../modules/htmlfile.r
do %../modules/color-code.r

;; [---------------------------------------------------------------------------]
;; [ This is the folder where the common modules are located.                  ]
;; [---------------------------------------------------------------------------]

MODULE-DIR: %../modules

;; [---------------------------------------------------------------------------]
;; [ When we examine one common module, we are going to assemble these         ]
;; [ parts.                                                                    ]
;; [                                                                           ]
;; [ LIST-FILENAME:  The name of the file, not the full path but just the name.]
;; [ LIST-FILE-PATH:  The full name of the module file.                        ]
;; [ LIST-FILE-DATA:  The contents of the module file.                         ]
;; [ LIST-TITLE:  The text after the TITLE keyword.                            ]
;; [ LIST-SUMMARY:  The text after the SUMMARY keyword.                        ]
;; [ LIST-DOCUMENTATION:  The text after the DOCUMENTATION keyword.            ]
;; [ LIST-SCRIPT:  The actual scipt after the SCRIPT keyword.                  ]
;; [ LIST-DOC-FILE:  The name of the file where we store the documentation.    ]
;; [ LIST-SCRIPT-FILE:  The name of the file where we store the script.        ]
;; [---------------------------------------------------------------------------]

LIST-FILENAME: none
LIST-FILE-PATH: none
LIST-FILE-DATA: ""
LIST-TITLE: ""
LIST-SUMMARY: ""
LIST-DOCUMENTATION: ""
LIST-SCRIPT: ""
LIST-DOC-FILE: ""
LIST-SCRIPT-FILE: ""

;; [---------------------------------------------------------------------------]
;; [ These are the html fragments that will be combined to make the main       ]
;; [ module list produced by this program.                                     ]
;; [---------------------------------------------------------------------------]

LIST-HEAD: {
<HTML>
<HEAD>
<TITLE>List of common modules</TITLE>
</HEAD>
<BODY>
<IMG SRC="../graphics/rfcp-logo.png">
<H1>List of common REBOL modules</H1>

<TABLE WIDTH="100%" BORDER="1">
}

LIST-FOOT: {
</TABLE>
</BODY>
</HTML>
}

LIST-ROW: {
<TR>
<TD> <% LIST-FILENAME %> </TD>
<TD> <% LIST-TITLE %> </TD>
<TD> <% LIST-SUMMARY %> </TD>
<TD> <A HREF="<% LIST-DOC-FILE %>"> Documentation</A></TD>
<TD> <A HREF="<% LIST-SCRIPT-FILE %>"> Script</A></TD>
</TR>
}

;; [---------------------------------------------------------------------------]
;; [ This is the html code that will be written to the file that contains      ]
;; [ the module documentation.                                                 ]
;; [---------------------------------------------------------------------------]

DOC-HTML-FILE: {
<HTML>
<HEAD>
<TITLE>Documentation for <% LIST-FILENAME %> </TITLE>
</HEAD>
<BODY>
<IMG SRC="../graphics/rfcp-logo.png">
<H1>Documentation for module <%LIST-FILENAME%></H1>
<PRE>
<% LIST-DOCUMENTATION %>
</PRE>
</BODY>
</HTML>
}

;; [---------------------------------------------------------------------------]
;; [ The code below, adapted from the rebol.org library, assembles             ]
;; [ a list of all the script files in the common module directory.            ]
;; [ We are going to read each one of these files, and for each, make a row    ]
;; [ on our html page that lists the files.                                    ]
;; [ we also will make, for each module, TWO other html files, as explained    ]
;; [ above.                                                                    ]
;; [---------------------------------------------------------------------------]

SCRIPT-FILE?: func ["Returns true if file is a script" FILENAME] [
    find [%.r] find/last FILENAME "."
]

FILE-LIST: read MODULE-DIR/.
while [not tail? FILE-LIST] [
    either SCRIPT-FILE? first FILE-LIST [FILE-LIST: next FILE-LIST][remove FILE-LIST]
]
FILE-LIST: head FILE-LIST   ;; DON'T FORGET TO DO THIS.

;; [---------------------------------------------------------------------------]
;; [ Set up our main html file that lists the modules.                         ]
;; [---------------------------------------------------------------------------]

HTMLFILE-FILE-ID: %common-module-list.html
HTMLFILE-OPEN-OUTPUT
HTMLFILE-EMIT LIST-HEAD

;; [---------------------------------------------------------------------------]
;; [ Now loop through the list of file names.                                  ]
;; [ For each file:                                                            ]
;; [ 1.  Read it into memory.                                                  ]
;; [ 2.  Parse the file to extract the four documentation blocks identified    ]
;; [     by the four keywords TITLE, SUMMARY, DOCUMENTATION, SCRIPT.           ]
;; [ 3.  Write the documentation to its own html file.                         ]
;; [ 4.  Run the script through a colorization function and write it to        ]
;; [     its own html file.                                                    ]
;; [ 5.  Make a row in the main html file that is the module list.             ]
;; [---------------------------------------------------------------------------]

foreach MODULE-FILE FILE-LIST [
;;  -- Initialize all our data for this module file.
    LIST-FILENAME: none
    LIST-FILE-PATH: none 
    LIST-FILE-DATA: copy []
    LIST-TITLE: copy ""
    LIST-SUMMARY: copy ""
    LIST-DOCUMENTATION: copy ""
    LIST-SCRIPT: copy ""
;;  -- Set up all our data for this module.
    LIST-FILENAME: MODULE-FILE
    LIST-FILE-PATH: to-file rejoin [to-string MODULE-DIR "/" MODULE-FILE]
    LIST-DOC-FILE: to-file rejoin ["module-" GLB-BASE-FILENAME MODULE-FILE "-documentation.html"]
    LIST-SCRIPT-FILE: to-file rejoin ["module-" GLB-BASE-FILENAME MODULE-FILE "-script.html"]
;;  -- Bring the whole module file into memory.
    LIST-FILE-DATA: read LIST-FILE-PATH
;;  -- Extract the four parts of the documentation.
    parse/case LIST-FILE-DATA [thru "TITLE" copy LIST-TITLE to "SUMMARY"]
    parse/case LIST-FILE-DATA [thru "SUMMARY" copy LIST-SUMMARY to "DOCUMENTATION"]
    parse/case LIST-FILE-DATA [thru "DOCUMENTATION" copy LIST-DOCUMENTATION to "SCRIPT"]
    parse/case LIST-FILE-DATA [thru "SCRIPT" copy LIST-SCRIPT to end]
;;  -- Write the script code to its own html file.
    write LIST-SCRIPT-FILE color-code to-string LIST-SCRIPT
;;  -- Write the documentation block to its own html file.
    write LIST-DOC-FILE build-markup DOC-HTML-FILE
;;  -- Write a row of the main module list.
    HTMLFILE-EMIT build-markup LIST-ROW
]

;; [---------------------------------------------------------------------------]
;; [ Do the closing procedures required by our various modules.                ]
;; [---------------------------------------------------------------------------]

HTMLFILE-CLOSE

alert "Module list is built"

