jostylr / litpro

Minimal command line client for literate programming

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Generate only text (and not code blocks)

shakthimaan opened this issue · comments

Hi,

Thanks for writing litpro. I am able to generate *.js files from .md files and execute code. I would like to know how I can export the .md files to other formats excluding the code blocks.

Appreciate any pointers or examples in this regard.

Thanks!

I haven't thought of doing that. What is your use case? I could see it being useful if, say, we could extract sections of h4 and they could be the user documentation for the code.

Off the top of my head, I would recommend using commonmark to parse it. That is what I use to get the code blocks for litpro to use. You can see the portion that does it at https://github.com/jostylr/literate-programming-lib/blob/master/lp.md#commonmark

You could also have it even keep some code blocks (maybe demo code) by using the fenced code blocks just as litpro can be told to ignore certain code blocks when compiling.

The requirements can be maintained (self-contained) along with the project code, with integration and User Acceptance Tests (UAT). Generating .js code can be used to run the tests, while, the text can produce the requirements document.

If there is a flag option with litpro, that will be nice. I shall have a look at commonmark.

Thanks!

I could see making a litpro plugin that allows one to pipe the markdown document through a transformation. I may do that in a couple of weeks as this general idea may scratch a couple of itches.

For now, you can try the following code as a stand-alone script (with an npm install commonmark) to process the md file:

var commonmark = require("commonmark");

var infile = "lp.md";
var outfile = "doc.md";

var fs = require('fs');
var md = fs.readFileSync(infile, {encoding:"utf8"});

var reader = new commonmark.Parser();
var parsed = reader.parse(md);

var out = md.split("\n");

var walker = parsed.walker();
var event, node, source, i;

while ((event = walker.next())) {
    node = event.node;
    //console.log(node.type, node.sourcepos);
    if (node.type === "code_block") {
        source = node.sourcepos;
        //console.log(source);
        for (i = source[0][0]-1; i < source[1][0]; i +=1) {
            out[i] = false;
        }
    }
}


out = out.filter(function (el) {
    if (el === false) {
        return false;
    } else {
        return true;
    }
});


fs.writeFileSync(outfile, out.join("\n"));

Replace infile and outfile strings with the appropriate soucre and target, respectively. Not elegant, but it should work for now.

I can live with this for now. Thanks for the quick resolution. Cheers!