site

Website's source files.
git clone git://git.ryanmj.xyz/site.git
Log | Files | Refs | LICENSE

commit 66302181c94cb7c7b6146e5da988146eea8876ed
parent ed22141051c6fefe837dc70503c5e1130a3f6a5e
Author: Ryan Jeffrey <ryan@ryanmj.xyz>
Date:   Sat, 19 Feb 2022 17:34:40 -0800

Can now render blog posts, read blog posts dynamically from /app/orgs

Diffstat:
MGemfile | 1+
MGemfile.lock | 8++++++++
Mapp/controllers/posts_controller.rb | 10+++++++++-
Mapp/models/post.rb | 1-
Mapp/views/posts/show.html.erb | 4++--
Mconfig/application.rb | 61+++++++++++++++++++++++++++++++++++++++++++++++++++++--------
Mconfig/database.yml | 2--
Mconfig/routes.rb | 8+++++++-
Ddb/migrate/20220217192914_create_posts.rb | 8--------
Adb/migrate/20220219224343_create_posts.rb | 13+++++++++++++
Mdb/schema.rb | 7++++++-
Mdb/seeds.rb | 6------
Mtest/fixtures/posts.yml | 10++++++++++
13 files changed, 109 insertions(+), 30 deletions(-)

diff --git a/Gemfile b/Gemfile @@ -24,6 +24,7 @@ gem "turbo-rails" # Hotwire's modest JavaScript framework [https://stimulus.hotwired.dev] gem "stimulus-rails" +gem 'listen' # Build JSON APIs with ease [https://github.com/rails/jbuilder] gem "jbuilder" diff --git a/Gemfile.lock b/Gemfile.lock @@ -89,6 +89,7 @@ GEM reline (>= 0.2.7) digest (3.1.0) erubi (1.10.0) + ffi (1.15.5) globalid (1.0.0) activesupport (>= 5.0) i18n (1.10.0) @@ -103,6 +104,9 @@ GEM jbuilder (2.11.5) actionview (>= 5.0.0) activesupport (>= 5.0.0) + listen (3.7.1) + rb-fsevent (~> 0.10, >= 0.10.3) + rb-inotify (~> 0.9, >= 0.9.10) loofah (2.14.0) crass (~> 1.0.2) nokogiri (>= 1.5.9) @@ -171,6 +175,9 @@ GEM thor (~> 1.0) zeitwerk (~> 2.5) rake (13.0.6) + rb-fsevent (0.11.1) + rb-inotify (0.10.1) + ffi (~> 1.0) rdoc (6.4.0) psych (>= 4.0.0) regexp_parser (2.2.1) @@ -228,6 +235,7 @@ DEPENDENCIES debug importmap-rails jbuilder + listen org-ruby pandoc-ruby puma (~> 5.0) diff --git a/app/controllers/posts_controller.rb b/app/controllers/posts_controller.rb @@ -10,6 +10,14 @@ class PostsController < ApplicationController end def show - @post = Post.find(params[:id]) rescue not_found + @post = Post.find(params[:url]) rescue not_found + end + + def front_show + @post = Post.find_by(url: params[:url]) rescue not_found + render 'show' + end + + def esoteric_show end end diff --git a/app/models/post.rb b/app/models/post.rb @@ -1,3 +1,2 @@ class Post < ApplicationRecord - has_one_attached :content end diff --git a/app/views/posts/show.html.erb b/app/views/posts/show.html.erb @@ -3,9 +3,9 @@ </div> <div id="content"> <hr class="prompt-break"> - <span class="prompt1">ryan</span><span class="prompt2">@</span><span class="prompt3">themainframe</span><span class="prompt4"></span> <span>cat {{args}}</span> + <span class="prompt1">ryan</span><span class="prompt2">@</span><span class="prompt3">themainframe</span><span class="prompt4"></span> <span>cat <%= @post.url %></span> <div class="cat"> - <%= @post.content.download { |line| puts Orgmode::Parser.new(line).to_html } %> + <%= @post.body.html_safe %> </div> </div> </div> diff --git a/config/application.rb b/config/application.rb @@ -1,25 +1,70 @@ require_relative "boot" require "rails/all" +require 'cgi' +require 'pathname' # Require the gems listed in Gemfile, including any gems # you've limited to :test, :development, or :production. Bundler.require(*Rails.groups) +# Has to be a function because Rails.root isn't defined for constant. +def org_dir + "#{Rails.root}/app/orgs" +end + +# Get the path to /app/org/f.org, chopping off anything before the org directory. +def get_org_path(file_path) + Pathname.new(file_path).relative_path_from(org_dir).to_s +end + +def remove_org_from_db(file_path) + bsname = File.basename(file_path) + begin + post = Post.where(title: bsname).sole + post.destroy + rescue => error + # Nothing to destroy + end +end + +def add_org_to_db(file_path) + # TODO actually get the title of the document. + # TODO get a description + title = File.basename(file_path) + begin + post = Post.where(title: title).sole + # Reset content and title. + post.body = Orgmode::Parser.new(File.read(file_path)).to_html + post.title = title + # TODO add more rescues for different errors + rescue => error + # File does not exist, create it. + dir_name = File.dirname(get_org_path file_path) + Post.new(title: title, description: '', + where: dir_name == '.' ? '' : dir_name, + url: File.basename(file_path, '.*'), + body: Orgmode::Parser.new(File.read(file_path)).to_html).save + end +end + module Site class Application < Rails::Application # Initialize configuration defaults for originally generated Rails version. config.load_defaults 7.0 - # Configuration for the application, engines, and railties goes here. - # - # These settings can be overridden in specific environments using the files - # in config/environments, which are processed later. - # - # config.time_zone = "Central Time (US & Canada)" - # config.eager_load_paths << Rails.root.join("extras") - # Handle 404's by myself config.exceptions_app = self.routes config.public_file_server.enabled = true + + # Init the database + config.after_initialize do + # Do change in orgs file. + listener = Listen.to(org_dir) do |modified, added, removed| + modified.each { |x| add_org_to_db x } + added.each { |x| add_org_to_db x } + removed.each { |x| remove_org_from_db x } + end + listener.start + end end end diff --git a/config/database.yml b/config/database.yml @@ -4,8 +4,6 @@ # Ensure the SQLite 3 gem is defined in your Gemfile # gem "sqlite3" # -encoding: utf8 - default: &default adapter: sqlite3 pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %> diff --git a/config/routes.rb b/config/routes.rb @@ -1,9 +1,15 @@ Rails.application.routes.draw do root 'posts#index' + # Blog. get '/posts', to: 'posts#blog_index' + get '/posts/:url', to: 'posts#show' + # Hidden blog. get '/esoteric', to: 'posts#esoteric' - get '/posts/:id', to: 'posts#show' + get '/esoteric/:url', to: 'posts#esoteric_show' + # Front page posts + get '/:url', to: 'posts#front_show' + # Errors. get '/404', to: 'errors#not_found' get '/500', to: 'errors#not_found' get '/422', to: 'errors#not_found' diff --git a/db/migrate/20220217192914_create_posts.rb b/db/migrate/20220217192914_create_posts.rb @@ -1,8 +0,0 @@ -class CreatePosts < ActiveRecord::Migration[7.0] - def change - create_table :posts do |t| - - t.timestamps - end - end -end diff --git a/db/migrate/20220219224343_create_posts.rb b/db/migrate/20220219224343_create_posts.rb @@ -0,0 +1,13 @@ +class CreatePosts < ActiveRecord::Migration[7.0] + def change + create_table :posts do |t| + t.text :body + t.string :title + t.string :description + t.string :where + t.string :url + + t.timestamps + end + end +end diff --git a/db/schema.rb b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema[7.0].define(version: 2022_02_17_225631) do +ActiveRecord::Schema[7.0].define(version: 2022_02_19_224343) do create_table "active_storage_attachments", force: :cascade do |t| t.string "name", null: false t.string "record_type", null: false @@ -40,6 +40,11 @@ ActiveRecord::Schema[7.0].define(version: 2022_02_17_225631) do end create_table "posts", force: :cascade do |t| + t.text "body" + t.string "title" + t.string "description" + t.string "where" + t.string "url" t.datetime "created_at", null: false t.datetime "updated_at", null: false end diff --git a/db/seeds.rb b/db/seeds.rb @@ -1,8 +1,2 @@ #post.content.attach(io: File.open('/home/ryan/TODO.org'), filename: "TODO.org", content_type: 'text') -Dir.glob("#{Rails.root}/app/orgs/*.org") do |filename| - post = Post.new - post.content.attach(io: File.open(filename, "r:UTF-8"), filename: File.basename(filename), - content_type: 'text') - post.save -end diff --git a/test/fixtures/posts.yml b/test/fixtures/posts.yml @@ -1,5 +1,15 @@ # Read about fixtures at https://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html one: + body: MyText + title: MyString + description: MyString + where: MyString + url: MyString two: + body: MyText + title: MyString + description: MyString + where: MyString + url: MyString