Fake Jekyll posts generator script

With this fake Jekyll posts Python generator script and example you can fill your Jekyll site fast to test how it looks. Real easy to follow guide.

Fake Jekyll posts generator script

I needed this because I’m creating a new Jekyll this, actually this jekyll site. I love creating my own themes but that really works best when you have some posts to test the site with. So I created this fake posts for jekyll script.

So, since a year or so I became a bit of a Python lover. I’ve been developing whole my life in many many languages, Basic, ASP (classic), ASP.NET, VB, C#, Bash, Javascript, ABAP, LotusScript, Lotus Notes Formula, PDF Interactive Forms script, Actionscript, PHP…. well you get the idea. As it once was my profession I still like to develop something every day.

Next to that I’ve always liked blogging. Just writing about stuff online on one of my many blogs. Since I really have been writing a lot of Python code since I’ve been addicted to this language I’ve been thinking about putting a blog online for this. And that is where itheo.tech saw the daylight.

Content management System

I have been using many Content Management Systems since I’v been blogging, My Own written, Mambo, Joomla, Blogger.com, WordPress, once again my own written but somehow the existing systems became slow over time and even how secure I mady my Joomla or WordPress sites, hackers liked my sites very much.

Since I’ve written my sites into Jekyll blogging became easier for me. Not much updating, not much worrying about hackers, it’s blazing fast and it has not much overkill with functionality. Together with Github pages I really found a good money for value option to host my sites and work on github for my code.

Jekyll Posts Python generator

Now, since I’m a code lover I wanted to create a clean Bootstrap 4 theme for my jekyll site. I saw this theme  and I liked the simpleness of it but I wanted to make it my own. So I’ve rewritten it in a very lightweight Bootstrap 4 theme. (still shaving the raw edges).

There was only one thing I needed to test my theme. Posts! With posts I would be able to test an overview page, pagination, category and tags page and a blog article page.

Now, I could of course create a dozen of fake posts, with lorum ipsum text, images and headers. But would it not be easier to have a Jekyll fake posts generator python script doing this for me?

A Fake Posts generator for creating an infinite list of posts (also to test performance when you have like a 1000 of more posts.

Jekyll fake posts generator

So I’ve created this Jekyll fake posts generator. Just for the fun of it and for automating stuff I don’t want to do manually.

This Jekyll fake post generator creates posts, fake posts. It creates posts beween two years, with a fake title, with a fake data, with fake tags, fake categories and fake text.

This generator uses Lorum ipsum and place holder images fill the page with headers, sections and images.

Have fun using it! And if you have any questions, let me know!

The whole fake jekyll posts python generator script

'''
author: Theo van der Sluijs
url: https://itheo.tech
copyright: CC BY-NC 4.0
creation date: 11-12-2018

This script uses the "md_data" folder to save the files in to
Make sure this folder exists where your run this script

Fake images by : https://imgplaceholder.com/

You need the lorum generator for this.
https://pypi.org/project/lorem/
Please install by :
pip install lorem
'''
from datetime import datetime
import random

import lorem
from lorem.text import TextLorem

class generateJekyllPosts:

    def __init__(self, number=10, folder="md_data/", tags=['one'], categories=['animals']):
        """

        :param number: number of posts to generate default 10
        :param folder: folder to place new posts into default md_data/
        :param tags: tags you want to use in post (random) default one
        :param categories: categories  you want to use in post (random) default animals
        """
        self.number = number
        self.filefolder = folder
        self.tags = tags
        self.categories = categories

        for x in range(0, self.number):
            self.generate()

    def generate(self):
        """
        function to create the fake text and save files
        """
        datum = self.fake_date(2015, 2018)
        lrm = TextLorem(wsep='-', srange=(3, 4))
        file_title = lrm.sentence()
        title = file_title.replace("-", " ")
        file_title = file_title.replace(".", "")

        data = {}

        data['datum'] = datum
        data['filename'] = "{}{}-{}.md".format(self.filefolder, datum, file_title)
        data['title'] = title

        str = "---\n"
        str += "layout: post\n"
        str += "title: {}\n".format(title)
        str += "date: {}\n".format(datum)
        str += "img: {}\n".format(self.rand_image())
        str += "tags: [{}]\n".format(random.choice(self.tags))
        str += "categories: [{}]\n".format(random.choice(self.categories))
        str += "excerpt_separator: <!--more-->\n"
        str += "---\n\n"

        str += self.gen_section(1)
        str += self.gen_image()
        str += self.gen_section(2)
        str += self.gen_section_title(3)
        str += self.gen_section_title_image()

        data['text'] = str

        self.save_file(data)

    def rand_image(self, size="1024x512"):
        """
        function to create random image (by using glyphicons
        :param size: default 1024x522
        :return: str image url
        """
        icons = ['glyphicon-asterisk', 'glyphicon-plus', 'glyphicon-eur', 'glyphicon-euro', 'glyphicon-minus',
                 'glyphicon-cloud', 'glyphicon-envelope', 'glyphicon-pencil', 'glyphicon-glass', 'glyphicon-music',
                 'glyphicon-search', 'glyphicon-heart', 'glyphicon-star', 'glyphicon-star-empty', 'glyphicon-user',
                 'glyphicon-film', 'glyphicon-th-large', 'glyphicon-th', 'glyphicon-th-list', 'glyphicon-ok',
                 'glyphicon-remove', 'glyphicon-zoom-in', 'glyphicon-zoom-out', 'glyphicon-off', 'glyphicon-signal',
                 'glyphicon-cog', 'glyphicon-trash', 'glyphicon-home', 'glyphicon-file', 'glyphicon-time',
                 'glyphicon-road', 'glyphicon-download-alt', 'glyphicon-download', 'glyphicon-upload',
                 'glyphicon-inbox', 'glyphicon-play-circle', 'glyphicon-repeat', 'glyphicon-refresh',
                 'glyphicon-list-alt', 'glyphicon-lock', 'glyphicon-flag', 'glyphicon-headphones',
                 'glyphicon-volume-off', 'glyphicon-volume-down', 'glyphicon-volume-up', 'glyphicon-qrcode',
                 'glyphicon-barcode', 'glyphicon-tag', 'glyphicon-tags', 'glyphicon-book', 'glyphicon-bookmark',
                 'glyphicon-print', 'glyphicon-camera', 'glyphicon-font', 'glyphicon-bold', 'glyphicon-italic',
                 'glyphicon-text-height', 'glyphicon-text-width', 'glyphicon-align-left', 'glyphicon-align-center',
                 'glyphicon-align-right', 'glyphicon-align-justify', 'glyphicon-list', 'glyphicon-indent-left',
                 'glyphicon-indent-right', 'glyphicon-facetime-video', 'glyphicon-picture', 'glyphicon-map-marker',
                 'glyphicon-adjust', 'glyphicon-tint', 'glyphicon-edit', 'glyphicon-share', 'glyphicon-check']

        return "https://imgplaceholder.com/{}/cccccc/757575/".format(size,random.choice(icons))


    def gen_image(self):
        """
        function to create image part
        :return: str markdown image
        """
        lrm = TextLorem(wsep=' ', srange=(2, 3))
        text = lrm.sentence()
        text = text.replace(".", "")
        img = self.rand_image()
        return "![{}]({})\n".format(text, img)

    def gen_section(self, sections=1):
        """
        function to create sections
        :param sections:  default 1
        :return: str section
        """
        str = ""
        for x in range(0, sections):
            str += "{}\n\n".format(lorem.paragraph())
        return str

    def gen_section_title(self, sections=2):
        """
        Function to create header with sections
        :param sections: number of sections default 2
        :return: str header with sections
        """
        str = self.gen_title("##")
        for x in range(0, sections):
            str += "{}\n\n".format(lorem.paragraph())
        return str

    def gen_title(self, header="##"):
        """
        Function to create header
        :param header: default ##
        :return: str header
        """
        lrm = TextLorem(wsep=' ', srange=(2, 3))
        return "{} {}\n".format(header, lrm.sentence())

    def gen_section_title_image(self, sections=2):
        """
        function to create lorem ipsum section with header and image
        :param sections: number of sections default 2
        :return: string with header, image and sections
        """
        str = self.gen_title("##")
        str += self.gen_image()
        str += self.gen_section(sections)
        return str

    def fake_date(self, startyear=1975, endyear=2018):
        """
        function to create fake date
        :param startyear: int
        :param endyear: int
        :return: date like "1975-05-14"
        """
        year = random.choice(range(startyear, endyear))
        month = random.choice(range(1, 13))
        day = random.choice(range(1, 29))
        datum = str(datetime(year, month, day))
        return datum.replace(" 00:00:00", "")

    def save_file(self, data=None):
        """
        saves MD file
        :param data: dict with minimal filename and output text
        :return: false if data is None
        """
        if data is None:
            return False

        with open(data['filename'], 'a') as out:
            out.write(data['text'])


if __name__ == '__main__':
    nr_posts = 10
    output_folder = "md_data/"
    tags = ['summer', 'winter', 'europe', 'geme-consoles']
    categories = ['blog', 'travel', 'tech', 'livestyle']

    g = generateJekyllPosts(nr_posts, output_folder, tags, categories)