Note: This post is originally posted here.
Have you ever thought:
If I just had a variable in my ugly HTML… So I wouldn’t have to repeat all those Bootstrap classes and could change them at one place…
Sbt-hepek to the rescue
So, after some thinking, I decided to write my first sbt plugin just for that (Sbt is a Scala build tool, like Maven or Gradle in Java ecosystem).
Note: This is not a standard template-based, special-language static site generator like Jekyll and others. It is a completely different approach, namely, we use Scala`object`s to represent pages.
We can use some of Scala’s features to our advantage:
- Scala has singleton object as a built-in feature. Every object that extends `Renderable` trait will be rendered to a file. You can specify a default extension etc.
- Also, Scala/Java packages can be used to represent file system structure! For example, if an `object` is in a `package mysite.posts` it could be rendered to a “mysite/posts” folder! Cool, right? :)
The meat of your page could look like this:
Ideas
Since we use a full-featured programming language, we can do many things that are not possible (or not as nice) with custom-made template languages like Markdown. These are some ideas that are already implemented.
Templates
This one is obvious, to represent a template you can use `trait`s. Then you make an `object` that extends it and implement(`override`) only the methods that are `abstract`. Scala’s trait methods can have default implementation so we can exploit that also.
Relative paths
Another thing I thought about was error-prone relative paths in HTML, like `../js/something.js`. We can make a method `def relTo(other: RelativePath): String` that will “calculate” it for us. Then a relative link looks like `Index.relTo(MyOtherPage)`, or from `Index` object itself `this.relTo(MyOtherPage)`. Very intuitive IMHO.
Sections
This one is “stolen” from Li Haoyi’s Scalatex. Idea is very simple, structure your content inside sections so you can render them like chapters in a book. Also, you can make a nice Table Of Contents! :)
Example site
For a full-blown example, please take a look at my repo alive @ blog.sake.ba.
It has a `core` project where the templates are (so if I decide to make another site, I could reuse it), and `sake-ba-blog` where the content is.
Conclusion
IMHO, this is a very powerful approach for static sites and all kinds of static stuff, like XML or JSON. Since there is no parsing we can incrementally render files by examining class dependencies. E.g. if we change a template we want all `Renderable`s to be rendered again, but if we change just the content of a page only that page should be rendered. See hepek-core for implementation details.
This approach can be implemented in other languages that have singleton objects as first-class sitizens, like Kotlin and others.
Opinions, suggestions are welcome! :)
