replace-references and lookup-references use profiles

This commit is contained in:
Robert C. Martin 2022-07-05 09:11:34 -05:00
parent 981aa6b412
commit f7bead98f4
2 changed files with 16 additions and 16 deletions

View File

@ -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)]]}]

View File

@ -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)]