|
| 1 | +---- |
| 2 | +--- Setup variables for default initialization |
| 3 | + |
| 4 | +-- Check to see if we've already defined the adsense Unit |
| 5 | +-- Note: This should only trigger _once_ |
| 6 | +local isAdsenseUnitMissing = true |
| 7 | + |
| 8 | +-- Check to see if we should show ads on the given page |
| 9 | +local enableAds = true |
| 10 | + |
| 11 | +---- |
| 12 | +--- Process initialization |
| 13 | + |
| 14 | +-- Check if variable is present and not just the empty string |
| 15 | +local function isVariableEmpty(s) |
| 16 | + return s == nil or s == '' |
| 17 | +end |
| 18 | + |
| 19 | +local function isVariablePopulated(s) |
| 20 | + return not isVariableEmpty(s) |
| 21 | +end |
| 22 | + |
| 23 | +-- Check if a value is true or false, including string representations |
| 24 | +local function isBoolean(value) |
| 25 | + if type(value) == "boolean" then |
| 26 | + return true |
| 27 | + elseif type(value) == "string" then |
| 28 | + local lowercaseValue = value:lower() |
| 29 | + return lowercaseValue == "true" or lowercaseValue == "false" |
| 30 | + else |
| 31 | + return false |
| 32 | + end |
| 33 | +end |
| 34 | + |
| 35 | +-- Coerce a "true" or "false" string to `true`/`false`. |
| 36 | +local function stringToBoolean(str) |
| 37 | + return str == "true" and true or false |
| 38 | +end |
| 39 | + |
| 40 | +-- Ad unit HTML |
| 41 | +local function adsense_html(publisher_id) |
| 42 | + local html_text = '<script async'.. |
| 43 | + ' src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=' .. publisher_id .. '"' .. |
| 44 | + ' crossorigin="anonymous">' .. |
| 45 | + '</script>' |
| 46 | + |
| 47 | + return html_text |
| 48 | +end |
| 49 | + |
| 50 | + |
| 51 | +-- Retrieve publisher and setup ad unit |
| 52 | +function Meta(m) |
| 53 | + |
| 54 | + -- Detect HTML format (excluding epub which won't handle fa) |
| 55 | + if not quarto.doc.is_format("html:js") then |
| 56 | + quarto.log.error("The `adsense` extensions works only on HTML powered Quarto projects.") |
| 57 | + end |
| 58 | + |
| 59 | + -- Check for configuration |
| 60 | + local adsense_meta = m.adsense |
| 61 | + if isVariableEmpty(adsense_meta) then |
| 62 | + quarto.log.error( |
| 63 | + "The Quarto project is missing the `adsense` key in either" .. |
| 64 | + "\n - the document header,\n - `_quarto.yml`, or\n - `_metadata.yml`." |
| 65 | + ) |
| 66 | + end |
| 67 | + |
| 68 | + -- Retrieve enable-ads from meta |
| 69 | + if isVariablePopulated(adsense_meta['enable-ads']) then |
| 70 | + enableAds = pandoc.utils.stringify(adsense_meta['enable-ads']) |
| 71 | + |
| 72 | + if not isBoolean(enableAds) then |
| 73 | + quarto.log.error("The `enable-ads` must be either `true` or `false`, not `" .. enableAds .. "`." .. |
| 74 | + "\nPlease fix by correcting the value supplied to `enable-ads` in the current document.") |
| 75 | + end |
| 76 | + |
| 77 | + -- Apply conversion from string to boolean |
| 78 | + enableAds = stringToBoolean(enableAds) |
| 79 | + |
| 80 | + -- If ads should be disabled; exit here to avoid running into a publisher-id unset error. |
| 81 | + if enableAds == false then |
| 82 | + return m |
| 83 | + end |
| 84 | + end |
| 85 | + |
| 86 | + -- Retrieve publisherID from meta |
| 87 | + local publisher_id = nil |
| 88 | + if isVariablePopulated(adsense_meta['publisher-id']) then |
| 89 | + publisher_id = pandoc.utils.stringify(adsense_meta['publisher-id']) |
| 90 | + else |
| 91 | + quarto.log.error( |
| 92 | + "The `publisher-id` is not set. Please set the `publisher-id` by either:" .. |
| 93 | + "\n - the value in the current document," .. |
| 94 | + "\n - the projects `_quarto.yml`, or" .. |
| 95 | + "\n - `_metadata.yml` for the directory.\n") |
| 96 | + end |
| 97 | + |
| 98 | + if enableAds and isAdsenseUnitMissing then |
| 99 | + -- Avoid re-running insertion |
| 100 | + isAdsenseUnitMissing = false |
| 101 | + -- Customize the ad unit script tag |
| 102 | + local adsense_customized_adunit_html = adsense_html(publisher_id) |
| 103 | + -- Inject the customized unit into the HTML document's header section |
| 104 | + -- using Quarto's API. |
| 105 | + quarto.doc.include_text("in-header", adsense_customized_adunit_html) |
| 106 | + end |
| 107 | + |
| 108 | + return m |
| 109 | +end |
0 commit comments