Clojure Enlive: How to convert nested map nested data back to 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 that may contain html formatting, so we have:

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

The question arises: can I take the contents of this: the value of the contents and convert them to HTML using the built-in animation function, for example:

Is it & lt; em> general </em>?

I can see how I can write something to process these tags, but I really do not dare to do anything from home, because I will probably miss extreme cases.

+4
source share
1 answer

Not built-in, as far as I know, and seems too terrible for its creation. My decision:

(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
+4
source

Source: https://habr.com/ru/post/1524915/


All Articles