TITLE
HTML report
SUMMARY
This is a module to help make a "report" that is directed to an html table.
It provides services to "open" and "close" the report, and to emit heading
and detail lines. The result will be a single html file for viewing on
a screen. For a paper copy of the "report," one would print the html page.
The module does not provide any page breaks that would make the printed
version of this page look good. Controlling printing to physical paper
is not part of the mission of html.
DOCUMENTATION
Load the module into your program with:
do %htmlrep.r
Before the first call:
1. Put a file name in HTMLREP-FILE-ID. This should be a value with
the type of "file." In other words, put a percent sign in front of it.
2. Put a value in HTMLREP-TITLE.
3. Optionally, call HTMLREP-OPEN. If you omit this step, it will
be done when you make the first call to "print" something.
Optionally, before "printing" the first detail line, call HTMLREP-EMIT-HEAD
in the following manner:
HTMLREP-EMIT-HEAD ["literal-1" ... "literal-n"]
where literal-1, etc., are strings to be turned into
entries.
To "print" a line of data, call HTMREP-EMIT-LINE in the following manner:
HTML-EMIT-LINE [word-1...word-n]
where word-n is the word whose value you want to print. The procedure will
generate a | entry for each word, in one row of an html table.
At the end:
Call HTMLREP-CLOSE. You MUST do this step because all the other procedures
just build up an html string in memory. The HTMLREP-CLOSE procedure actually
writes the data to disk under the name you loaded into HTMLREP-FILE-ID.
SCRIPT
REBOL [
Title: "HTML report"
]
;; [---------------------------------------------------------------------------]
;; [ Items set up by the caller. ]
;; [---------------------------------------------------------------------------]
HTMLREP-FILE-ID: %htmlrep.html
HTMLREP-TITLE: " "
;; [---------------------------------------------------------------------------]
;; [ Internal working items. ]
;; [---------------------------------------------------------------------------]
HTMLREP-FILE-OPEN: false
;; [---------------------------------------------------------------------------]
;; [ This is the top of the html page. ]
;; [---------------------------------------------------------------------------]
HTMLREP-PAGE-HEAD: {
<%HTMLREP-TITLE%>
<%HTMLREP-TITLE%>
}
;; [---------------------------------------------------------------------------]
;; [ This is the end of the html page. ]
;; [---------------------------------------------------------------------------]
HTMLREP-PAGE-FOOT: {
}
;; [---------------------------------------------------------------------------]
;; [ This is the area where we will build up the html page in memory. ]
;; [---------------------------------------------------------------------------]
HTMLREP-PAGE: make string! 5000
;; [---------------------------------------------------------------------------]
;; [ This is the procedure to "open" the report. ]
;; [ The "build-markup" function will replace the placeholders in the html ]
;; [ with the values resulting from their evaluation. ]
;; [---------------------------------------------------------------------------]
HTMLREP-OPEN: does [
HTMLREP-PAGE: copy ""
append HTMLREP-PAGE build-markup HTMLREP-PAGE-HEAD
append HTMLREP-PAGE newline
HTMLREP-FILE-OPEN: true
]
;; [---------------------------------------------------------------------------]
;; [ This is the procedure to "close" the report. ]
;; [ It writes to disk the html page we have built up in memeory. ]
;; [---------------------------------------------------------------------------]
HTMLREP-CLOSE: does [
append HTMLREP-PAGE HTMLREP-PAGE-FOOT
append HTMLREP-PAGE newline
write HTMLREP-FILE-ID HTMLREP-PAGE
HTMLREP-FILE-OPEN: false
]
;; [---------------------------------------------------------------------------]
;; [ This procedure emits a row of an html table containing heading ]
;; [ elements supplied by the caller in a block of strings. ]
;; [---------------------------------------------------------------------------]
HTMLREP-EMIT-HEAD: func [
"Emit a heading row with literals supplied in a block"
HTMLREP-HEADING-BLOCK [block!]
] [
append HTMLREP-PAGE " | "
foreach HTMLREP-HEAD-LIT HTMLREP-HEADING-BLOCK [
append HTMLREP-PAGE ""
append HTMLREP-PAGE HTMLREP-HEAD-LIT
append HTMLREP-PAGE " | "
]
append HTMLREP-PAGE "
"
append HTMLREP-PAGE newline
]
;; [---------------------------------------------------------------------------]
;; [ This procedure emits a row of an html table containing the values of ]
;; [ words supplied by the caller in a block. ]
;; [ Note that words are passed in a block, and we use the "get" function to ]
;; [ get the values of the words. ]
;; [---------------------------------------------------------------------------]
HTMLREP-EMIT-LINE: func [
"Emit a detail row with words supplied in a block"
HTMLREP-DETAIL-BLOCK [block!]
] [
append HTMLREP-PAGE ""
foreach HTMLREP-WORD HTMLREP-DETAIL-BLOCK [
append HTMLREP-PAGE ""
append HTMLREP-PAGE get HTMLREP-WORD
append HTMLREP-PAGE " | "
]
append HTMLREP-PAGE "
"
append HTMLREP-PAGE newline
]