aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authors-ol <s-ol@users.noreply.github.com>2018-10-27 07:31:37 +0000
committers-ol <s-ol@users.noreply.github.com>2018-10-27 07:31:37 +0000
commit9a68eccca995ee5b4dc0ffd2698a7a6eb039f6de (patch)
tree266b2a74f25564bba469b32332bb520bcec40a49
parentrename to mmmfs, add canvas on client (diff)
downloadmmm-9a68eccca995ee5b4dc0ffd2698a7a6eb039f6de.tar.gz
mmm-9a68eccca995ee5b4dc0ffd2698a7a6eb039f6de.zip
comment mmmfs
-rw-r--r--app/mmmfs/init.moon46
-rw-r--r--app/mmmfs/tree.moon29
2 files changed, 58 insertions, 17 deletions
diff --git a/app/mmmfs/init.moon b/app/mmmfs/init.moon
index 0808aee..d6ed9fe 100644
--- a/app/mmmfs/init.moon
+++ b/app/mmmfs/init.moon
@@ -1,10 +1,9 @@
export ^
+-- list of interps
+-- interp signature is (fileder, value) -> value
interps = {
- {
- name: 'moon',
- transform: (method) => method @
- },
+ moon: (method) => method @
}
split = (str, delim='->') ->
@@ -14,7 +13,14 @@ split = (str, delim='->') ->
interp, rest = str\match ' *(%w+) *-> *(.*)'
{ interp }, rest
+-- Key of a Fileder Property
+-- contains:
+-- * @name - key name or '' for main content
+-- * @type - final type after interps
+-- * @interps - array describing interp chain
class Key
+ -- instantiate from table w/ keys described above
+ -- or string like 'name: interp -> interp -> type' (name + interps optional)
new: (opts) =>
if 'string' == type opts
@name, rest = opts\match '(%w+): *(.+)'
@@ -29,8 +35,10 @@ class Key
else
error 'wrong argument type'
- -- get a function that interpretes thi type according to @interps
- get_interp: (overrides) =>
+ -- get a function that interpretes a raw value according to @interps
+ -- and returns a value of @type
+ -- overrides is a map of interp overrides
+ get_interp: (overrides={}) =>
return ((val) => val) if #@interps == 0
assert #@interps == 1, 'not supported rn' -- @TODO
@@ -38,12 +46,13 @@ class Key
return overrides[_name] if overrides and overrides[_name]
- for { :name, :transform } in *interps
- if name == _name
- return transform
-
- error "interp not found: '#{_name}'"
+ assert overrides[_name] or interps[_name], "interp not found: '#{_name}'"
+-- list of converts
+-- converts each have
+-- * inp - input type
+-- * out - output type
+-- * transform - function (inp) -> out
converts = {
{
inp: 'mmm/dom',
@@ -108,7 +117,14 @@ do
.innerHTML = markdown md
}
+-- Fileder itself
+-- contains:
+-- * @props - Property Map (Key to Value)
+-- * @children - Children Array
class Fileder
+ -- instantiate from props and children tables
+ -- or mix in one table (numeric keys are children, remainder props)
+ -- prop-keys are passed to Key constructor
new: (props, @children) =>
if not @children
@children = for i, child in ipairs props
@@ -117,7 +133,10 @@ class Fileder
@props = { (Key k), v for k, v in pairs props }
- gett: (...) => assert @get ...
+ -- get property according to criteria, crash if no value or conversion path
+ -- * name - property name (optional: defaults to main content)
+ -- * type - wanted result type
+ -- * overrides - map of interp overrides (optional)
get: (name='', type, overrides) =>
if not type
type = name
@@ -143,6 +162,9 @@ class Fileder
nil, "node doesn't have value for #{name}:#{type}"
+ -- like get, throw if no value or conversion path
+ gett: (...) => assert @get ...
+
require = relative ...
root = require '.tree'
diff --git a/app/mmmfs/tree.moon b/app/mmmfs/tree.moon
index ee25490..2bb578f 100644
--- a/app/mmmfs/tree.moon
+++ b/app/mmmfs/tree.moon
@@ -1,6 +1,10 @@
require = relative ..., 1
Fileder {
+ -- main content
+ -- doesn't have a name prefix (e.g. preview: moon -> mmm/dom)
+ -- uses the 'moon' interp to execute the lua/moonscript function on get
+ -- resolves to a value of type mmm/dom
'moon -> mmm/dom': () =>
html = require 'lib.html'
import article, h1, h2, h3, p, div, a, sup, ol, li, span, code, pre, br from html
@@ -86,13 +90,17 @@ Fileder {
}
append div for child in *@children
- title = child\gett 'title', 'text/plain' -- get 'title' as 'text/plain' (error if no value or conversion possible)
- content = child\get 'preview', 'mmm/dom' -- get 'preview' as a DOM description (nil if no value or conversion possible)
+ -- get 'title' as 'text/plain' (error if no value or conversion possible)
+ title = child\gett 'title', 'text/plain'
+
+ -- get 'preview' as a DOM description (nil if no value or conversion possible)
+ content = child\get 'preview', 'mmm/dom'
+
preview title, content
append h2 "converts"
append p "Well actually it's a bit more complex. You see, the code that renders these previews ", (html.i "asks"), " for those
- name/type pairs (", (code 'title: text/plain'), ' ', (code 'preview: mmm/dom'), "), but the values don't actually have to
+ name/type pairs (", (code 'title: text/plain'), ', ', (code 'preview: mmm/dom'), "), but the values don't actually have to
be ", (html.i "defined"), " as these types."
append pre code [[
@@ -104,8 +112,12 @@ preview = (title, content) -> div {
}
append div for child in *@children
- title = child\gett 'title', 'text/plain' -- get 'title' as 'text/plain' (error if no value or conversion possible)
- content = child\get 'preview', 'mmm/dom' -- get 'preview' as a DOM description (nil if no value or conversion possible)
+ -- get 'title' as 'text/plain' (error if no value or conversion possible)
+ title = child\gett 'title', 'text/plain'
+
+ -- get 'preview' as a DOM description (nil if no value or conversion possible)
+ content = child\get 'preview', 'mmm/dom'
+
preview title, content
]]
@@ -190,7 +202,11 @@ If you are reading this in the source, then c'mon, just scroll past and give me
Fileder {
'title: text/plain': "Hey I'm like a link to picture or smth",
+
+ -- main content is image/png, to be interpreted by URL to access
'URL -> image/png': 'https://picsum.photos/200?random',
+
+ -- preview is a lua/moonscript function that neturns an mmm/dom value
'preview: moon -> mmm/dom': =>
import img from require 'lib.html'
img src: @gett nil, -- look for: main content
@@ -200,6 +216,8 @@ If you are reading this in the source, then c'mon, just scroll past and give me
Fileder {
'title: text/plain': "I'm not even five lines of markdown but i render myself!",
+
+ -- preview can be rendered using global convert
'preview: text/markdown': "See I have like
- a list of things
@@ -208,6 +226,7 @@ If you are reading this in the source, then c'mon, just scroll past and give me
and some bold **text** and `code tags` with me.",
}
+ -- if we are on client (mmm.s-ol.nu/?client=mmmfs), throw in twisted as a child
if MODE == 'CLIENT' then Fileder {
'title: text/plain': "canvas animation that doesn't quite fit",
'preview: moon -> mmm/component': => require '.twisted'