| 
75 | 75 | (defn section? [node]  | 
76 | 76 |   (and (list? node) (= "section" (first node))))  | 
77 | 77 | 
 
  | 
 | 78 | +(defn fenced-code-block? [node]  | 
 | 79 | +  (and (list? node) (= "fenced_code_block" (first node))))  | 
 | 80 | + | 
78 | 81 | (defn description-section? [content node]  | 
79 | 82 |   (when-let [atx-header-node (first (filter #(= "atx_heading" (first %)) node))]  | 
80 | 83 |     (= "description"  (string/trim (from-range (-> atx-header-node (nth 3) (nth 1)) content)))))  | 
 | 
117 | 120 | 
 
  | 
118 | 121 | (def heading-1-loc->top-level-section-node (comp zip/node zip/up zip/up))  | 
119 | 122 | 
 
  | 
120 |  | -(defn extract-prompts-with-descriptions [content ast]  | 
 | 123 | +(defn extract-prompts-with-descriptions [content metadata ast]  | 
121 | 124 |   (->>  | 
122 | 125 |    (iterate zip/next (zip/seq-zip ast))  | 
123 | 126 |    (take-while (complement zip/end?))  | 
124 | 127 |    (filter heading-1-section?)  | 
125 | 128 |    (map heading-1-loc->top-level-section-node)  | 
126 | 129 |    (filter (partial prompt-section? content))  | 
127 |  | -   (map (partial h1-prompt-content content))))  | 
 | 130 | +   (map (partial h1-prompt-content content))  | 
 | 131 | +   (map (fn [m] (-> m  | 
 | 132 | +                    (assoc :name (cond  | 
 | 133 | +                                   (and (:title m) (:name metadata)) (str (:name metadata) ":" (:title m))  | 
 | 134 | +                                   (:title m) (:title m)  | 
 | 135 | +                                   (:name metadata) (:name metadata)  | 
 | 136 | +                                   ;; TODO fix this  | 
 | 137 | +                                   :else "missing name")  | 
 | 138 | +                           :description (or  | 
 | 139 | +                                         (:description m)  | 
 | 140 | +                                         (:description metadata)  | 
 | 141 | +                                         "missing description")))))))  | 
128 | 142 | 
 
  | 
129 | 143 | (defn extract-prompts [content ast]  | 
130 | 144 |   (->>  | 
 | 
174 | 188 |       (println ex)  | 
175 | 189 |       nil)))  | 
176 | 190 | 
 
  | 
 | 191 | +(defn extract-first-yaml-code-block [content ast]  | 
 | 192 | +  (try  | 
 | 193 | +    (when-let [loc (when-let [first-section  | 
 | 194 | +                              (->>  | 
 | 195 | +                               (iterate zip/right (zip/down (zip/seq-zip ast)))  | 
 | 196 | +                               (some (fn [loc] (when (section? (zip/node loc)) loc))))]  | 
 | 197 | +                     (when-let [first-code-block  | 
 | 198 | +                                (->>  | 
 | 199 | +                                  (iterate zip/right (zip/down first-section))  | 
 | 200 | +                                  (some (fn [loc] (when (fenced-code-block? (zip/node loc)) loc))))]  | 
 | 201 | +                       first-code-block))]  | 
 | 202 | +      (->  | 
 | 203 | +       (from-range (-> loc zip/node (nth 5) (nth 1)) content)  | 
 | 204 | +       (clj-yaml/parse-string)))  | 
 | 205 | +    (catch Throwable ex  | 
 | 206 | +      (println ex)  | 
 | 207 | +      nil)))  | 
 | 208 | + | 
 | 209 | +(comment  | 
 | 210 | +  (let [content (slurp "prompts/examples/github_issues.md")]  | 
 | 211 | +    (extract-first-yaml-code-block content (parse-markdown content))))  | 
 | 212 | + | 
177 | 213 | (defn parse-markdown  | 
178 | 214 |   "use the custom sexp representation"  | 
179 | 215 |   [content]  | 
 | 
189 | 225 |   "parse out the h1 prompt sections and the metadata"  | 
190 | 226 |   [content]  | 
191 | 227 |   (let [content (str content "\n# END\n\n")  | 
192 |  | -        ast (parse-markdown content)]  | 
 | 228 | +        ast (parse-markdown content)  | 
 | 229 | +        metadata (or  | 
 | 230 | +                  (extract-metadata content ast)  | 
 | 231 | +                  (extract-first-comment content ast)  | 
 | 232 | +                  (extract-first-yaml-code-block content ast)  | 
 | 233 | +                  {})]  | 
193 | 234 |     {:messages  | 
194 | 235 |      (->> ast  | 
195 |  | -          (extract-prompts-with-descriptions content)  | 
 | 236 | +          (extract-prompts-with-descriptions content metadata)  | 
196 | 237 |           (into []))  | 
197 |  | -     :metadata  (or  | 
198 |  | -                 (extract-metadata content ast)  | 
199 |  | -                 (extract-first-comment content ast)  | 
200 |  | -                 {})}))  | 
 | 238 | +     :metadata  metadata}))  | 
201 | 239 | 
 
  | 
202 | 240 | ;; ---------- future ---------  | 
203 | 241 | 
 
  | 
 | 
0 commit comments