Convert github markdown to pandoc format¶

There's sample data in a file '../testy-for-pandoc.md'

In [ ]:
getm←{↑⊃⎕NGET ⍵ 1} ⍝ reads a native file and returns an APL character matrix

Read the file and show the first 19 rows

In [ ]:
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.

In [ ]:
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.

In [ ]:
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.

In [ ]:
rpad←{⍵,⍤1⊢4⍴' '}
+spun ← rot ⌽ rpad md
In [ ]:
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
In [ ]:
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.

In [12]:
indent←{¯4×⍵<2|+\⍵}
md (⍪rot←indent t)
Out[12]:
┌────────────────────────────────────────────────────────┬──┐ │# Hello, explorer! │ 0│ │ │ 0│ │In the main *mu* window, type the following program: │ 0│ │ │ 0│ │```python │ 0│ │# blink the red LED on the Explorer HAT │¯4│ │import explorerhat as eh │¯4│ │from time import sleep │¯4│ │ │¯4│ │eh.light.red.on() │¯4│ │sleep(2) │¯4│ │eh.light.red.off() │¯4│ │``` │ 0│ │ │ 0│ │### Typing in the program │ 0│ │ │ 0│ │1. Open a new file in *mu* by clicking the `New` button.│ 0│ │2. Type in the following program: │ 0│ │ │ 0│ │```python │ 0│ │import explorerhat as eh │¯4│ │from time import sleep │¯4│ │ │¯4│ │while True: │¯4│ │ eh.light.red.on() │¯4│ │ sleep(1) │¯4│ │ eh.light.red.off() │¯4│ │ sleep(1) │¯4│ │``` │ 0│ └────────────────────────────────────────────────────────┴──┘

We're almost done. spun contains the contents of the file with each code row rotated four characters to the right.

In [14]:
rpad←{⍵,⍤1⊢4⍴' '}
+spun ← rot ⌽ rpad md
Out[14]:
# Hello, explorer! In the main *mu* window, type the following program: ```python # blink the red LED on the Explorer HAT import explorerhat as eh from time import sleep eh.light.red.on() sleep(2) eh.light.red.off() ``` ### Typing in the program 1. Open a new file in *mu* by clicking the `New` button. 2. Type in the following program: ```python import explorerhat as eh from time import sleep while True: eh.light.red.on() sleep(1) eh.light.red.off() sleep(1) ```

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

In [15]:
spun⌿⍨~t
Out[15]:
# Hello, explorer! In the main *mu* window, type the following program: # blink the red LED on the Explorer HAT import explorerhat as eh from time import sleep eh.light.red.on() sleep(2) eh.light.red.off() ### Typing in the program 1. Open a new file in *mu* by clicking the `New` button. 2. Type in the following program: import explorerhat as eh from time import sleep while True: eh.light.red.on() sleep(1) eh.light.red.off() sleep(1)

We can combine all the steps above in a new finction called conv

In [16]:
conv←{t←ticks ⍵ ⋄ r←indent t ⋄ spun←r⌽rpad ⍵ ⋄ spun⌿⍨~t}
 conv md
Out[16]:
# Hello, explorer! In the main *mu* window, type the following program: # blink the red LED on the Explorer HAT import explorerhat as eh from time import sleep eh.light.red.on() sleep(2) eh.light.red.off() ### Typing in the program 1. Open a new file in *mu* by clicking the `New` button. 2. Type in the following program: import explorerhat as eh from time import sleep while True: eh.light.red.on() sleep(1) eh.light.red.off() sleep(1)