summaryrefslogtreecommitdiff
path: root/src/mf2clj/core.clj
blob: 2d67e2fb3ea2c4a6baa59f6dc6ce212d6d4322b2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
(ns mf2clj.core
  (:require [clojure.java.io :as io]
            [clojure.walk :refer [keywordize-keys]]
            [cheshire.core :as json])
  (:import [org.extism.sdk.chicory Manifest ManifestWasm Plugin]
           [java.nio.charset StandardCharsets]))

(def plugin
  (let [^ManifestWasm wasm-manifest
        (-> "mf2go.wasm" io/resource io/input-stream .readAllBytes
            ManifestWasm/fromBytes
            .build)
        ^Manifest manifest
        (-> (into-array ManifestWasm [wasm-manifest])
            Manifest/ofWasms
            .build)]
    (-> manifest
        Plugin/ofManifest
        .build)))

(def charset StandardCharsets/UTF_8)

(defn parse-bytes [html-bytes]
  {:pre [(bytes? html-bytes)]}
  (let [^bytes result (.call ^Plugin plugin "parse" html-bytes)]
    (-> (String. result charset)
        json/parse-string
        keywordize-keys)))

(defn parse [^String html-string]
  {:pre [(string? html-string)]}
  (let [html-bytes (.getBytes html-string charset)]
    (parse-bytes html-bytes)))