|
11 | 11 |
|
12 | 12 | (defn- compare-text-content |
13 | 13 | "Given two content text structures, conformed by maps and vectors, |
14 | | - compare them, and returns a set with the type of differences. |
15 | | - The possibilities are :text-content-text :text-content-attribute and :text-content-structure." |
16 | | - [a b] |
| 14 | + compare them, and returns a set with the differences info. |
| 15 | + If the structures are equal, it returns an empty set. If the structure |
| 16 | + has changed, it returns :text-content-structure. There are two |
| 17 | + callbacks to specify what to return when there is a text change with |
| 18 | + the same structure, and when attributes change." |
| 19 | + [a b {:keys [text-cb attribute-cb] :as callbacks}] |
17 | 20 | (cond |
18 | 21 | ;; If a and b are equal, there is no diff |
19 | 22 | (= a b) |
|
38 | 41 | #{:text-content-structure} |
39 | 42 | (into acc |
40 | 43 | (apply set/union |
41 | | - (map #(compare-text-content %1 %2) v1 v2)))) |
| 44 | + (map #(compare-text-content %1 %2 callbacks) v1 v2)))) |
42 | 45 |
|
43 | 46 | ;; If the key is :text, and they are different, it is a text differece |
44 | 47 | (= k :text) |
45 | 48 | (if (not= v1 v2) |
46 | | - (conj acc :text-content-text) |
| 49 | + (text-cb acc) |
47 | 50 | acc) |
48 | 51 |
|
49 | 52 | :else |
50 | 53 | ;; If the key is not :text, and they are different, it is an attribute differece |
51 | 54 | (if (not= v1 v2) |
52 | | - (conj acc :text-content-attribute) |
| 55 | + (attribute-cb acc k) |
53 | 56 | acc)))) |
54 | 57 | #{} |
55 | 58 | keys)) |
56 | 59 |
|
57 | 60 | :else |
58 | 61 | #{:text-content-structure})) |
59 | 62 |
|
60 | | - |
61 | 63 | (defn equal-attrs? |
62 | 64 | "Given a text structure, and a map of attrs, check that all the internal attrs in |
63 | 65 | paragraphs and sentences have the same attrs" |
|
79 | 81 | (defn get-diff-type |
80 | 82 | "Given two content text structures, conformed by maps and vectors, |
81 | 83 | compare them, and returns a set with the type of differences. |
82 | | - The possibilities are :text-content-text :text-content-attribute, |
83 | | - :text-content-structure and :text-content-structure-same-attrs." |
| 84 | + The possibilities are |
| 85 | + :text-content-text |
| 86 | + :text-content-attribute, |
| 87 | + :text-content-structure |
| 88 | + :text-content-structure-same-attrs." |
84 | 89 | [a b] |
85 | | - (let [diff-type (compare-text-content a b)] |
| 90 | + (let [diff-type (compare-text-content a b |
| 91 | + {:text-cb (fn [acc] (conj acc :text-content-text)) |
| 92 | + :attribute-cb (fn [acc _] (conj acc :text-content-attribute))})] |
86 | 93 | (if-not (contains? diff-type :text-content-structure) |
87 | 94 | diff-type |
88 | 95 | (let [;; get attrs of the first paragraph of the first paragraph-set |
|
92 | 99 | #{:text-content-structure :text-content-structure-same-attrs} |
93 | 100 | diff-type))))) |
94 | 101 |
|
| 102 | +(defn get-diff-attrs |
| 103 | + "Given two content text structures, conformed by maps and vectors, |
| 104 | + compare them, and returns a set with the attributes that have changed. |
| 105 | + This is independent of the text structure, so if the structure changes |
| 106 | + but the attributes are the same, it will return an empty set." |
| 107 | + [a b] |
| 108 | + (let [diff-attrs (compare-text-content a b |
| 109 | + {:text-cb identity |
| 110 | + :attribute-cb (fn [acc attr] (conj acc attr))})] |
| 111 | + (if-not (contains? diff-attrs :text-content-structure) |
| 112 | + diff-attrs |
| 113 | + (let [;; get attrs of the first paragraph of the first paragraph-set |
| 114 | + attrs (get-first-paragraph-text-attrs a)] |
| 115 | + (if (and (equal-attrs? a attrs) |
| 116 | + (equal-attrs? b attrs)) |
| 117 | + #{} |
| 118 | + (disj diff-attrs :text-content-structure)))))) |
| 119 | + |
95 | 120 | ;; TODO We know that there are cases that the blocks of texts are separated |
96 | 121 | ;; differently: ["one" " " "two"], ["one " "two"], ["one" " two"] |
97 | 122 | ;; so this won't work for 100% of the situations. But it's good enough for now, |
|
116 | 141 | :else |
117 | 142 | true)) |
118 | 143 |
|
119 | | - |
120 | 144 | (defn copy-text-keys |
121 | 145 | "Given two equal content text structures, deep copy all the keys :text |
122 | 146 | from origin to destiny" |
|
0 commit comments