+
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Changelog

## Unreleased

- Fix [#4](https://github.com/borkdude/html/issues/4): support HTML5 by default (void tags)
14 changes: 7 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ Benefits over some (but definitely not all) hiccup libraries may be:
Drawbacks of this library:

- New and thus not as mature and battle tested as other libraries. Issues + PRs welcome though.
- This library only outputs HTML5. If you want to output XML, use a different library.

In this README, all example results are written as strings. In reality they are
a `borkdude.html.Html` object which just contains a string. This is done to
Expand Down Expand Up @@ -148,19 +149,18 @@ Note that this data reader isn't enabled by default since it's not recommended
to use unqualified data readers for libraries since this can be a source of
conflicts.

## HTML versions
## Complete document

By default, this library outputs XHTML. So `[:br]` compiles to `<br></br>`.
Here is an example of how to to output a valid XHTML 1.1 document:
This library only outputs HTML5. So `[:br]` compiles to `<br>` without a closing tag.
Here is an example of how to to output a complete HTML5 document:

``` clojure
(html
[:<>
[:$ "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.1//EN\" \"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd\">"]
[:html {:xmlns "http://www.w3.org/1999/xhtml" :lang "en"}
[:$ "<!DOCTYPE html>"]
[:html {:lang "en"}
[:head
[:meta {:http-equiv "Content-Type"
:content "text/html; charset=utf-8"}]
[:meta {:charset "utf-8"}]
[:title "Hi"]]
[:body
[:div "ok"]
Expand Down
17 changes: 17 additions & 0 deletions script/changelog.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#!/usr/bin/env bb

(ns changelog
(:require [clojure.string :as str]))

(let [changelog (slurp "CHANGELOG.md")
replaced (str/replace changelog
#" #(\d+)"
(fn [[_ issue after]]
(format " [#%s](https://github.com/borkdude/html/issues/%s)%s"
issue issue (str after))))
replaced (str/replace replaced
#"@([a-zA-Z0-9-_]+)([, \.)])"
(fn [[_ name after]]
(format "[@%s](https://github.com/%s)%s"
name name after)))]
(spit "CHANGELOG.md" replaced))
41 changes: 24 additions & 17 deletions src/borkdude/html.cljc
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
(ns borkdude.html
(:require [clojure.string :as str])
#?(:cljs (:require-macros [borkdude.html :refer [html #_str*]])))
#?(:cljs (:require-macros [borkdude.html :refer [html]])))

(deftype Html [s]
Object
Expand Down Expand Up @@ -50,15 +50,21 @@
(->attrs m)))

#_(defmacro str* [& xs]
(loop [acc ""
xs (seq xs)]
(if xs
(let [x (first xs)
xs (next xs)]
(if (string? x)
(recur (str acc x) xs)
`(str ~acc ~x (str* ~@xs))))
acc)))
(loop [acc ""
xs (seq xs)]
(if xs
(let [x (first xs)
xs (next xs)]
(if (string? x)
(recur (str acc x) xs)
`(str ~acc ~x (str* ~@xs))))
acc)))

(def ^{:doc "A list of elements that must be rendered without a closing tag. From hiccup."
:private true}
void-tags
#{"area" "base" "br" "col" "command" "embed" "hr" "img" "input" "keygen" "link"
"meta" "param" "source" "track" "wbr"})

(defn reader [form]
(cond
Expand All @@ -80,14 +86,15 @@
(if unsafe?
`(->Html (str ~(first children)))
`(->Html (str ~@(if omit-tag?
nil
(if (string? attrs)
[(str "<" tag attrs ">")]
["<" tag " " attrs ">"]))
nil
(if (string? attrs)
[(str "<" tag attrs ">")]
["<" tag " " attrs ">"]))
~@(map #(list `html %) children)
~(if omit-tag?
nil
(str "</" tag ">"))))))
~(if (or omit-tag?
(contains? void-tags tag))
nil
(str "</" tag ">"))))))
(string? form) `(->Html ~(escape-html form))
(number? form) form
:else `(->Html (->safe ~form))))
Expand Down
10 changes: 4 additions & 6 deletions test/borkdude/html_test.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,6 @@
"<a href=\"#\">Hi</a>"
(html [:a {:href "#"} "Hi"])

;; XHTML5?
"<hr></hr>"
(html [:hr])

"<div>&lt;script&gt;</div>"
(html [:div "<script>"])

Expand Down Expand Up @@ -62,11 +58,13 @@
(map (fn [i]
(html [:li i]))
[2 3])])
)

)
"<div><br></div>"
(html [:div [:br]]))
)

(comment
(ok)
(require '[hiccup2.core :as h])
(require '[clojure.walk :refer [macroexpand-all]])
(defn ul []
Expand Down
点击 这是indexloc提供的php浏览器服务,不要输入任何密码和下载