Skip to content

Commit

Permalink
Merge pull request #55 from gadenbuie/make-sync
Browse files Browse the repository at this point in the history
Add Makefile with sync designation
  • Loading branch information
gadenbuie committed Feb 15, 2024
2 parents 0f13fac + b81046a commit c810f16
Show file tree
Hide file tree
Showing 7 changed files with 83 additions and 21 deletions.
50 changes: 50 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
.PHONY: help debug sync-all sync-python sync-quarto sync-r
.DEFAULT_GOAL := help

help: ## Show help messages for make targets
@grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(firstword $(MAKEFILE_LIST)) | sort | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[32m%-18s\033[0m %s\n", $$1, $$2}'

# Define the source directory for assets
SRC_DIR := lib
SRC_FILES := $(wildcard $(SRC_DIR)/*.js)
SRC_FILES += $(wildcard $(SRC_DIR)/*.css)
SRC_FILES += $(wildcard $(SRC_DIR)/*.mp3)

# Read version from version distribution file
VERSION_FILE := $(SRC_DIR)/version.txt
VERSION := $(shell grep '^Version:' $(VERSION_FILE) | awk '{print $$2}')

# Define the destination directories for different languages
SYNC_DEST_PYTHON := python/countdown/assets
SYNC_DEST_R := r/inst/countdown
SYNC_DEST_QUARTO := quarto/_extensions/countdown/assets

sync-all: sync-python sync-r sync-quarto ## Sync web assets to all subpackages

sync-python: ## Sync web assets to Python package
@echo "Syncing web assets to the Python package..."
@test -d $(SYNC_DEST_PYTHON) || mkdir -p $(SYNC_DEST_PYTHON)
cp -r $(SRC_FILES) $(SYNC_DEST_PYTHON)
echo "countdown_version = '$(VERSION)'" > python/countdown/config.py

sync-r: ## Sync web assets to R package
@echo "Syncing web assets to R directory..."
@test -d $(SYNC_DEST_R) || mkdir -p $(SYNC_DEST_R)
cp -r $(SRC_FILES) $(SYNC_DEST_R)
echo "countdown_version <- '$(VERSION)'" > r/R/config.R


sync-quarto: ## Sync web assets to Quarto extension
@echo "Syncing web assets to Quarto directory..."
@test -d $(SYNC_DEST_QUARTO) || mkdir -p $(SYNC_DEST_QUARTO)
cp -r $(SRC_FILES) $(SYNC_DEST_QUARTO)
echo "local countdown_version = '$(VERSION)'\n\nreturn { countdownVersion = countdown_version }" > quarto/_extensions/countdown/config.lua

debug: ## Print all variables for debugging
@printf "\033[32m%-18s\033[0m %s\n" "VERSION" "$(VERSION)"
@printf "\033[32m%-18s\033[0m %s\n" "SRC_DIR" "$(SRC_DIR)"
@printf "\033[32m%-18s\033[0m %s\n" "SRC_FILES" "$(SRC_FILES)"
@printf "\033[32m%-18s\033[0m %s\n" "VERSION_FILE" "$(VERSION_FILE)"
@printf "\033[32m%-18s\033[0m %s\n" "SYNC_DEST_PYTHON" "$(SYNC_DEST_PYTHON)"
@printf "\033[32m%-18s\033[0m %s\n" "SYNC_DEST_R" "$(SYNC_DEST_R)"
@printf "\033[32m%-18s\033[0m %s\n" "SYNC_DEST_QUARTO" "$(SYNC_DEST_QUARTO)"
1 change: 1 addition & 0 deletions lib/version.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Version: 0.5.0
3 changes: 3 additions & 0 deletions quarto/_extensions/countdown/config.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
local countdown_version = '0.5.0'

return { countdownVersion = countdown_version }
39 changes: 20 additions & 19 deletions quarto/_extensions/countdown/countdown.lua
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
-- Specify embedded version
local countdownEmbeddedVersion = "0.0.1"
-- Retrieve countdown configuration data
local configData = require("config")
-- Specify the embedded version
local countdownVersion = configData.countdownVersion

-- Only embed resources once if there are multiple timers present
-- Only embed resources once if there are multiple timers present
local needsToExportDependencies = true

-- List CSS default options
Expand Down Expand Up @@ -37,7 +39,7 @@ local function isVariablePopulated(s)
return not isVariableEmpty(s)
end

-- Check if a table is empty
-- Check if a table is empty
local function isTableEmpty(tbl)
return next(tbl) == nil
end
Expand All @@ -50,7 +52,7 @@ end
-- Check whether an argument is present in kwargs
-- If it is, return the value
local function tryOption(options, key)

-- Protect against an empty options
if not (options and options[key]) then
return nil
Expand Down Expand Up @@ -96,12 +98,12 @@ local function safeStyle(options, key, fmtString)
end

-- Construct the CSS style attributes
local function structureCountdownCSSVars(options)
local function structureCountdownCSSVars(options)
-- Concatenate style properties with their values using %:?% from kwargs
local stylePositional = {"top", "right", "bottom", "left"}
local stylePositionalTable = {}
local styleDefaultOptionsTable = {}

-- Build the positional style without prefixing countdown variables
for i, key in ipairs(stylePositional) do
stylePositionalTable[i] = safeStyle(options, key, "%s: %s;")
Expand Down Expand Up @@ -131,9 +133,9 @@ local function countdown_style(options)
local structuredCSS = structureCountdownCSSVars(options)

-- Embed into the document to avoid rendering to disk and, then, embedding a URL.
quarto.doc.include_text('in-header',
quarto.doc.include_text('in-header',
string.format(
"<!-- Countdown Global CSS -->\n<style text='text/css'>%s {%s}</style>",
"<!-- Countdown Global CSS -->\n<style text='text/css'>%s {%s}</style>",
possibleSelector,
structuredCSS
)
Expand All @@ -144,11 +146,10 @@ end

-- Handle embedding/creation of assets once
local function ensureHTMLDependency(meta)

-- Register _all_ assets together.
quarto.doc.addHtmlDependency({
name = "countdown",
version = countdownEmbeddedVersion,
version = countdownVersion,
scripts = { "assets/countdown.js"},
stylesheets = { "assets/countdown.css"},
resources = {"assets/smb_stage_clear.mp3"}
Expand All @@ -171,7 +172,7 @@ local function parseTimeString(args)

-- Attempt to extract minutes and seconds from the time string
local minutes, seconds = args[1]:match("(%d+):(%d+)")

-- Check if the pattern matching was successful
if isVariableEmpty(minutes) or isVariableEmpty(seconds) then
-- Log an error message if the format is incorrect
Expand All @@ -195,7 +196,7 @@ local function countdown(args, kwargs, meta)
if isVariablePopulated(arg_time) then
minutes = arg_time.minutes
seconds = arg_time.seconds
if isVariablePopulated(tryOption(kwargs, "minutes")) or
if isVariablePopulated(tryOption(kwargs, "minutes")) or
isVariablePopulated(tryOption(kwargs, "seconds")) then
quarto.log.warning(
"Please do not specify `minutes` or `seconds` parameters" ..
Expand All @@ -214,7 +215,7 @@ local function countdown(args, kwargs, meta)

-- Calculate remaining seconds after extracting minutes
seconds = time - minutes * 60

-- Check if minutes is greater than or equal to 100 (the maximum possible for display)
if minutes >= 100 then
quarto.log.error("The number of minutes must be less than 100.")
Expand All @@ -234,7 +235,7 @@ local function countdown(args, kwargs, meta)

-- Determine if a warning should be given
local warn_when = tonumber(getOption(kwargs, "warn_when", 0))

-- Retrieve and convert "update_every" attribute to a number, default to 1 if not present or invalid
local update_every = tonumber(getOption(kwargs, "update_every", 1))

Expand All @@ -251,14 +252,14 @@ local function countdown(args, kwargs, meta)
if isVariableEmpty(tryOption(kwargs, "top")) and
isVariableEmpty(tryOption(kwargs, "bottom")) then
kwargs["bottom"] = 0
end
end

if isVariableEmpty(tryOption(kwargs, "left")) and
isVariableEmpty(tryOption(kwargs, "right")) then
kwargs["right"] = 0
kwargs["right"] = 0
end

local style = structureCountdownCSSVars(kwargs)
local style = structureCountdownCSSVars(kwargs)

local rawHtml = table.concat({
'<div ',
Expand Down
1 change: 1 addition & 0 deletions r/R/config.R
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
countdown_version <- '0.5.0'
2 changes: 1 addition & 1 deletion r/R/countdown.R
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ countdown <- function(
html_dependency_countdown <- function() {
htmlDependency(
"countdown",
version = utils::packageVersion("countdown"),
version = countdown_version,
package = "countdown",
src = "countdown",
script = "countdown.js",
Expand Down
8 changes: 7 additions & 1 deletion r/inst/countdown/countdown.css
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
.countdown {
--_margin: 0.6em;
--_running-color: var(--countdown-color-running-text, rgba(0, 0, 0, 0.8));
--_running-background: var(--countdown-color-running-background, #43AC6A);
--_running-border-color: var(--countdown-color-running-border, rgba(0, 0, 0, 0.1));
Expand All @@ -19,7 +20,7 @@
border-style: solid;
border-radius: var(--countdown-border-radius, 0.9rem);
box-shadow: var(--countdown-box-shadow, 0px 4px 10px 0px rgba(50, 50, 50, 0.4));
margin: var(--countdown-margin, 0.6em);
margin: var(--countdown-margin, var(--_margin, 0.6em));
padding: var(--countdown-padding, 0.625rem 0.9rem);
text-align: center;
z-index: 10;
Expand Down Expand Up @@ -152,6 +153,11 @@
--_button-bump: 0;
}

/* ---- Quarto Reveal.js ---- */
.reveal .countdown {
--_margin: 0;
}

/* ----- Fullscreen ----- */
.countdown.countdown-fullscreen {
z-index: 0;
Expand Down

0 comments on commit c810f16

Please sign in to comment.