{ "always_run_in_app": false, "icon": { "color": "deep-blue", "glyph": "sf:pencil.and.scribble" }, "name": "Blog Publishing Tracker", "script": "// Variables used by Scriptable.\n// These must be at the very top of the file. Do not edit.\n// icon-color: deep-blue; icon-glyph: sf:pencil.and.scribble;\n\n/*\n * Blog Publishing Tracker\n * Scriptable widget for a Hugo blog\n * Diego Zamboni, November 2026\n *\n * Small: monthly goal and publishing history\n * Medium: statistics and activity heatmap\n * Large: statistics, heatmap and recent articles\n */\n\n// ── Configuration ───────────────────────────\n\nconst SETTINGS = {\n site: \"https://zzamboni.org\",\n name: \"ZZAMBONI.ORG\",\n monthlyGoal: 4,\n refreshMinutes: 60,\n previewSize: \"small\",\n historyMonths: 6,\n heatmapWeeks: 12,\n timeZone: \"Europe/Zurich\",\n photoTag: \"photos\",\n linkTag: \"linky\"\n};\n\nconst COLORS = {\n background: \"#101D26\",\n backgroundEnd: \"#172C36\",\n primary: \"#FFFFFF\",\n secondary: \"#A2B5BF\",\n article: \"#32D6C5\",\n link: \"#D0A3FF\",\n photo: \"#FFBE55\",\n track: \"#30414B\",\n empty: \"#263B46\"\n};\n\nconst fm = FileManager.local();\nconst cachePath = fm.joinPath(\n fm.documentsDirectory(),\n \"zzamboni-blog-activity.json\"\n);\n\nconst now = new Date();\n\n// ── Date helpers ────────────────────────────\n\nfunction dateKey(date) {\n return new Intl.DateTimeFormat(\"en-CA\", {\n timeZone: SETTINGS.timeZone,\n year: \"numeric\",\n month: \"2-digit\",\n day: \"2-digit\"\n }).format(date);\n}\n\nfunction monthKey(date) {\n return dateKey(date).slice(0, 7);\n}\n\nfunction formatDate(date, options) {\n return new Intl.DateTimeFormat(\"en\", {\n ...options,\n timeZone: SETTINGS.timeZone\n }).format(date);\n}\n\nfunction localDay(date) {\n const parts = dateKey(date).split(\"-\").map(Number);\n return new Date(Date.UTC(\n parts[0], parts[1] - 1, parts[2]\n ));\n}\n\nfunction daysAgo(date) {\n return Math.max(0, Math.round(\n (localDay(now) - localDay(date)) / 86400000\n ));\n}\n\nfunction relativeDate(date) {\n const days = daysAgo(date);\n\n if (days === 0) return \"Today\";\n if (days === 1) return \"Yesterday\";\n\n return `${days} days ago`;\n}\n\n// ── RSS feed handling ───────────────────────\n\nasync function fetchFeed(path) {\n const req = new Request(SETTINGS.site + path);\n req.timeoutInterval = 20;\n\n const xml = await req.loadString();\n\n if (req.response.statusCode !== 200) {\nthrow new Error(\n `RSS HTTP ${req.response.statusCode}: ${path}`\n);\n }\n\n return parseRSS(xml);\n}\n\nasync function fetchOptionalFeed(...paths) {\n for (const path of paths) {\n try {\n const posts = await fetchFeed(path);\n console.log(`${path}: ${posts.length} posts`);\n return posts;\n } catch (error) {\n console.warn(String(error));\n }\n }\n\n throw new Error(\n \"No available feed: \" + paths.join(\", \")\n );\n}\n\nfunction parseRSS(xml) {\n const posts = [];\n const stack = [];\n\n let item = null;\n\n const parser = new XMLParser(xml);\n\n parser.didStartElement = function(name) {\n stack.push({ name, value: \"\" });\n\n if (name === \"item\") {\n item = {\n title: \"\",\n link: \"\",\n pubDate: \"\",\n categories: []\n };\n }\n };\n\n parser.foundCharacters = function(value) {\n if (stack.length) {\n stack[stack.length - 1].value += value;\n }\n };\n\n parser.didEndElement = function(name) {\n const element = stack.pop();\n\n if (!item || !element) return;\n\n const parent = stack.length\n ? stack[stack.length - 1].name\n : \"\";\n\n if (parent === \"item\") {\n const value = element.value.trim();\n\n if (name === \"category\") {\n item.categories.push(value);\n } else if (\n [\"title\", \"link\", \"pubDate\"].includes(name)\n ) {\n item[name] = value;\n }\n }\n\n if (name === \"item\") {\n const date = new Date(item.pubDate);\n\n if (\n item.link &&\n Number.isFinite(date.getTime())\n ) {\n posts.push({\n title: item.title,\n url: item.link.replace(/\\/$/, \"\"),\n date: date.toISOString(),\n categories: item.categories\n });\n }\n\n item = null;\n }\n };\n\n parser.parse();\n\n return posts;\n}\n\n// ── Load and classify posts ─────────────────\n\nasync function getPosts() {\n let previous = null;\n\n if (fm.fileExists(cachePath)) {\n try {\n previous = JSON.parse(\n fm.readString(cachePath)\n );\n } catch (_) {}\n }\n\n try {\n // Taxonomy feeds identify photos and links\n // even if the main RSS feed omits categories.\n\n const posts = await fetchFeed(\"/post/index.xml\");\n\n if (!posts.length) {\n throw new Error(\"Empty blog feed\");\n }\n\nconst [photos, links] = await Promise.all([\n fetchOptionalFeed(\n `/tags/${encodeURIComponent(SETTINGS.photoTag)}/index.xml`\n ),\n fetchOptionalFeed(\n `/tags/${encodeURIComponent(SETTINGS.linkTag)}/index.xml`\n )\n]);\n\n const photoURLs = new Set(\n photos.map(p => p.url)\n );\n\n const linkURLs = new Set(\n links.map(p => p.url)\n );\n\n const allPosts = new Map();\n\n for (const post of [...posts, ...photos, ...links]) {\nconst tags = (post.categories || [])\n .map(t => t.toLowerCase());\n\nlet type = \"article\";\n\nif (\n photoURLs.has(post.url) ||\n tags.includes(SETTINGS.photoTag.toLowerCase())\n) {\n type = \"photo\";\n} else if (\n linkURLs.has(post.url) ||\n tags.includes(SETTINGS.linkTag.toLowerCase())\n) {\n type = \"link\";\n}\n\n allPosts.set(post.url, {\n ...post,\n type\n });\n }\n\n const data = {\n posts: [...allPosts.values()].sort(\n (a, b) => new Date(b.date) - new Date(a.date)\n ),\n fetched: now.toISOString(),\n cached: false\n };\n\n fm.writeString(\n cachePath,\n JSON.stringify(data)\n );\n\n return data;\n\n } catch (error) {\n console.warn(String(error));\n\n if (previous && previous.posts.length) {\n return {\n ...previous,\n cached: true\n };\n }\n\n throw error;\n }\n}\n\n// ── Publishing statistics ───────────────────\n\nfunction statistics(posts) {\n const currentMonth = monthKey(now);\n\n const thisMonth = posts.filter(\n p => monthKey(new Date(p.date)) === currentMonth\n );\n\n const articles = posts.filter(\n p => p.type === \"article\"\n );\n\n const currentArticles = thisMonth.filter(\n p => p.type === \"article\"\n );\n\n const months = [];\n const today = localDay(now);\n\n for (\n let i = SETTINGS.historyMonths - 1;\n i >= 0;\n i--\n ) {\n const date = new Date(Date.UTC(\n today.getUTCFullYear(),\n today.getUTCMonth() - i,\n 15\n ));\n\n const key = monthKey(date);\n\n months.push({\n label: formatDate(date, {\n month: \"short\"\n }),\n current: i === 0,\n count: articles.filter(\n p => monthKey(new Date(p.date)) === key\n ).length\n });\n }\n\n return {\n month: formatDate(now, { month: \"long\" }),\n articles: currentArticles.length,\n links: thisMonth.filter(\n p => p.type === \"link\"\n ).length,\n photos: thisMonth.filter(\n p => p.type === \"photo\"\n ).length,\n goal: SETTINGS.monthlyGoal,\n lastArticle: articles[0] || null,\n recentArticles: articles.slice(0, 3),\n months\n };\n}\n\n// ── Drawing helpers ─────────────────────────\n\nfunction addText(\n parent,\n value,\n size = 12,\n color = COLORS.primary,\n weight = \"regular\"\n) {\n const t = parent.addText(String(value));\n\n if (weight === \"bold\") {\n t.font = Font.boldSystemFont(size);\n } else if (weight === \"medium\") {\n t.font = Font.mediumSystemFont(size);\n } else if (weight === \"semibold\") {\n t.font = Font.semiboldSystemFont(size);\n } else {\n t.font = Font.systemFont(size);\n }\n\n t.textColor = new Color(color);\n t.lineLimit = 1;\n t.minimumScaleFactor = 0.65;\n\n return t;\n}\n\nfunction roundedRect(ctx, rect, color, radius = 2) {\n const path = new Path();\n\n path.addRoundedRect(rect, radius, radius);\n ctx.addPath(path);\n ctx.setFillColor(new Color(color));\n ctx.fillPath();\n}\n\nfunction progressBar(ratio, width) {\n const ctx = new DrawContext();\n\n ctx.size = new Size(width, 6);\n ctx.opaque = false;\n ctx.respectScreenScale = true;\n\n roundedRect(\n ctx,\n new Rect(0, 0, width, 6),\n COLORS.track,\n 3\n );\n\n const filled = width * Math.min(1, Math.max(0, ratio));\n\n if (filled > 0) {\n roundedRect(\n ctx,\n new Rect(0, 0, filled, 6),\n COLORS.article,\n Math.min(3, filled / 2)\n );\n }\n\n return ctx.getImage();\n}\n\n// ── Monthly bar chart ───────────────────────\n\nfunction monthlyChart(months, width, height) {\n const ctx = new DrawContext();\n\n ctx.size = new Size(width, height);\n ctx.opaque = false;\n ctx.respectScreenScale = true;\n\n const labelHeight = 12;\n const chartHeight = height - labelHeight - 10;\n\n const slot = width / months.length;\n const barWidth = slot * 0.62;\n\n const max = Math.max(\n SETTINGS.monthlyGoal,\n ...months.map(m => m.count),\n 1\n );\n\n for (let i = 0; i < months.length; i++) {\n const m = months[i];\n\n const x = i * slot +\n (slot - barWidth) / 2;\n\n const h = Math.max(\n 2,\n m.count / max * chartHeight\n );\n\n const y = 10 + chartHeight - h;\n\n const color = m.current\n ? COLORS.article\n : \"#39596D\";\n\n roundedRect(\n ctx,\n new Rect(x, y, barWidth, h),\n color\n );\n\n // Post count above each bar\n ctx.setFont(Font.mediumSystemFont(8));\n ctx.setTextColor(new Color(COLORS.secondary));\n ctx.setTextAlignedCenter();\n\n ctx.drawTextInRect(\n String(m.count),\n new Rect(i * slot, y - 10, slot, 10)\n );\n\n // Month label\n ctx.setFont(Font.systemFont(8));\n\n ctx.setTextColor(new Color(\n m.current ? COLORS.article : COLORS.secondary\n ));\n\n ctx.drawTextInRect(\n m.label,\n new Rect(\n i * slot,\n height - labelHeight + 2,\n slot,\n labelHeight\n )\n );\n }\n\n return ctx.getImage();\n}\n\n// ── Publishing heatmap ──────────────────────\n\nfunction heatmap(posts, width) {\n const weeks = SETTINGS.heatmapWeeks;\n const gap = 2;\n const cell = (width - (weeks - 1) * gap) / weeks;\n const height = 7 * cell + 6 * gap;\n\n const ctx = new DrawContext();\n\n ctx.size = new Size(width, height);\n ctx.opaque = false;\n ctx.respectScreenScale = true;\n\n const activity = {};\n\n for (const post of posts) {\n const key = dateKey(new Date(post.date));\n\n if (!activity[key]) {\n activity[key] = new Set();\n }\n\n activity[key].add(post.type);\n }\n\n // Start each column on Monday.\n const today = localDay(now);\n const end = new Date(today);\n\n end.setUTCDate(\n end.getUTCDate() +\n (7 - end.getUTCDay()) % 7\n );\n\n const start = new Date(end);\n\n start.setUTCDate(\n start.getUTCDate() - weeks * 7 + 1\n );\n\n for (let col = 0; col < weeks; col++) {\n for (let row = 0; row < 7; row++) {\n const date = new Date(start);\n\n date.setUTCDate(\n date.getUTCDate() + col * 7 + row\n );\n\n const key = date.toISOString().slice(0, 10);\n\n const x = col * (cell + gap);\n const y = row * (cell + gap);\n\n // Future days are left blank.\n if (date > today) {\n continue;\n }\n\n const types = activity[key];\n\n if (!types) {\n roundedRect(\n ctx,\n new Rect(x, y, cell, cell),\n COLORS.empty\n );\n\n continue;\n }\n\n // Multiple post types on the same day\n // are shown as colored segments.\n const colors = [...types].map(\n type => COLORS[type]\n );\n\n if (colors.length === 1) {\n roundedRect(\n ctx,\n new Rect(x, y, cell, cell),\n colors[0]\n );\n } else {\n const segment = cell / colors.length;\n\n colors.forEach((color, i) => {\n ctx.setFillColor(new Color(color));\n ctx.fillRect(new Rect(\n x + i * segment,\n y,\n segment,\n cell\n ));\n });\n }\n }\n }\n\n return {\n image: ctx.getImage(),\n height\n };\n}\n\n// ── Widget components ───────────────────────\n\nfunction baseWidget() {\n const widget = new ListWidget();\n\n const gradient = new LinearGradient();\n\n gradient.colors = [\n new Color(COLORS.background),\n new Color(COLORS.backgroundEnd)\n ];\n\n gradient.locations = [0, 1];\n\n widget.backgroundGradient = gradient;\n widget.setPadding(14, 14, 14, 14);\n widget.url = SETTINGS.site;\n\n widget.refreshAfterDate = new Date(\n Date.now() +\n SETTINGS.refreshMinutes * 60000\n );\n\n return widget;\n}\n\nfunction addHeader(widget, data, small) {\n const row = widget.addStack();\n row.centerAlignContent();\n\n const icon = row.addImage(\n SFSymbol.named(\"pencil.and.outline\").image\n );\n\n icon.imageSize = new Size(13, 13);\n icon.tintColor = new Color(COLORS.article);\n\n row.addSpacer(5);\n\n addText(\n row,\n SETTINGS.name,\n 11,\n COLORS.article,\n \"bold\"\n );\n\n row.addSpacer();\n\n if (data.cached) {\n addText(row, \"Cached\", 9, COLORS.photo);\n } else if (!small) {\n addText(\n row,\n formatDate(now, {\n month: \"short\",\n year: \"numeric\"\n }),\n 10,\n COLORS.secondary\n );\n }\n}\n\nfunction addGoal(parent, stats, width, small) {\n const ratio = stats.goal > 0\n ? stats.articles / stats.goal\n : 0;\n\n const noun = stats.articles === 1\n ? \"article\"\n : \"articles\";\n\n addText(\n parent,\n `${stats.articles} ${noun}`,\n small ? 25 : 29,\n COLORS.primary,\n \"bold\"\n );\n\n parent.addSpacer(2);\n\n addText(\n parent,\n `Published in ${stats.month}`,\n 10,\n COLORS.secondary\n );\n\n if (stats.goal > 0) {\n parent.addSpacer(5);\n\n const bar = parent.addImage(\n progressBar(ratio, width)\n );\n\n bar.imageSize = new Size(width, 6);\n\n parent.addSpacer(4);\n\n addText(\n parent,\n stats.articles >= stats.goal\n ? \"Monthly goal achieved!\"\n : `${stats.articles} of ${stats.goal} monthly goal`,\n 10,\n COLORS.article,\n \"medium\"\n );\n }\n}\n\nfunction addLegend(parent, fontSize = 8) {\n const row = parent.addStack();\n row.centerAlignContent();\n\n for (const [type, label] of [\n [\"article\", \"Articles\"],\n [\"link\", \"Links\"],\n [\"photo\", \"Photos\"]\n ]) {\n const dot = row.addStack();\n\n dot.size = new Size(6, 6);\n dot.backgroundColor = new Color(COLORS[type]);\n dot.cornerRadius = 2;\n\n row.addSpacer(3);\n\n addText(row, label, fontSize, COLORS.secondary);\n row.addSpacer(5);\n }\n}\n\nfunction addHeatmap(parent, posts, width) {\n const h = heatmap(posts, width);\n\n const image = parent.addImage(h.image);\n\n image.imageSize = new Size(\n width,\n h.height\n );\n\n parent.addSpacer(4);\n\n addLegend(parent);\n\n parent.addSpacer(3);\n\n addText(\n parent,\n `Last ${SETTINGS.heatmapWeeks} weeks`,\n 9,\n COLORS.secondary\n );\n}\n\nfunction addCounts(parent, stats) {\n const row = parent.addStack();\n\n for (const [type, label, count] of [\n [\"article\", \"Articles\", stats.articles],\n [\"link\", \"Links\", stats.links],\n [\"photo\", \"Photos\", stats.photos]\n ]) {\n const column = row.addStack();\n column.layoutVertically();\n\n addText(\n column,\n label.toUpperCase(),\n 9,\n COLORS.secondary\n );\n\n column.addSpacer(2);\n\n addText(\n column,\n count,\n 19,\n COLORS[type],\n \"bold\"\n );\n\n row.addSpacer();\n }\n}\n\nfunction addFooter(widget, stats, data) {\n if (data.cached) {\n addText(\n widget,\n \"Cached: \" + formatDate(\n new Date(data.fetched),\n { month: \"short\", day: \"numeric\" }\n ),\n 9,\n COLORS.photo\n );\n } else {\n const last = stats.lastArticle\n ? relativeDate(new Date(stats.lastArticle.date))\n : \"No articles\";\n\n addText(\n widget,\n `Last article: ${last.toLowerCase()}`,\n 10,\n COLORS.secondary\n );\n }\n}\n\n// ── Widget layouts ──────────────────────────\n\nfunction smallWidget(stats, data) {\n const widget = baseWidget();\n widget.spacing = 0;\n widget.setPadding(11, 14, 11, 14);\n\n // Match the usable width of the small widget.\n const width = 138;\n\n widget.addSpacer();\n addHeader(widget, data, true);\n widget.addSpacer(9);\n\n const goal = widget.addStack();\n goal.layoutVertically();\n goal.spacing = 0;\n addGoal(goal, stats, width, true);\n\n widget.addSpacer(4);\n\n const chart = widget.addImage(\n monthlyChart(stats.months, width, 42)\n );\n chart.imageSize = new Size(width, 42);\n\n widget.addSpacer(4);\n addFooter(widget, stats, data);\n widget.addSpacer();\n\n return widget;\n}\n\nfunction mediumWidget(stats, data) {\n const widget = baseWidget();\n widget.spacing = 0;\n\n addHeader(widget, data, false);\n widget.addSpacer(5);\n\n const row = widget.addStack();\n row.topAlignContent();\n row.spacing = 0;\n\n const left = row.addStack();\n left.layoutVertically();\n left.spacing = 0;\n left.size = new Size(144, 0);\n\n addGoal(left, stats, 144, true);\n left.addSpacer(5);\n\n const chart = left.addImage(\n monthlyChart(stats.months, 144, 46)\n );\n chart.imageSize = new Size(144, 46);\n\n row.addSpacer();\n\n // A subtle divider separates progress from activity.\n const dividerColumn = row.addStack();\n dividerColumn.layoutVertically();\n dividerColumn.size = new Size(1, 113);\n dividerColumn.spacing = 0;\n dividerColumn.addSpacer(5);\n\n const divider = dividerColumn.addStack();\n divider.size = new Size(1, 108);\n divider.backgroundColor = new Color(COLORS.secondary, 0.18);\n divider.cornerRadius = 0.5;\n\n row.addSpacer();\n\n const right = row.addStack();\n right.layoutVertically();\n right.spacing = 0;\n\n const width = 140;\n right.size = new Size(width, 0);\n const h = heatmap(data.posts, width);\n const image = right.addImage(h.image);\n image.imageSize = new Size(width, h.height);\n\n right.addSpacer(3);\n addText(\n right,\n `Last ${SETTINGS.heatmapWeeks} weeks`,\n 9,\n COLORS.secondary\n );\n right.addSpacer(4);\n\n // The colored counts also serve as the heatmap legend.\n const counts = right.addStack();\n counts.spacing = 0;\n\n for (const [type, label, count] of [\n [\"article\", \"Articles\", stats.articles],\n [\"link\", \"Links\", stats.links],\n [\"photo\", \"Photos\", stats.photos]\n ]) {\n const column = counts.addStack();\n column.layoutVertically();\n column.spacing = 0;\n column.size = new Size(width / 3, 0);\n\n addText(column, label, 9, COLORS[type], \"medium\");\n addText(column, count, 17, COLORS[type], \"bold\");\n }\n\n widget.addSpacer();\n\n return widget;\n}\n\nfunction largeWidget(stats, data) {\n const widget = baseWidget();\n\n widget.setPadding(14, 14, 14, 14);\n widget.spacing = 0;\n\n // Header\n addHeader(widget, data, false);\n widget.addSpacer(8);\n\n // Monthly goal and activity heatmap\n const body = widget.addStack();\n body.centerAlignContent();\n\n const left = body.addStack();\n left.layoutVertically();\n\n addGoal(left, stats, 120, false);\n\n body.addSpacer();\n\n const right = body.addStack();\n right.layoutVertically();\n\n addHeatmap(right, data.posts, 116);\n\n widget.addSpacer(8);\n\n // Monthly counts\n addCounts(widget, stats);\n\n widget.addSpacer(8);\n\n // Publishing history\n const chart = widget.addImage(\n monthlyChart(stats.months, 270, 48)\n );\n\n chart.imageSize = new Size(270, 48);\n chart.centerAlignImage();\n\n widget.addSpacer(10);\n\n // Recent articles\n addText(\n widget,\n \"RECENT ARTICLES\",\n 10,\n COLORS.article,\n \"bold\"\n );\n\n widget.addSpacer(5);\n\n for (const post of stats.recentArticles) {\n const row = widget.addStack();\n\n row.url = post.url;\n row.layoutVertically();\n row.spacing = 2;\n\n addText(\n row,\n post.title,\n 12,\n COLORS.primary,\n \"medium\"\n );\n\n addText(\n row,\n relativeDate(new Date(post.date)),\n 9,\n COLORS.secondary\n );\n\n widget.addSpacer(4);\n }\n\n // Absorb any remaining vertical space\n widget.addSpacer();\n\n // Only show footer when data is cached.\n // Otherwise the latest article date is\n // already visible in the article list.\n if (data.cached) {\n addFooter(widget, stats, data);\n }\n\n return widget;\n}\n\n\nfunction errorWidget(error) {\n const widget = baseWidget();\n\n addText(\n widget,\n SETTINGS.name,\n 11,\n COLORS.article,\n \"bold\"\n );\n\n widget.addSpacer();\n\n addText(\n widget,\n \"Unavailable\",\n 20,\n COLORS.primary,\n \"bold\"\n );\n\n widget.addSpacer(5);\n\n const text = widget.addText(\n error.message || String(error)\n );\n\n text.font = Font.systemFont(11);\n text.textColor = new Color(COLORS.secondary);\n text.lineLimit = 4;\n\n widget.addSpacer();\n\n return widget;\n}\n\n// ── Main ────────────────────────────────────\n\nconst size = config.runsInWidget\n ? config.widgetFamily\n : SETTINGS.previewSize;\n\nlet widget;\n\ntry {\n const data = await getPosts();\n const stats = statistics(data.posts);\n\n if (size === \"large\") {\n widget = largeWidget(stats, data);\n } else if (size === \"medium\") {\n widget = mediumWidget(stats, data);\n } else {\n widget = smallWidget(stats, data);\n }\n\n} catch (error) {\n widget = errorWidget(error);\n}\n\nScript.setWidget(widget);\n\nif (!config.runsInWidget) {\n if (size === \"large\") {\n await widget.presentLarge();\n } else if (size === \"medium\") {\n await widget.presentMedium();\n } else {\n await widget.presentSmall();\n }\n}\n\nScript.complete();\n", "share_sheet_inputs": [] }