function translate(literate_code)
This file translates Literate Kal files to regular Kal files for the compiler. Literate Kal files are Markdown files with embedded code blocks that contain Kal code. All code blocks (denoted by four leading spaces) are treated as source code while all other Markdown is translated to comments.
function translate(literate_code)
This function reads the Literate code line by line, building a new array of non-Literate (illiterate?) code.
regular_kal_lines = []
last_line = ''
in_code_block = no
for line in literate_code.split '\n'
If the line starts with four spaces and the previous line was blank or code, we keep the line but remove the spaces. Otherwise, we prepend a #
comment marker to make it a comment.
if line.match(/^ /) and (last_line is '' or in_code_block)
in_code_block = yes
regular_kal_lines.push line.slice 4
else
in_code_block = no
regular_kal_lines.push '# ' + line
last_line = line
The translated code is standard Kal.
return regular_kal_lines.join '\n'
exports.translate = translate