from the HTML text
- email_html = string.gsub(email_html, "^
", '')
- email_html = string.gsub(email_html, "
$", '')
+ -- Determine the location of the Quarto project directory
+ local project_output_directory = quarto.project.output_directory
+ local dir
+ if (project_output_directory ~= nil) then
+ dir = project_output_directory
+ else
+ local file = quarto.doc.input_file
+ dir = pandoc.path.directory(file)
+ end
- -- Use the Connect email template components along with the `email_html` and
- -- `email_html_preview` objects to generate the email message body for Connect
- -- and the email HTML file (as a local preview)
+ quarto.log.warning("Generating V2 multi-email output format with " .. tostring(email_count) .. " email(s).")
- html_email_body = generate_html_email_from_template(
- email_html,
- connect_date_time,
- connect_report_rendering_url,
- connect_report_url,
- connect_report_subscription_url
- )
-
- html_preview_body = generate_html_email_from_template(
- email_html_preview,
- connect_date_time,
- connect_report_rendering_url,
- connect_report_url,
- connect_report_subscription_url
- )
-
- -- Right after the tag in `html_preview_body` we need to insert a subject line HTML string;
- -- this is the string to be inserted:
- subject_html_preview = "
subject: " .. subject .. "
"
-
- -- insert `subject_html_preview` into `html_preview_body` at the aforementioned location
- html_preview_body = string.gsub(html_preview_body, "", "\n" .. subject_html_preview)
-
- -- For each of the
![]()
tags we need to create a Base64-encoded representation
- -- of the image and place that into the table `email_images` (keyed by `cid`)
-
- local image_data = nil
-
- for cid, img in pairs(image_tbl) do
-
- local image_file = io.open(img, "rb")
-
- if type(image_file) == "userdata" then
- image_data = image_file:read("*all")
- image_file:close()
+ -- Process all emails and generate their previews
+ local emails_for_json = {}
+
+ for idx, email_obj in ipairs(emails) do
+
+ -- Apply document-level fallbacks with warnings
+ if email_obj.subject == "" and subject ~= "" then
+ quarto.log.warning("Email #" .. tostring(idx) .. " has no subject. Using document-level subject.")
+ email_obj.subject = subject
+ end
+
+ if email_obj.email_text == "" and email_text ~= "" then
+ quarto.log.warning("Email #" .. tostring(idx) .. " has no email-text. Using document-level email-text.")
+ email_obj.email_text = email_text
end
- local encoded_data = quarto.base64.encode(image_data)
-
- -- Insert `encoded_data` into `email_images` table with prepared key
- email_images[cid] = encoded_data
- end
+ if not email_obj.suppress_scheduled_email and suppress_scheduled_email then
+ quarto.log.warning("Email #" .. tostring(idx) .. " has no suppress-scheduled setting. Using document-level setting.")
+ email_obj.suppress_scheduled_email = suppress_scheduled_email
+ end
- -- Encode all of the strings and tables of strings into the JSON file
- -- (`.output_metadata.json`) that's needed for Connect's email feature
+ if is_empty_table(email_obj.attachments) and not is_empty_table(attachments) then
+ email_obj.attachments = attachments
+ end
- if (is_empty_table(email_images)) then
+ -- Clean up HTML
+ local email_html_clean = string.gsub(email_obj.email_html, "^
", '')
+ email_html_clean = string.gsub(email_html_clean, "
$", '')
+
+ -- Generate HTML bodies
+ local html_email_body = generate_html_email_from_template(
+ email_html_clean,
+ connect_date_time,
+ connect_report_rendering_url,
+ connect_report_url,
+ connect_report_subscription_url
+ )
+
+ local html_preview_body = generate_html_email_from_template(
+ email_obj.email_html_preview,
+ connect_date_time,
+ connect_report_rendering_url,
+ connect_report_url,
+ connect_report_subscription_url
+ )
+
+ -- Add subject to preview
+ local subject_html_preview = "
subject: " .. email_obj.subject .. "
"
+ html_preview_body = string.gsub(html_preview_body, "", "\n" .. subject_html_preview)
+
+ -- Build email object for JSON
+
+ -- rsc_email_suppress_report_attachment (now inverted to send_report_as_attachment) referred to
+ -- the attachment of the rendered report to each connect email.
+ -- This is always true for all emails unless overridden by blastula (as is the case in v1)
+ local email_json_obj = {
+ email_id = idx,
+ subject = email_obj.subject,
+ body_html = html_email_body,
+ body_text = email_obj.email_text,
+ attachments = email_obj.attachments,
+ suppress_scheduled = email_obj.suppress_scheduled_email,
+ send_report_as_attachment = false
+ }
- metadata_str = quarto.json.encode({
- rsc_email_subject = subject,
- rsc_email_attachments = attachments,
- rsc_email_body_html = html_email_body,
- rsc_email_body_text = email_text,
- rsc_email_suppress_report_attachment = true,
- rsc_email_suppress_scheduled = suppress_scheduled_email
- })
+ -- Only add images if present
+ if not is_empty_table(email_obj.email_images) then
+ email_json_obj.images = email_obj.email_images
+ end
- else
+ table.insert(emails_for_json, email_json_obj)
- metadata_str = quarto.json.encode({
- rsc_email_subject = subject,
- rsc_email_attachments = attachments,
- rsc_email_body_html = html_email_body,
- rsc_email_body_text = email_text,
- rsc_email_images = email_images,
- rsc_email_suppress_report_attachment = true,
- rsc_email_suppress_scheduled = suppress_scheduled_email
- })
+ -- Write individual preview file
+ if meta_email_preview ~= false then
+ local preview_filename = "email-preview/email_id-" .. tostring(idx) .. ".html"
+ quarto._quarto.file.write(pandoc.path.join({dir, preview_filename}), html_preview_body)
+ end
end
- -- Determine the location of the Quarto project directory; if not defined
- -- by the user then set to the location of the input file
- local project_output_directory = quarto.project.output_directory
+ -- Generate V2 JSON with version field
+ local metadata_str = quarto.json.encode({
+ rsc_email_version = 2,
+ emails = emails_for_json
+ })
- if (project_output_directory ~= nil) then
- dir = project_output_directory
- else
- local file = quarto.doc.input_file
- dir = pandoc.path.directory(file)
- end
+ -- Write V2 metadata file
+ local metadata_path_file = pandoc.path.join({dir, ".output_metadata.json"})
+ io.open(metadata_path_file, "w"):write(metadata_str):close()
- -- For all file attachments declared by the user, ensure they copied over
- -- to the project directory (`dir`)
+ -- Copy attachments to project directory
for _, v in pairs(attachments) do
-
local source_attachment_file = pandoc.utils.stringify(v)
local dest_attachment_path_file = pandoc.path.join({dir, pandoc.utils.stringify(v)})
- -- Only if the file exists should it be copied into the project directory
if (file_exists(source_attachment_file)) then
local attachment_text = io.open(source_attachment_file):read("*a")
io.open(dest_attachment_path_file, "w"):write(attachment_text):close()
end
end
-
- -- Write the `.output_metadata.json` file to the project directory
- local metadata_path_file = pandoc.path.join({dir, ".output_metadata.json"})
- io.open(metadata_path_file, "w"):write(metadata_str):close()
- -- Write the `email-preview/index.html` file unless meta_email_preview is false
- if meta_email_preview ~= false then
- quarto._quarto.file.write(pandoc.path.join({dir, "email-preview/index.html"}), html_preview_body)
- end
+ return doc
end
function render_email()