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.
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:
To remove the umlauts, we also create a method
And since any colon in yaml within the content can cause problems, we replace them with the unicode representation.
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.
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
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.
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.
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.
Subtitle and tags are both optional, so we just ask if there is any input, otherwise nothing happens.
For the description we do the same we did for the title
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.
At the end we’ll put out the created path and open the file in brackets, executing the command brackets <path>.