|
240 | 240 | (>= cursor (:end stack-top))
|
241 | 241 | false)))
|
242 | 242 |
|
243 |
| -(defn parse2 [message] |
| 243 | +(defn parse2-inner [message] |
244 | 244 | (let [matches (->> (match-all-patterns message-patterns message)
|
245 | 245 | (map fix-blockquote-match)
|
246 | 246 | (sort match-compare))]
|
247 | 247 |
|
248 | 248 | ;; (clojure.pprint/pprint matches)
|
249 | 249 |
|
250 | 250 | ;; Loop starting from the beginning of the message string...
|
251 |
| - (loop [iteration 0 |
| 251 | + (loop [iteration 0 |
252 | 252 | last-cursor 0
|
253 |
| - matches matches |
254 |
| - stack [] |
255 |
| - result []] |
| 253 | + matches matches |
| 254 | + stack [] |
| 255 | + result []] |
256 | 256 |
|
257 | 257 | ;; (newline)
|
258 | 258 | ;; (println (str "vvvvvvvvvvvvvv Loop start " iteration " vvvvvvvvvvvvvv"))
|
|
265 | 265 | ;; (println ">> result")
|
266 | 266 | ;; (clojure.pprint/pprint result)
|
267 | 267 |
|
268 |
| - (let [item (first matches) |
269 |
| - cursor (:start item) ;; Cursor is always the starting position of the item being processed |
| 268 | + (let [item (first matches) |
| 269 | + cursor (:start item) ;; Cursor is always the starting position of the item being processed |
270 | 270 | stack-top (last stack)]
|
271 | 271 | (cond
|
272 | 272 | ;; Completed processing for all matches?
|
273 | 273 | (empty? matches)
|
274 |
| - (let [msg-len (count message) |
| 274 | + (let [msg-len (count message) |
275 | 275 | ;; _ (clojure.pprint/pprint stack)
|
276 | 276 | [new-stack token cursor] (collapse-match-stack message stack msg-len)
|
277 |
| - _ (assert (empty? new-stack)) |
278 |
| - cursor (or cursor 0) |
| 277 | + _ (assert (empty? new-stack)) |
| 278 | + cursor (or cursor 0) |
279 | 279 | ;; _ (println "result: " (type result))
|
280 | 280 | ;; _ (clojure.pprint/pprint result)
|
281 | 281 | ]
|
|
312 | 312 | (not= last-cursor cursor))
|
313 | 313 | (conj (extract-undecorated-text message last-cursor cursor)))))))))
|
314 | 314 |
|
| 315 | +(defn parse2 |
| 316 | + "Parse markdown message into hiccup. |
| 317 | +
|
| 318 | + Return the un-parsed message if any error happens." |
| 319 | + [message] |
| 320 | + (try |
| 321 | + (parse2-inner message) |
| 322 | + (catch Exception ex |
| 323 | + (println "Failed to parse message:" message) |
| 324 | + #_(.printStackTrace ex) |
| 325 | + [[:undecorated message]]))) |
| 326 | + |
315 | 327 | (defn parse-with-pattern [pattern-k message]
|
316 | 328 | (let [pattern (get message-patterns pattern-k)]
|
317 | 329 | (re-seq-pos pattern message)))
|
|
330 | 342 | doall))
|
331 | 343 | nil)
|
332 | 344 |
|
333 |
| -) |
| 345 | + (let [data (clojurians-log.db.queries/channel-day-messages (user/db) "clojure" "2018-02-02")] |
| 346 | + (->> data |
| 347 | + (map #(parse2 (:message/text %))) |
| 348 | + doall)) |
| 349 | + |
| 350 | + (with-redefs-fn {#'parse2-inner (fn [_] (throw (ex-info "error" {})))} |
| 351 | + (fn [] (let [data (clojurians-log.db.queries/channel-day-messages (user/db) "clojure" "2018-02-02")] |
| 352 | + (->> data |
| 353 | + (map #(parse2 (:message/text %))) |
| 354 | + doall)))) |
| 355 | + ) |
0 commit comments