emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
From: Martin Alsinet <martin@alsinet.com.ar>
To: stardiviner <numbchild@gmail.com>
Cc: emacs-orgmode <emacs-orgmode@gnu.org>
Subject: Re: Hope ob-js can support :session feature
Date: Fri, 22 Dec 2017 18:32:07 +0000	[thread overview]
Message-ID: <CABUJmkDEC3MYrE6mDtB1xPXYHPggfoNnCsOfmZvgemZpoj5reA@mail.gmail.com> (raw)
In-Reply-To: <0a1fd382-6c99-e69d-c998-b3157edab040@gmail.com>

[-- Attachment #1: Type: text/plain, Size: 1918 bytes --]

Hello stardiviner,

On Fri, Dec 22, 2017 at 6:57 AM stardiviner <numbchild@gmail.com> wrote:

>
> I wish to do JavaScript Literate Programming in Org-mode.
>
> So the :session header argument is very necessary.
>
>
I do Literate Programming in Javascript with Org-mode, and I have found a
workaround using a combination of tangled files and require.

I separate the logic in source code blocks which then I tangle into js
files and I require those js files in other source blocks.

Example:

#+BEGIN_SRC js :tangle src/parser.js
const fs = require('fs');
const parse = require('csv-parse')

function columns(line){
    return line.map(s => s.toLowerCase());
}
parse_csv = function(filename, fn, limit){
    fs.readFile(filename, "utf8", function (err, fileData) {
        var opts = {columns: columns, trim: true};
        if (limit) {
            opts.to = limit;
        }
        parse(fileData, opts, (err, rows) => fn(rows));
    });
}
module.exports = parse_csv;
#+END_SRC

So, I tangle that source block into a js file, and then I can use it from
other blocks, without needing sessions at all:

#+BEGIN_SRC
const parser = require("/app/src/parser.js");
const inputFile = './data/records.csv';
parse_csv(inputFile, console.log);
#+END_SRC

The only drawback is that you have to use absolute paths requiring the js
files, but since I am using docker containers to run node, I always mount
the current directory as a volume in /app inside the container, so that
works out fine.
I also think that this way forces me to separate the code in modular
blocks, which is already a good practice in itself.
With sessions you have to make sure to execute the blocks in the correct
order to build the "state" that your current block needs.
This way source blocks function as standalone units that can be run at any
time, I just run *org-babel-tangle* and then hit *C-c *inside the js block.

I hope that helps you.


Martin

[-- Attachment #2: Type: text/html, Size: 3603 bytes --]

  reply	other threads:[~2017-12-22 18:32 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-12-22 11:57 Hope ob-js can support :session feature stardiviner
2017-12-22 18:32 ` Martin Alsinet [this message]
2017-12-23  1:07   ` numbchild
2017-12-23  3:06     ` Martin Alsinet
2017-12-24  3:13       ` numbchild
2017-12-24 16:15         ` Martin Alsinet

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.orgmode.org/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=CABUJmkDEC3MYrE6mDtB1xPXYHPggfoNnCsOfmZvgemZpoj5reA@mail.gmail.com \
    --to=martin@alsinet.com.ar \
    --cc=emacs-orgmode@gnu.org \
    --cc=numbchild@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).