Class: Bridgetown::Causeway::Command
- Inherits:
-
Bridgetown::Command
- Object
- Bridgetown::Command
- Bridgetown::Causeway::Command
- Includes:
- Bridgetown::Commands::ConfigurationOverridable
- Defined in:
- lib/bridgetown/causeway/command.rb
Instance Method Summary collapse
-
#call ⇒ Object
rubocop:disable Metrics.
Instance Method Details
#call ⇒ Object
rubocop:disable Metrics
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 |
# File 'lib/bridgetown/causeway/command.rb', line 24 def call # rubocop:disable Metrics path = post_path unless File.exist?(path) abort "No post found at #{path}" end config = configuration_with_overrides(, Bridgetown::Current.preloaded_configuration) config.run_initializers! context: :static content = File.read(path) front_matter, _body = parse_front_matter(content) already_published = front_matter["published"] == "true" unless already_published # Ask for summaries if summarize_command is present cmd = Bridgetown::Causeway.configuration.summarize_command if cmd summaries = [] 3.times do |i| puts "Running summary #{i + 1}/3..." eval_cmd = cmd.gsub("%{path}", path.shellescape) output = `#{eval_cmd} 2>/dev/null` begin summaries << JSON.parse(output.split("Summarizing...").last)["result"] rescue StandardError => e puts "Failed to parse summary output: #{e.}" end end # Prompt to choose if summaries.any? desc_values = summaries.map { |s| s["summary"] } tag_values = summaries.map { |s| s["tags"]&.join(" ") } concept_values = summaries.map { |s| s["concepts"] } edited_summary = pick_field("Description", desc_values) = pick_field("Tags", tag_values) edited_categories = pick_field("Categories", concept_values) if edited_summary safe_summary = edited_summary.strip.gsub('"', '\\"') content = update_front_matter_field(content, "description", %("#{safe_summary}")) end content = update_front_matter_field(content, "tags", .strip) if if edited_categories capitalized_cats = edited_categories.split(',').map(&:strip).map(&:capitalize).join(', ') content = update_front_matter_field(content, "preview_text", capitalized_cats) end File.write(path, content) front_matter, _body = parse_front_matter(content) end end print "\nType 'publish' to confirm publishing: " confirmation = $stdin.gets.strip abort "Publishing aborted." unless confirmation.downcase == "publish" end if already_published url = generate_url(path, front_matter) # check pending pending = [] pending << ::Causeway::Publishers::Mastodon.new if front_matter["mastodon_toot_id"].nil? || front_matter["mastodon_toot_id"].to_s.empty? pending << ::Causeway::Publishers::Bluesky.new if front_matter["bluesky_post_uri"].nil? || front_matter["bluesky_post_uri"].to_s.empty? if pending.empty? abort "Post is already fully published to all networks: #{path}" end puts "Post already published. Resuming social posts for: #{pending.map(&:name).join(', ')}" else # Rename and update date old_path = path now = Time.now new_date_str = now.strftime("%Y-%m-%d %H:%M:%S %z") new_prefix = now.strftime("%Y-%m-%d") if content =~ /^date:.*$/ content = content.sub(/^date:.*$/, "date: #{new_date_str}") else content = update_front_matter_field(content, "date", new_date_str) end content = update_front_matter_field(content, "published", "true") basename = File.basename(path) if basename.match?(/^\d{4}-\d{2}-\d{2}-/) new_basename = basename.sub(/^\d{4}-\d{2}-\d{2}-/, "#{new_prefix}-") else new_basename = "#{new_prefix}-#{basename}" end new_path = File.join(File.dirname(path), new_basename) if old_path != new_path File.write(old_path, content) File.rename(old_path, new_path) path = new_path git("rm", "--cached", old_path, allow_failure: true) if Bridgetown::Causeway.configuration.git_commit git("add", path) if Bridgetown::Causeway.configuration.git_commit else File.write(path, content) git("add", path) if Bridgetown::Causeway.configuration.git_commit end # Re-parse front matter front_matter, _body = parse_front_matter(content) url = generate_url(path, front_matter) if Bridgetown::Causeway.configuration.after_publish Bridgetown::Causeway.configuration.after_publish.call(path, front_matter) end if Bridgetown::Causeway.configuration.git_commit git("commit", "-m", "Publish #{File.basename(path)}") git("push") puts "Committed and pushed." end puts "\nWaiting for deployment..." print "Press Enter when the site is live: " $stdin.gets end # Post to networks description = front_matter["description"] || "A handful of things that caught my attention." = (front_matter["tags"] || "").split(/\s+/).reject(&:empty?).map { |t| "##{t}" }.join(" ") suffix = "\n\n#{url}\n\n#{} #blog".rstrip if [:template] base_text = [:template].gsub("{url}", url) else base_text = "#{description}#{suffix}" end puts "Base social post text (#{base_text.length} chars):\n---\n#{base_text}\n---" publishers = [ [::Causeway::Publishers::Mastodon.new, "mastodon_toot_id"], [::Causeway::Publishers::Bluesky.new, "bluesky_post_uri"] ] posted = [] errors = [] publishers.each do |pub_entry| publisher, fm_key = pub_entry content = File.read(path) front_matter, _ = parse_front_matter(content) existing = front_matter[fm_key] if existing && !existing.to_s.empty? puts "#{publisher.name}: already posted (#{existing}), skipping." next end publisher_text = base_text if ![:template] && publisher.name == "Bluesky" max_desc_len = 300 - suffix.grapheme_clusters.size if description.grapheme_clusters.size > max_desc_len puts "\nWarning: Description is too long for Bluesky (#{description.grapheme_clusters.size} graphemes). Truncating." truncated_desc = description.grapheme_clusters[0...(max_desc_len - 3)].join.strip + "..." publisher_text = "#{truncated_desc}#{suffix}" end end print "Posting to #{publisher.name}... " result = publisher.post(publisher_text) if result[:error] puts "FAILED" puts " #{result[:error]}" errors << publisher.name else puts "OK" puts " #{result[:url]}" content = update_front_matter_field(content, fm_key, result[:id]) File.write(path, content) posted << publisher.name end end if posted.any? && Bridgetown::Causeway.configuration.git_commit git("add", path) git("commit", "-m", "Add social post IDs to #{File.basename(path)}") git("push") puts "\nCommitted and pushed social post IDs (#{posted.join(', ')})." end if errors.any? puts "\nFailed to post to: #{errors.join(', ')}" puts "Fix the issue and re-run to resume." exit 1 end puts "\nDone!" end |