Brackets and then we can get started.
Since we need to create a new file, we will need a require. And then we start with the methods.
Since I like my output to be colorful, I googled and found the classes to create some color.
1# encoding: utf-82require 'fileutils'3
4# for colored output5class String6def red; "\033[31m#{self}\033[0m" end7def green; "\033[32m#{self}\033[0m" end8def brown; "\033[33m#{self}\033[0m" end9def gray; "\033[37m#{self}\033[0m" end10def bold; "\033[1m#{self}\033[22m" end11end
We need different methods so that the generator works a bit faster.
First we have createURl, which replaces all of the german umlaut characters with an equivalent representation and all of the spaces with a hyphen. It looks like this:
1# create File name for URL2def createURL(string)3 string.downcase!4 string.gsub! " ", "-"5 string.encode!("UTF-8")6 string.gsub! "ä", "ae"7 string.gsub! "ö", "oe"8 string.gsub! "ü", "ue"9end
To remove the umlauts, we also create a method
1# replace all ä,ö,ü with Unicode2def umlautify(string)3 string.encode!("UTF-8")4 string.gsub! "ä", "ä"5 string.gsub! "Ä", "Ä"6 string.gsub! "ö", "ö"7 string.gsub! "Ö", "Ö"8 string.gsub! "ü", "ü"9 string.gsub! "Ü", "Ü"10end
And since any colon in yaml within the content can cause problems, we replace them with the unicode representation.
1# replace every colon within yml, so that it still can get parsed correctly2def sanitizeYML(string)3 string.gsub! ":", ":"4end
Since this is going to generate posts we need to work on the date. So first we’ll save some variables. First the time which exactly spits out what we need for the date variable and then we’ll get the year, day and month for the file name. And we’ll also make sure that the numbers below 10 will be prepended with a zero.
1# date2#------3time = Time.new4year = time.year.to_s5
6#if day and month are below 10, add a 0 in front of it7if time.day <= 98 day = "0#{time.day}"9else10 day = time.day.to_s11end12
13if time.month <= 914 month = "0#{time.month}"15else16 month = time.month.to_s17end
As soon as that’s done, we can start the generator.
We start by inserting a initiation message \n
tells it, to start a new line. And we’ll do that in bold and green
1# Generator2#-----------3
4# starting msg5puts "Initating Front Matter Generator for Posts.\nPlease enter the following information as per instruction.\n".bold.green6puts ""
Then we’ll ask for the title and save the input in a variable. We then check the value. If it’s above 55 characters or empty, we will ask once again. If it is not inserted correctly a second time, we’ll abort the program and ask for a restart.
1# Title2puts "Enter post title. It should not be longer than 55 characters".bold.gray3title = gets.chomp4
5if title.length > 556 puts "Your title is too long, please enter a shorter title".bold.brown7 title = gets.chomp8 if title == ''9 abort("Program was aborted, because no valid title was entered. Please restart".bold.red)10 elsif title.length > 5511 abort('Program was aborted, because title was too long. Please restart'.bold.red)12 end13elsif title == ''14 puts "Please enter a title, shorter than 55 characters".bold.gray15 title = gets.chomp16 if title == ''17 abort("Program was aborted, because no valid title was entered. Please restart".bold.red)18 elsif title.length > 5519 abort('Program was aborted, because title was too long. Please restart'.bold.red)20 end21end
Then we ask if the title is equal to the URL part, so the file name. If it is, we’ll set the file name variable to title, else we ask for input. And also if somethings wrong we abort the program.
1# URL Title2puts ""3puts "Is the title going to be equal to the URL Title?\n(yes/no)".bold.gray4url_title_yes_or_no = gets.chomp5
6case url_title_yes_or_no7when "yes", "y"8 file_name = title9when "no", "n"10 puts "Enter the URL title".bold.gray11 file_name = gets.chomp12 if file_name == ''13 puts "Please enter a valid file name".bold.brown14 file_name = gets.chomp15 if file_name == ''16 abort("Program was aborted. Please restart and follow instructions".bold.red)17 end18 end19else20 puts "Please enter either 'yes' or 'no'".bold.brown21 url_title_yes_or_no = gets.chomp22 case url_title_yes_or_no23 when "yes", "y"24 file_name = title25 when "no", "n"26 puts "Enter the URL title".bold.gray27 file_name = gets.chomp28 if file_name == ''29 puts "Please enter a valid file name".bold.brown30 file_name = gets.chomp31 if file_name == ''32 abort("Program was aborted. Please restart and follow instructions".bold.red)33 end34 end35 else36 abort("Program was aborted. Please restart and follow instructions".bold.red)37 end38end
Now that we have the URL title, we’ll transform it into the URL format. For that we use the method we defined before. And then we’ll set the file path. We define the folder and if it does not already exist, we’ll create it.
1# create URL2createURL(file_name)3
4# if there is a folder specified, add it and create a new folder, if it does not already exist5folder = "_posts/"6
7file_format = "markdown"8case file_format9when "md", "markdown"10 file_ending = ".md"11when "html", "HTML"12 file_ending = ".html"13when "txt", "textile"14 file_ending = ".txt"15else16 file_ending = ""17end18
19# set path for file20if folder != nil21 path = "#{folder}/#{year}-#{month}-#{day}-#{file_name}#{file_ending}"22
23 dirname = File.dirname(path)24 unless File.directory?(dirname)25 FileUtils.mkdir_p(dirname)26 end27else28 path = "#{year}-#{month}-#{day}-#{file_name}#{file_ending}"29end
Subtitle and tags are both optional, so we just ask if there is any input, otherwise nothing happens.
1#subtitle2puts ""3puts "Enter subtitle\n(press enter if you don't want to add a subtitle)".bold.gray4subtitle = gets.chomp5
6#tags7puts ""8puts "Enter tags, separate with commas\n(press enter if you don't want to add tags)".bold.gray9tags = gets.chomp
For the description we do the same we did for the title
1# Description2puts ""3puts "Enter post description. It shouldn't be longer than 115 characters".bold.gray4desc = gets.chomp5
6if desc.length > 1157 puts "Your description is too long, please enter a shorter description".bold.brown8 desc = gets.chomp9 if desc == ''10 abort("Program was aborted, because no valid description was entered. Please restart".bold.red)11 elsif desc.length > 11512 abort('Program was aborted, because description was too long. Please restart'.bold.red)13 end14elsif desc == ''15 puts "Please enter a description, shorter than 55 characters".bold.gray16 desc = gets.chomp17 if desc == ''18 abort("Program was aborted, because no valid desription was entered. Please restart".bold.red)19 elsif desc.length > 11520 abort('Program was aborted, because description was too long. Please restart'.bold.red)21 end22end
And then we’re ready to create the file. We’ll execute the methods to clean up the content and then we’ll add it into the file content.
1# create new file2p = File.open( path,"w" )3
4sanitizeYML(title)5sanitizeYML(subtitle)6sanitizeYML(desc)7
8umlautify(title)9umlautify(subtitle)10umlautify(tags)11umlautify(desc)12
13# file content14p.puts "---"15p.puts "date: #{time}"16p.puts "title: #{title}"17if subtitle != ""18 p.puts "subtitle: #{subtitle}"19end20if tags != ""21 p.puts "tags: [#{tags}]"22end23p.puts "description: #{desc}"24p.puts "---"25p.close
At the end we’ll put out the created path and open the file in brackets, executing the command brackets <path>
.
And then the program is done.
1# add notification that file was created2puts "file '#{path}' created".bold.green3puts ""4puts "opening file in brackets".bold.green5
6# add command to open file in brackets7value = `brackets #{path}`
And you can see the whole thing here.