Raynes / laser

HTML transformation/templating for people who do that sort of thing and stuff
120 stars 16 forks source link

Problem inserting HTML inside a node #7

Closed samrat closed 11 years ago

samrat commented 11 years ago

I'm trying to get an output like this:

    <section>
        <a href="/foo">Foo</a>
        <a href="/bar">Bar</a>
        ...
    </section>

where the links are inserted using laser. The problem is that I can't seem to unescape the content. Here's what I have now:

 (defn generate-item [post]
  (let [title (:title post)
        a (l/parse-fragment (str "<a>" title "</a>"))
        link (post-url post)
        html (l/fragment-to-html (l/fragment a
                                             (l/element= :a)
                                             (l/attr :href link)))]
    html))

(defn generate-index [in-dir]
  (let [t (slurp (str in-dir "/templates/index.html"))]
    (l/document (l/parse t)
                    (l/element= :section)
                    (fn [node]
                      (reduce (fn [node post]
                                (update-in node [:content]
                                           conj (generate-item post)))
                              node (all-posts in-dir))))))

,where

;; (all-posts "/tmp/input")
;=> ({:file #<File /tmp/mdrand/nginx_vagrant_flask_gunicorn.md>, :tags ["deployment" "flask" "nginx" "gunicorn"], :date "2012-05-27", :title "Flask + Nginx + Gunicorn(on a Vagrant box)"} 
;    {:file #<File /tmp/mdrand/db-cljs.md>, :tags ["clojure" "clojurescript" "programming"], :date "2012-10-17", :title "Building a database-backed Clojurescript app"}
;    {:file #<File /tmp/mdrand/clojurescript.md>, :date "2012-10-14", :tags ["programming" "clojure" "clojurescript"], :title "Getting started with Clojurescript"})

Could you please hint at where I need to unescape it?

Raynes commented 11 years ago

You can't insert HTML strings. They have to be parsed nodes (the output of fragment is suitable for direct insertion into other nodes, just add it to the content instead of a string).

samrat commented 11 years ago

Thanks, that solves my problem.