Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Catch parsing markdown error #68

Merged
merged 1 commit into from
Sep 17, 2019
Merged
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
44 changes: 33 additions & 11 deletions src/clojurians_log/message_parser.clj
Original file line number Diff line number Diff line change
@@ -240,19 +240,19 @@
(>= cursor (:end stack-top))
false)))

(defn parse2 [message]
(defn parse2-inner [message]
(let [matches (->> (match-all-patterns message-patterns message)
(map fix-blockquote-match)
(sort match-compare))]

;; (clojure.pprint/pprint matches)

;; Loop starting from the beginning of the message string...
(loop [iteration 0
(loop [iteration 0
last-cursor 0
matches matches
stack []
result []]
matches matches
stack []
result []]

;; (newline)
;; (println (str "vvvvvvvvvvvvvv Loop start " iteration " vvvvvvvvvvvvvv"))
@@ -265,17 +265,17 @@
;; (println ">> result")
;; (clojure.pprint/pprint result)

(let [item (first matches)
cursor (:start item) ;; Cursor is always the starting position of the item being processed
(let [item (first matches)
cursor (:start item) ;; Cursor is always the starting position of the item being processed
stack-top (last stack)]
(cond
;; Completed processing for all matches?
(empty? matches)
(let [msg-len (count message)
(let [msg-len (count message)
;; _ (clojure.pprint/pprint stack)
[new-stack token cursor] (collapse-match-stack message stack msg-len)
_ (assert (empty? new-stack))
cursor (or cursor 0)
_ (assert (empty? new-stack))
cursor (or cursor 0)
;; _ (println "result: " (type result))
;; _ (clojure.pprint/pprint result)
]
@@ -312,6 +312,18 @@
(not= last-cursor cursor))
(conj (extract-undecorated-text message last-cursor cursor)))))))))

(defn parse2
"Parse markdown message into hiccup.

Return the un-parsed message if any error happens."
[message]
(try
(parse2-inner message)
(catch Exception ex
(println "Failed to parse message:" message)
#_(.printStackTrace ex)
[[:undecorated message]])))

(defn parse-with-pattern [pattern-k message]
(let [pattern (get message-patterns pattern-k)]
(re-seq-pos pattern message)))
@@ -330,4 +342,14 @@
doall))
nil)

)
(let [data (clojurians-log.db.queries/channel-day-messages (user/db) "clojure" "2018-02-02")]
(->> data
(map #(parse2 (:message/text %)))
doall))

(with-redefs-fn {#'parse2-inner (fn [_] (throw (ex-info "error" {})))}
(fn [] (let [data (clojurians-log.db.queries/channel-day-messages (user/db) "clojure" "2018-02-02")]
(->> data
(map #(parse2 (:message/text %)))
doall))))
)