REBOL [ Title: "MakeDoc 2 - The REBOL Standard Document Formatter" Version: 2.5.7 Copyright: "REBOL Technologies 1999-2005" Author: "Carl Sassenrath" File: %makedoc2.r Date: 10-Mar-2007 ;10-Jan-2005 Purpose: { This is the official MakeDoc document formatter that is used by REBOL Technologies for all documentation. It is the fastest and easiest way to create good looking documentation using any text editor (even ones that do not auto-wrap text). It creates titles, headings, contents, bullets, numbered lists, indented examples, note blocks, and more. For documentation, notes, and other info visit http://www.rebol.net/docs/makedoc.html } Usage: { Create a text document in any editor. Separate each paragraph with a blank line. Run this script and provide your text file. The output file will be the same name with .html following it. If you use REBOL/View the output file will be displayed in your web browser as well. You can also call this script from other scripts (e.g. CGI). These are supported: do %makedoc2.r do/args %makedoc2.r %document.txt do/args %makedoc2.r 'load-only doc: scan-doc read %file.txt set [title out] gen-html/options doc [(options)] write %out.html out } Library: [ level: 'intermediate platform: 'all type: [tool] domain: [html cgi markup] tested-under: none support: none license: 'BSD see-also: none ] ] ; Below you can specify an HTML output template to use for all your docs. ; See the default-template example below as a starting suggestion. template-file: %template.html ; Example: %template.html ; There are three parts to this script: ; 1. The document input scanner. ; 2. The document output formatter (for HTML). ; 3. The code that deals with input and output files. *scanner*: context [ ;-- Debugging: verbose: off debug: func [data] [if verbose [print data]] ;-- Module Variables: text: none para: none code: none title: none left-flag: off opts: [] ;[no-toc no-nums] out: [] ; The output block (static, reused) option: none ;--- Parser rules for the Makedoc text language (top-down): rules: [some commands] commands: [ newline here: (debug ["---PARSE:" copy/part here find here newline]) ;-- Document sections: | ["===" | "-1-"] text-line (emit-section 1) | ["---" | "-2-"] text-line (emit-section 2) | ["+++" | "-3-"] text-line (emit-section 3) | ["..." | "-4-"] text-line (emit-section 4) | "###" to end (emit end none) ; allows notes, comments to follow ;-- Common commands: | #"*" [ [">>" | "**"] text-block (emit bullet3 para) | [">" | "*" ] text-block (emit bullet2 para) | text-block (emit bullet para) ] | #"#" [ ">>" text-block (emit enum3 para) | ">" text-block (emit enum2 para) | text-block (emit enum para) ] | #":" define opt newline (emit define reduce [text para]) ; ">>" reserved ; "<<" reserved ;-- Enter a special section: | #"\" [ "in" (emit indent-in none) | "note" text-line (emit note-in text) | "table" text-line (emit table-in text) | "group" (emit group-in none) | "center" (emit center-in none) | "column" (emit column-in none) ] ;-- Exit a special section: | #"/" [ "in" (emit indent-out none) | "note" (emit note-out none) | "table" (emit table-out none) | "group" (emit group-out none) | "center" (emit center-out none) | "column" (emit column-out none) ] ;-- Extended commands (all begin with "="): | #";" text-block ; comments and hidden paragraphs | #"=" [ #"=" output (emit output trim/auto code) | "image" image | "row" (emit table-row none) | "column" (emit column none) ; (for doc, not tables) | "options" [ any [ spaces copy option [ "toc" | "nums" | "indent" | "no-indent" | "no-toc" | "no-nums" | "no-template" | "no-title" | "old-tags" | "root-images" ] (append opts to-word option) ] ] | "template" some-chars (repend opts ['template as-file text]) ] ;-- Primary implied paragraph types: | example (emit code trim/auto detab code) | paragraph ( either title [emit para para][emit title title: para] ) | skip (debug "???WARN: Unrecognized") ] space: charset " ^-" nochar: charset " ^-^/" chars: complement nochar spaces: [any space] some-chars: [some space copy text some chars] text-line: [any space copy text thru newline] text-block: [any space paragraph opt newline] ; ignore leading space, extra NL !??? paragraph: [copy para some [chars thru newline]] example: [copy code some [indented | some newline indented]] indented: [some space chars thru newline] output: [ some space copy code thru newline any ["==" ["^-" | " "] copy text thru newline (append code text)] ] define: [copy text to " -" 2 skip text-block] image: [ left? any space copy text some chars ( if text/1 = #"%" [remove text] ; remove %file text: as-file text emit image reduce [text pick [left center] left-flag] ) ] left?: [some space "left" (left-flag: on) | none (left-flag: off)] as-file: func [str] [to-file trim str] ;-- Output emitters: emit: func ['word data] [ debug ["===EMIT: " word] if block? word [word: do word] ;???? if string? data [trim/tail data] repend out [word data] ] emit-section: func [num] [ emit [to-word join "sect" num] text title: true ] ;-- Export function to scan doc. Returns format block. set 'scan-doc func [str /options block] [ clear out title: none if options [ if find block 'no-title [title: true] ] emit options opts str: join str "^/^/###" ; makes the parse easier parse/all detab str rules if verbose [ n: 1 foreach [word data] out [ print [word data] if (n: n + 1) > 5 [break] ] ] copy out ] ] ;-- HTML Output Generator ---------------------------------------------------- *html*: context [ ;-- HTML foprmat global option variables: no-nums: ; Do not use numbered sections no-toc: ; Do not generate table of contents no-title: ; Do not generate a title or boilerplate no-indent: ; Do not indent each section no-template: ; Do not use a template HTML page old-tags: ; Allow old markup convention (slower) root-images: ; Images should be located relative to / none toc-levels: 2 ; Levels shown in table of contents image-path: "" ; Path to images set 'gen-html func [ doc [block!] /options opts [block!] /local title template tmp ][ clear out ; (reused) group-count: 0 ; Options still need work!!! no-nums: no-toc: no-title: no-indent: no-template: old-tags: root-images: none set-options opts: any [opts []] set-options select doc 'options if root-images [image-path: %/] ; Template can be provided in =template or in ; options block following 'template. If options ; has 'no-template, then do not use a template. if not no-template [ template: any [select opts 'template select doc 'template template-file] if file? template [template: attempt [read template]] if not template [template: trim/auto default-template] ] ; Emit title and boilerplate: if not no-title [title: emit-boiler doc] ; Emit table of contents: clear-sects if not no-toc [ emit-toc doc clear-sects ] prior-cmd: none forskip doc 2 [ ; If in a table, emit a cell each time. if all [ in-table zero? group-count ; do not emit cell if in group not find [table-out table-row] doc/1 not find [table-in table-row] prior-cmd ][ emit-table-cell ] switch prior-cmd: doc/1 [ para [emit-para doc/2] sect1 [emit-sect 1 doc/2] sect2 [emit-sect 2 doc/2] sect3 [emit-sect 3 doc/2] sect4 [emit-sect 4 doc/2] bullet [emit-item doc doc/1] bullet2 [emit-item doc doc/1] bullet3 [emit-item doc doc/1] enum [emit-item doc doc/1] enum2 [emit-item doc doc/1] enum3 [emit-item doc doc/1] code [doc: emit-code doc] output [doc: emit-code doc] define [emit-def doc] image [emit-image doc/2] table-in [emit-table doc/2] table-out [emit-table-end] table-row [emit-table-row] center-in [emit
] indent-out [emit] column-in [emit {
}] column-out [emit { |
![]() |
$content |
![]() | MakeDoc2 by REBOL - $date |
fix-tags text
] ] emit-code: func [doc] [ emit
while [
switch doc/1 [
code [emit [escape-html doc/2]]
output [emit [ escape-html doc/2 ]]
]
][doc: skip doc 2]
emit
doc: skip doc -2
]
emit-image: func [spec /local tag] [
; Emit image. Spec = 'center or default is 'left.
emit [
either spec/2 = 'center [][
]
join {}]
" " | any [doc/2/1 " "] | fix-tags any [doc/2/2 " "] |
} ] emit-table-end: does [ in-table: false emit " |
] ] emit-toc: func [doc /local w sn] [ ; Output table of contents: emit ["Contents:"
] foreach [word str] doc [ if w: find [sect1 sect2 sect3 sect4] word [ w: index? w if w <= toc-levels [ sn: next-section w emit [make-heading/toc w sn str
] ] ] ] ] emit-boiler: func [doc /local title info temp] [ ; Output top boiler plate: title: any [ select doc 'title select doc 'sect1 "Untitled" ] emit [title
] foreach [word val] doc [ if word = 'code [ emit {} emit-lines val emit {} remove/part find doc 'code 2 break ] if not find [title template options] word [break] ] title ] ] do-makedoc: has [in-view? file msg doc] [ in-view?: all [value? 'view? view?] ; Are we using View? ; Get the file name from the script argument: file: system/script/args if any-string? file [file: to-file file] ; makes copy too ; If no file provided, should we do the last file again? if all [ not file exists? %last-file.tmp ][ file: load %last-file.tmp either confirm reform ["Reprocess" file "?"] [ system/script/args: none ][ file: none ] ] ; If no file still, then ask the user for the file name: if not file [ either in-view? [ file: request-file/only ][ file: ask "Filename? " file: all [not empty? trim file to-file file] ] ] ; No file provided: if not file [exit] ; File must exist: if not exists? file [ msg: reform ["Error:" file "does not exist"] either in-view? [alert msg] [ask msg] exit ] ; Save this as the last file processed: save %last-file.tmp file ; Process the file. Returns [title doc] doc: second gen-html scan-doc read file ; Create output file name: append clear find/last file #"." ".html" write file doc if all [in-view? not system/script/args] [browse file] file ; return new file (entire path) ] ; Start process (but caller may request it only be loaded): if system/script/args <> 'load-only [do-makedoc]