#import "@preview/sourcerer:0.2.1": code
#import "@preview/ansi-render:0.6.1": *
#import "@preview/based:0.1.0": base64
#import "@preview/cmarker:0.1.0"
#import "@preview/showybox:2.0.1": showybox

#let sb = it => showybox(
    frame: (
        border-color: gray.lighten(70%),
        body-color: gray.lighten(90%),
        thickness: 1pt
    ),
    it
)
#let jconv = (ipynb) => {
  for cell in ipynb.cells {
    if cell.cell_type == "code" {
      let src = ""
      for line in cell.source {
        src = src + line
      }
      code(
        raw(src, lang: ipynb.metadata.kernelspec.language),
        lang: ipynb.metadata.kernelspec.display_name,
      )
      for out in cell.outputs {
        if out.data.keys().contains("text/html") {
          let text = ""
          for line in out.data.at("text/plain") {
            text = text + line
          }
          [
            #sb(
              ansi-render(text, font: "FiraCode Nerd Font Mono", width: 100%, inset: 0.5em)
            )
          ]
        }
        if out.data.keys().contains("image/png") {
          [#figure(image.decode(base64.decode(out.data.at("image/png")), format:"png"))]
        }
      }
    } else if cell.cell_type == "markdown" {
      let src = ""
      for line in cell.source {
        src = src + line
      }
      [
        #sb(
          cmarker.render(src)
        )
      ]
    }
  }
}
