colorful rat Ratfactor.com > Dave's Repos

rat-tools

Tools I use to build and maintain this website.
git clone http://ratfactor.com/repos/rat-tools/rat-tools.git

rat-tools/new-book

Download raw file: new-book

1 #!/usr/bin/env ruby 2 3 $books_path = '/home/dave/wiki/ratf/src/b' 4 5 if ARGV.count < 1 6 puts "ERROR: You gotta send me a line of text!" 7 exit 1 8 end 9 10 bookline = ARGV[0] 11 12 date, title, author, pages = nil 13 14 # Now the input, bookline, is one of two things: 15 # * A journal entry 16 # * A website link 17 if bookline.match?(/^[0-9-]+ /) 18 bits = bookline.rstrip.split(/\s{2,}/) 19 if bits.size != 4 20 puts "ERROR: Line didn't match expected book journal format:" 21 puts bookline 22 exit 1 23 end 24 date, title, author, pages = bits 25 26 # ======================================================= 27 # Journal entry, create website link 28 29 index_page = "#{$books_path}/index.adoc" 30 index_page_temp = "#{$books_path}/index.adoc.temp" 31 fname = title.downcase.gsub(/ +/, '_').gsub(/[^a-z0-9_-]+/, '') 32 33 # Read lines of existing books page, collect 34 my_lines = [] 35 File.foreach(index_page) do |line| 36 my_lines << line 37 38 # insert new link in books page 39 if line.match(/ENTRIES_HERE/) 40 my_lines << "* #{date} link:#{fname}[#{title}] by #{author}, #{pages} pages" 41 end 42 end 43 44 # Write new books page with link added 45 File.open(index_page_temp, 'w') do |f| 46 f.puts my_lines # writes all lines 47 end 48 49 # if the OLD page is bigger than the new one, do NOT continue! 50 if File.size(index_page) > File.size(index_page_temp) 51 puts "ERROR: Books page is smaller after writing link...so something went wrong!" 52 puts "Check it at: #{index_page_temp}" 53 puts "Exiting and not doing anything else." 54 exit 1 55 end 56 57 # the NEW page is bigger than the old, so replacing old with new 58 File.unlink(index_page) 59 File.rename(index_page_temp, index_page) 60 61 # now write the filename out so vim can open it 62 puts index_page 63 64 elsif bits = bookline.match(/^\* ([0-9-]+) link:(.*)\[(.*)\] by (.*), (\d+) pages/) 65 66 # ======================================================= 67 # Books page link. Create review page. 68 69 _, date, fname, title, author, pages = bits.to_a 70 fname = "#{$books_path}/#{fname}.adoc" 71 72 if File.exist?(fname) 73 puts "ERROR: #{fname} already exists." 74 exit 1 75 end 76 77 # Write new books page with link added 78 File.open(fname, 'w') do |f| 79 f.puts <<~HTML 80 :title: Dave's book review for #{title} 81 :created: #{Time.new.strftime('%Y-%m-%d')} 82 :updated: 83 84 ++++ 85 <div style="background-color: #EFC;"> 86 Book: <b>#{title}</b><br> 87 Author: <b>#{author}</b><br> 88 Pages: <b>#{pages}</b><br> 89 Finished reading: <b>#{date}</b><br> 90 Back to <a href=".">my books page</a> for more reviews, etc. 91 </div> 92 ++++ 93 94 == My Review 95 HTML 96 end 97 98 # Return file name so vim can open it 99 puts fname 100 101 exit 102 else 103 puts "ERROR: Input param does not look like journal entry or web link!" 104 exit 1 105 end