aboutsummaryrefslogtreecommitdiff
path: root/dists/amiga/RM2AG.rexx
blob: f496605ffdc83c27a7796de6a1b8087767b0df82 (plain)
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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
/*
$VER: RM2AG.rexx 0.22 (22.10.2019) README(.md) to .guide converter.
This script converts a given markdown README file (right now, only
ScummVM is supported) to a basic hypertext Amiga guide file and installs
it to a given path, if available.
*/

PARSE ARG readme_md install_path

/*
Check if arguments are available, otherwise quit.
*/
IF ~ARG() THEN DO
	SAY 'No Arguments given!'
	SAY 'Usage: RM2AG.rexx README_MD INSTALL_PATH'
	EXIT No Arguments given!
END

/*
If the given filename/path has spaces in it, AmigaDOS/CLI
will add extra quotation marks to secure a sane working path.
Get rid of them to make AREXX find the file and remove leading
and trailing spaces.
*/
IF ~EXISTS(readme_md) THEN DO
	SAY readme_md' not available!'
	EXIT README.md not available!
END
ELSE DO
	readme_md=STRIP(readme_md)
	readme_md=COMPRESS(readme_md,'"')
END
IF installpath='' THEN DO
	SAY 'No installation destination given!'
	EXIT No installation destination given!
END
ELSE DO
	install_path=STRIP(install_path)
	install_path=COMPRESS(install_path,'"')
	/*
	Check for destination path and create it, if needed.
	*/
	IF ~EXISTS(install_path'sobjs/') THEN
		ADDRESS COMMAND 'makedir 'install_path'sobjs'
END

IF ~OPEN(check_readme,readme_md,'R') THEN DO
	SAY readme_md' opening failed!'
	EXIT README.md opening failed!
END

IF READCH(check_readme,18) = '# [ScummVM README]' THEN DO
	IF ~CLOSE(check_readme) THEN DO
		SAY readme_md' closing failed!'
		EXIT README.md closing failed!
	END
END
ELSE DO
	IF ~CLOSE(check_readme) THEN DO
		SAY readme_md' closing failed!'
		EXIT File README.md closing failed!
	END
	SAY "Not the ScummVM README.md file. Aborting!"
	EXIT Not the ScummVM README.md file.
END

IF ~OPEN(readme_read,readme_md,'R')  THEN DO
	SAY 'File 'readme_md' opening failed!'
	EXIT File README.md opening failed!
END
IF ~OPEN(guide_write,'README.guide','W') THEN DO
	SAY README.guide' opening failed!'
	EXIT README.md opening failed!
END

/*
Prepare the Amiga guide file, add intro and fixed text.
*/
WRITELN(guide_write,'@DATABASE ScummVM README.guide')
WRITELN(guide_write,'@$VER: ScummVM Readme 2.2.0git')
WRITELN(guide_write,'@(C) by The ScummVM team')
WRITELN(guide_write,'@AUTHOR The ScummVM team')
WRITELN(guide_write,'@WORDWRAP')
WRITELN(guide_write,'@NODE "main" "ScummVM README Guide"')
WRITELN(guide_write,'@{b}')
WRITELN(guide_write,SUBSTR(READLN(readme_read),4,14))
WRITELN(guide_write,'@{ub}')

/*
Creating main (TOC) link nodes.
*/

DO WHILE EOF(readme_read) = 0
	working_line=READLN(readme_read)

	/*
	Check for start of actual content and, if available, leave TOC link loop.
	*/
	IF POS('## <>1.0<>)',working_line) = 1 THEN
		LEAVE

	/*
	Check for any "rolled over" lines and, if available, read in the rest
	(on the following line) and rejoin them again before processing any further.
	e.g.	- [<>3.6.3<>) Broken Sword games
		cutscenes](#363-broken-sword-games-cutscenes)
	*/
	IF POS('- [<>',working_line) > 0 THEN DO
		IF POS('(#',working_line) = 0 THEN DO
			rejoin_line=COMPRESS(READLN(readme_read),' ')
			working_line=working_line rejoin_line
		END
	END

	/*
	If no chapter has been found, simply write the line and skip the rest.
	*/
	IF POS('- [',working_line) = 0 THEN
		WRITELN(guide_write,working_line)
	ELSE DO
		/*
		Fix empty chapters:
		Two chapters (1.0 and 7.8) are "empty", consisting of only
		it's chapter names. Link them to their respective sub chapters
		(1.1 and 7.8.1) to not display a blank page.
		
		 If chapter 1.0 is found, add a link node to chapter 1.1.
		*/
		IF POS('  - [<>1.0<>)',working_line) = 1 THEN DO
			/*
			Get rid of the markers, so the following loops won't process
			them again.
			*/
			working_line=COMPRESS(working_line,'-[<>')
			WRITELN(guide_write,'  @{" 1.0 " Link "1.1"} 'SUBSTR(working_line,1,LASTPOS(']',working_line)-1))
		END

		/*
		If chapter 7.8 is found, add a link node to 7.8.1.
		*/
		IF POS('      - [<>7.8<>)',working_line) = 1 THEN DO
			/*
			Get rid of the markers, so the following loops won't process
			them again.
			*/
			working_line=COMPRESS(working_line,'-[<>')
			WRITELN(guide_write,'    @{" 7.8 " Link "7.8.1"} 'SUBSTR(working_line,1,LASTPOS(']',working_line)-1))
		END

		/*
		If a single number main chapter is found (1.0 upto 9.0),
		prepare and write the link node.
		Just for the record:
		A "\" (backslash) is treated as escape character in AmigaGuides.
		Remove it from the node links.
		*/
		IF POS('- [<>',working_line) = 3 THEN DO
			WRITELN(guide_write,' ')
			WRITELN(guide_write,'  @{" 'SUBSTR(working_line,POS('<>',working_line)+2,LASTPOS('<>',working_line)-POS('<>',working_line)-2) '" Link "'SUBSTR(working_line,POS('<>',working_line)+2,LASTPOS('<>',working_line)-POS('<>',working_line)-2)'"} 'COMPRESS(SUBSTR(working_line,1,LASTPOS(']',working_line)-1),'*<>[]\'))
			/*
			Get rid of the markers, so the following loops won't process
			them again.
			*/
			working_line=COMPRESS(working_line,'-[<>')
		END

		/*
		If a level one sub chapter is found (i.e. 1.1, 1.2 etc.),
		prepare and write the link node.
		*/
		IF POS('- [<>',working_line) = 7 THEN DO
			WRITELN(guide_write,'    @{" 'SUBSTR(working_line,POS('<>',working_line)+2,LASTPOS('<>',working_line)-POS('<>',working_line)-2) '" Link "'SUBSTR(working_line,POS('<>',working_line)+2,LASTPOS('<>',working_line)-POS('<>',working_line)-2)'"} 'COMPRESS(SUBSTR(working_line,1,LASTPOS(']',working_line)-1),'*<>[]\'))
			/*
			Get rid of the markers, so the following loops won't process
			them again.
			*/
			working_line=COMPRESS(working_line,'.[<>')
		END

		/*
		If a level two sub chapter is found (i.e. 3.6.1, 3.6.2 etc.),
		prepare and write the link node.
		*/
		IF POS('- [<>',working_line) = 11 THEN DO
			WRITELN(guide_write,'      @{" 'SUBSTR(working_line,POS('<>',working_line)+2,LASTPOS('<>',working_line)-POS('<>',working_line)-2) '" Link "'SUBSTR(working_line,POS('<>',working_line)+2,LASTPOS('<>',working_line)-POS('<>',working_line)-2)'"} 'COMPRESS(SUBSTR(working_line,1,LASTPOS(']',working_line)-1),'*<>[]\'))
			/*
			Get rid of the markers, so the following loops won't process
			them again.
			*/
			working_line=COMPRESS(working_line,'.[<>')
		END
	END
END

/*
Finish TOC (hardcoded as the outro text would be read in last,
but needs to be written after TOC creation finished).
*/
WRITELN(guide_write,'-----')
WRITELN(guide_write,' ')
WRITELN(guide_write,'Good Luck and Happy Adventuring!')
WRITELN(guide_write,'The ScummVM team.')
WRITELN(guide_write,'@{"https://www.scummvm.org/" System "URLOpen https://www.scummvm.org/"}')

/*
Creating sub link nodes.
*/
DO WHILE EOF(readme_read) = 0
	/*
	Change html/markdown links to AmigaGuide ones.
	*/
	IF POS('[here](',working_line) > 0 THEN DO
		working_line=INSERT('@{"',working_line,POS('[',working_line)-1)
		working_line=INSERT('" link ',working_line,POS(']',working_line))
		working_line=INSERT('/main}',working_line,POS(')',working_line))
		working_line=COMPRESS(working_line,'()')
	END

	/*
	If no chapter has been found, simply write the line and skip the rest.
	*/
	IF POS('<>',working_line) = 0 THEN
		WRITELN(guide_write,working_line)
	ELSE DO
		/*
		Fix empty chapters:
		Two chapters (1.0 and 7.8) are "empty", consisting of only
		it's chapter names. Link them to their respective sub chapters
		(1.1 and 7.8.1) to not display a blank page.
		If chapter 1.1 is found, don't close the NODE, just write the line.
		*/
		IF POS('<>1.1<>',working_line) = 1 THEN DO
			/*
			Get rid of the markers, so the following loops won't process
			them again.
			*/
			WRITELN(guide_write,COMPRESS(working_line,'<>'))
		END

		/*
		If chapter 7.8.1 is found don't close the NODE, just write the line.
		*/
		IF POS('<>7.8.1<>',working_line) = 1 THEN DO
			/*
			Get rid of the markers, so the following loops won't process
			them again.
			*/
			WRITELN(guide_write,COMPRESS(working_line,'<>'))
		END

		IF POS('<>',working_line) > 0 THEN DO
			/*
			Check for link references inside the text and create link
			nodes for them.
			*/
			IF POS('section <>',working_line) > 0 THEN DO
				working_line=SUBSTR(working_line,1,POS('<>',working_line)-1)'@{"'SUBSTR(working_line,POS('<>',working_line)+2,LASTPOS('<>',working_line)-POS('<>',working_line)-2)'" Link "'SUBSTR(working_line,POS('<>',working_line)+2,LASTPOS('<>',working_line)-POS('<>',working_line)-2)'"}'SUBSTR(working_line,LASTPOS('<>',working_line)+2)
				/*
				Get rid of the markers, so the following loops won't
				process them again.
				*/
				WRITELN(guide_write,COMPRESS(working_line,'<>'))
			END
			ELSE DO
				/*
				If a chapter has been found, prepare and write the link node.
				*/
				WRITELN(guide_write,'@ENDNODE')
				WRITELN(guide_write,'@NODE "'SUBSTR(working_line,POS('<>',working_line)+2,LASTPOS('<>',working_line)-POS('<>',working_line)-2)'" "'COMPRESS(working_line,'<>#')'"')
				WRITELN(guide_write,' ')
				/*
				Get rid of the markers, so the following loops won't process
				them again.
				*/
				WRITELN(guide_write,COMPRESS(working_line,'<>'))
			END
		END
	END
	/*
	Read in the line at the end of the second loop, as the first line to
	work with was already read in on the end of the first loop.
	*/
	working_line=READLN(readme_read)

	/*
	If the outtro text has been found, leave loop and prepare for closing.
	*/
	IF POS('-----',working_line,1) =1 THEN
		LEAVE
END

WRITELN(guide_write,'@ENDNODE')

/*
Close guide and clean up.
*/
WRITELN(guide_write,'@ENDNODE')

IF ~CLOSE(readme_read) THEN DO
	SAY readme_md' closing failed!'
	EXIT README.md closing failed!
END
IF ~CLOSE(guide_write) THEN DO
	SAY 'README.guide closing failed!'
	EXIT README.guide closing failed!
END

/*
Install finished README.guide to installation path
and delete README.guide.
*/
ADDRESS COMMAND 'copy README.guide 'install_path
ADDRESS COMMAND 'delete README.guide'

EXIT 0