-
Notifications
You must be signed in to change notification settings - Fork 433
feat: email multiple recipients #14098
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 8 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
88ad981
plan generation
juleswg23 2d0a61c
parse and test new recipients field
juleswg23 3f396b1
delete claude files
juleswg23 5c23113
support write_yaml_metadata_block as a path for passing recipients
juleswg23 621feff
compress into fewer separate test files
juleswg23 65c6107
update changelog.
juleswg23 a5bf2c7
rename test
juleswg23 7a546b9
set email version in tests
juleswg23 ab01b32
parse emails with simplified regex (and add specific plaintext tests)
juleswg23 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -65,6 +65,127 @@ function str_truthy_falsy(str) | |
| return false | ||
| end | ||
|
|
||
| -- Parse recipients from inline code output or plain text | ||
| -- Supports multiple formats: | ||
| -- 1. Python list: ['a', 'b'] or ["a", "b"] | ||
| -- 2. R vector: "a" "b" "c" | ||
| -- 3. Comma-separated: a, b, c | ||
| -- 4. Line-separated: a\nb\nc | ||
| -- Returns an empty array if parsing fails | ||
| function parse_recipients(recipient_str) | ||
| recipient_str = str_trunc_trim(recipient_str, 10000) | ||
|
|
||
| if recipient_str == "" then | ||
| return {} | ||
| end | ||
|
|
||
| local recipients = {} | ||
|
|
||
| -- Try Python list format ['...', '...'] or ["...", "..."] | ||
| if string.match(recipient_str, "^%[") and string.match(recipient_str, "%]$") then | ||
| local content = string.sub(recipient_str, 2, -2) | ||
|
|
||
| -- Try to parse as Python/R list by splitting on commas | ||
| -- and stripping quotes and brackets from each item | ||
| recipients = {} | ||
| for item in string.gmatch(content, "[^,]+") do | ||
| local trimmed = str_trunc_trim(item, 1000) | ||
| -- Strip leading/trailing brackets | ||
| trimmed = string.gsub(trimmed, "^%[", "") | ||
| trimmed = string.gsub(trimmed, "%]$", "") | ||
| trimmed = str_trunc_trim(trimmed, 1000) | ||
|
|
||
| -- Strip leading/trailing quotes (ASCII single/double and UTF-8 curly quotes) | ||
| -- ASCII single quote ' | ||
| trimmed = string.gsub(trimmed, "^'", "") | ||
| trimmed = string.gsub(trimmed, "'$", "") | ||
| -- ASCII double quote " | ||
| trimmed = string.gsub(trimmed, '^"', "") | ||
| trimmed = string.gsub(trimmed, '"$', "") | ||
| -- UTF-8 curly single quotes ' and ' (U+2018, U+2019) | ||
| trimmed = string.gsub(trimmed, "^" .. string.char(226, 128, 152), "") | ||
| trimmed = string.gsub(trimmed, string.char(226, 128, 153) .. "$", "") | ||
| -- UTF-8 curly double quotes " and " (U+201C, U+201D) | ||
| trimmed = string.gsub(trimmed, "^" .. string.char(226, 128, 156), "") | ||
| trimmed = string.gsub(trimmed, string.char(226, 128, 157) .. "$", "") | ||
|
|
||
| trimmed = str_trunc_trim(trimmed, 1000) | ||
| if trimmed ~= "" then | ||
| table.insert(recipients, trimmed) | ||
| end | ||
| end | ||
| if #recipients > 0 then | ||
| return recipients | ||
| end | ||
| end | ||
|
|
||
| -- Try R-style quoted format (space-separated quoted strings outside of brackets) | ||
| recipients = {} | ||
| local found_any = false | ||
|
|
||
| -- Try single quotes: 'a' 'b' 'c' | ||
| for quoted_pair in string.gmatch(recipient_str, "'([^']*)'") do | ||
| local trimmed = str_trunc_trim(quoted_pair, 1000) | ||
| if trimmed ~= "" then | ||
| table.insert(recipients, trimmed) | ||
| found_any = true | ||
| end | ||
| end | ||
| if found_any then | ||
| return recipients | ||
| end | ||
|
|
||
| -- Try double quotes: "a" "b" "c" | ||
| recipients = {} | ||
| for quoted_pair in string.gmatch(recipient_str, '"([^"]*)"') do | ||
| local trimmed = str_trunc_trim(quoted_pair, 1000) | ||
| if trimmed ~= "" then | ||
| table.insert(recipients, trimmed) | ||
| found_any = true | ||
| end | ||
| end | ||
| if found_any then | ||
| return recipients | ||
| end | ||
|
|
||
| -- Try line-separated format (newlines or spaces) | ||
| -- Check if there are newlines or multiple space-separated emails | ||
| if string.match(recipient_str, "\n") or | ||
| (string.match(recipient_str, "@.*%s+.*@") and not string.match(recipient_str, ",")) then | ||
| recipients = {} | ||
| -- Split on newlines or spaces | ||
| for item in string.gmatch(recipient_str, "[^\n%s]+") do | ||
| local trimmed = str_trunc_trim(item, 1000) | ||
| if trimmed ~= "" and string.match(trimmed, "@") then | ||
| table.insert(recipients, trimmed) | ||
| found_any = true | ||
| end | ||
| end | ||
| if found_any then | ||
| return recipients | ||
| end | ||
| end | ||
|
|
||
| -- Try comma-separated format without quotes | ||
| -- Split by comma and trim each part | ||
| recipients = {} | ||
| found_any = false | ||
| for part in string.gmatch(recipient_str, "[^,]+") do | ||
| local trimmed = str_trunc_trim(part, 1000) | ||
| if trimmed ~= "" and not string.match(trimmed, "^[%[%]]") then | ||
| table.insert(recipients, trimmed) | ||
| found_any = true | ||
| end | ||
| end | ||
| if found_any then | ||
| return recipients | ||
| end | ||
|
|
||
| -- Could not parse - log warning and return empty | ||
| quarto.log.warning("Could not parse recipients format: " .. recipient_str) | ||
| return {} | ||
| end | ||
|
|
||
| local html_email_template_1 = [[ | ||
| <!DOCTYPE html> | ||
| <html> | ||
|
|
@@ -254,6 +375,7 @@ function process_div(div) | |
| image_tbl = {}, | ||
| email_images = {}, | ||
| suppress_scheduled_email = nil, -- nil means not set | ||
| recipients = {}, | ||
| attachments = {} | ||
| } | ||
|
|
||
|
|
@@ -270,14 +392,50 @@ function process_div(div) | |
| local email_scheduled_str = str_trunc_trim(string.lower(pandoc.utils.stringify(child)), 10) | ||
| local scheduled_email = str_truthy_falsy(email_scheduled_str) | ||
| current_email.suppress_scheduled_email = not scheduled_email | ||
| elseif child.classes:includes("recipients") then | ||
| current_email.recipients = parse_recipients(pandoc.utils.stringify(child)) | ||
| else | ||
| table.insert(remaining_content, child) | ||
| end | ||
| else | ||
| table.insert(remaining_content, child) | ||
| end | ||
| end | ||
|
|
||
|
|
||
| -- Check for recipients attribute on the email div itself | ||
| -- This allows referencing metadata set via write_yaml_metadata_block() | ||
| if div.attributes.recipients then | ||
| local meta_key = div.attributes.recipients | ||
| local meta_value = quarto.metadata.get(meta_key) | ||
|
|
||
| if meta_value then | ||
| -- Convert metadata to recipients array | ||
| if quarto.utils.type(meta_value) == "List" then | ||
| local recipients_from_meta = {} | ||
| for _, item in ipairs(meta_value) do | ||
| local recipient_str = pandoc.utils.stringify(item) | ||
| if recipient_str ~= "" then | ||
| table.insert(recipients_from_meta, recipient_str) | ||
| end | ||
| end | ||
|
|
||
| -- If recipients were also found in child divs, merge them | ||
| if #current_email.recipients > 0 then | ||
| quarto.log.warning("Recipients found in both attribute and child div. Merging both lists.") | ||
| for _, recipient in ipairs(recipients_from_meta) do | ||
| table.insert(current_email.recipients, recipient) | ||
| end | ||
| else | ||
| current_email.recipients = recipients_from_meta | ||
| end | ||
| else | ||
| quarto.log.warning("Recipients metadata '" .. meta_key .. "' is not a list. Expected format: ['[email protected]', '[email protected]']") | ||
| end | ||
| else | ||
| quarto.log.warning("Recipients attribute references metadata key '" .. meta_key .. "' which does not exist.") | ||
| end | ||
| end | ||
|
|
||
| -- Create a modified div without metadata for processing | ||
| local email_without_metadata = pandoc.Div(remaining_content, div.attr) | ||
|
|
||
|
|
@@ -508,6 +666,11 @@ function process_document(doc) | |
| send_report_as_attachment = false | ||
| } | ||
|
|
||
| -- Only add recipients if present | ||
| if not is_empty_table(email_obj.recipients) then | ||
| email_json_obj.recipients = email_obj.recipients | ||
| end | ||
|
|
||
| -- Only add images if present | ||
| if not is_empty_table(email_obj.email_images) then | ||
| email_json_obj.images = email_obj.email_images | ||
|
|
||
125 changes: 125 additions & 0 deletions
125
tests/docs/email/email-recipients-all-patterns-python.qmd
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,125 @@ | ||
| --- | ||
| title: Email Recipients - All Patterns (Python) | ||
| author: Jules Walzer-Goldfeld | ||
| format: | ||
| email: | ||
| email-version: 2 | ||
| --- | ||
|
|
||
| ```{python} | ||
| #| echo: false | ||
| import yaml | ||
| from IPython.display import Markdown | ||
|
|
||
| def write_yaml_metadata_block(**kwargs): | ||
| """Write YAML metadata block that will be parsed by Quarto.""" | ||
| yaml_content = yaml.dump( | ||
| kwargs, | ||
| default_flow_style=False, | ||
| allow_unicode=True, | ||
| sort_keys=False | ||
| ) | ||
| yaml_block = f"---\n{yaml_content}---\n" | ||
| return Markdown(yaml_block) | ||
| ``` | ||
|
|
||
| Test document demonstrating all recipient patterns with Python. | ||
|
|
||
| ```{python} | ||
| # Email 1: Static inline recipients | ||
| static_recipients = ["[email protected]", "[email protected]", "[email protected]"] | ||
| ``` | ||
|
|
||
| ::: {.email} | ||
|
|
||
| ::: {.subject} | ||
| Email 1: Static Inline Recipients | ||
| ::: | ||
|
|
||
| ::: {.recipients} | ||
| `{python} static_recipients` | ||
| ::: | ||
|
|
||
| ::: {.email-text} | ||
| Text version of email with static inline recipients. | ||
| ::: | ||
|
|
||
| First email with static inline recipients. | ||
|
|
||
| ::: | ||
|
|
||
| ```{python} | ||
| # Email 2: Conditional inline recipients | ||
| is_weekday = True # Fixed value for deterministic testing | ||
|
|
||
| if is_weekday: | ||
| conditional_recipients = ["[email protected]", "[email protected]"] | ||
| else: | ||
| conditional_recipients = ["[email protected]"] | ||
| ``` | ||
|
|
||
| ::: {.email} | ||
|
|
||
| ::: {.subject} | ||
| Email 2: Conditional Inline Recipients | ||
| ::: | ||
|
|
||
| ::: {.recipients} | ||
| `{python} conditional_recipients` | ||
| ::: | ||
|
|
||
| ::: {.email-text} | ||
| Text version of conditional recipients email. | ||
| ::: | ||
|
|
||
| Second email with conditional inline recipients. | ||
|
|
||
| ::: | ||
|
|
||
| ```{python} | ||
| #| output: asis | ||
| # Email 3: Metadata attribute pattern | ||
| metadata_recipients = ["[email protected]", "[email protected]"] | ||
| write_yaml_metadata_block(metadata_recipients=metadata_recipients) | ||
| ``` | ||
|
|
||
| ::: {.email recipients=metadata_recipients} | ||
|
|
||
| ::: {.subject} | ||
| Email 3: Metadata Attribute Pattern | ||
| ::: | ||
|
|
||
| ::: {.email-text} | ||
| This email uses the metadata attribute pattern. | ||
| ::: | ||
|
|
||
| Third email using metadata attribute pattern. | ||
|
|
||
| ::: | ||
|
|
||
| ```{python} | ||
| #| output: asis | ||
| # Email 4: Conditional metadata attribute pattern | ||
| is_admin = True # Fixed for testing | ||
|
|
||
| if is_admin: | ||
| admin_recipients = ["[email protected]", "[email protected]"] | ||
| else: | ||
| admin_recipients = ["[email protected]"] | ||
|
|
||
| write_yaml_metadata_block(admin_recipients=admin_recipients) | ||
| ``` | ||
|
|
||
| ::: {.email recipients=admin_recipients} | ||
|
|
||
| ::: {.subject} | ||
| Email 4: Conditional Metadata Attribute | ||
| ::: | ||
|
|
||
| ::: {.email-text} | ||
| This email uses conditional metadata attribute pattern. | ||
| ::: | ||
|
|
||
| Fourth email using conditional metadata attribute pattern. | ||
|
|
||
| ::: |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The approach here is parsing through various printed versions of list/vectors in Python + R and then extract the strings inside of them which (presumably) works (I haven't checked the tests yet, so maybe there actually are corner cases or bugs or something, but either way...).
What if, instead, we do the opposite: look for email addresses in the string and ignore everything else. My lua is not strong ™️ but Claude came up with the following:
What do you think of doing it that way?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think I prefer this... I've updated this PR.