5.2.7 Formatting output for a specific purpose
Our output thus far has been plain text without any serious
structure or layout. Here, as the next task for refining our example generator,
we will spice up our spec sheet with some HTML formatting. The outcome of this
exercise looks like this:
SpecSheet()
_translators()
@defaultDir = __(subreport '_default directory' run)
@cssFilename = _myFilename('css')
filename @defaultDir @cssFilename write _css() close
foreach >Watch; orderby y num
{ filename @defaultDir _myFilename('html') write
'<html>' newline
'<link rel="stylesheet" type="text/css" '
'href="' @cssFilename '"/>' newline
'<body>' newline
'<h1>' id '</h1>' newline
'<p class="description">' :Documentation '</p>'
newline
do .Display
{ '<p class="info">Number of time units shown: '
@count = '0'
do :UnitZones { @count++%null }
@count '</p>' newline
'<p class="info">Buttons: '
doWhile :Buttons { id ' ' }
'</p>' newline
}
do .LogicalWatch
{ '<p class="info">Apps: '
do decompositions
{ foreach .Start [Watch]
{ $visited = ' '
do ~From~To.()
{ _handleStateAndRecurse() }
}
}
'</p>' newline
}
'</body>' newline
'</html>' newline
close
}
As can be seen above, the HTML tags have been embedded
as part of the output stream. We have followed good HTML practices by sticking
with the basic HMTL tags within the generator itself and having a separate style
sheet for defining the actually look and layout. The style sheet definition is
encapsulated into a subgenerator called _css() and written to a CSS file that is
then referred from the HTML documents created for the Watch models. This is a
good way to incorporate predefined and constant pieces of code or some other
text into your generator structure. Having the constant parts hidden in their
own subgenerators doesn’t obscure the core part of your generator with
excessive content, and leaves that fixed content easy to access and modify if
needed.
You can initially define _css() as a new empty generator;
later you can make its contents the CSS you want, embedded in a MERL string.
Since any single quotes inside MERL strings must be escaped by doubling them,
there is a menu item Edit | Paste as Quoted that will allow you to copy
the literal CSS from elsewhere, and have it pasted into MERL as a string
surrounded by single quotes, and with contained single quotes doubled.
We also refer to a _myFilename generator, which takes the
desired file extension as an argument, and outputs a filename with that
extension based on the current element’s name. You can define it like
this:
_myFilename(@ext)
id%file '.' @ext