mirror of
https://codeberg.org/setop/elm-scripting
synced 2025-11-08 21:49:57 +00:00
Compare commits
4 Commits
main
...
6050fb02a3
| Author | SHA1 | Date | |
|---|---|---|---|
| 6050fb02a3 | |||
| 9566f305b5 | |||
| 7025bc1104 | |||
| a261077f6c |
@@ -1 +1,2 @@
|
|||||||
elm 0.19.1
|
elm 0.19.1
|
||||||
|
python 3.13.1-v2
|
||||||
|
|||||||
70
README.md
70
README.md
@@ -22,8 +22,8 @@ Next, we concatenate this with the Elm JavaScript output and an app launcher sni
|
|||||||
## Limitations
|
## Limitations
|
||||||
|
|
||||||
* No event loop
|
* No event loop
|
||||||
* Hence, no [TEA](https://guide.elm-lang.org/architecture/); no `update` can be triggered
|
* Hence, no [TEA](https://guide.elm-lang.org/architecture/); the `main` function must return a static view
|
||||||
* Hence, no Time, no Random, no Http
|
* Hence, no Time, no Random, no Json Encoder/Decoder (!), no Http
|
||||||
* Nodes can only have one parent (this should always be the case)
|
* Nodes can only have one parent (this should always be the case)
|
||||||
* Does not scale well : creating thousands of Nodes consumes a [lot of RAM](#Performances)
|
* Does not scale well : creating thousands of Nodes consumes a [lot of RAM](#Performances)
|
||||||
|
|
||||||
@@ -37,73 +37,19 @@ Next, we concatenate this with the Elm JavaScript output and an app launcher sni
|
|||||||
```elm
|
```elm
|
||||||
module Hello exposing(main)
|
module Hello exposing(main)
|
||||||
|
|
||||||
import Html exposing (text)
|
import Html exposing (p, text)
|
||||||
|
|
||||||
main = text "Hello World!"
|
main = p [] [text "Hello World!"]
|
||||||
```
|
```
|
||||||
|
|
||||||
4. Run `./elmscript.sh src/Hello.elm`
|
4. Run `./build.sh src/Hello.elm`; this generates the corresponding HTML code:
|
||||||
|
|
||||||
This produces the expected output:
|
```html
|
||||||
|
<p>
|
||||||
```text
|
|
||||||
Hello World!
|
Hello World!
|
||||||
|
</p>
|
||||||
```
|
```
|
||||||
|
|
||||||
## passing input
|
|
||||||
|
|
||||||
Input content can be passed to the script via the standard input. The content is then passed to the Elm script as "flags". The script must then use `Browser.element` and implement `init flags` function.
|
|
||||||
|
|
||||||
|
|
||||||
Create `src/Greet.elm` with the following content:
|
|
||||||
|
|
||||||
```elm
|
|
||||||
module Greet exposing (main)
|
|
||||||
|
|
||||||
import Browser
|
|
||||||
import Html exposing (text)
|
|
||||||
|
|
||||||
|
|
||||||
main =
|
|
||||||
Browser.element
|
|
||||||
{ init = \f -> (f, Cmd.none)
|
|
||||||
, update = \m _ -> (m, Cmd.none)
|
|
||||||
, view = \m -> text ("hello " ++ m)
|
|
||||||
, subscriptions = always Sub.none
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
Run `./elmscript.sh src/Greet.elm <<< 'Elm'`
|
|
||||||
|
|
||||||
This produces the expected output: `hello Elm`.
|
|
||||||
|
|
||||||
|
|
||||||
I's even more natural when passing file :
|
|
||||||
|
|
||||||
* run `elm install elm-explorations/markdown`
|
|
||||||
* create `src/MdRender.elm` with the following content:
|
|
||||||
|
|
||||||
```elm
|
|
||||||
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
|
|
||||||
}
|
|
||||||
|
|
||||||
```
|
|
||||||
|
|
||||||
* run `./elmscript.sh src/MdRender.elm < README.md > README.html`
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
## Performances
|
## Performances
|
||||||
|
|
||||||
Acceptable for small scripts : 250ms on a modest x86_64 CPU and 64MB RAM for a 500 records into a table ; but is does not scale well as everything is loaded before processing ; no streaming contrary to the usual Unix way.
|
Acceptable for small scripts : 250ms on a modest x86_64 CPU and 64MB RAM for a 500 records into a table ; but is does not scale well as everything is loaded before processing ; no streaming contrary to the usual Unix way.
|
||||||
|
|||||||
12
TODO.md
12
TODO.md
@@ -1,8 +1,8 @@
|
|||||||
|
|
||||||
# Major
|
# Major
|
||||||
|
|
||||||
- [ ] find a way to process external data
|
- find a way to process external data
|
||||||
- [x] from stdout (use case : json), use flags
|
- [ ] from stdout (use case : json)
|
||||||
- [ ] from files (use case : SSG)
|
- [ ] from files (use case : SSG)
|
||||||
- [ ] from http (use case : spider)
|
- [ ] from http (use case : spider)
|
||||||
|
|
||||||
@@ -19,10 +19,10 @@
|
|||||||
# Minor
|
# Minor
|
||||||
|
|
||||||
- [ ] support for Elm debug (`-d`) mode (qjs does not implement `console.warn`, only `console.log`)
|
- [ ] support for Elm debug (`-d`) mode (qjs does not implement `console.warn`, only `console.log`)
|
||||||
- [ ] in debug mode, keep the output
|
- [ ] in debug mode, keep the output
|
||||||
- [x] allow to specify or guess the main module name
|
- [x] allow to specify or guess the main module name
|
||||||
- [x] if `innerHTML` attribute is set, output its value instead of the node tree
|
- [x] if `innerHTML` attribute is set, output its value instead of the node tree
|
||||||
- [x] allow to create standalone (`-s`) HTML document instead of fragment => use template engine instead
|
- [ ] allow to create standalone (`-s`) HTML document instead of fragment
|
||||||
- [x] silence elm compiler message when successful
|
- [ ] silence elm compiler message when successful
|
||||||
- [ ] skip compilation steps if source has not been modified ; run directly ; this mean keeping the output
|
- [ ] skip compilation steps if source has not been modified ; run directly ; this mean keeping the output
|
||||||
- [x] add proper copyright and license
|
- [ ] add proper copyright and license
|
||||||
|
|||||||
18
build.debug.sh
Executable file
18
build.debug.sh
Executable file
@@ -0,0 +1,18 @@
|
|||||||
|
#!/bin/sh -eu
|
||||||
|
|
||||||
|
w1="$(mktemp out_$$_1_XXXX.js)"
|
||||||
|
|
||||||
|
elm make --output=${w1} $1 1>&2
|
||||||
|
sed -i -e 's!console.warn(!console.log(!g' ${w1}
|
||||||
|
|
||||||
|
w2="$(mktemp out_$$_XXXX.js)"
|
||||||
|
|
||||||
|
cat dom.js ${w1} launch.js > ${w2}
|
||||||
|
|
||||||
|
rm ${w1}
|
||||||
|
|
||||||
|
qjs --std ${w2}
|
||||||
|
|
||||||
|
# if qjs fails, output file will stay for debugging, else
|
||||||
|
|
||||||
|
rm ${w2}
|
||||||
19
build.sh
Executable file
19
build.sh
Executable file
@@ -0,0 +1,19 @@
|
|||||||
|
#!/bin/sh -eu
|
||||||
|
|
||||||
|
CMDD=$(dirname $(realpath 0))
|
||||||
|
|
||||||
|
w1="$(mktemp out_$$_1_XXXX.js)"
|
||||||
|
|
||||||
|
elm make --optimize --output=${w1} $1 1>&2
|
||||||
|
|
||||||
|
w2="$(mktemp out_$$_XXXX.js)"
|
||||||
|
|
||||||
|
cat ${CMDD}/dom.js ${CMDD}/preelm.js ${w1} ${CMDD}/postelm.js > ${w2}
|
||||||
|
|
||||||
|
rm ${w1}
|
||||||
|
|
||||||
|
qjs --std ${w2}
|
||||||
|
|
||||||
|
# if qjs fails, output file will stay for debugging, else
|
||||||
|
|
||||||
|
rm ${w2}
|
||||||
50
dom.js
50
dom.js
@@ -5,36 +5,28 @@ function Node(parent, tag) {
|
|||||||
this.text = null;
|
this.text = null;
|
||||||
// attributes are stored directly on the node object
|
// attributes are stored directly on the node object
|
||||||
// see https://github.com/elm/virtual-dom/blob/master/src/Elm/Kernel/VirtualDom.js#L511
|
// see https://github.com/elm/virtual-dom/blob/master/src/Elm/Kernel/VirtualDom.js#L511
|
||||||
// but Html.Attributes.attribute/2 uses Element.setAttribute
|
|
||||||
this.setAttribute = (key, val) => {
|
|
||||||
// as of now, there is no check if key conflict with the ones defined by Dom implementation
|
|
||||||
this[key] = val;
|
|
||||||
}
|
|
||||||
this.children = []; // List of Node
|
this.children = []; // List of Node
|
||||||
this.replaceChild = (newNode, domNode) => {
|
this.replaceChild = (newNode, domNode) => {
|
||||||
this.children[domNode.pos-1] = newNode;
|
this.children[domNode.pos-1] = newNode;
|
||||||
newNode.pos = domNode.pos;
|
newNode.pos = domNode.pos;
|
||||||
};
|
};
|
||||||
if (parent != null) {
|
if (parent != null) {
|
||||||
this.pos = parent.children.push(this);
|
this.pos = parent.children.push(this);
|
||||||
}
|
}
|
||||||
this.appendChild = (node) => {
|
this.appendChild = (node) => {
|
||||||
node.pos = this.children.push(node);
|
node.pos = this.children.push(node);
|
||||||
}
|
}
|
||||||
this.replaceData = (offset, count, data) => {
|
this.dump = (d=0) => {
|
||||||
this.text = data; // elm runtime will always replace the whole text
|
if (this.text != null) {
|
||||||
}
|
print(this.text);
|
||||||
this.dump = (d=0) => {
|
return
|
||||||
if (this.text != null) {
|
}
|
||||||
print(this.text);
|
if (this.innerHTML) {
|
||||||
return
|
print(this.innerHTML);
|
||||||
}
|
return
|
||||||
if (this.innerHTML) {
|
}
|
||||||
print(this.innerHTML);
|
std.printf("<%s", this.tagName)
|
||||||
return
|
// Set.difference(other) is not avalable in qjs
|
||||||
}
|
|
||||||
std.printf("<%s", this.tagName)
|
|
||||||
// Set.difference(other) is not avalable in qjs
|
|
||||||
for (a of Object.keys(this)) {
|
for (a of Object.keys(this)) {
|
||||||
if (!NodeKeys.has(a)) {
|
if (!NodeKeys.has(a)) {
|
||||||
std.printf(' %s="%s"', a, this[a])
|
std.printf(' %s="%s"', a, this[a])
|
||||||
@@ -43,18 +35,18 @@ function Node(parent, tag) {
|
|||||||
if (this.children.length==0) {
|
if (this.children.length==0) {
|
||||||
print("/>")
|
print("/>")
|
||||||
} else {
|
} else {
|
||||||
print(">")
|
print(">")
|
||||||
for (c of this.children) {
|
for (c of this.children) {
|
||||||
c.dump(d+1)
|
c.dump(d+1)
|
||||||
}
|
}
|
||||||
print("</"+this.tagName+">");
|
print("</"+this.tagName+">");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
const document = new Node(null, "document");
|
var document = new Node(null, "document");
|
||||||
const target = new Node(document, "target");
|
var target = new Node(document, "target");
|
||||||
const NodeKeys = new Set(Object.keys(target));
|
const NodeKeys = new Set(Object.keys(target));
|
||||||
|
|
||||||
// getElementById is only used once, to get node Elm must hook into.
|
// getElementById is only used once, to get node Elm must hook into.
|
||||||
@@ -67,6 +59,6 @@ document.createTextNode = (text) => { t = new Node(null, "#text" ); t.text = tex
|
|||||||
|
|
||||||
|
|
||||||
// workaround for elm-explorations/markdown Markdown.toHtml
|
// workaround for elm-explorations/markdown Markdown.toHtml
|
||||||
const global = {};
|
global = {};
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
41
elmscript.sh
41
elmscript.sh
@@ -1,41 +0,0 @@
|
|||||||
#!/bin/sh -eu
|
|
||||||
|
|
||||||
CMDD=$(dirname $(realpath 0))
|
|
||||||
|
|
||||||
w1="$(mktemp out_$$_1_XXXX.js)"
|
|
||||||
|
|
||||||
elm make --optimize --output=${w1} $1 1>/dev/null
|
|
||||||
|
|
||||||
w2="$(mktemp out_$$_XXXX.js)"
|
|
||||||
|
|
||||||
cat ${CMDD}/dom.js >> ${w2}
|
|
||||||
cat << EEE >> ${w2}
|
|
||||||
try {
|
|
||||||
EEE
|
|
||||||
cat ${w1} >> ${w2}
|
|
||||||
cat << EEE >> ${w2}
|
|
||||||
Elm[Object.keys(Elm)[0]].init({
|
|
||||||
node: document.getElementById("elm")
|
|
||||||
EEE
|
|
||||||
flags=$(grep -c 'main = $elm$browser$Browser$element' ${w1} || true)
|
|
||||||
if [ $flags -eq 1 ];
|
|
||||||
then
|
|
||||||
cat << EEE >> ${w2};
|
|
||||||
, flags: std.in.readAsString()
|
|
||||||
EEE
|
|
||||||
fi
|
|
||||||
cat << EEE >> ${w2}
|
|
||||||
});
|
|
||||||
document.children[0].dump();
|
|
||||||
} catch(e) {
|
|
||||||
throw e;
|
|
||||||
}
|
|
||||||
EEE
|
|
||||||
|
|
||||||
rm ${w1}
|
|
||||||
|
|
||||||
qjs --std ${w2}
|
|
||||||
|
|
||||||
# if qjs fails, output file will stay for debugging, else
|
|
||||||
|
|
||||||
rm ${w2}
|
|
||||||
13
postelm.js
Normal file
13
postelm.js
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
|
||||||
|
|
||||||
|
// above is the DOM implementation
|
||||||
|
// and the Elm app code
|
||||||
|
|
||||||
|
// here we lanch the app
|
||||||
|
Elm[Object.keys(Elm)[0]].init({ node: document.getElementById("elm") });
|
||||||
|
|
||||||
|
document.children[0].dump();
|
||||||
|
|
||||||
|
} catch(e) {
|
||||||
|
throw e;
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user