Code Monkey home page Code Monkey logo

xom's Introduction

Documentation

xom

Transform XML trees into performant JavaScript DOM calls at compile-time using Nim code.

hello.nim

The above will compile to the following JavaScript code:

var p0 = document.createElement("p");
p0.appendChild(document.createTextNode("Hello!"));
document.body.appendChild(p0);

Installation

xom supports Nim 1.4.0+ and can be installed using Nimble:

$ nimble install xom

Customizing code generation

xom can be customized to generate code that is optimized for a particular use case. Nodes can be modified in-place, new child nodes can be created, the code generation can be suppressed all together for particular nodes, and variables are only created for nodes that you specify.

All this customization is done through the use of the two simple callbacks of the Xom context object:

  • onEnter*: XmlNode -> Command
  • onEmitNamed*: (XmlNode, NimNode) -> void

onEnter is called for every node that is found, and onEmitNamed is called for every node for which a variable has been requested. onEnter returns a Command object, which is an enum type that can be one of the following:

  • Emit: the node will be emitted but no variable will be created (default for all nodes).
  • EmitNamed: the node will be emitted and a variable for it will be created. This also triggers a call to onEmitNamed on the node.
  • Skip: the node will be skipped and no variable will be created.

Inside both callbacks, you can modify nodes in-place, and changes will be reflected in the generated code at compile-time.

By default, no variables are created for nodes that are not requested if not necessary (and necessary but not requested variables are always scoped by default). If you want to reference a node inside onEmitNamed, you have to return EmitNamed from onEnter for that node. In particular, text nodes are always merged together unless a variable is being emitted for them. Having a variable for a node is useful for dinamically modifying the node in separately generated code (see examples/ and tests/ for some simple use cases).

xom's People

Contributors

schneiderfelipe avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar

xom's Issues

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.