|
106 | 106 | *pos (atom 0)] |
107 | 107 | (parse-anything!))) |
108 | 108 |
|
109 | | -(def escapes |
110 | | - {"\"" "\\\"" |
111 | | - "\\" "\\\\"}) |
112 | | - |
113 | | -(def escape-re |
114 | | - #"[\n\"\\]") |
115 | | - |
116 | 109 | (defn- serialize-impl [form] |
117 | 110 | (cond |
118 | | - (string? form) (if (re-matches #"[a-zA-Z0-9._/]+" form) |
119 | | - form |
120 | | - (str \" (str/replace form escape-re escapes) \")) |
121 | | - (keyword? form) (name form) |
122 | | - (number? form) (str form) |
| 111 | + (string? form) |
| 112 | + (if (re-matches #"[a-zA-Z0-9._/]+" form) |
| 113 | + form |
| 114 | + (str \" (str/replace form #"[\"\\]" {"\"" "\\\"" |
| 115 | + "\\" "\\\\"}) \")) |
| 116 | + |
| 117 | + (keyword? form) |
| 118 | + (name form) |
| 119 | + |
| 120 | + (number? form) |
| 121 | + (str form) |
| 122 | + |
123 | 123 | (instance? clojure.lang.MapEntry form) |
124 | | - (str |
125 | | - (serialize-impl (key form)) |
126 | | - " = " |
127 | | - (if (= ".appVersion" (key form)) ;; https://github.com/googlefonts/glyphsLib/issues/209 |
128 | | - (str \" (val form) \") |
129 | | - (serialize-impl (val form))) |
130 | | - ";") |
131 | | - (sequential? form) (if (empty? form) |
132 | | - "(\n)" |
133 | | - (str "(\n" (str/join ",\n" (map serialize-impl form)) "\n)")) |
134 | | - (map? form) (if (empty? form) |
135 | | - "{\n}" |
136 | | - (str "{\n" (str/join "\n" (map serialize-impl form)) "\n}")))) |
| 124 | + (str |
| 125 | + (serialize-impl (key form)) |
| 126 | + " = " |
| 127 | + (case (key form) |
| 128 | + ;; https://github.com/googlefonts/glyphsLib/issues/209 |
| 129 | + ".appVersion" |
| 130 | + (str \" (val form) \") |
| 131 | + |
| 132 | + ;; ¯\_(ツ)_/¯ |
| 133 | + (:stemValues "stemValues") |
| 134 | + (str "(\n" (str/join ",\n" (val form)) "\n)") |
| 135 | + |
| 136 | + ;; else |
| 137 | + (serialize-impl (val form))) |
| 138 | + ";") |
| 139 | + |
| 140 | + (and (sequential? form) (empty? form)) |
| 141 | + "(\n)" |
| 142 | + |
| 143 | + (and (sequential? form) (<= 2 (count form) 3) (not (coll? (first form)))) |
| 144 | + (str "(" (str/join "," (map serialize-impl form)) ")") |
| 145 | + |
| 146 | + (sequential? form) |
| 147 | + (str "(\n" (str/join ",\n" (map serialize-impl form)) "\n)") |
| 148 | + |
| 149 | + (and (map? form) (empty? form)) |
| 150 | + "{\n}" |
| 151 | + |
| 152 | + (map? form) |
| 153 | + (str "{\n" (str/join "\n" (map serialize-impl form)) "\n}"))) |
137 | 154 |
|
138 | 155 | (defn serialize [font] |
139 | 156 | (str (serialize-impl font) "\n")) |
|
154 | 171 | (binding [*out* os] |
155 | 172 | (fipp/pprint font {:width 200}))))) |
156 | 173 |
|
157 | | -(defn update-code [font key name f & args] |
158 | | - (let [idx (coll/index-of #(= (:tag %) name) (get font key))] |
159 | | - (assert (>= idx 0) (str "Can’t find " key " tag=" name ", got " (str/join ", " (map :name (get font key))))) |
| 174 | +(defn update-code [font key name-attr name f & args] |
| 175 | + (let [idx (coll/index-of #(= (get % name-attr) name) (get font key))] |
| 176 | + (assert (>= idx 0) (str "Can’t find " key " tag=" name ", got " (str/join ", " (map #(get % name-attr) (get font key))))) |
160 | 177 | (apply update-in font [key idx :code] f args))) |
161 | 178 |
|
162 | 179 | (defn lines [s] |
|
166 | 183 | (count (re-seq #"[^\s]+" s))) |
167 | 184 |
|
168 | 185 | (defn set-feature [font name feature] |
169 | | - (let [idx (coll/index-of #(= (:name %) name) (:features font))] |
| 186 | + (let [idx (coll/index-of #(= (:tag %) name) (:features font))] |
170 | 187 | (if (pos? idx) |
171 | 188 | (do |
172 | 189 | (println " replacing feature" name "with" (lines (:code feature)) "lines") |
|
0 commit comments