application.rb (3411B)
1 require_relative "boot" 2 3 require "rails/all" 4 require 'cgi' 5 require 'pathname' 6 # Require the gems listed in Gemfile, including any gems 7 # you've limited to :test, :development, or :production. 8 Bundler.require(*Rails.groups) 9 # <%= javascript_include_tag 'generated/movementInSquares', type: 'module', integrity: true %> 10 # Has to be a function because Rails.root isn't defined for constant. 11 def org_dir 12 "#{Rails.root}/app/orgs" 13 end 14 15 # Get the title of a org mode file. 16 def org_get_title(file_path) 17 title='' 18 File.readlines(file_path).each do |line| 19 if line.start_with?('#+TITLE:') 20 line['#+TITLE:'] = '' 21 title = line.strip 22 break 23 end 24 end 25 title 26 end 27 28 # Get the path to /app/org/f.org, chopping off anything before the org directory. 29 def get_org_path(file_path) 30 Pathname.new(file_path).relative_path_from(org_dir).to_s 31 end 32 33 def remove_org_from_db(file_path) 34 # Check to see if we're actually getting an org file. 35 return if File.extname(file_path) != '.org' 36 37 dir_name = File.dirname(get_org_path file_path) 38 dir_name = dir_name == '.' ? '' : dir_name 39 url = CGI.escape(File.basename(file_path, '.*')) 40 post = Post.find_by(url: url, where: dir_name) 41 post.destroy if post 42 end 43 44 def compile_org_file(file_path_org) 45 # Generate the HTML file. 46 system("emacs -q --script #{Rails.root.join('app', 'publish.el')} #{file_path_org}") 47 end 48 49 def update_org_db(file_path) 50 # Check to see if we're actually getting an org file. 51 return if File.extname(file_path) != '.org' 52 53 # TODO get a description 54 title = org_get_title file_path 55 dir_name = File.dirname(get_org_path file_path) 56 dir_name = dir_name == '.' ? '' : dir_name 57 output_file_path = File.basename(file_path, '.org') 58 url = CGI.escape(output_file_path) 59 # Get the post. 60 post = Post.find_by(url: url, where: dir_name) 61 return if not post 62 # Reset content and title. 63 puts "Setting post new body page" 64 65 compile_org_file file_path 66 post.title = title 67 post.save 68 # TODO add more rescues for different errors 69 end 70 71 def add_org_to_db(file_path) 72 # Check to see if we're actually getting an org file. 73 return if File.extname(file_path) != '.org' 74 75 # TODO get a description 76 title = org_get_title file_path 77 dir_name = File.dirname(get_org_path file_path) 78 dir_name = dir_name == '.' ? '' : dir_name 79 80 output_file_path = File.basename(file_path, '.org') 81 url = CGI.escape(output_file_path) 82 # Update org if it exists. 83 post = Post.find_by(url: url, where: dir_name) 84 return update_org_db(post) if post 85 # TODO add more rescues for different errors 86 # File does not exist, create it. 87 88 compile_org_file file_path 89 90 # TODO description. 91 Post.new(title: title, description: '', 92 where: dir_name, 93 url: url, 94 body: output_file_path + '.html').save 95 end 96 97 module Site 98 class Application < Rails::Application 99 # Initialize configuration defaults for originally generated Rails version. 100 config.load_defaults 7.0 101 # Handle errors myself. 102 config.exceptions_app = self.routes 103 104 config.public_file_server.enabled = true 105 106 # Init the database 107 config.after_initialize do 108 # Do change in orgs file. 109 listener = Listen.to(org_dir) do |modified, added, removed| 110 modified.each { |x| update_org_db x } 111 added.each { |x| add_org_to_db x } 112 removed.each { |x| remove_org_from_db x } 113 end 114 listener.start 115 end 116 end 117 end