Create content from a bash script

I noticed @cblte commenting on a post that is 3 years old now, and I went to take a look at how much my script to generate content changed since then.

Here are the useful bits in case someone needs a similar script:

GREEN=$(tput -T screen setaf 2)
POWDER_BLUE=$(tput -T screen setaf 153)
NORMAL=$(tput sgr0)

if [[ $1 = "story" ]]; then
	## Usage
	# `new STORY TITLE-with-dashes`
	# `new story section section-name`

	if [[ $2 = 'section' ]]; then
		hugo new story/"$3"/
		open content/story/"$3"/
			export STORY=$2
			export SLUG=$3
			DATE=`date "+%Y-%m-%d"`

			hugo new story/$STORY/$DATE-$SLUG/

			export DESTINATION="content/story/$STORY/$DATE-$SLUG"

			mkdir -p $DESTINATION/gallery;
			mkdir -p $DESTINATION/images/;


			printf "\n$DESTINATION\n";


if [[ $1 = "post" ]]; then
	export SLUG=$2
	DATE=`date "+%Y-%m-%d"`

	hugo new post/$DATE-$SLUG/

	mkdir -p content/post/$DATE-$SLUG/gallery;
	mkdir -p content/post/$DATE-$SLUG/images/;

	open content/post/$DATE-$SLUG/; $EDITOR content/post/$DATE-$SLUG/; cd content/post/$DATE-$SLUG/;

# This was a special project that needed a new page every day
# Usage: `./new pagina-tantas`
if [[ $1 = "paginas-tantas" ]]; then
	printf "\n ${POWDER_BLUE}### Creating files ###\n${NORMAL}"
	FILE_EN=`date +%Y-%m-%d`
	FILE_PT=`date +%Y-%m-%d`

	hugo new stories/paginas-tantas/$FILE_EN/;
	hugo new stories/paginas-tantas/$FILE_PT/;

	sed -i'' -e 's/draft\: true/draft\: false/g' content/story/paginas-tantas/$FILE_EN/;
	sed -i'' -e 's/draft\: true/draft\: false/g' content/story/paginas-tantas/$FILE_PT/;

	sed -i'' -e 's/stories\:/stories\: \[\"paginas-tantas\"\]/g' content/story/paginas-tantas/$FILE_EN/;
	sed -i'' -e 's/stories\:/stories\: \[\"paginas-tantas\"\]/g' content/story/paginas-tantas/$FILE_PT/;

	rm content/story/paginas-tantas/$FILE_EN/
	rm content/story/paginas-tantas/$FILE_PT-e/

	printf "\n ${POWDER_BLUE}### Opening files ###\n${NORMAL}"
	$EDITOR content/story/paginas-tantas/$FILE_PT/;
	open content/story/paginas-tantas/$FILE_EN/;

# I think Hugo now does this on it's own.
if [[ $1 = 'undraft' ]]; then
	DATE=`date "+%FT%TZ"`

	gsed -i "s/date:.*$/date: ${DATE}/g" $2;
	gsed -i "/draft: true/d" $2;

# Send it to github
if [[ $1 = 'publish' ]]; then
	if [[ $2 ]]; then
		commit_message="novo post"
	git add --all ; git commit -am "$commit_message" ; git push;

if [[ $1 = 'deploy' ]]; then

	printf "\n${GREEN}### Pushing changes and building on the other side ###\n${NORMAL}"
	git push && ssh -t DeLorean 'cd Digital-Insanity; ./'

if [[ $1 = 'help' ]]; then
	echo "${GREEN}post : ${NORMAL}creates a new post, sintax is :: post TITLE-with-dashes"
	echo "${GREEN}story : ${NORMAL}creates a new post inside a story, sintax is :: story STORY-NAME TITLE-with-dashes"
	echo "${GREEN}paginas-tantas : ${NORMAL}creates a new post insite the paginas-tantas story"
	echo "${GREEN}publish : ${NORMAL}adds all files to git and commits the changes to the current branch"
	echo "${GREEN}deploy : ${NORMAL}pushes any commits to origin and connects to the DeLorean server to run the build script"

In archetypes, if I want to get the title from an env variable, I make sure to export it
export SLUG=$3
And to fetch it replacing the dashes:
title: "{{ replace (getenv "SLUG") "-" " " | title }}"

Hope this helps someone.


Thank you even more sharing a new version of the script.
I actually did not recognize how old the post was, as I was searching in all for “bash script”.
I modified it a litle bit to my needs, as I want my posts in a folder ‘blog’ with a subfolder of ‘year-month’.

So my part of your ‘post’ block looks now like this:

    if [[ $1 = "blog" ]]; then
	# blog
	export SLUG=$2
	DATE=`date "+%Y-%m"`

	hugo new blog/$DATE/$SLUG/

	# mkdir -p content/post/$DATE-$SLUG/gallery; # dont need a gallery
	mkdir -p content/blog/$DATE/$SLUG/images/;

	$EDITOR content/blog/$DATE/$SLUG/; cd content/blog/$DATE/$SLUG/;

All I actually need is a little helper to create a new page bundle for a blog post. Currently, migrating a lot of content from my Ghost blog to Hugo.

Oh I should mention, I am using gitbash on windows

1 Like

That first bit for creating new content is mostly the same, I just kept adding “modules” as I needed them. :slightly_smiling_face:

Lately I have been using python to make the whole thing easier to read. Then I just run hugo with this:

args = ("/usr/local/bin/hugo", "-d", website_path,"--cacheDir", path)
popen = subprocess.Popen(args, stdout=subprocess.PIPE, universal_newlines=True)