REBOL [
    Title: "Document Generator"
    Date: 19-Jul-1999
    File: %generate-doc.r
    Author: "Daan Oosterveld"
    Usage: {
         makes a document 
         
are parts of documents is a example. inserts a toc with the content docs and chapters } Purpose: "XMLish doc generator using parse-XML" library: [ level: 'advanced platform: none type: none domain: [markup file-handling] tested-under: none support: none license: none see-also: none ] ] ; Foreach label there is a 'function' label-functions: [ file [ ; throws contents in a named file. ; Returns a html with a link to that file ; This then can be thrown into another file print [".file" select attributes "name"] write to-file select attributes "name" reform [ newline gathercontent content newline ] either not none? select attributes "link" [ rejoin [{} select attributes "link" {}] ][ "" ] ] document [ print [".document" select attributes "name"] name: select attributes "name" rejoin [ {

} name {

} gathercontent content ] ] chapter [ print [".chapter" select attributes "name"] name: select attributes "name" either ((select attributes "indent") = "no") [ rejoin [ {

} name {

} gathercontent content ] ][ rejoin [ {

} name {

} "
" gathercontent content "
" ] ] ] section [ print [".section" select attributes "name"] name: select attributes "name" rejoin [ {

} select attributes "name" {

} gathercontent content ] ] subsection [ print [".sub-section" select attributes "name"] name: select attributes "name" rejoin [ {

} select attributes "name" {

} gathercontent content ] ] example [ print [".example"] rejoin [ {
} {} {
}
           gathercontent content
           {
} {
} ] ] toc [ ; the toc filters the contents twice. print [",toc"] rejoin ["
" gathertoc content "
^/^/" gathercontent content]] history [ ; history. print [".history"] temp: make string! 100 append temp "
" foreach entry content [ set [label attributes contents] entry if block? entry [ append temp reform [ ] ] ] append temp
select attributes "version" to-date select attributes "date" select attributes "author" gathercontent contents
temp ] ; if you want to add some html functions then do it here... ; notivy me to add your change... br ["
"] p ["

"] hr ["


"] ] ; generate the functions.. forskip label-functions 2 [ change next label-functions func [attributes content] second label-functions ] label-functions: head label-functions ; gathers the information for the doc.. gathercontent: function [parsed-xml][temp label attributes content stringed-content][ stringed-content: make string! 100 foreach entry parsed-xml [ do select [ string! [ append stringed-content entry ] block! [ temp: select label-functions to-word first set [label attributes content] entry ; don't be confused. This is a 'temponary' function call append stringed-content temp attributes content ] ] type?/word entry ] stringed-content ] ; Gathers the information for the toc and generates the HTML gathertoc: func [ parsed-xml /local localtemp label attributes content stringed-toc chapter section subsection ][ set [chapter section subsection] 0 stringed-toc: make string! 100 foreach entry parsed-xml [ do select [ string! [] block! [ set [label attributes content] entry if not none? temp: select [ "document" [ {} name {
}] "chapter" [ {} name {
}] "section" [ {} name {
}] "subsection" [ {} name {
}] ] label [ name: select attributes "name" append stringed-toc rejoin temp if not empty? temp: gathertoc content [ append stringed-toc reform [
gathertoc content
] ] ] if label = "file" [ name: select attributes "name" if found? find attributes "toc" [ append stringed-toc rejoin [ {} select attributes "toc" #
] ] ] ] ] type?/word entry ] stringed-toc ] ; Above is the script, the rest is a simple example, maybe to long for ; simplicity but the above is quite simpel for a doc generator like ; this. either not exists? %DocGen.html [ ; Making the standart example docs... Print ["Generating the usage docs for this script...^/" "After generating, open GenDoc.html in a browser.^/" "The next time you run this, you will be prompted" "for your own generation file.^/" "Press return"] input parsed: parse-XML { Made this document for the contest to show how to use docgen.
I'm currently using it to write another doc.
I've written this docgenerator to have a the same style everywhere in a document. I also had to have a program that could generate TOCs and more files at once. Resulting is a doc generator that can generate docs with more html pages which are interlinked by a TOC. Also more documents fit into one file, as with this file. I asume you wnat to create one now, this makes explaining easier.
Open a text file. Then insert a file tag like XML like this: <File name="Doc.html"> </File> The name is the file name where the generated html document should be saved. All tags between the file tags will be saved into this file.

Each tag generated pure html. The file tag gathers this information and rejoins or appends this information into one string. Then this is saved to disk.

This is the bare bone of the document. After this we can put in text and parts.

This tag makes the title of the document. Nicely centered. We will need this if we want to make a document. So added to the .rml it will look like this: <File name="Doc.html"> <Document name="A Document title"> </Document> </File> The name is the title, it will become a centered H1 in the html. Again all between the tags is gathered by the document and passed back to the file tag.
The history tag makes a table with four columns where the writter can fill in when where and who has written the document. Since this is the first version only one history will be added. <File name="Doc.html"> <Document name="A Document title"> <History> <Entry version="1.0.0" date="18-7-1999" author="You"> More information on the history can be placed between the tags.<br/> You don't have to place the history at the beginning. </Entry> </History> </Document> </File> The history table is to standarize the history. The fields explain theirself. Also notice the <br/>, it works like the normal <br> in html. <p/> and <hr/> also work like this. If you generate the history it will look like this:

More information on the history can be placed between the tags.
You don't have to place the history at the beginning.

These tags define a text as a chapter or (sub)section. They are simple to use. The name is again the title of the chapter or (sub)section. The next example has a simple layout of some chapters and (sub)sections. <File name="Doc.html"> <Document name="A Document title"> <History> <Entry version="1.0.0" date="18-7-1999" author="You"> More information on the history can be placed between the tags.<br/> You don't have to place the history at the beginning. </Entry> </History> <Chapter name="Chapter1"> This is chapter #1 </Chapter> <Chapter name="Chapter2"> This is chapter #2 <Section name="Section2.1"> This is section 2.1 <SubSection name="SubSection2.1.1"> This is subsection 2.1.1 </SubSection> <SubSection name="SubSection2.1.2"> This is subsection 2.1.2 </SubSection> </Section> <Section name="Section2.2"> This is section 2.2 </Section> </Chapter> </Document> </File> Notice that text can always be inserted. Like html. The structure is important for the generating the TOC, more on that is with the <toc> tag.
This is the tag that generated a Table Of Content, TOC for short. Every Document, Chapter Section and SubSection defined between the <Toc> and </Toc> tags will get an entry in the toc. The TOC generates two pieces and then glues these two together. First the TOC and then the content between the tags. To make a toc in our document we'll have to insert the toc tags around the chapters. To make it even better we insert a extra chapter for the TOC. This will be done outside the TOC tags so it will not be listed. We will use Chapter, the problem now is that chapters indents all contents, thats why there is a indent="no" option. Your Document.rml will look like this: <File name="Doc.html"> <Document name="A Document title"> <History> <Entry version="1.0.0" date="18-7-1999" author="You"> More information on the history can be placed between the tags.<br/> You don't have to place the history at the beginning. </Entry> </History> <Chapter name="Content" indent="no"> <Toc> <Chapter name="Chapter1"> This is chapter #1 </Chapter> <Chapter name="Chapter2"> This is chapter #2 <Section name="Section2.1"> This is section 2.1 <SubSection name="SubSection2.1.1"> This is subsection 2.1.1 </SubSection> <SubSection name="SubSection2.1.2"> This is subsection 2.1.2 </SubSection> </Section> <Section name="Section2.2"> This is section 2.2 </Section> </Chapter> </Toc> </Chapter> </Document> </File> With all this you will be able to generate a document with a toc and a good layout. One thing still has to be explained. The grey example blocks.
It works like the chapter and section tags, it will make a grey block with a fixed size font. It made to put sources in. Like a rebol script. It works fine. Beneath is a example block which explains how to make a example block. <Example> REBOL [] Print "This is a example block..." </Example>
The next chapter will show your document so far.
The nice thing is that if you look at the rml of this document, you will see that the example document we have created earlier is just placed in the middle of this chapter. It is generated in this chapter and the file tag profides a link to the other document. You give the name of the link by setting the attribute link="Link to your generated document". The link to open your document should be behind this: More information on the history can be placed between the tags.
You don't have to place the history at the beginning.
This is chapter #1 REBOL [] Print "This is a rebol example block..." This is chapter #2
This is section 2.1 This is subsection 2.1.1 This is subsection 2.1.2
This is section 2.2
.

There is also a attribute toc="Name in toc" which puts the file into the toc. I've added this to the example, notice the top of this document, in the toc there is a link to your document again. You can choose to insert it into the text and/or insert it into the toc. toc="Your Document" was added to the <file> tag.

The whole document source is in the script inself, take a look. } ][ ; The user has to type in a file parsed: parse-XML read to-file ask "rml file: " ] ; The statement that makes starts the generating gathercontent third parsed