EzDev.org

enlive

a selector-based (à la CSS) templating and transformation system for Clojure Home · cgrand/enlive Wiki · GitHub enlive - a selector-based (à la css) templating and transformation system for clojure


Using templates from outside the src folder in enlive

Is it possible to serve a template from outside the source folder in enlive ?

I would like to pickup the templates from my resources/public/templates folder instead of the src/templates folder, how do I do that ?

Thanks, Murtaza


Source: (StackOverflow)

How to use selector negation (but ...) in Enlive on a more complex HTML snippet?

I have got an HTML snippet similar to:

<div id="root">
    <div id="A" attrib_2="bar"></div>
    <div id="B" attrib_2="baz">
        <div id="H" attrib_1="gnu">
            <p>
                <div id="F" attrib_2="baz"></div>
            </p>
        </div>
    </div>
    <div id="C" attrib_2="owl"></div>
    <div id="D" attrib_2="uhu"></div>
    <div id="E" attrib_2="boom"></div>
</div>

Now, I would like to select all snippets having an attrib_2 (*[attrb_2]) excluding those being descendands of a node having attrib_1 set. There can be more nesting levels with arbitrary tags (like <p> in this example). With Enlive (http://enlive.cgrand.net/), I have already tried something like:

(select snippet [(but (attr? :attrib_1)) (attr? :attrib_2)])

But this doesn't work because the negation (but (attr? :attrib_1)) matches also the <p> tag. Is there a way to express this with the given selector predicates (http://enlive.cgrand.net/syntax.html), or do I have to write my own one?

Thanks in advance

-Jochen


Source: (StackOverflow)

Selector based html templating library for Java similar to Enlive

Is anyone aware of a template library for java similar in nature to Enlive?

The concept of using plain html for pages, without any markup inside, and to use the equivalent of CSS selectors to populate the page with dynamic data seems pretty good to me.

For more information what kind of templating I am looking for take a look at this blog post for an example.

I know that it would be possible to create Enlive wrapper in Java and use that, but I am looking at the existing Java alternative.


Source: (StackOverflow)

Clojure web frameworks for responsive apps

I have recently inherited a non-finished web app written in Clojure, based on compojure and hiccup basically. It's a bad attempt to model some sort of MVC with OO style not in the FP style as seen here . So I bet to re-start the project almost from the scratch reusing the useful parts. I consider these alternatives:

The least breaking alternative would be Compojure+Enlive+jquery-pjax

Using a clojure web framework like Pedestal Any experiences about this?

The initial idea was to implement a RESTful API serving JSON so for the more elaborated solution I have Backbone+react.js in my mind for the front-end and Liberator for the back-end but it's likely to take longer to develop than a traditional server app.

Thoughts and alternatives taking into account that an Ajax experience is required please, thanks a lot!


Source: (StackOverflow)

Enlive - Extract content of tag if attribute has designated value

I am trying use Clojure and Enlive to extract content of p html tag under condition that one of attributes has values I designated. Something like this

<p itemprop="description"> Some content I want to extract </p>

So I want to get Some content I want to extract if itemprop="description".

I am very new to Clojure so help would be great.


Source: (StackOverflow)

With enlive, how do you get the tag of a link based off of its content?

I've got some html that looks like this:

<html>
<body>
<a rel='nofollow' href="www.google.com">text</a>
</body>
</html>

How do I get the a by using a selector that says "find any link with a content of text"? I've learned about text-pred, but that just returns the text, not the tag with the text.


Source: (StackOverflow)

Parsing snippets of html with enlive

Why does the following snippet does not work?

(html/select (:body (client/post "http://www.web.onpe.gob.pe/modElecciones/elecciones/elecciones2011/2davuelta/onpe/presidente/extras/provincias.php" {:form-params {"elegido" "010000"}})) [:option])

Do I have to do something with the html-string to turn it into a clojure datastructure first or something like that?


Source: (StackOverflow)

How to use enlive's clone-for with an HTML template to remove dummy child elements

See this google groups topic

Several people have expressed confusion with using clone-for to insert a list of dynamically generated elements into a template which includes several dummy elements. For example, maybe the template looks like this,

<ul>
    <li>foo</li>
    <li>bar</li>
    <li>baz</li>
</ul>

And we want to generate HTML like this,

<ul>
    <li>real</li>
    <li>data</li>
    <li>here</li>
    <li>wurdz</li>
</ul>

The naive thing to try is something like

(defsnippet my-snippet "my-template.html" [:ul] [items] 
    [[:li first-of-type]] (clone-for [ii items]
                             [:li] (content ii)))

But that leaves the 2nd through nth elements with the dummy data. How can we simply remove all the dummy elements and replace with real one?


Source: (StackOverflow)

Clojure, using Enlive to extract raw HTML from a selector?

I need to retrieve some some raw HTML from a certain part of an HTML page.

I wrote the scraper and it grabs the appropriate div, but it returns a map of tags.

(:use [net.cgrand.enlive-html :as html])

(defn fetch-url [url]
 (html/html-resource (java.net.URL. url)))

(defn parse-test []
  (let [url "http://www.ncbi.nlm.nih.gov/pubmedhealth/PMH0000928/"
        url-data (fetch-url url)
        id "a693025"]
    (first (html/select url-data [(keyword (str "#" id "-why"))]))))

This outputs:

{:tag :div, :attrs {:class "section", :id "a693025-why"}, :content ({:tag :h2, :attrs nil, :content ({:tag :span, :attrs {:class "title"}, :content ("Why is this medication prescribed?")})} {:tag :p, :attrs nil, :content ("Zolpidem is used to treat insomnia (difficulty falling asleep or staying asleep). Zolpidem belongs to a class of medications called sedative-hypnotics. It works by slowing activity in the brain to allow sleep.")})}

How do I convert this to raw html? I couldn't find any enlive function to do this.


Source: (StackOverflow)

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.


Source: (StackOverflow)

Binding a local var in deftemplate for enlive

I'm brand new to clojure and the web development stack. I'm trying to use enlive to set values in an HTML template:

(en/deftemplate project-main-page
  (en/xml-resource "project-main.html")
  [id]
  [:#project-name] (en/content (str "Name: " ((get-project id) :name)))
  [:#project-desc] (en/content (str "Desc: " ((get-project id) :desc))))

This works fine to set my two HTML elements, but it involves a repeated call to my function get-project. At the moment this just reads from a local map, but eventually it will involve some external storage access, so I'd prefer to just perform it once in this function.

I was thinking of using let:

(en/deftemplate project-main-page
  (en/xml-resource "project-main.html")
  [id]
  (let [project (get-project id)]
    [:#project-name] (en/content (str "Name: " (project :name)))
    [:#project-desc] (en/content (str "Desc: " (project :desc)))))

But this only affects the description element and ignores the name forms.

What is the best way to bind a local var within deftemplate?


Source: (StackOverflow)

clojure, enlive, multi-site

Trying to load a particular template based on what :server-name returns in the request:

(ns rosay.views.common
  (:use noir.core)
  (:require [noir.request :as req]
            [clojure.string :as string]
            [net.cgrand.enlive-html :as html]))

(defn get-server-name
  "Pulls servername for template definition"
  []
  (or (:server-name (req/ring-request)) "localhost"))

(defn get-template
  "Grabs template name for current server"
  [tmpl]
  (string/join "" (concat [(get-server-name) tmpl])))

(html/deftemplate base (get-template "/base.html")
  []
  [:p] (html/content (get-template "/base.html")))

It works for localhost which returns /home/usr/rosay/resources/localhost/base.html, but when I test against a different host say "hostname2" I see where get-template is looking at /home/usr/rosay/resources/hostname2/base.html but when it renders in the browser it always points back to ../resources/localhost/base.html.

Is there a macro or different way to handle this use-case?


Source: (StackOverflow)

Extracting consecutive html fragments with enlive

I need to scrape html that has the following form:

<div id='content'>
    <h3>Headline1</h3>
    <div>Text1</div>
    <div>Text2</div>
    <div>Text3</div>
    <h3>Headline2</h3>
    <div>Text4</div>
    <div>Text5</div>
    <h3>Headline3</h3>
    <div>Text6</div>
    <div>... and so on ...</div>
</div>

I need to get the content between the headline tags as separate chunks. So from one headline up to the next. Unfortunately there is no container tag for the desired ranges.

I tried the fragment selector {[:h3] [:h3]} but somehow this only returns all h3 tags, without the tags in between them: (({:tag :h3, :attrs nil, :content ("Headline1")}) ({:tag :h3, :attrs nil, :content ("Headline2")}) ({:tag :h3, :attrs nil, :content ("Headline3")}))

What does work, is {[[:h3 (html/nth-of-type 1)]] [[:h3 (html/nth-of-type 2)]]}. This gives me all of the html between the first and second h3-tag. However this does not give me all of the desired chunks with one selector.

Can enlive do this at all or should I resort to a regular expression?

Thanks!


Source: (StackOverflow)

Append to an attribute in Enlive

Is it possible to append a value to an attribute using enlive?

example: I have this

<a rel='nofollow' href="/item/edit/">edit</a>

and would like this

<a rel='nofollow' href="/item/edit/123">edit</a>

I am currently doing this:

(html/defsnippet foo "views/foo.html" [:#main]
  [ctxt]
  [:a] (html/set-attr :href (str "/item/edit/" (ctxt :id))))

But I would prefer not to embed the URL into my code, by just appending the id to the existing URL

(html/defsnippet foo "views/foo.html" [:#main]
  [ctxt]
  [:a@href] (html/append (ctxt :id)))

Source: (StackOverflow)

combine multiple html fragment files with enlive, clojure

I have multiple html files, which is to be combined into a single html file. Those multiple files are like header, footer, etc, which are common to multiple files. I'm using enlive's html-resource method. but, that method inserting missing html tags into the final file, which I don't want.

Following is the output map,

({:tag :html, :attrs nil, :content (
 {:tag :head, :attrs nil, :content (
 {:tag :meta, :attrs {:content text/html; charset=utf-8, :http-equiv Content-Type}, :content ()} 
 {:tag :title, :attrs nil, :content (HewaniLife | Changing The Way You Live)} 
 {:tag :link, :attrs {:href styles/main.css, :rel stylesheet, :type text/css}, :content ()} )} 

 {:tag :body, :attrs nil, :content (
 {:tag :html, :attrs nil, :content ({:tag :body, :attrs nil, :content ({:tag :div, :attrs {:id header}, :content (
 {:tag :h1, :attrs nil, :content ({:tag :a, :attrs {:href index.xhtml, :id logo}, :content (
 {:tag :span, :attrs {:class img-replace}, :content (hewaniLife)})})} 

 {:tag :div, :attrs {:id main-nav}, :content (
 {:tag :ul, :attrs nil, :content (
 {:tag :li, :attrs nil, :content ({:tag :a, :attrs {:href login.xhtml, :id btn-login}, :content (
 {:tag :span, :attrs {:class img-replace}, :content (Login)})})} 
 {:tag :li, :attrs nil, :content ({:tag :a, :attrs {:href index.xhtml, :id btn-home}, :content (
 {:tag :span, :attrs {:class img-replace}, :content (Home)})})} 
 {:tag :li, :attrs nil, :content ({:tag :a, :attrs {:href search.xhtml, :id btn-search}, :content (
 {:tag :span, :attrs {:class img-replace}, :content (Search)})})})})} 
 {:type :comment, :data  end of div#main-nav } 
 {:tag :br, :attrs {:class clear-all}, :content nil})} {:type :comment, :data  end of div#header })})})})}

Here, you can see the html tags nested when I insert the files.

Is there are any way to insert these files..?

Can anybody used any other methods..?


Source: (StackOverflow)