Comments (8)
It's possible in theory. It might need a bit of rearranging to do, but I'd be okay with it in principle. I've been meaning to tidy up Hiccup's source code for a while, as all that precompilation has made the code a bit labyrinthine.
from hiccup.
Connecting hiccup and enlive would really be very useful.
I have written a render-nodes function that takes a hiccup form and turns it into a node or seq of nodes that enlive is happy with. I had to write it inside src/hiccup/core.clj because it uses the normalize-element private function defined there.
I did not spend time yet developing something to pre-compile the nodes like hiccup does for HTML. It would be great to have that too.
Here is the code I have written. Please, feel free to use it to add to hiccup. If you do not have time for it, please, let me know and I will continue work on my fork. Thank you for such a useful library.
fxt
(defn- nodalize-attribute [[name value]]
(cond
(true? value) [(as-str name) (escape-html name)]
(not value) nil
:else [(as-str name) (escape-html value)]))
(defn- nodalize-attr-map [attrs]
(if (nil? attrs)
nil
(->> attrs seq (map nodalize-attribute) (remove nil?)
(apply concat) (apply sorted-map))))
(defmulti render-nodes
"Turn a Clojure data type into a seq of XML nodes."
{:private false}
type)
(defn- nodalize-element
"Render an tag vector as an XML node."
[element]
(let [[tag attrs content] (normalize-element element)]
{:tag tag
:attrs (nodalize-attr-map attrs)
:content (render-nodes content)}))
(defmethod render-nodes IPersistentVector [element]
(nodalize-element element))
(defmethod render-nodes ISeq [coll]
(map render-nodes coll))
(defmethod render-nodes nil [_]
nil)
(defmethod render-nodes :default [x]
(str x))
BTW, I did some little bit of testing of the type (html [hiccup stuff]) == (enlive-html/emit* (render-nodes [hiccup stuff])) and got the two to agree. That is why I did a bit of transformation on the attribute map and used a sorted-map for it.
from hiccup.
I think if I was going to add this capability, I'd want the library refactored so that the output was controlled by a protocol that could be changed depending on whether you wanted a string of HTML, a string of XML or a tree of XML node maps.
So I don't think there should be a separate render-nodes
function, but what you're written is a good start and feel free to continue refining it.
from hiccup.
since #36, normalize-element is a public function, so you can use @fxt's code in your project like so:
(ns myproject.utils.hiccup2xml
(:require [hiccup.compiler :as cpl])
(:use [hiccup.util :only [as-str escape-html]])
(:import (clojure.lang IPersistentVector ISeq)))
; ... as above
from hiccup.
Enlive added a hiccup-like helper about a year ago (v1.1.0), so I wonder if this feature request is necessary anymore? @weavejester, mind taking a look and closing this ticket if you agree?
from hiccup.
I think there's benefit in allowing hiccup to emit different output structures. Having an output to a Clojure XML structure doesn't sound a bad idea. Aside from Enlive, it would allow the use of the clojure.xml libraries.
from hiccup.
Hey James, I was wondering what the status of this issue and the refactor
branch mentioned in #64 is?
from hiccup.
The latest branch is 2.0
. I haven't done any more work on it in a while.
from hiccup.
Related Issues (20)
- Shouldn't this line call the html function in hiccup2.core? HOT 1
- Hi How to write hiccup code for observable embed code in Roam ? HOT 3
- Null pointer exception with `:class` attribute containing vector nil HOT 3
- Replace license.html with license.txt (for artifact scanners) HOT 9
- Passing nil query param values to to url-encode throws exception
- New version? HOT 15
- :script async HOT 1
- Clojerl support HOT 2
- Historical question: Why is hiccup called hiccup? HOT 2
- link to api docs in project description is broken HOT 3
- Please remove.
- Fragment equivalent for the back end. HOT 2
- *escape-strings?* HOT 1
- hiccup.core/h not backward-compatible HOT 1
- Getting RuntimeException: no such var util/raw-string HOT 2
- Parameters for iframe src attribute HOT 8
- clj-kondo exported config HOT 4
- void-tags might not be complete HOT 3
- "Method code too large!" when compiling the hiccup2.core/html macro HOT 1
- Exponential increase in generated code when there are multiple nested hiccup2.core/html calls HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from hiccup.