DEV Community

sfrunza13
sfrunza13

Posted on

Static Site Gen

Getting started with OSD

The first project we as a class had to do for OSD was kind of an ice breaker to get us into coding in our preferred language. For me I wanted to try something a little bit different and I picked Python due to its popularity in the job market right now, its more abstract and readable code syntax and the sheer amount of libraries and packages it offers for my future dev needs.

As a quick preface to what the project is meant to do baseline, it is meant to take an input .txt file and convert it into a simple .html which is the same text but wrapped in <p> tags to a dist directory.

Having worked before in C++, Java and JavaScript (along with some of its frameworks and libraries) the jump to programming in Python was not too difficult, I started the project by having everything under one big main method but eventually separated the site generation logic into its own class called SSJ.py. The way the program works is it will take an output directory, be it the default one or one the user specifies through the -o option when running the program, and it will check if it can delete the directory and its contents and then it will create it again.

Afterwards the mandatory -i option for input is checked, if it is a single text file it will be treated as such by the program and if there is multiple newlines after the first line it will be parsed as a H1 and the title will be changed to match the first line's contents, otherwise everything will be parsed as HTML paragraphs and inserted into a template HTML stored as a class variable in SSJ. The template is stored in a temp variable, changed, written to the output .html file and then the temp variable's original value is re-assigned to the class variable template to be re-used for the next file if it needs to be.

If the -i option specifies a directory then the program will iterate through the files within and find the ones with a .txt extension and add them as anchor tags to an index.html, then it will send them to the function in the self-same SSJ.py class that processes single files into their .html targets.

I learned the analogous syntax for concepts I knew in other programming languages and that was fun, although this is just the very beginning of my Python journey it seems like everything is really easy and convenient on the surface because of the awesome methods you can perform on arrays and strings out of the box and how all the memory seems to be managed for me like it was in Java. However I am not naïve enough to really believe it's going to be that simple to be proficient in Python, I am sure that in more complex programs having the language not be hard typed could lead to weird problems with implicit casts or some wonky behavior and I am too green to really know much about optimization in terms of performance and how language difference factors into that so I don't know how pros generally view Python in that regard. So far, I am having fun with it.

My Implementation

So, I am going to talk about the features of my implementation of the Site Generation project. Lets start with the table from my git's README:

ShortCut LongOption Result
-v --version Displays name and version of program
-h --help Displays a help message with useful information about program and possible options
-i --input Specify an Input directory or file only .txt suffix will be correctly parsed (requires argument)
-o --ouput Specify a name for output directory (optional argument)

As you can see you can check the version, a help text, you must select an input and an optional output directory otherwise it will default to ./dist.

Afterwards as I mentioned the program will create an index.html for directories with .txt files within them and add links to the generated .html files. It will generate the .html files treating empty lines in the .txt files as delimiters to create new paragraph tags in the HTML. It will attempt to parse a title from the first line if there are 2 or more empty lines that follow it, and wrap it in an H1 tag.

Here is the github repo to my project: https://github.com/sfrunza13/SiteGenerationTool

Here is an example of it running with the sherlock test directory as input and a directory that does not exist upon running as output, the myOutput dir:

Image description

Image description

Image description

You can see the index file replaces the spaces in file names with %20 so that it can work as a href:

Image description

Image description

Issues

I highlighted some of my experiences with issues and closing my first issue filed under the git system in a previous blog post: https://dev.to/sfrunza13/collaborating-1mno

That being said I did have another problem where I was getting a unicode error character, I found out after some trial and error that it was because I opened the file to read from as utf-8 encoded but when opening the file to write the .html to I did not specify encoding and that messed it up a bit. Once I specified utf-8 encoding there as well all was well.

Conclusion

It was fun to do a project where code was not supplied for us, more often than not in our previous web courses for example large sections of code are provided for us to use as a very, sometimes VERY generous starting point. In this project it was us on our own, and then together, and it was different and fun. I am excited to see what's next in this course.

Top comments (0)