Dominic Kexel  committed ef85141

some refactoring (use when-let macro to remove some clutter)

  • Participants
  • Parent commits 165b7c7
  • Branches master

Comments (0)

Files changed (3)

File src/mdclj/blocks.clj

 (defn- collect-prefixed-lines [lines prefix]
-  (let [[prefixed remaining] (partition-while #(startswith % prefix) lines)]
+  (when-let [[prefixed remaining] (partition-while #(startswith % prefix) lines)]
      [(map #(to-string (drop (count prefix) %)) prefixed) remaining]))
 (defn- line-seperated [lines]
-  (let [[par [r & rrest :as remaining]] (partition-while (complement blank?) lines)]
+  (when-let [[par [r & rrest :as remaining]] (partition-while (complement blank?) lines)]
     (list par rrest)))
 (declare parse-blocks)
       (concat [{:type ::hrule}] (parse-blocks remaining)))))
 (defn- match-codeblock [text]
-  (let [[code remaining] (collect-prefixed-lines text "    ")]
-    (when (seq code)
-      (concat-blocks ::codeblock code remaining))))
+  (when-let [[code remaining] (collect-prefixed-lines text "    ")]
+    (concat-blocks ::codeblock code remaining)))
 (defn- match-blockquote [text]
-  (let [[quote remaining] (collect-prefixed-lines text "> ")]
-    (when (seq quote)
-      (concat-blocks ::blockquote (parse-blocks quote) remaining))))
+  (when-let [[quote remaining] (collect-prefixed-lines text "> ")]
+    (concat-blocks ::blockquote (parse-blocks quote) remaining)))
 (defn- match-paragraph [text]
-  (let [[lines remaining] (line-seperated text)]
-    (when (seq lines)
-      (concat-blocks ::paragraph (parse-spans (clojure.string/join "\n" lines)) remaining)))) ;; clojure.string/join
+  (when-let [[lines remaining] (line-seperated text)]
+    (concat-blocks ::paragraph (parse-spans (clojure.string/join "\n" lines)) remaining)))
 (defn- match-empty [[head & remaining :as text]]
   (when (and (blank? head) (seq remaining))

File src/mdclj/misc.clj

   "Checks if coll starts with opening and ends with closing.
    If so, returns a list of the elements between 'opening' and 'closing', and the
    remaining elements"
-  (let [remaining (startswith coll opening)]
-    (when (seq remaining)
-      (bracketed-body closing '() remaining))))
+  (when-let [remaining (startswith coll opening)]
+    (bracketed-body closing '() remaining)))
 (defn delimited [coll pattern]
   "Checks if coll starts with pattern and also contains pattern.

File src/mdclj/spans.clj

 (defn- apply-formatter [text [pattern spantype]]
    "Checks if text starts with the given pattern. If so, return the spantype, the text
     enclosed in the pattern, and the remaining text"
-  (let [[body remaining :as match] (delimited text pattern)]
-    (when (seq match)
-      [spantype body remaining])))
+  (when-let [[body remaining] (delimited text pattern)]
+      [spantype body remaining]))
 (defn- get-spantype [text]
   (let [[spantype body remaining :as match] (some #(apply-formatter text %) formatter)]
       (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
-      (when (seq match)
-        (let [span {:type spantype :content (parse-span-body spantype body)}]
-          (concat-spans acc span remaining)))))
+  (when-let [[spantype body remaining :as match] (get-spantype text)] ;; get the first matching span
+      (let [span {:type spantype :content (parse-span-body spantype body)}]
+        (concat-spans acc span remaining))))
 (defn- extract-link-title [text]
   (reduce #(clojure.string/replace % %2 "") (to-string text) [#"\"$" #"'$" #"^\"" #"^'"]))  
       {:url link})))
 (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])
+  (when-let [[linkbody remaining :as body] (bracketed text "[" "]")]
+    (when-let [[linktext remaining :as link] (bracketed 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 remaining ::image)))
 (defn- match-break [acc text]
-  (let [remaining (some #(startswith text %) ["  \n\r" "  \n" "  \r"])]                ;; match hard-breaks
-    (when (not (nil? remaining))
-      (concat-spans acc {:type ::hard-break} remaining))))
+  (when-let [remaining (some #(startswith text %) ["  \n\r" "  \n" "  \r"])]                ;; match hard-breaks
+    (concat-spans acc {:type ::hard-break} remaining)))
 (defn- match-literal [acc [t & trest :as text]]