Skip to content

Instantly share code, notes, and snippets.

@mccarthy-m-g
Last active May 15, 2023 22:05
Show Gist options
  • Select an option

  • Save mccarthy-m-g/00f755efa6e983a3e0fc06b16ab30bb3 to your computer and use it in GitHub Desktop.

Select an option

Save mccarthy-m-g/00f755efa6e983a3e0fc06b16ab30bb3 to your computer and use it in GitHub Desktop.

Revisions

  1. mccarthy-m-g revised this gist May 15, 2023. 1 changed file with 15 additions and 8 deletions.
    23 changes: 15 additions & 8 deletions embed_post.R
    Original file line number Diff line number Diff line change
    @@ -1,16 +1,24 @@
    #' Embed a Mastodon post in Quarto
    #'
    #' Embeds a Mastodon post in the style of a Quarto callout block using
    #' {rtoot} and {htmltools}. No API key required.
    #'
    #' @param id Character. Local ID of a post.
    #' @param instance Character. Server name of the post's author.
    #' @return
    #' A `list()` with a `shiny.tag` class that can be converted into an HTML string
    #' via `as.character()` and saved to a file with `htmltools::save_html()`.
    embed_post <- function(id, instance) {

    post <- rtoot::get_status(id = id, instance = instance, anonymous = TRUE)

    # Post author's account
    account <- post$account[[1]]
    display_name <- account$display_name
    username <- htmltools::tags$a(
    author <- htmltools::tags$a(
    href = account$url,
    target = "_blank",
    class = "callout-mastodon-username",
    paste0("@", account$username, "@", instance)

    paste0(account$display_name, " @", account$username, "@", instance)
    )

    # Post content
    @@ -25,7 +33,7 @@ embed_post <- function(id, instance) {
    htmltools::tags$div(
    class = "callout callout-style-simple callout-mastodon callout-titled",
    cite = post$url,
    # Poster's account
    # Post author
    htmltools::tags$div(
    class = "callout-header d-flex align-content-center",
    htmltools::tags$div(
    @@ -34,8 +42,7 @@ embed_post <- function(id, instance) {
    ),
    htmltools::tags$div(
    class = "callout-title-container flex-fill",
    display_name,
    username
    author
    )
    ),
    # Post content
    @@ -49,4 +56,4 @@ embed_post <- function(id, instance) {
    )
    )

    }
    }
  2. mccarthy-m-g created this gist May 15, 2023.
    32 changes: 32 additions & 0 deletions callout-mastodon.css
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,32 @@
    .callout-mastodon {
    border-left-color: #6364ff !important;
    }

    .callout-header {
    border-bottom: none;
    font-size: .9rem;
    font-weight: 600;
    opacity: 75%;
    }

    div.callout-mastodon.callout-titled .callout-icon::before {
    background-image: url('data:image/svg+xml,<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" style="fill: %236364ff" class="fab fa-mastodon" viewBox="0 0 448 512"><path d="M433 179.11c0-97.2-63.71-125.7-63.71-125.7-62.52-28.7-228.56-28.4-290.48 0 0 0-63.72 28.5-63.72 125.7 0 115.7-6.6 259.4 105.63 289.1 40.51 10.7 75.32 13 103.33 11.4 50.81-2.8 79.32-18.1 79.32-18.1l-1.7-36.9s-36.31 11.4-77.12 10.1c-40.41-1.4-83-4.4-89.63-54a102.54 102.54 0 0 1-.9-13.9c85.63 20.9 158.65 9.1 178.75 6.7 56.12-6.7 105-41.3 111.23-72.9 9.8-49.8 9-121.5 9-121.5zm-75.12 125.2h-46.63v-114.2c0-49.7-64-51.6-64 6.9v62.5h-46.33V197c0-58.5-64-56.6-64-6.9v114.2H90.19c0-122.1-5.2-147.9 18.41-175 25.9-28.9 79.82-30.8 103.83 6.1l11.6 19.5 11.6-19.5c24.11-37.1 78.12-34.8 103.83-6.1 23.71 27.3 18.4 53 18.4 175z"/></svg>');
    }

    .callout-mastodon-username {
    color: inherit;
    text-decoration: none;
    }

    .callout-body {
    padding-left: 1.6em;
    margin-top: .2em;
    font-size: .9rem;
    font-weight: 400;
    }

    .callout-footer {
    font-size: .825em;
    margin-top: 1.25rem;
    align-items: center;
    }
    52 changes: 52 additions & 0 deletions embed_post.R
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,52 @@
    embed_post <- function(id, instance) {

    post <- rtoot::get_status(id = id, instance = instance, anonymous = TRUE)

    # Post author's account
    account <- post$account[[1]]
    display_name <- account$display_name
    username <- htmltools::tags$a(
    href = account$url,
    target = "_blank",
    class = "callout-mastodon-username",
    paste0("@", account$username, "@", instance)

    )

    # Post content
    content <- htmltools::HTML(post$content)

    # Post date and time
    created_at <- trimws(format(post$created_at, "%l:%M %P - %B%e, %Y (%Z)"))
    created_at <- sub("am", "AM", created_at)
    created_at <- sub("pm", "PM", created_at)
    created_at <- htmltools::tags$a(href = post$url, target = "_blank", created_at)

    htmltools::tags$div(
    class = "callout callout-style-simple callout-mastodon callout-titled",
    cite = post$url,
    # Poster's account
    htmltools::tags$div(
    class = "callout-header d-flex align-content-center",
    htmltools::tags$div(
    class = "callout-icon-container",
    htmltools::tag("i", list(class = "callout-icon"))
    ),
    htmltools::tags$div(
    class = "callout-title-container flex-fill",
    display_name,
    username
    )
    ),
    # Post content
    htmltools::tags$div(
    class = "callout-body-container callout-body",
    content,
    htmltools::tags$div(
    class = "callout-footer",
    created_at
    )
    )
    )

    }