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/make.rb

Download raw file: make.rb

1 #!/usr/bin/ruby -w 2 3 require 'asciidoctor' 4 require 'erb' 5 require 'pathname' 6 #require 'fileutils' 7 8 generate_all = (ARGV.length == 1 and ARGV[0] == '-a') 9 if generate_all 10 puts "Generating all pages!" 11 end 12 13 # Read the page template 14 template_file = 'template.html' 15 template = File.read(template_file) 16 @erb = ERB.new(template) 17 @erb.filename = template_file 18 19 new_pages = [] 20 mod_pages = [] 21 22 def make_page(infile, outfile) 23 # note: safe mode 1 allows include[] macros but paths must be within parent dir 24 doc = Asciidoctor.load_file infile, header_footer: false, safe: 1 25 26 # Set vars used in template - note that the "_" prefix prevents an 27 # 'assigned but unused variable' warning in Ruby. 28 _title = doc.attributes['title'] || "Untitled Page" 29 _subtitle = doc.attributes['subtitle'] || "" 30 _created = doc.attributes['created'] || "" 31 _updated = doc.attributes['updated'] || "" 32 _updated_reason = doc.attributes['updated_reason'] || "" 33 _tags = doc.attributes['tags'] || "" 34 _draft = doc.attributes['draft'] || "" 35 _body = doc.convert 36 _generated = Time.now 37 _copyright_year = Time.now.strftime("%Y") 38 _pkgblog = false 39 _cards = false 40 41 # see if it's a pkgblog path - super dumb/easy way :-) 42 if infile.to_s =~ /pkgblog/ 43 _pkgblog = true 44 end 45 46 # see if it's a cards/ path - super dumb/easy way :-) 47 if infile.to_s =~ /cards/ 48 _cards = true 49 end 50 51 52 File.open(outfile, 'w') do |f| 53 f.puts @erb.result(binding) 54 end 55 end 56 57 Pathname.glob("src/**/*.adoc").each do |infile| 58 needs_update = false 59 60 # Wow, replacing ".adoc" not with ".html", but with NOTHING! 61 outfile = infile.sub_ext('') 62 63 #puts "input: #{infile} (#{infile.mtime})" 64 65 if not outfile.exist? 66 needs_update = true 67 new_pages << outfile 68 else 69 if (outfile.mtime < infile.mtime) or generate_all 70 needs_update = true 71 mod_pages << outfile 72 end 73 end 74 75 if needs_update 76 make_page infile, outfile 77 end 78 end 79 80 puts "NEW: #{new_pages.join(' ')}" 81 puts "MOD: #{mod_pages.join(' ')}"