Skip to content

Mark this

The Way We Were

First stage

smile.md:

The All-American Smile 

In a way he was like the land itself. 
Everything came to him too easily. 

But at least he knew it, 
and from time to time he worried about being a phoney.
Write a single expression that reads the file,1 follows the applicable Markdown rules, and returns an HTML document as a string, e.g.

<html><body><p>The All-American Smile</p><p>In a way he was like the land itself. Everything ..

Hint

The .h namespace and File Binary; however .h.ht is not a solution.

Not tempted yet? Let’s make it easier and harder.

  • Easier: file lines have no trailing spaces.
  • Harder: let your expression contain no more than one reference to .h.htc.

Oh yes. Now we’re having fun.

First stage

A few variations from Rian Ó’Cuinnegáin:

-1 {i:where ""~/:x;raze "<html><body><p>",@[x;i;:;count[i]#enlist "</p><p>"],"</p></body></html>"} read0 `:smile.md;
-1"<html><body><p>",,[;"</p></body></html>"] ssr[;"n";""] "</p><p>" sv "\n\n" vs`char$read1`:smile.md;
-1"<html><body><p>",ssr["</p><p>"sv"\n\n"vs`char$read1`:smile.md;"\n";""],"</p></body></html>";
-1{.h.htc[y;x]}over(ssr["</p><p>"sv"\n\n"vs`char$read1`:smile.md;"\n";""];`p;`body;`html);
Finally,
q){.h.htc[y;x]}/[;`body`html].h.text"\n\n"vs"c"$ read1 `:smile.md
"<html><body><p>The All-American Smile </p>\n<p>In a way he was like the land..

Casting the result of read1 makes it easy to find the empty lines that divide paragraphs. We can safely ignore – as HTML does – single linebreaks in the markup.

Notice how the pattern {f[y;x]}/[;LLL]R with binary function f applies a list LLL of left arguments successively to a right argument R.

Second stage

smile2.md:

# The All-American Smile 

In a way he was like the land itself. 
Everything came to him too easily. 

But at least he knew it, 
and from time to time he worried about being a phoney.
Output:
<html><body><h1>The All-American Smile</h1><p>In a way he was like the land ..

Solution

q){.h.htc[y;x]}/[;`body`html]raze{.h.htc'[;(2*h)_'x]`p`h1 h:x like"# *"}"\n\n"vs"c"$read1`:smile2.md
"<html><body><h1>The All-American Smile </h1><p>In a way he was like the land..
Of course, this does not interpret the full Markdown syntax, only enough to process the example.


  1. The opening of a short story by Hubbel Gardiner in The Way We Were (1973).