~crc_/block-tools

4de6b1b2835349ff0e60681a2aa29bbdc5130eac — crc 1 year, 8 months ago bd68787
add a block_export tool in retro
1 files changed, 51 insertions(+), 0 deletions(-)

A block_export.retro
A block_export.retro => block_export.retro +51 -0
@@ 0,0 1,51 @@
#!/usr/bin/env retro

Begin by opening the input and output files, and setting up a
couple of variables.

~~~
'ilo.blocks.txt file:W file:open 'OUT const
'ilo.blocks     file:R file:open 'IN  const

'Block var
'Line  var
~~~

Draw the horizontal rule. This is intended to match that used in
the retro/ilo block editor, with the addition of the block
number at the end.

~~~
:rule '___ s:put #6 [ '+----5---- s:put ] times '+--- s:put ;
:id   @Block $: c:put n:put nl ;
~~~

Displaying the block is a matter of reading in 16 lines of 64
characters, outputting the line number, and then the text.
The output is setup to match that in `block_export.py` to keep
compatibility.

~~~
:n:put/padded dup #9 lteq? [ sp ] if n:put sp ;
:display @Line n:put/padded s:trim-right s:put nl &Line v:inc ;
:next s:empty dup buffer:set ;
:read-line #64 [ IN file:read buffer:add ] times ;
:read-block #0 !Line #16 [ next read-line display ] times ;
~~~

The top level loop just iterates over the blocks, displaying the
rules and then the block contents. This overrides `c:put` to
write the output to file.

~~~
[ OUT file:write ] &c:put set-hook
#4096 [ rule id read-block &Block v:inc ] times rule id ;
&c:put unhook
~~~

Final cleanup is just closing the input and output files.

~~~
IN file:close
OUT file:close
~~~