Module: Abqari::Syndication::Formatter
- Defined in:
- lib/abqari/syndication.rb
Overview
Build the snippet posted to each network. Format:
<title> — <description, truncated as needed> <url>
The title + URL are non-negotiable; the description shrinks to
whatever fits inside the per-network character limit. Posts can
opt out per-frontmatter via syndicate: false, or supply their
own snippet via syndication_text:.
Class Method Summary collapse
Class Method Details
.clamp(text, limit) ⇒ Object
260 261 262 |
# File 'lib/abqari/syndication.rb', line 260 def clamp(text, limit) text.length <= limit ? text : "#{text[0, limit - 1]}…" end |
.snippet_for(post, url, limit:) ⇒ Object
234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 |
# File 'lib/abqari/syndication.rb', line 234 def snippet_for(post, url, limit:) custom = post.respond_to?(:frontmatter) ? post.frontmatter['syndication_text'] : nil return clamp(custom.to_s, limit) if custom && !custom.to_s.empty? title = post.respond_to?(:title) ? post.title.to_s : '' description = post.respond_to?(:frontmatter) ? post.frontmatter['description'].to_s : '' # URL with a leading space. Reserve room for `<title>` (required) # and `<url>` (required); fill the remainder with description. url_part = " #{url}" title_part = title.to_s.strip remaining = limit - title_part.length - url_part.length - 3 # 3 chars for " — " if remaining > 20 && !description.empty? desc_part = description.strip desc_part = "#{desc_part[0, remaining - 1]}…" if desc_part.length > remaining "#{title_part} — #{desc_part}#{url_part}" else # Title alone (+ url) — no room for a description, or no # description supplied. base = "#{title_part}#{url_part}" base = clamp(base, limit) base end end |