htmlclojurescreen-scrapingenlive

Clojure Enlive: How to convert enlive nested map data back into HTML?


I am writing a scraper for a site, and the goal is to create a reformatted version of the site. As part of the scraping, I dive into some comments, which might contain html formatting, so that we have:

{... :content ("And, in a lower voice, \"Is this " {:tag :em, :attrs nil, :content ("common")} "?\"")}

The question is: can I take the contents of this :content value and convert them into HTML using a built-in enlive function, like so:

Is this <em>common</em>?

I can see how I might write something to handle these tags but I am extremely hesitant to homebrew anything because I am likely to miss edge cases.


Solution

  • Not built in as far as I know and seems way too awfully specific for it to be built in. My solution:

    (require '[net.cgrand.enlive-html :as html])
    
    (def my-node '{:tag :p, 
                   :content ("And, in a lower voice, \"Is this" 
                            {:tag :em, :attrs nil, :content ("common")} "?\"")})
    
    ;; for escaped string:
    (apply str (html/emit* (:content my-node)))
    => "And, in a lower voice, \"Is this<em>common</em>?\""
    
    ;; print in human readable form
    (print (apply str (html/emit* (:content my-node))))
    => And, in a lower voice, "Is this<em>common</em>?"
       nil