|
| 1 | +function Div(div) |
| 2 | + -- Check if this div has warning output classes |
| 3 | + -- R/Python warnings: cell-output-stderr |
| 4 | + -- Julia warnings: cell-output-stdout |
| 5 | + if div.classes:includes('cell-output-stderr') or div.classes:includes('cell-output-stdout') then |
| 6 | + -- Check if any CodeBlock or RawBlock in this div contains warning patterns |
| 7 | + for _, block in ipairs(div.content) do |
| 8 | + local should_add_warning = false |
| 9 | + |
| 10 | + if block.tag == "CodeBlock" then |
| 11 | + -- R/Python warnings in CodeBlocks (stderr) |
| 12 | + if div.classes:includes('cell-output-stderr') and |
| 13 | + string.match(block.text, "^%w*Warning:") then |
| 14 | + should_add_warning = true |
| 15 | + end |
| 16 | + elseif block.tag == "RawBlock" and block.format == "html" then |
| 17 | + -- Julia warnings in HTML RawBlocks (stdout) |
| 18 | + if div.classes:includes('cell-output-stdout') and |
| 19 | + string.match(block.text, "Warning: ") then |
| 20 | + should_add_warning = true |
| 21 | + end |
| 22 | + elseif block.tag == "Div" and block.classes:includes('ansi-escaped-output') then |
| 23 | + -- Julia warnings are nested in ansi-escaped-output divs |
| 24 | + if div.classes:includes('cell-output-stdout') then |
| 25 | + for _, nested_block in ipairs(block.content) do |
| 26 | + if nested_block.tag == "RawBlock" and nested_block.format == "html" then |
| 27 | + if string.match(nested_block.text, "Warning: ") then |
| 28 | + should_add_warning = true |
| 29 | + break |
| 30 | + end |
| 31 | + end |
| 32 | + end |
| 33 | + end |
| 34 | + end |
| 35 | + |
| 36 | + if should_add_warning then |
| 37 | + div.classes:insert('cell-output-warning') |
| 38 | + break |
| 39 | + end |
| 40 | + end |
| 41 | + end |
| 42 | + |
| 43 | + return div |
| 44 | +end |
| 45 | + |
| 46 | +-- Add the CSS file to the document |
| 47 | +function Pandoc(doc) |
| 48 | + if not (quarto.doc and quarto.doc.is_format and quarto.doc.is_format("html")) then |
| 49 | + return doc |
| 50 | + end |
| 51 | + |
| 52 | + -- Get value of `appearance` key from YAML fron matter |
| 53 | + local appearance_value = "default" |
| 54 | + |
| 55 | + if doc.meta['output-styling'] and doc.meta['output-styling'].appearance then |
| 56 | + local raw_appearance = pandoc.utils.stringify(doc.meta['output-styling'].appearance) |
| 57 | + |
| 58 | + -- Possible values here |
| 59 | + -- MAYBE: someday add others? |
| 60 | + local valid_appearances = {default = true, minimal = true, custom = true} |
| 61 | + |
| 62 | + if valid_appearances[raw_appearance] then |
| 63 | + appearance_value = raw_appearance |
| 64 | + else |
| 65 | + quarto.log.warning( |
| 66 | + "Invalid value for `appearance` '" .. raw_appearance .. |
| 67 | + "'. Using 'default'. Valid options are: `default`, `minimal`, or `custom`" |
| 68 | + ) |
| 69 | + end |
| 70 | + end |
| 71 | + |
| 72 | + -- Include CSS file only if not custom |
| 73 | + if appearance_value ~= "custom" then |
| 74 | + local css_file = "output-styling-" .. appearance_value .. ".css" |
| 75 | + |
| 76 | + quarto.doc.add_html_dependency({ |
| 77 | + name = 'output-styling', |
| 78 | + stylesheets = {css_file} |
| 79 | + }) |
| 80 | + end |
| 81 | + |
| 82 | + return doc |
| 83 | +end |
0 commit comments