A project for creating html tag lists and tag clouds for use in your blogging application written in Clojure.
dog-tags is essentially two functions.
- create-tag-list
- create-tag-cloud
Each takes the same data as input but outputs different html.
Leiningen is the recommended way to use dog-tags. Just add
[dog-tags "1.1.0"]
to your project.clj and run
$lein deps
to get dog-tags into your application
user=> (use 'dog-tags.core)
nil
Pass a sequence of maps to create-tag-list. This will return an html unordered list of tag links with their occurrences.
user=> (create-tag-list [
{:tags ["programming" "clojure"]}
{:tags ["Programming" "c" "rant" "programming"]}
{:tags ["politics" "rant"]}
{:tags ["programming" "python"]}
])
<ul>
<li><a href="/tags/c">c</a> (1)</li>
<li><a href="/tags/clojure">clojure</a> (1)</li>
<li><a href="/tags/politics">politics</a> (1)</li>
<li><a href="/tags/programming">programming</a> (4)</li>
<li><a href="/tags/python">python</a> (1)</li>
<li><a href="/tags/rant">rant</a> (2)</li>
</ul>
You may also pass in options to sort by values (higher values at the top) and to limit the results.
user=> (create-tag-list [
{:tags ["programming" "clojure"]}
{:tags ["Programming" "c" "rant" "programming"]}
{:tags ["politics" "rant"]}
{:tags ["programming" "python"]}
]
{:sort :values :limit 3})
<ul>
<li><a href="/tags/programming">programming</a> (4)</li>
<li><a href="/tags/rant">rant</a> (2)</li>
<li><a href="/tags/clojure">clojure</a> (1)</li>
</ul>
Pass a sequence of maps to create-tag-cloud. This will return an html sequence of tag links with their size adjusted according to their frequency of occurrence.
user=> (create-tag-cloud [
{:tags ["programming" "clojure"]}
{:tags ["Programming" "c" "rant" "programming"]}
{:tags ["politics" "rant"]}
{:tags ["programming" "python"]}
])
<a href="/tags/c" style="font-size: 100%;">c</a>
<a href="/tags/clojure" style="font-size: 100%;">clojure</a>
<a href="/tags/politics" style="font-size: 100%;">politics</a>
<a href="/tags/programming" style="font-size: 150%;">programming</a>
<a href="/tags/python" style="font-size: 100%;">python</a>
<a href="/tags/rant" style="font-size: 117%;">rant</a>
the default sizes for cloud links range from 100% to 150%. This can be changed by binding
*min-percent* or *max-percent*
For example, this will change the percent range to 100% to 200%:
user=> (binding [*max-percent* 200]
(create-tag-cloud [
{:tags ["programming" "clojure"]}
{:tags ["Programming" "c" "rant" "programming"]}
{:tags ["politics" "rant"]}
{:tags ["programming" "python"]}
]))
By default the results are returned in html. However, I am using the hiccup project to turn the results into html. If you are also using hiccup in your project and would like to receive the results in the hiccup format, use the :hiccup option in either function. For example,
user=> (create-tag-cloud [
{:tags ["programming" "clojure"]}
{:tags ["Programming" "c" "rant" "programming"]}
{:tags ["politics" "rant"]}
{:tags ["programming" "python"]}
]
{:format :hiccup}))
(
([:a {:href /tags/c, :style font-size: 100%;} c] )
([:a {:href /tags/clojure, :style font-size: 100%;} clojure] )
([:a {:href /tags/politics, :style font-size: 100%;} politics] )
([:a {:href /tags/programming, :style font-size: 150%;} programming] )
([:a {:href /tags/python, :style font-size: 100%;} python] )
([:a {:href /tags/rant, :style font-size: 117%;} rant] )
)
You can change the tag-root (e.g. the value "/tags/") to something else by binding tag-root to another value
user=> (binding [*tag-root* "/labels/" ]
(create-tag-list [{:tags [ "chad" "bob" ] } {:tags [ "chad" "sammy" ] }]))
<ul>
<li><a href="/labels/bob">bob</a> (1)</li>
<li><a href="/labels/chad">chad</a> (2)</li>
<li><a href="/labels/sammy">sammy</a> (1)</li>
</ul>