Coder Social home page Coder Social logo

pzavolinsky / elmx Goto Github PK

View Code? Open in Web Editor NEW
351.0 351.0 11.0 520 KB

A tiny precompiler that takes an Elm program with embedded HTML and desugars the HTML into elm-html syntax. Elmx is to Elm what React's JSX is to Javascript

License: MIT License

JavaScript 92.76% Elm 1.81% HTML 0.78% TypeScript 3.20% Gherkin 1.46%

elmx's People

Contributors

cxa avatar freekh avatar jesseskinner avatar prozacchiwawa avatar pzavolinsky avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

elmx's Issues

Events not qualified in attributes

Using onClick in an attribute (like the readme example shows) does not work the way the other tags do. I understand why this is happening, since the onClick is being interpolated, not transpiled, but it would be worth noting this in the README. Here's code to demonstrate:

<button {onClick Decrement}>-</button>

results in:

, Html.button [(onClick Decrement)] [Html.text "-"]

This breaks on compiling unless Html.Events specifically exposes the events it will use, unlike Html and Html.Attributes. It looks like this:

import Html
import Html.Attributes
import Html.Events exposing (onClick)

I wonder if there's an alternative syntax to this that wouldn't require qualifying. Perhaps something more JSX-like that will transpile into Html.Events.onClick automatically? Example:

<button onClick={Decrement}>-</button>
// with a param
<button onClick={Decrement model.range}>-</button>

would result in

, Html.button [(Html.Events.onClick Decrement model.range)] [Html.text "-"]

Nested Elmx

Hi, great library!
Sometimes I'd like to nest Elmx syntax, for example when mapping over children:

<ul>{notes |> List.map (\ note -> <li>{view note}</li>)}</ul>

but this doesn't get the right result.
Is there a fundamental barrier to nesting, or is it just extra tricky to implement?

Attribute spread

The attribute spread example in the live demo seems to be broken

attrsGreeting : List (Attribute msg) -> Html msg
attrsGreeting attrs = <div {:attrs}>Hi</div>

goes to

attrsGreeting : List (Attribute msg) -> Html msg
attrsGreeting attrs = Html.div [(:attrs)] [Html.text "Hi"]

Handling attribute spread would be great for handling styling children natively in elm

Keyed nodes

In JSX there's special syntax for keyed nodes in a list:

<ul>{notes |> map (\ note -> <li key={note.id}>{note.task}</li>)}</ul>

There is functionality for keyed nodes in Elm but so far as I can tell they use a different datastructure from normal nodes. So, it will be tricky to implement this transform as it
A possible first step is, rather than nesting like above (#9), simply translate <li key={k}>{stuff}</li> to (k,li [] [stuff]) and require the keyed parent node explicitly.

elm-format + autocomplete

Hi, thank You for the super library!

I really want to use it, but I don't know how to solve incompatibility with other IDE extensions - I tried:

  1. Visual Studio Code (*.elmx compilation with Gulp and elmx extension for syntax highlighting)

    • Is it possible to format code on save (with elm-format) and don't break autocomplete (and other functions) provided by elm extension?
  2. Atom (extensions: elm-format, language-elm, language-elmx, linter-elm-make)

    • A) Is it possible to format code on save?
    • C) I think it has some problems with linter-ui-default (viz screenshot) (It will be better to create a new issue for this imho)
    • B) There is maybe another bug with linter - some red lines aren't associated with any lint errors

Thank you very much! I'll try to help as possible.
Windows 10 - Atom 1.16.0 - VS Code 1.12.1

Elmx produces invalid multiline strings

<p>
    foo
</p>

produces

Html.node "p" [] [Html.text "
    foo
"]

(error: unexpected \n)

of course, that could be fixed with

Html.node "p" [] [Html.text """
    foo
"""]

for some reason it seems like elm switched from single-pair to triple quote multiline strings in the last year, and i can't find a changelog. maybe i'm going crazy.

Update examples to Elm 0.17

Elm 0.17 changed the way html works quite significantly, so when I saw that the examples were still using Html instead of Html Msg, I was worried. But I worried for nothing: I just tried it and elmx works just fine with Elm 0.17, since it is only the types which changed, not the term-level syntax.

Please update your examples to Elm 0.17 so others don't incorrectly think your tool only works with older versions of Elm!

wrong infix expression translation

  • Expression with two infix operators:
<img src={"http://foo.com/bar" ++ authorId ++ ".png"}></img>

becomes

Html.img [Html.Attributes.attribute "src" ("http://foo.com/bar" ++ authorId ".png")] []
  • The second thing should be evt. a separate issue. Note a space here before closing tag:
<ul class="message-list">{: List.map chatMessage model.messages } </ul>

it translates to

Html.ul [Html.Attributes.attribute "class" "message-list"] ( List.map chatMessage model.messages  ++ [ ])

Without the space it doesn't compile anymore

Html.ul [Html.Attributes.attribute "class" "message-list"]  List.map chatMessage model.messages 

reproducible in TryElmx

haml syntax?

this is a great effort, thank you! isn't it worth considering using something like haml or slim syntax? the jsx is perceived by many is kinda "messy", while haml/slim is more like a cleaned up and familiar version of the Elm's native syntax, that also feels a bit messy.

Left pipe symbol (<|) produces invalid Elm

The <| operator is being seen as an open tag by htmlparser2.

Here's a contrived example:

update : Msg -> Model -> (Model, Cmd Msg)
update msg model =
  case msg of
    NoOp ->
      ( model, Cmd.batch <| [Cmd.none] )

subscriptions : Model -> Sub Msg
subscriptions model =
  Sub.none

results in this code:

update : Msg -> Model -> (Model, Cmd Msg)
update msg model =
  case msg of
    NoOp ->
      ( model, Cmd.batch Html.| [, , , , , ] [Html.text " Sub Msg
subscriptions model =
  Sub.none

Which is definitely not valid Elm. ๐Ÿ˜„

I checked into the elmx parser, and as expected, the htmlparser2 is seeing the pipe symbol as a tag.

# parser.js
    onopentag: function onopentag(name, attrs) {
      // name === "|"

I tried various things to skip operation if the name was a pipe but everything I tried resulted in an error:

TypeError: Cannot read property 'expr' of undefined
    at generate (/Users/bbugh/projects/elm/whaticaneat.com/node_modules/elmx/dist/generator.js:83:18)

It looks like since htmlparser2 reports an open tag, it never properly reports a closed tag, so everything comes through as text mode. I'm not familiar with that library and other than specifying a custom tokenizer, I didn't see any way to tell it to skip certain tags.

I fixed it temporarily for myself by simply replacing <| with โญ๏ธ before the tokenization, and then swapping them back after the generation. I'm sure there's a better way, though. ๐Ÿ˜†

List interpolation

Nice work! Can you expand on why the : operator is required for lists? Seems like you would always interpolate if the list was a elmx fragment. Requiring = for string interpolation definitely makes sense.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.