Making of an index page

Continuation of static site generator….

Each and every blog has an index page that contains 5-6 posts of your blog posts, like wordpress has 10 posts in the index page/ Home page by default. I used to wonder how wordpress shows only the last 10 of my posts in its index page. And yes finally I’ve figured out the way 😉 . We are working on static site generator/ static blog generator in dgplug for the last 10 days and on the last Wednesday kushal shoot an assignment of making an index page to us. His question was a bit confusing, all the folks of dgplug were really confused. Because we were not thinking what kushal was thinking, so he decided to elaborate more on this Monday.

But I couldn’t wait for 3-4 days to solve the assignment, thus guessing what kushal was thinking I finally did something and kushal finally said that it matched with at least what he thought. kushal guided me with what else I have to include in the script and I followed him. Hence I was almost done with the index page of static site generator. Let me share with you what I have done 🙂

from markdown2 import Markdown
import os

def index_page(source_path, destination, N):
    files = []
    for f in os.listdir(source_path):
        if f.endswith('.md'):
            files.append(f)
    return files
    with open(destination, 'w') as d:
        for source in files[:N]:
            with open(source, 'r') as s:
                smd = s.read()
            markdowner = Markdown()
            dest = markdowner.convert(smd)
            d.write(dest)

index_page('source_dir_path', 'index.html', 2)

This script searches for all the .md / markdown files in the source directory and makes a list of it. Then converts it to html as needed. According to this script the index page is having 2 posts.

Thanks for your time :). Can’t wait to contribute a project on static site generator in my github.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s