Skip to content

Instantly share code, notes, and snippets.

@usure
Last active December 31, 2015 00:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save usure/7905130 to your computer and use it in GitHub Desktop.
Save usure/7905130 to your computer and use it in GitHub Desktop.
it works
#!/usr/local/rvm/rubies/ruby-2.0.0-p247/bin/ruby
require 'fileutils'
require 'optparse'
require 'redcarpet'
current_time = Time.now
@pdir = "posts"
@output = "/var/www/blog/posts"
@template = "<center>Title:
<br>Date: **#{current_time}**
</center>"
files = Dir.entries("#{@pdir}").select { |f| File.file?(f) }
options = {}
OptionParser.new do |opts|
opts.banner = "Usage: blog.rb [options]"
opts.on("-n", "New file") do |v|
options[:verbose] = v
@filename = ARGV.to_s
@filename = @filename.gsub("[","").gsub("]","").gsub('"',"")
FileUtils.touch("#{@pdir}/#{@filename}")
File.open("#{@pdir}/#{@filename}", 'w') { |file| file.write(@template) }
end
opts.on("-p", "Parse") do |v|
options[:verbose] = v
Dir.glob("#{@pdir}/*") do |post_file|
#puts post_file
text = File.read(post_file)
markdown = Redcarpet.new(text)
new = post_file.gsub("posts/","")
File.open("#{@output}/#{new}.html", 'w') {|f| f.write(markdown.to_html) }
end
end
opts.on("-u", "Update Index") do |v|
options[:verbose] = v
Dir.glob("#{@output}/*") do |post_link|
post_link = post_link.gsub("/var/www/blog/","")
File.open('/var/www/blog/index.html', 'w') { |file| file.write("<center><a href=\"#{post_link}\">#{post_link}</a></center>") }
end
end
end.parse!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment