Skip to content
Draft
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
103 changes: 103 additions & 0 deletions scripts/blog_events.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
library(airtabler)
library(tidyverse)
library(glue)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you load airtabler and glue in the functions script, so you wouldnt need to load it again here.

library(readr)

source("scripts/blog_functions.R")

# Get data and draft blogposts----
process_events <- function(base_id) {

# Connect and get data
base <- connect_to_airtable(base_id, tables = "Event Reports")
events_data <- base$`Event Reports`$select()

# Filter ready-to-draft events
events_to_draft <- filter_ready_to_draft(events_data, title_col = "title")

if (nrow(events_to_draft) == 0) {
message("No new event reports to draft.")
return(0)
}

message("Found ", nrow(events_to_draft), " new event report(s) to draft.")

# Process each event
for (i in 1:nrow(events_to_draft)) {
report <- events_to_draft[i, ]

tryCatch({
# Validate required fields
if (!validate_required_fields(report, c("title", "createdTime"))) {
next
}

# Create slug and paths
slug <- create_slug(report$title)
post_date <- ymd_hms(report$createdTime)
year <- year(post_date)
month_day_slug <- paste0(format(post_date, "%m-%d"), "-", slug)

# Create folder
folder_path <- create_post_folder(year, month_day_slug)

# Download images if they exist
image_markdown <- ""
if (!is.na(report$media) && report$media != "") {
message("Downloading images for event: ", report$title)
downloaded_images <- download_images(report$media, folder_path)

# Create markdown for images
if (!is.null(downloaded_images) && length(downloaded_images) > 0) {
image_markdown <- create_image_markdown(downloaded_images)
}
}
# Markdown front-matter ----

front_matter <- glue(
'---
title: "Event Report: {report$Title}"
author: "{report$Author}"
date: "{format(post_date, "%Y-%m-%d")}"
tags: [{report$Keywords}]
---
'
)
# standardised blogpost

post_body <- glue(
'On {report$Date}, {report$Chapter} hosted the event "{report$Title}".
The session was led by {report$Speakers} and was attended by {report$Participants} participants.

## Event Summary

Here are the key topics that were covered:

{report$Summary}

## Attendee Feedback

> {report$`Quotes or Reactions (optional)`}

## Resources

A big thank you to the speakers and everyone who attended!'
)

# Write file
content <- paste0(front_matter, post_body)
filepath <- write_markdown_file(content, folder_path)
message("✓ Successfully created: ", filepath)

}, error = function(e) {
warning("Error processing event ", i, ": ", e$message)
})
}

return(nrow(events_to_draft))
}

# After successfully creating the markdown file:
# Update the Airtable record to:
# - Status = "Post drafted"
# - Add PR URL
58 changes: 58 additions & 0 deletions scripts/blog_functions.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
library(tidyverse)
library(glue)
library(httr)
library(airtabler)

# Connect to Airtable base
connect_to_airtable <- function(base_id, tables) {
message("Connecting to Airtable...")
base <- airtable(base = base_id, tables = tables)
return(base)
}

# Filter data to get records ready for drafting
# Removes the dont-delete row and filters for In progress + Accept status
filter_ready_to_draft <- function(data, title_col = "Title") {
data %>%
filter(
.data[[title_col]] != "dont-delete",
Status == "In progress",
Decision == "Accept"
)
}

# Create a URL-friendly slug from text
create_slug <- function(text) {
text %>%

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

consider switching to base pipe?

str_to_lower() %>%
str_replace_all("[^a-z0-9\\s-]", "") %>%
str_replace_all("\\s+", "-")
}

# Create blog post folder structure (content/blog/YYYY/mm-dd-slug/)
create_post_folder <- function(year, month_day_slug) {
folder_path <- file.path("content", "blog", as.character(year), month_day_slug)
dir.create(folder_path, recursive = TRUE, showWarnings = FALSE)
return(folder_path)
}

# Escape special characters for YAML frontmatter
escape_yaml <- function(text) {
if (is.null(text) || is.na(text) || text == "") return('""')
# Escape quotes and wrap in quotes if text contains special YAML characters
if (str_detect(text, '[":\\n]')) {
text <- str_replace_all(text, '"', '\\\\"')
text <- paste0('"', text, '"')
}
return(text)
}

# Download images from URLs and save to folder
# Returns vector of local filenames
download_images <- function(image_urls, folder_path) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it returns nothing when there are images to download?


# Handle NULL or empty input
if (is.null(image_urls) || is.na(image_urls) || length(image_urls) == 0 || image_urls == "") {
return(NULL)
}
}
74 changes: 74 additions & 0 deletions scripts/blog_proposal.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
library(airtabler)
library(tidyverse)
library(glue)
library(lubridate)

source("scripts/blog_functions.R")

# Get data and create draft blogpost ----
process_proposals <- function(base_id) {

# Connect and get data
base <- connect_to_airtable(base_id, tables = "Proposals")
proposals_data <- base$Proposals$select()

# Filter ready-to-draft proposals
proposals_to_draft <- filter_ready_to_draft(proposals_data, title_col = "Title")

if (nrow(proposals_to_draft) == 0) {
message("No new proposals to draft.")
return(0)
}

message("Found ", nrow(proposals_to_draft), " new proposal(s) to draft.")

# Process each proposal
for (i in 1:nrow(proposals_to_draft)) {
proposal <- proposals_to_draft[i, ]

tryCatch({
# Validate required fields
if (!validate_required_fields(proposal, c("Title", "Post date"))) {
next
}

# Create slug and paths
slug <- create_slug(proposal$Title)
post_date_obj <- ymd(proposal$`Post date`)
year <- year(post_date_obj)
month_day_slug <- paste0(format(post_date_obj, "%m-%d"), "-", slug)

# Create folder
folder_path <- create_post_folder(year, month_day_slug)

# Build content
front_matter <- glue(
'---
title: {escape_yaml(proposal$Title)}
author: {escape_yaml(proposal$Author)}
date: "{proposal$`Post date`}"
slug: {slug}
---

'
)

content <- paste0(front_matter, proposal$Description)

# Write file
filepath <- write_markdown_file(content, folder_path)
message("✓ Successfully created: ", filepath)

}, error = function(e) {
warning("Error processing proposal ", i, ": ", e$message)
})
}

return(nrow(proposals_to_draft))
}


# After successfully creating the markdown file:
# Update the Airtable record to:
# - Status = "Post drafted"
# - Add PR URL (once you know it)
Loading