Skip to content

Instantly share code, notes, and snippets.

@kdmsnr
Last active December 16, 2025 18:37
Show Gist options
  • Select an option

  • Save kdmsnr/2d93c48c1b3ab0154a6623af59439147 to your computer and use it in GitHub Desktop.

Select an option

Save kdmsnr/2d93c48c1b3ab0154a6623af59439147 to your computer and use it in GitHub Desktop.
jekyllの _data/tweets.csv に書き込むためのWebインターフェイス
require 'bundler/inline'
gemfile do
source 'https://rubygems.org'
gem 'sinatra'
gem 'rackup'
gem 'puma'
gem "csv"
end
require 'sinatra'
require 'date'
require 'fileutils'
require 'csv'
DATA_FILE = "_data/tweets.csv"
set :port, 4567
set :run, true
set :inline_templates, __FILE__
helpers do
def h(text)
Rack::Utils.escape_html(text)
end
end
FileUtils.mkdir_p(File.dirname(DATA_FILE))
get '/' do
@logs = []
if File.exist?(DATA_FILE)
@logs = CSV.read(DATA_FILE).reverse.take(20).map { |row| row[0] }
end
erb :index
end
post '/tweet' do
content = params[:content]
return "Empty" if content.nil? || content.empty?
text = content.gsub(/\r\n|\r|\n/, ' ')
text = text.gsub('"', '""')
date = DateTime.now.strftime("[%Y-%m-%d %H:%M:%S]")
entry = %Q("#{date} #{text}")
File.open(DATA_FILE, "a") do |f|
f.puts entry
end
redirect '/'
end
__END__
@@ index
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>_Log</title>
<style>
body { font-family: monospace; max-width: 800px; margin: 2rem auto; padding: 0 1rem; background: #f4f4f4; color: #333; }
h1 { font-size: 1.2rem; margin-bottom: 1rem; }
.input-area { margin-bottom: 2rem; border-bottom: 1px solid #ccc; padding-bottom: 2rem; }
input[type="text"] { width: 100%; padding: 0.8rem; font-size: 14px; margin-bottom: 1rem; box-sizing: border-box; font-family: monospace; border: 1px solid #999; border-radius: 4px; }
button { padding: 0.8rem 2rem; background: #333; color: #fff; border: none; cursor: pointer; font-family: monospace; border-radius: 4px; }
button:hover { background: #000; }
.log-list { font-size: 13px; line-height: 1.6; }
.log-item { border-bottom: 1px solid #e0e0e0; padding: 0.5rem 0; display: flex; }
.log-date { color: #888; margin-right: 1rem; white-space: nowrap; }
.log-content { word-break: break-all; }
</style>
</head>
<body>
<h1>>_Log</h1>
<div class="input-area">
<form action="/tweet" method="POST">
<input type="text" name="content" placeholder="Tweet..." autofocus autocomplete="off">
<button type="submit">Tweet</button>
</form>
</div>
<div class="log-list">
<% if @logs.empty? %>
<div style="color: #999;">No logs found.</div>
<% else %>
<% @logs.each do |log| %>
<div class="log-item">
<span class="log-date"><%=h log[0..21] %></span>
<span class="log-content"><%=h log[21..-1] %></span>
</div>
<% end %>
<% end %>
</div>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment