Elm scripting, in the terminal

I wrote a little hack to be able to produce static HTML code with Elm, in the terminal.

You give it :

import Html exposing (p, text)

main = p [] [text "Hello World!"]

It generates the corresponding HTML code:

<p>
Hello World!
</p>

By extension, using only text <content>, it can produce any text output, like csv, any.

This is a cheap way to use Elm for scripting in the terminal.

14 Likes

My first use case is to do some programmatic CAD.

I implement all the logic in Elm : model, translation, rotation, drawing ; use elm-scripting to generate the code for OpenSCAD which does the rendering, and supports live reload. :nerd_face:

16 Likes

Sidebar: I looked into QuickJS and then went down this deep rabbit hole learning about Fabrice Bellard. His output and work ethic seems fascinating to me.

2 Likes

Yes, it’s nice to be standing on the shoulders of giants.

OK - this is super cool. Thanks for sharing! :slight_smile:

Yesterday I was playing with elm-explorations/markdown to do some markdown to html rendering.

I discovered that it is using innerHTML attribute to store the result of toHtml which my code was not handling properly. BTW it also requires global to exist.

This has been fixed. Now the code first looks for text content, if not, looks for inner HTML content, if not, recurse to children.

2 Likes

I made an improvement to the tool today. Input content can be passed to the script via standard input. On script side, this is captured as a parameter of the init function.

Now, with the following script :

module MdRender exposing (main)

import Browser
import Html.Attributes exposing (class)
import Markdown

main =
  Browser.element
    { init = \s -> (s, Cmd.none)
    , update = \m c -> (m, Cmd.none)
    , view = \m -> Markdown.toHtml [class "content"] m
    , subscriptions = always Sub.none
    }

We can render md content into html with elmscript.sh src/MdRender.elm < README.md > README.html.

I really enjoy how hackable the Elm runtime is :slight_smile:

1 Like

Is the code for this available somewhere? I’d be very interested in checking it out :grin:
I’ve been playing around with using Lua as a frontend for OpenSCAD: GitHub - ad-si/LuaCAD: Create CAD models with Lua and OpenSCAD
But using Elm would actually be cooler! :smiling_face_with_sunglasses:

2 Likes

@setop, amazing project!

Could you please share some code on OpenSCAD? I had never heard of it until you mentioned your first use case. Programmatic CAD with Elm will be really cool!

Woah, your project looks very interesting.

I’m very far from having anything similar to show.

Programmatic CAD in Elm is currently just an experimentation and elm-script is a side product born form the fact that I was tired of constantly copy-pasting code between the browser and the editor.

I may eventually publish something if it become usable.

If you don’t know OpenSCAD yet, you should definitely start by reading the tutorial, very nice reading.

Then, when using it, the cheatsheet is really useful.

If you want to see what can be done with it in the 3D printing field, have a look at their thigiverse group.

Thanks, @setop! Really nice!

Do you use elm-scripting to produce OpenSCAD code with the text function, as in text “cube(10);”?

Exactly

Create a file src/Cube10.elm with content :

module Cube10 exposing(main)

import Html exposing (text)

main = text "cube(10);"

Then invoking ./elmscript.sh src/Cube10.elm will do the trick.

1 Like

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.