mirror of
https://github.com/unclebob/more-speech.git
synced 2024-09-30 10:30:56 +00:00
replace-references and lookup-references use profiles
This commit is contained in:
parent
981aa6b412
commit
f7bead98f4
@ -145,41 +145,41 @@ the proposition that all men are created equal."
|
||||
|
||||
(it "replaces a single p reference"
|
||||
(let [content "the #[0] reference"
|
||||
nicknames {0 "x"}
|
||||
event-context (atom {:nicknames nicknames})
|
||||
profiles {0 {:name "x"}}
|
||||
event-context (atom {:profiles profiles})
|
||||
_ (reset! ui-context {:event-context event-context})
|
||||
event {:content content :tags [[:p (hexify 0)]]}]
|
||||
(should= "the @x reference" (replace-references event))))
|
||||
|
||||
(it "replaces a single p reference at the start"
|
||||
(let [content "#[0] reference"
|
||||
nicknames {0 "x"}
|
||||
event-context (atom {:nicknames nicknames})
|
||||
profiles {0 {:name "x"}}
|
||||
event-context (atom {:profiles profiles})
|
||||
_ (reset! ui-context {:event-context event-context})
|
||||
event {:content content :tags [[:p (hexify 0)]]}]
|
||||
(should= "@x reference" (replace-references event))))
|
||||
|
||||
(it "replaces a single p reference at the end"
|
||||
(let [content "the #[0]"
|
||||
nicknames {0 "x"}
|
||||
event-context (atom {:nicknames nicknames})
|
||||
profiles {0 {:name "x"}}
|
||||
event-context (atom {:profiles profiles})
|
||||
_ (reset! ui-context {:event-context event-context})
|
||||
event {:content content :tags [[:p (hexify 0)]]}]
|
||||
(should= "the @x" (replace-references event))))
|
||||
|
||||
(it "replaces a single p reference alone"
|
||||
(let [content "#[0]"
|
||||
nicknames {0 "x"}
|
||||
event-context (atom {:nicknames nicknames})
|
||||
profiles {0 {:name "x"}}
|
||||
event-context (atom {:profiles profiles})
|
||||
_ (reset! ui-context {:event-context event-context})
|
||||
event {:content content :tags [[:p (hexify 0)]]}]
|
||||
(should= "@x" (replace-references event))))
|
||||
|
||||
(it "replaces a two p references"
|
||||
(let [content "the #[0] and #[1] reference"
|
||||
nicknames {0 "x"
|
||||
1 "y"}
|
||||
event-context (atom {:nicknames nicknames})
|
||||
profiles {0 {:name "x"}
|
||||
1 {:name "y"}}
|
||||
event-context (atom {:profiles profiles})
|
||||
_ (reset! ui-context {:event-context event-context})
|
||||
event {:content content :tags [[:p (hexify 0)]
|
||||
[:p (hexify 1)]]}]
|
||||
|
@ -96,24 +96,24 @@
|
||||
|
||||
(defn replace-references [event]
|
||||
(let [padded-content (str " " (:content event) " ")
|
||||
nicknames (:nicknames @(:event-context @ui-context))
|
||||
pattern #"\#\[\d+\]"
|
||||
references (re-seq pattern padded-content)
|
||||
segments (string/split padded-content pattern)
|
||||
referents (mapv (partial lookup-reference nicknames event) references)
|
||||
referents (mapv (partial lookup-reference event) references)
|
||||
referents (conj referents " ")
|
||||
]
|
||||
(string/trim (apply str (interleave segments referents)))))
|
||||
|
||||
(defn lookup-reference [nicknames event reference]
|
||||
(let [ref-string (re-find #"\d+" reference)
|
||||
(defn lookup-reference [event reference]
|
||||
(let [profiles (:profiles @(:event-context @ui-context))
|
||||
ref-string (re-find #"\d+" reference)
|
||||
index (Integer/parseInt ref-string)
|
||||
tags (:tags event)]
|
||||
(if (>= index (count tags))
|
||||
reference
|
||||
(let [id-string (-> tags (nth index) second)
|
||||
id (util/hex-string->num id-string)
|
||||
name (get nicknames id)
|
||||
name (get-in profiles [id :name])
|
||||
name (if (nil? name)
|
||||
(str "id:" (abbreviate id-string 8))
|
||||
name)]
|
||||
|
Loading…
Reference in New Issue
Block a user