Menu

I built a 37-line static site generator. Then I ran it on real markdown.
📰

I built a 37-line static site generator. Then I ran it on real markdown.

DEV Community: beginners·Marcus Beauregard·17 days ago
#VrXNC5cW
#devtobeginners#devto#dev#line#bold
Long article (846 words) — no AI summary yet.
Reading 0:00
15s threshold

I have a small pile of side projects, and one of them is staticgen : a static site generator in Python with no dependencies at all. The goal was not to replace Hugo. The goal was to understand it.

The first version is 37 lines. It reads a folder of markdown, turns it into HTML, and drops it into a template:

def render ( post_md , template ) : lines = post_md . strip ( ) . split ( "\n" ) out = [ ] for line in lines : if line . startswith ( "# " ) : out . append ( "<h1>" + line [ 2 : ] + "</h1>" ) elif line . startswith ( "## " ) : out . append ( "<h2>" + line [ 3 : ] + "</h2>" ) else : line = line . replace ( "**" , "<strong>" , 1 ) . replace ( "**" , "</strong>" , 1 ) out . append ( "<p>" + line + "</p>" ) body = "\n" . join ( out ) title = lines [ 0 ] . lstrip ( "# " ) . strip ( ) return template . replace ( TITLE , title ) . replace ( BODY , body )

Enter fullscreen mode

Exit fullscreen mode

It worked on the one test post I wrote.…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.