Created
March 2, 2017 00:46
-
-
Save greggirwin/3085f9cbed23f62b37cc5bb4e7095066 to your computer and use it in GitHub Desktop.
Red Pretty Printer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Red [ | |
title: "Red Pretty Printer" | |
notes: "Derived from http://www.rebol.net/cookbook/recipes/0042.html" | |
] | |
output: none ; output text | |
indent: copy "" ; indentation chars | |
emit-val: func [val] [append output val] | |
emit-space: func [pos] [ | |
either newline = last output [emit-val indent] [ | |
if not any [find "[(" last output find ")]" first pos] [emit-val space] | |
] | |
] | |
emit-part: func [from to] [ | |
emit-space from | |
emit-val copy/part from to | |
] | |
clean-script: func [ | |
"Returns new script text with standardized layout." | |
script "Original Script text" | |
/local blk-rule str new | |
][ | |
output: make string! length? script | |
clear indent | |
parse script blk-rule: [ | |
some [ | |
str: ; Get position for emitting | |
[crlf | newline] (emit-val newline) | |
| #";" [thru newline | to end] new: (emit-part str new) | |
; Emit the opening bracket/paren we just found, update the | |
; indent level, and parse into the block/paren. | |
| [#"[" | #"("] (emit-part str 1 append indent tab) blk-rule | |
| [#"]" | #")"] (remove indent emit-part str 1) break | |
| [space | tab] ; Do nothing. Emit-part adds spaces for us. | |
| skip (value: load/next str 'new emit-part str new) :new | |
] | |
] | |
; We may have added a leading space before the first value we found, | |
; and we don't want that in the output. | |
trim/head output | |
] | |
print clean-script read request-file | |
halt |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment