{"id":15629688,"url":"https://github.com/anko/whatxml","last_synced_at":"2026-01-08T00:08:36.108Z","repository":{"id":19004978,"uuid":"22227467","full_name":"anko/whatxml","owner":"anko","description":"LiveScript library for XML/HTML templating using cascades","archived":false,"fork":false,"pushed_at":"2019-02-28T03:17:23.000Z","size":59,"stargazers_count":1,"open_issues_count":0,"forks_count":1,"subscribers_count":4,"default_branch":"master","last_synced_at":"2025-03-05T21:11:15.270Z","etag":null,"topics":["dsl","html","livescript","template-engine","xml"],"latest_commit_sha":null,"homepage":"","language":"LiveScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"isc","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/anko.png","metadata":{"files":{"readme":"readme.markdown","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2014-07-24T19:26:00.000Z","updated_at":"2019-02-28T03:17:11.000Z","dependencies_parsed_at":"2022-09-25T03:28:05.440Z","dependency_job_id":null,"html_url":"https://github.com/anko/whatxml","commit_stats":null,"previous_names":[],"tags_count":9,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/anko%2Fwhatxml","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/anko%2Fwhatxml/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/anko%2Fwhatxml/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/anko%2Fwhatxml/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/anko","download_url":"https://codeload.github.com/anko/whatxml/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246223325,"owners_count":20743168,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["dsl","html","livescript","template-engine","xml"],"created_at":"2024-10-03T10:28:10.342Z","updated_at":"2026-01-08T00:08:36.067Z","avatar_url":"https://github.com/anko.png","language":"LiveScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# whatxml\n\nXML/HTML templating with [LiveScript][1]'s [cascade][2] syntax.\n\n\"What XML?\"  *None, ever again.*\n\n[![npm package](https://img.shields.io/npm/v/whatxml.svg?style=flat-square)][3]\n[![Build status](https://img.shields.io/travis/anko/whatxml.svg?style=flat-square)][4]\n[![npm dependencies](https://img.shields.io/david/anko/whatxml.svg?style=flat-square)][5]\n\n\u003c!-- !test program\n# Prepend module import statement to input\n# Remove trailing newline from output\nsed '1s/^/whatxml = require \".\\\\/index.ls\" ;/' \\\n| lsc \\-\\-stdin \\\n| head -c -1\n--\u003e\n\n\u003c!-- !test in initial example --\u003e\n\n```ls\nx = whatxml \\html\n  .. \\head\n    .. \\title ._ \"My page\"\n    ..self-closing \\link rel : \\stylesheet type : \\text/css href : \\main.css\n  .. \\body\n    .. \\p ._ (.content)\n\nconsole.log x.to-string { content : \"Here's a paragraph.\" }\n```\n\n→\n\n\u003c!-- !test out initial example --\u003e\n\n```html\n\u003chtml\u003e\u003chead\u003e\u003ctitle\u003eMy page\u003c/title\u003e\u003clink rel=\"stylesheet\" type=\"text/css\" href=\"main.css\" /\u003e\u003c/head\u003e\u003cbody\u003e\u003cp\u003eHere\u0026#x27;s a paragraph.\u003c/p\u003e\u003c/body\u003e\u003c/html\u003e\n```\n\n## API summary\n\n-   `.. \u003cstring\u003e [\u003cattr-object\u003e]` adds a tag (with optional attributes)\n-   `..self-closing \u003cstring\u003e [\u003cattr-object\u003e]` same, but a self-closing tag\n-   `.. \u003cobject\u003e` sets attributes\n-   `.._ \u003cstring\u003e` adds text\n-   `..raw \u003cstring\u003e` adds text (without escaping it)\n-   `..comment \u003cstring\u003e` adds a comment\n\n`to-string` recursively renders that tag's tree.\n\nAny of the setters can also take a function parameter which is called with the\nvalue passed to `to-string`.  It is expected to return the value that should be\ninserted at that point.  (See [§ *Templating*][6].)\n\n## API tutorial\n\n### Basics\n\nCreate a **root tag**, call it with a `string` to create **child tags**, with\nan `object` to **add attributes** or call `_` to **add text** between the tags.\n\n\u003c!-- !test in basics example --\u003e\n\n```ls\ngandalf = whatxml \\person      # Create a root tag.\n  .. { profession : \\wizard }  # Set an attribute.\n  .. \\name                     # Add a child node.\n    .._ \"Gandalf\"              # Put text in it.\nconsole.log gandalf.to-string!\n```\n\n\u003c!-- !test out basics example --\u003e\n\n```xml\n\u003cperson profession=\"wizard\"\u003e\u003cname\u003eGandalf\u003c/name\u003e\u003c/person\u003e\n```\n\nHandy shortcut:  When creating a tag, pass attributes as an object.\n\n\u003c!-- !test in attribute shortcut example --\u003e\n\n```ls\nt = whatxml \\tower lean : \"3.99\"\n  .. \\place city : \"Pisa\", country : \"Italy\"\nconsole.log t.to-string!\n```\n\n\u003c!-- !test out attribute shortcut example --\u003e\n\n```xml\n\u003ctower lean=\"3.99\"\u003e\u003cplace city=\"Pisa\" country=\"Italy\"\u003e\u003c/place\u003e\u003c/tower\u003e\n```\n\nAdd **self-closing tags** and **comments**.\n\n\u003c!-- !test in self-closing tag and comment example --\u003e\n\n```ls\nx = whatxml \\a\n  ..self-closing \\b\n  ..comment \"what\"\nconsole.log x.to-string!\n```\n\n\u003c!-- !test out self-closing tag and comment example --\u003e\n\n```xml\n\u003ca\u003e\u003cb /\u003e\u003c!--what--\u003e\u003c/a\u003e\n```\n\nYou can have **stand-alone attributes** without a value by setting them to\n`true`.  ([It's invalid XML][7], but fine in HTML.)\n\n\u003c!-- !test in stand-alone attribute example --\u003e\n\n```ls\nwhatxml \\input { +selected }\n  ..to-string! |\u003e console.log\n```\n\n\u003c!-- !test out stand-alone attribute example --\u003e\n\n```ls\n\u003cinput selected\u003e\u003c/input\u003e\n```\n\nSetting an attribute to another value overwrites the previous value.  Setting\nattributes to `false`, `null` or `undefined` removes that attribute, if\npresent.\n\nText is **escaped automatically**, but you can **bypass** that with `raw` if\nyou have ready-escaped text (e.g. from [`marked`][8]).\n\n\u003c!-- !test in escaping example --\u003e\n\n```ls\ngreeting = whatxml \\p\n  .._ \"What's up \u003c3\"\nconsole.log greeting.to-string!\n\nx = whatxml \\p\n  ..raw \"\u003cem\u003eI know this is \u0026gt; properly escaped already\u003c/em\u003e\"\nconsole.log x.to-string!\n```\n\n\u003c!-- !test out escaping example --\u003e\n\n```xml\n\u003cp\u003eWhat\u0026#x27;s up \u0026#x3C;3\u003c/p\u003e\n\u003cp\u003e\u003cem\u003eI know this is \u0026gt; properly escaped already\u003c/em\u003e\u003c/p\u003e\n```\n\nYou can also have **multiple top-level tags**:\n\n\u003c!-- !test in multiple top-level example --\u003e\n\n```ls\nx = whatxml!\n  .. \\a\n  .. \\b\nconsole.log x.to-string!\n```\n\n\u003c!-- !test out multiple top-level example --\u003e\n\n```xml\n\u003ca\u003e\u003c/a\u003e\u003cb\u003e\u003c/b\u003e\n```\n\n### Templating\n\nTo **generate content based on data**, you can pass a function to any setter\ncall.  When a tag's `to-string` is called, the functions passed to its setters\nbefore are called with its arguments to produce the final value.\n\n\u003c!-- !test in templating example --\u003e\n\n```ls\nlink = whatxml \\a href : (.href)\n  .._ (.name.to-upper-case!)\n\nconsole.log link.to-string name : \\google    href : \"https://google.com\"\nconsole.log link.to-string name : \\runescape href : \"http://runescape.com\"\n```\n\n\u003c!-- !test out templating example --\u003e\n\n```xml\n\u003ca href=\"https://google.com\"\u003eGOOGLE\u003c/a\u003e\n\u003ca href=\"http://runescape.com\"\u003eRUNESCAPE\u003c/a\u003e\n```\n\n## Limitations\n\nCheck your XML comments are [valid by the XML spec][9]:  They may not contain\ntwo consecutive hyphens (`--`).  Whatxml doesn't check for you.\n\n[`CDATA`-sections][10] and XML declarations (`\u003c?xml version=\"1.0\"?\u003e` and such)\naren't explicitly supported, but you can happily add them using `raw`.\n\n## Related libraries\n\nWhatxml aims to be a serious general-purpose XML/HTML templating engine for\n[LiveScript][11]'s syntax.\n\nExisting attempts have their flaws:\n\n-   [`live-templ`][12] came closest to my goals, but objects in nested arrays\n    cannot represent comments, raw text data or self-closing tags. It also has\n    no templating.\n-   [`create-xml-ls`][13] is based on nested objects, so it can't represent two\n    tags with the same name on the same level of nesting…\n-   [`htmls`][14] supports only the base HTML tag set.  Templating code is\n    [stringly typed][15] and compiled separately.\n\n[1]: http://livescript.net/\n[2]: http://livescript.net/#property-access-cascades\n[3]: https://www.npmjs.com/package/whatxml\n[4]: https://travis-ci.org/anko/whatxml\n[5]: https://david-dm.org/anko/whatxml\n[6]: #templating\n[7]: http://stackoverflow.com/questions/6926442/is-an-xml-attribute-without-value-valid\n[8]: https://github.com/chjj/marked\n[9]: http://www.w3.org/TR/2006/REC-xml11-20060816/#sec-comments\n[10]: http://en.wikipedia.org/wiki/CDATA\n[11]: http://livescript.net/\n[12]: https://www.npmjs.org/package/live-tmpl\n[13]: https://www.npmjs.org/package/create-xml-ls\n[14]: https://www.npmjs.org/package/htmls\n[15]: http://c2.com/cgi/wiki?StringlyTyped\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fanko%2Fwhatxml","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fanko%2Fwhatxml","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fanko%2Fwhatxml/lists"}