Since handling filenames is typically system-dependent, it is best to centralise it in one place. This code is straightforward. If generate-filename is called from a program element, we use the output attribute to generate a filename. When called from the section element, we can generate an arbitrary filename; here, we use the index of the corresponding item of the weaved-file shelf. Finally, if the call is made from the code element, we use its output attribute as the filename.
<34 generating output filenames> =define string source function generate-filename as do when element is "program" output (attribute "output" drop ([any \ "/"]+ "/")* take any ++ lookahead ("." [any \ "."]+ value-end)) || weaved-filename-suffix else when element is "section" output "4fzd" % number of weaved-file || weaved-filename-suffix else when element is "code" output attribute "output" else not-reached message "ERROR: We shouldn't be generating a filename" || " in element %"" || name of element || "%"." done
As mentioned earlier, it seems easier to split up the weaved files during the weaving process, rather than putting it off to the formatting phase. We end up generating weaved filenames often, but we keep track of it using the weaved-filename shelf.
<35 generate weaved filename> =set new weaved-filename to generate-filename
Previous section: Main Loop
Next section: Epilogue