mirror of
https://github.com/unclebob/more-speech.git
synced 2024-09-30 10:30:56 +00:00
ms-linkifying @references.
This commit is contained in:
parent
ffb1d9ddc9
commit
207a226f3a
@ -230,27 +230,44 @@
|
||||
(describe "Segment article content"
|
||||
(it "returns empty list if content is empty"
|
||||
(should= '() (segment-article "")))
|
||||
|
||||
(it "returns a single :text element if no url in content"
|
||||
(should= '([:text "no url"]) (segment-article "no url")))
|
||||
|
||||
(it "returns a single :url element if whole content is a url"
|
||||
(should= '([:url "http://nostr.com"]) (segment-article "http://nostr.com")))
|
||||
(it "returns a list of :text and :url elements when content contains multiple text and url segments"
|
||||
(should= '([:text "Check this "] [:url "http://nostr.com"] [:text " It's cool"])
|
||||
(segment-article "Check this http://nostr.com It's cool")))
|
||||
|
||||
(it "returns a :namereference segment"
|
||||
(should= [[:namereference "@name"] [:text " text"]]
|
||||
(segment-article "@name text")))
|
||||
|
||||
(it "returns a list of :text and :url and :namereference segments"
|
||||
(should= [[:text "Hey "] [:namereference "@bob"] [:text " Check this "] [:url "http://nostr.com"] [:text " It's cool"]]
|
||||
(segment-article "Hey @bob Check this http://nostr.com It's cool")))
|
||||
)
|
||||
|
||||
(describe "Format article"
|
||||
(it "should escape HTML entities"
|
||||
(should= "<b>text</b>" (reformat-article "<b>text</b>")))
|
||||
|
||||
(it "should linkify url"
|
||||
(should= "<a href=\"https://nostr.com\">nostr.com</a>" (reformat-article "https://nostr.com")))
|
||||
|
||||
(it "should ms-link a namereference"
|
||||
(should= "<a href=\"ms-namereference://@name\">@name</a>"
|
||||
(reformat-article "@name")))
|
||||
|
||||
(it "should escape HTML entities and linkify url"
|
||||
(should= "<b>Clojure</b>: <a href=\"https://clojure.org/\">clojure.org/</a>"
|
||||
(reformat-article "<b>Clojure</b>: https://clojure.org/")))
|
||||
|
||||
(it "should format replies and escape HTML entities properly"
|
||||
(should= ">this is<br>>a reply" (reformat-article ">this is >a reply")))
|
||||
|
||||
(it "should replace multiple spaces with  "
|
||||
(should= "one two  three   ." (reformat-article "one two three .")))
|
||||
|
||||
|
||||
)
|
||||
|
||||
(declare db)
|
||||
@ -305,3 +322,17 @@
|
||||
(set-mem :pubkey my-pubkey)
|
||||
(should= "2-deg<-trusted-pet" (format-user-id trusted-by-trusted-user))))
|
||||
)
|
||||
|
||||
(describe "combine patterns"
|
||||
(it "combines a single pattern and name"
|
||||
(let [pattern (combine-patterns [:name1 #"pattern1"])]
|
||||
(should= java.util.regex.Pattern (type pattern))
|
||||
(should= "(?<name1>pattern1)" (str pattern))))
|
||||
|
||||
(it "combines multiple patterns and names"
|
||||
(let [pattern (combine-patterns [:name1 #"pattern1"]
|
||||
[:name2 #"pattern2"])]
|
||||
(should= java.util.regex.Pattern (type pattern))
|
||||
(should= "(?<name1>pattern1)|(?<name2>pattern2)" (str pattern))))
|
||||
|
||||
)
|
||||
|
@ -6,8 +6,7 @@
|
||||
[more-speech.nostr.contact-list :as contact-list]
|
||||
[more-speech.ui.formatter-util :refer :all]
|
||||
[more-speech.config :as config :refer [get-db]]
|
||||
[more-speech.db.gateway :as gateway])
|
||||
)
|
||||
[more-speech.db.gateway :as gateway]))
|
||||
|
||||
(defn format-user-id
|
||||
([user-id]
|
||||
@ -153,30 +152,56 @@
|
||||
uri (if (= 2 (count split-url)) (second split-url) url)]
|
||||
(str "<a href=\"" url "\">" uri "</a>")))
|
||||
|
||||
(defn ms-linkify [type subject]
|
||||
(str "<a href=\"" (str type "://" subject) "\">" subject "</a>"))
|
||||
|
||||
(defn combine-patterns
|
||||
"patterns are a list of [:name pattern]"
|
||||
[& patterns]
|
||||
(let [grouped-patterns (map #(str "(?<" (name (first %)) ">" (second %) ")") patterns)
|
||||
combined-patterns (interpose "|" grouped-patterns)]
|
||||
(re-pattern (apply str combined-patterns))))
|
||||
|
||||
(defn segment-article
|
||||
([content]
|
||||
(let [segment (re-find config/url-pattern content)]
|
||||
(segment-article content []))
|
||||
|
||||
([content segments]
|
||||
(let [patterns [[:url config/url-pattern]
|
||||
[:namereference config/user-name-pattern]]
|
||||
pattern (apply combine-patterns patterns)
|
||||
group-names (map first patterns)]
|
||||
(loop [content content
|
||||
segments segments]
|
||||
(let [matcher (re-matcher pattern content)
|
||||
segment (first (re-find matcher))]
|
||||
(cond
|
||||
(not (nil? segment))
|
||||
(let [url-start-index (string/index-of content segment)
|
||||
(empty? content)
|
||||
segments
|
||||
|
||||
(some? segment)
|
||||
(let [grouped-by-name (map #(vector (keyword %) (.group matcher (name %))) group-names)
|
||||
the-group (filter #(some? (second %)) grouped-by-name)
|
||||
segment-type (ffirst the-group)
|
||||
url-start-index (string/index-of content segment)
|
||||
url-end-index (+ url-start-index (.length segment))
|
||||
text-sub (subs content 0 url-start-index)
|
||||
url-sub (subs content url-start-index url-end-index)
|
||||
rest (subs content url-end-index)]
|
||||
(concat
|
||||
(recur rest
|
||||
(concat segments
|
||||
(if (empty? text-sub)
|
||||
[[:url url-sub]]
|
||||
[[:text text-sub] [:url url-sub]])
|
||||
(segment-article rest)))
|
||||
(not (empty? content)) (list [:text content])
|
||||
:else '()))))
|
||||
[[segment-type url-sub]]
|
||||
[[:text text-sub] [segment-type url-sub]]))))
|
||||
:else
|
||||
(concat segments (list [:text content]))))))))
|
||||
|
||||
(defn reformat-article [article]
|
||||
(let [segments (segment-article article)]
|
||||
(reduce
|
||||
(fn [formatted-content [seg-type seg]]
|
||||
(cond
|
||||
(= seg-type :text)
|
||||
(condp = seg-type
|
||||
:text
|
||||
(str formatted-content
|
||||
((comp
|
||||
non-breaking-spaces
|
||||
@ -185,8 +210,11 @@
|
||||
format-replies
|
||||
) seg)
|
||||
)
|
||||
(= seg-type :url)
|
||||
(str formatted-content (linkify seg))))
|
||||
:url
|
||||
(str formatted-content (linkify seg))
|
||||
|
||||
:namereference
|
||||
(str formatted-content (ms-linkify "ms-namereference" seg))))
|
||||
""
|
||||
segments)))
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user