-
Notifications
You must be signed in to change notification settings - Fork 5
/
mush.reb
151 lines (136 loc) · 4.36 KB
/
mush.reb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
Rebol [
Title: "Mush"
Purpose: "MOLD a block in Mushed form"
Date: 14-Jul-2014
Author: "Christopher Ross-Gill"
Version: 0.1.0
License: 'bsd
Type: module
Name: Mush
History: [
0.1.0 [14-Jul-2014 {First pass at a Mushing function. Limited scope:
* Returns a string
* Does not discern word structure e.g. for separate handling of
words with numbers or symbols
* Few special cases for minimizing space between two values
* Uses MOLD and not MOLD/ALL
* Does not replace Rebol words with their RebMu counterpart
* NOT RIGOROUSLY TESTED}]
]
]
export mush: function [
{Mushes words in an array}
source [any-array!] {Array containing words to be mushed}
][
head collect/into [
words: either any-path? source [
[set value [set-word! | word!] (keep value)]
][
[
copy value [set-word! any word! | some word!] (
lower?: true
keep to word! unspaced collect [
if set-word? value.1 [
keep uppercase mold to word! take value
]
for-each word value [
keep either lower? [lowercase mold word][uppercase mold word]
lower?: not lower?
]
]
)
]
]
parse source [
any [
set value any-array! (keep/only mush value)
|
words
|
set value skip (keep :value)
]
]
] make type of source length of source
]
export mold-compact: function [
{Converts a value to a Rebol-readable string (compact)}
source "The value to mold"
[any-value!]
/only "For a block value, mold only its contents, no outer []"
][
if not only [source: reduce source]
unspaced collect [
rule: [
(
space: ""
last-type: none
)
any [
mark: (value: none)
; Blocks: no space before, no space after
[
block! :mark
(
keep "["
)
into rule
(
keep "]"
space: ""
last-type: block!
)
]
| ; Parens: space before, no space after
[
paren! :mark (
keep space
keep "("
)
into rule
(
keep ")"
space: ""
last-type: paren!
)
]
| ; Other Values: space before, space after (unless where exceptions)
[
set value skip (
any [
; Exceptions
case [
last-type = set-word! [
any [
number? value
string? value
tag? value
]
]
last-type = word! [
any [
string? value
tag? value
]
]
]
] else [
keep space
]
keep mold :value
space: either any [
; Exceptions
match [tag! text!] type of value
][""][" "]
last-type: type of value
)
]
]
]
parse source rule else [
keep "<< Mold-Compact Error"
]
]
]
export mush-and-mold-compact: adapt :mold-compact [
value: mush :value
]