There's sample data in a file '../testy-for-pandoc.md'
getm←{↑⊃⎕NGET ⍵ 1} ⍝ reads a native file and returns an APL character matrix
Read the file and show the first 19 rows
tp ← '../testy-for-pandoc.md'
19↑md ← getm tp
HWe need to find whic lines start with three back-ticks. The ticks function returns a boolean vector with a 1 for each such row and a 0 for the other rows.
The second line below displays the contents of the file beside a column containing the boolean result. It stores the boolean result in a variable t for later use.
ticks←{'```'≡⍤1⊢3↑⍤1⊢⍵}
md (⍪t←ticks md)
The next function starts by computing a running total of the 0s and ones in t. Then it calculates the parity of each item in the running total, and compares that value with t.
That gives a 0 for rows that are not code, and a 1 for rows that are code.
That vector is muliplied by minus 4, since we're shortly going to rotate code rows 4 spaces to the right and those are the values that APL needs to do that.
indent←{¯4×⍵<2|+\⍵}
md (⍪rot←indent t)
We're almost done. spun contains the contents of the file with each code row rotated four characters to the right.
rpad←{⍵,⍤1⊢4⍴' '}
+spun ← rot ⌽ rpad md
To get our result we need to get rid of the rows that start with three back-ticks.
So we negate t and use if to compress the rows in spin
spun⌿⍨~t
We can combine all the steps above in a new finction called conv
The next function starts by computing a running total of the 0s and ones in t. Then it calculates the partiy of each item in the running total, and compares that value with t.
That gives a 0 for rows that are not code, and a 1 for rows that are code.
That vector is muliplied by minus 4, since we're shortly going to rotate code rows 4 spaces to the right and those are the values that APL needs to do that.
indent←{¯4×⍵<2|+\⍵}
md (⍪rot←indent t)
We're almost done. spun contains the contents of the file with each code row rotated four characters to the right.
rpad←{⍵,⍤1⊢4⍴' '}
+spun ← rot ⌽ rpad md
To get our result we need to get rid of the rows that start with three back-ticks. So we negate t and use it to compress the rows in spin
spun⌿⍨~t
We can combine all the steps above in a new finction called conv
conv←{t←ticks ⍵ ⋄ r←indent t ⋄ spun←r⌽rpad ⍵ ⋄ spun⌿⍨~t}
conv md