// Helpers for code block displaying #import "@preview/based:0.2.0": base64 #let code_font_scale = 0.6em #let cell_matcher(cell, cell_tag) = { // Matching function to check if a cell has a specific tag if cell.cell_type != "code" { return false } let metadata = cell.metadata if metadata.keys().contains("tags") == false { return false } return cell.metadata.tags.contains(cell_tag) } #let code_cell(fcontent, cell_tag) = { // Extract the content of a cell and display it as a code block let cells = fcontent.cells let matching_cell = cells.find(x => cell_matcher(x, cell_tag)) let cell_content = matching_cell.source let single_line = cell_content.fold("", (acc, x) => acc + x) text( raw( single_line, lang: "python", block: true ), size: code_font_scale ) } #let image_cell(fcontent, cell_tag) = { // Extract the output (image) of a cell and display it as an image let cells = fcontent.cells let matching_cell = cells.find(x => cell_matcher(x, cell_tag)) let outputs = matching_cell.outputs for output in outputs { let image_data = output.at("data", default: (:)).at("image/png", default: none) if image_data != none { align( center, image.decode( base64.decode(image_data), // height: 70% // the height should be set by the caller. This gives the flexibility to adjust the height of the image ) ) } } } #let code_reference_cell(fcontent, cell_tag) = { // Extract the output (text) of a cell and display it as a code block // This is useful for showing the code of imported functions let cells = fcontent.cells let matching_cell = cells.find(x => cell_matcher(x, cell_tag)) let outputs = matching_cell.outputs for output in outputs { let cell_output = output.at("text", default: (:)) if cell_output != none { let single_line = cell_output.join("") text( raw( single_line, lang: "python", block: true ), size: code_font_scale ) } } }