parse_file.cljs 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. (ns parse-file
  2. (:require [logseq.graph-parser.cli :as gp-cli]
  3. [clojure.pprint :as pprint]
  4. [datascript.core :as d]))
  5. (defn- colorize-or-pretty-print
  6. [results]
  7. (if (zero? (.-status (gp-cli/sh ["which" "puget"] {})))
  8. (gp-cli/sh ["puget"] {:input (pr-str results)
  9. :stdio ["pipe" "inherit" "inherit"]})
  10. (pprint/pprint results)))
  11. (defn- get-all-page-properties
  12. [db]
  13. (->> (d/q '[:find (pull ?b [*])
  14. :where
  15. [?b :block/properties]]
  16. db)
  17. (map first)
  18. (map (fn [m] (zipmap (keys (:block/properties m)) (repeat 1))))
  19. (apply merge-with +)
  20. (into {})))
  21. (defn- analyze-file
  22. [db file]
  23. (let [results (map first
  24. (d/q '[:find (pull ?b [:db/id :block/content])
  25. :in $ ?path
  26. :where
  27. [?b :block/page ?page]
  28. [?page :block/file ?file]
  29. [?file :file/path ?path]]
  30. db
  31. file))]
  32. (colorize-or-pretty-print results)
  33. (println "Block count:" (count results))
  34. (println "Properties count:" (get-all-page-properties db))))
  35. (defn -main
  36. "Prints blocks for given file along with basic file stats"
  37. [& args]
  38. (when-not (= 2 (count args))
  39. (throw (ex-info "Usage: $0 DIR FILE" {})))
  40. (println "Parsing...")
  41. (let [[dir file] args
  42. {:keys [conn]} (gp-cli/parse-graph dir
  43. {:verbose false
  44. :files [{:file/path file
  45. :file/content (gp-cli/slurp file)}]})]
  46. (analyze-file @conn file)))
  47. (apply -main *command-line-args*)