-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathskrish.ksh
More file actions
266 lines (214 loc) · 5.39 KB
/
skrish.ksh
File metadata and controls
266 lines (214 loc) · 5.39 KB
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
#!/bin/ksh
#skrish .1
#steal-but-improve-this-script license
#the author is not responsible for any summoned gremlins
#br@triapul.cz
HELP() {
cat <<EOF_
skrish is a simple plain text language
for composing plays and dialogue heavy
texts.
skrish translates skrish-lang documents
into readable, well formated playscripts.
Anatomy of a skrish-lang doc:
Lines beginning with the following strings
or characters will format subsequent text
accordingly. All blocks of text must
be followed by an empty line. CH must
be followed by a block of text. TITLE,
SUB and ACT do not have to be followed
by an empty line. All tags, with the
exception of # must be followed by a space
or <TAB>.
see examples at https://triapul.cz/soft/skrish
Tags (case sensitive):
TITLE Name of the text
SUB Subtitle.
ACT Title of act.
CH Character name, followed by direct speech.
* Paragraph describing an action.
# Internal comment. Doesn't get printed.
Must be bordered by empty lines.
Lines beginning with anything else are
treated as regular paragraphs.
Dialogue and paragraphs can be split between
several lines or written on a single long one.
Usage: ksh skrish.ksh FILE > FORMATED_FILE
FILE is a properly syntaxed skrish-lang play.
By default the output is formated at
76 characters width with empty margins
on each side. This is to make it
compatible with gopher.
Options:
-h Display this text.
-A INT Margin of Act title. (Default 0)
-d INT Fold dialogue at INT characters.
(Default 40)
-D INT Margin of dialogue from the left.
(Default 8)
-C INT Margin of character name.
(Default 4)
-e INT Width of event paragraphs.
(Default 66)
-E INT Margin of event paragraphs.
(Default 4)
-S INT Margin of subtitle. (Default 0)
-T INT Margin of title. (Default 0)
-w INT Width of regular paragraphs.
Effectively the width of the entire
document. (Default 72)
-W INT Margin of paragraphs. (Default 2)
EOF_
}
#DEFAULT MARGINS
MARGIN_CH=" "
#TO-DO(ATTEMPT) MARGIN_AC=$(tput col)/2 + 1/2 of string l etc
#center text; string-length-fold-length/2 -(pagemargin/2)
MARGIN_AC=""
WIDTH_DL=40
MARGIN_DL=" "
WIDTH_PRG=72
MARGIN_PRG=" "
MARGIN_STAR=" "
WIDTH_STAR=66
MARGIN_TI=""
MARGIN_SU=""
LINE_COUNT() {
LINE=$(sed -n $x\p $LOCAL_FILE)
P_END=$(sed -n $g\p $LOCAL_FILE)
}
LINE_ADV() {
((x++))
}
COUNT_PASSAGE() {
g=$x;TMP=0;
until [[ $P_END = "" ]]; do
LINE_COUNT
((TMP++))
((g++));
done
#print -n "passage long $TMP lines"; read
}
CORRECT_LINE() {
x=$((x+TMP-2))
}
DLG() {
print -n "$MARGIN_DL\""
until [[ $LINE = "" ]]; do
LINE_COUNT
print -n "$LINE "
LINE_ADV
done | sed 's/ *$//'
print -n \"
}
PRG() {
print -n "$MARGIN_PRG"
until [[ $LINE = "" ]]; do
LINE_COUNT
print -n "$LINE "
LINE_ADV
done
}
TRANSFORM_TEXT() {
if [[ $LINE = TITLE* ]]; then
print "$MARGIN_TI$(print $LINE | sed 's/^TITLE *//' |\
tr '[:lower:]' '[:upper:]')"
elif [[ $LINE = \#* ]]; then :
elif [[ $LINE = SUB* ]]; then
print "$MARGIN_SU$(print $LINE | sed 's/^SUB *//')"
elif [[ $LINE = ACT* ]]; then
print "$MARGIN_AC$(print $LINE | sed 's/^ACT *//')"
elif [[ $LINE = CH* ]]; then
print "$MARGIN_CH$(print $LINE | sed 's/^CH *//' |\
tr '[:lower:]' '[:upper:]'):"
LINE_ADV
COUNT_PASSAGE;
DLG | fmt -w $WIDTH_DL
CORRECT_LINE
elif [[ $LINE = \** ]]; then
COUNT_PASSAGE;
MARGIN_PRG=$MARGIN_STAR
PRG | fmt -w $WIDTH_STAR
CORRECT_LINE
else
COUNT_PASSAGE;
PRG | fmt -w $WIDTH_PRG
CORRECT_LINE
fi
}
SKRISH() {
NUMLINES=$(sed -n '$=' $LOCAL_FILE)
x=1
while (( x < NUMLINES + 1 )); do
LINE_COUNT
#print -n "DEBUG: $LINE";read
if [[ $LINE = "" ]]; then
print
else
TRANSFORM_TEXT
fi
LINE_ADV
done
}
CHCK_INT() {
case $1 in
("" | *[!0123456789]*)
print "Option -$FLAG must be an integer!"
exit
esac
}
f=0;
while getopts :A:d:D:C:e:E:w:W:S:T:h FLAG; do
case $FLAG in
h) HELP; exit;;
A) A_F=1;MAR_A=$OPTARG;CHCK_INT $MAR_A;((f++));;
d) WIDTH_DL=$OPTARG;CHCK_INT $WIDTH_DL;((f++));;
D) A_D=1;MAR_D=$OPTARG;CHCK_INT $MAR_D;((f++));;
C) A_C=1;MAR_C=$OPTARG;CHCK_INT $MAR_C;((f++));;
e) WIDTH_STAR=$OPTARG;CHCK_INT $WIDTH_STAR;((f++));;
E) A_S=1;MAR_S=$OPTARG;CHCK_INT $MAR_S;((f++));;
w) WIDTH_PRG=$OPTARG;CHCK_INT $WIDTH_PRG;((f++));;
W) A_W=1;MAR_W=$OPTARG;CHCK_INT $MAR_W;((f++));;
T) A_T=1;MAR_T=$OPTARG;CHCK_INT $MAR_T;((f++));;
S) A_B=1;MAR_B=$OPTARG;CHCK_INT $MAR_B;((f++));;
esac
done
if (( f > 0 )); then
for SHIFTNUM in $(seq $f); do
shift 2
done;fi
WIDEN() {
WID() {
for k in $(seq "$1"); do
print -n " ";
done
}
if [[ $2 = MARGIN_AC ]]; then
MARGIN_AC=$(print -n "$(WID $1 $2)");
elif [[ $2 = MARGIN_DL ]]; then
MARGIN_DL=$(print -n "$(WID $1 $2)");
elif [[ $2 = MARGIN_CH ]]; then
MARGIN_CH=$(print -n "$(WID $1 $2)");
elif [[ $2 = MARGIN_STAR ]]; then
MARGIN_STAR=$(print -n "$(WID $1 $2)");
elif [[ $2 = MARGIN_PRG ]]; then
MARGIN_PRG=$(print -n "$(WID $1 $2)");
elif [[ $2 = MARGIN_TI ]]; then
MARGIN_TI=$(print -n "$(WID $1 $2)");
elif [[ $2 = MARGIN_SU ]]; then
MARGIN_SU=$(print -n "$(WID $1 $2)");
fi
}
if (( A_F == 1 )); then WIDEN $MAR_A MARGIN_AC; fi
if (( A_D == 1 )); then WIDEN $MAR_D MARGIN_DL; fi
if (( A_C == 1 )); then WIDEN $MAR_C MARGIN_CH; fi
if (( A_S == 1 )); then WIDEN $MAR_S MARGIN_STAR; fi
if (( A_W == 1 )); then WIDEN $MAR_W MARGIN_PRG; fi
if (( A_T == 1 )); then WIDEN $MAR_T MARGIN_TI; fi
if (( A_B == 1 )); then WIDEN $MAR_B MARGIN_SU; fi
LOCAL_FILE=$1
if [[ ! -f $LOCAL_FILE ]]; then
print "You need to put in something that exists (and better yet: is formatted for skrish)."
exit
fi
SKRISH