Background
LISP programmers have taken over the world! Parentheses have been declared as sacred characters, and from now on, they can only be used in LISP programs. It has been decided that parentheses in literary works shall be replaced by footnotes, and it's your job to automate this for simplified Markdown text.
Input
Your input is a single string containing alphabetic ASCII characters, spaces, and the special characters ,.!?()
.
It will not contain newlines or digits.
The parentheses will be correctly matched.
Output
You shall convert each matched pair of parentheses in the input string into a footnote. This happens as follows:
- Replace the first matching pair of parentheses and the substring between them by a running number that starts from
1
, wrapped between the Markdown tags<sup>
and</sup>
. - Append to the end of the string
- two newlines,
- the Markdown tag
<sub>
, - the number from step 1,
- a space,
- the substring between the parentheses, and
- the closing tag
</sub>
, in this order.
- If there are still parentheses left in the string, go to step 1.
Your output is the resulting string, possibly with a trailing newline. You don't have to implement this exact algorithm, as long as your output is correct. Note that there may be nested parentheses; in that case, we'll have footnotes that contain references to other footnotes. The substring between parentheses may also be empty. See the test cases below for examples.
Rules and Scoring
Your can write either a full program or a function. The lowest byte count wins, and standard loopholes are disallowed.
If your language does not natively support decimal numbers (cough Retina cough), you may give the footnote numbers in another base, including binary or unary; however, using unary numbers imposes a penalty of +20%.
Test Cases
Input:
This input contains no parentheses.
Output:
This input contains no parentheses.
Input:
This has (some) parentheses (but not so many).
Output:
This has <sup>1</sup> parentheses <sup>2</sup>.
<sub>1 some</sub>
<sub>2 but not so many</sub>
Input:
This has (nested (deeply (or highly?) nested)) parentheses (and several groups).
Output:
This has <sup>1</sup> parentheses <sup>2</sup>.
<sub>1 nested <sup>3</sup></sub>
<sub>2 and several groups</sub>
<sub>3 deeply <sup>4</sup> nested</sub>
<sub>4 or highly?</sub>
Input:
Hmm()(()(,)) a()((trt)(v( (((((wut)))))(X)(Y)(Z) )!?!?!?!))oooooooo(oooo)oooo
Output:
Hmm<sup>1</sup><sup>2</sup> a<sup>3</sup><sup>4</sup>oooooooo<sup>5</sup>oooo
<sub>1 </sub>
<sub>2 <sup>6</sup><sup>7</sup></sub>
<sub>3 </sub>
<sub>4 <sup>8</sup><sup>9</sup></sub>
<sub>5 oooo</sub>
<sub>6 </sub>
<sub>7 ,</sub>
<sub>8 trt</sub>
<sub>9 v<sup>10</sup>!?!?!?!</sub>
<sub>10 <sup>11</sup><sup>12</sup><sup>13</sup><sup>14</sup> </sub>
<sub>11 <sup>15</sup></sub>
<sub>12 X</sub>
<sub>13 Y</sub>
<sub>14 Z</sub>
<sub>15 <sup>16</sup></sub>
<sub>16 <sup>17</sup></sub>
<sub>17 <sup>18</sup></sub>
<sub>18 wut</sub>
Note the empty lines between the footnotes.
foo (bar)\nfoot (note)
? \$\endgroup\$