Dominic Kexel committed f6d1ecd

Support for Inline image syntax added

related to bug #6

  • Participants
  • Parent commits 36ef721

Comments (0)

Files changed (4)

File src/mdclj/html.clj

 (ns mdclj.html
   (:use [mdclj.misc]))
-(defn output-element [tag attributes bodyf]
+(defn output-element 
+  ([tag attributes bodyf]
   (let [attr (clojure.string/join \space (for [[k v] attributes :when (seq v)] (str k \= \" v \")))
         attr (if (seq attributes) (str \space attr) attr)]
     (str \< tag attr \> (bodyf) \< \/ tag \>)))
+  ([tag attributes]
+  (let [attr (clojure.string/join \space (for [[k v] attributes :when (seq v)] (str k \= \" v \")))
+        attr (if (seq attributes) (str \space attr) attr)]
+    (str \< tag attr \space \/ \>))))
 (defn html-encode [text]
   (to-string (map #(str "&#x" (.toUpperCase (Integer/toHexString (int %))) \;) text)))
 (defmethod format-span :mdclj.spans/link [{inner :content url :url title :title}]
   (output-element "a" {"href" url "title" title} (content-parser inner)))
+(defmethod format-span :mdclj.spans/image [{inner :content url :url}]
+  (output-element "img" {"src" url "alt" inner}))
 (defmethod format-span :mdclj.spans/hard-break [span]

File src/mdclj/spans.clj

     (parse-span-body nil body))
   ([spantype body]
-    (if (= spantype ::inlinecode)
+    (if (in? [::inlinecode ::image] spantype)
       (to-string body)
-      (parse-spans [] body)))) ;; all spans except inlinecode can be nested
+      (parse-spans [] body)))) ;; all spans except inlinecode and image can be nested
 (defn- match-span [acc text] ;; matches ::inlinecode ::strong ::emphasis
   (let [[spantype body remaining :as match] (get-spantype text)] ;; get the first matching span
       {:url link :title (extract-link-title title)}
       {:url link})))
-(defn- match-link [acc text]
+(defn- match-link-impl [acc text type]
   (let [[linkbody remaining :as body] (bracketed text "[" "]")
         [linktext remaining :as link] (bracketed remaining "(" ")")]
     (when (every? seq [body link])
-      (concat-spans acc (into {:type ::link :content (parse-span-body linkbody)} (parse-link-text linktext)) remaining))))
+      (concat-spans acc (into {:type type :content (parse-span-body type linkbody)} (parse-link-text linktext)) remaining))))
+(defn- match-link [acc text]
+  (match-link-impl acc text ::link))
+(defn- match-inline-image [acc [exmark & remaining :as text]]
+  (when (= exmark \!)
+	  (match-link-impl acc remaining ::image)))
 (defn- match-break [acc text]
   (let [remaining (some #(startswith text %) ["  \n\r" "  \n" "  \r"])]                ;; match hard-breaks
 (def ^:private span-matcher  
+   match-inline-image

File test/mdclj/test/html.clj

 (deftest parse-urls
   (is (= (format-blocks (parse-text "[Example]( Click Me!)")) "<p><a href=\"\" title=\"Click Me!\">Example</a></p>"))
   (is (= (format-blocks (parse-text "[Example]( \"Click Me!\")")) "<p><a href=\"\" title=\"Click Me!\">Example</a></p>"))
-  (is (= (format-blocks (parse-text "[Example](")) "<p><a href=\"\">Example</a></p>")))
+  (is (= (format-blocks (parse-text "[Example](")) "<p><a href=\"\">Example</a></p>"))
+  (is (= (format-blocks (parse-text "![](")) "<p><img src=\"\" /></p>"))
+  (is (= (format-blocks (parse-text "![Example]( Click Me!)")) "<p><img src=\"\" alt=\"Example\" /></p>")))
 (deftest parse-urls1
   (let [[http-text  http-result]  ["<http://stuff>"  "<p><a href=\"http://stuff\">http://stuff</a></p>"]]

File test/mdclj/test/spans.clj

                                                                          {:type :mdclj.spans/literal, :content "literal "}
                                                                          {:type :mdclj.spans/inlinecode, :content "code **literal**"})))
   (is (= (parse-spans "hello  \n\rworld  \r!!") '({:type :mdclj.spans/literal, :content "hello"} {:type :mdclj.spans/hard-break} {:type :mdclj.spans/literal, :content "world"} {:type :mdclj.spans/hard-break} {:type :mdclj.spans/literal, :content "!!"})))
-  (is (= (parse-spans "[google](") '({:type :mdclj.spans/link, :url "", :content ({:type :mdclj.spans/literal, :content "google"})})))
+  (is (= (parse-spans "[google](")  '({:type :mdclj.spans/link, :url "", :content ({:type :mdclj.spans/literal, :content "google"})})))
+  (is (= (parse-spans "![google](") '({:type :mdclj.spans/image, :url "", :content "google"})))
   (is (= (parse-spans "[google]( \"Click Me!\")") '({:type :mdclj.spans/link, 
                                                                       :url "", 
                                                                       :title "Click Me!",
                                                                       :content ({:type :mdclj.spans/literal, :content "google"})})))
+  (is (= (parse-spans "![google]( \"Click Me!\")") '({:type :mdclj.spans/image, 
+                                                                      :url "", 
+                                                                      :title "Click Me!",
+                                                                      :content "google"})))
   (is (= (parse-spans "***strongem***") '({:type :mdclj.spans/strong, :content 
                                               ({:type :mdclj.spans/emphasis, :content 
                                                 ({:type :mdclj.spans/literal, :content "strongem"})})})))