Generating image preview easily
Tags: easygen, codegen
Published: 2019-06-22 14:06:59

---
title: Generating image preview easily
published: true
description: Using easygen to generating screenshots gallery 
cover_image: https://i.stack.imgur.com/1e89d.jpg
tags: easygen,codegen
---

### Background

For a long time, `geany` provides only screenshots of their themes but not preview of them all. I know that I could [browse through them](https://github.com/geany/geany-themes/tree/master/screenshots) and get an idea of what each theme looks like one by one, but that's something I want to avoid totally.

With [`easgen`](https://github.com/go-easygen/easygen), this is so trivial that the preview can be done within seconds. (More about `easgen` at the end of this blog) 

### TL'DR

This is what I did, and how you can replicate that. 

- Grab a copy of [Images.tmpl](https://gist.github.com/suntong/3a31faf8129d3d7a380122d5a6d48ff6#file-images-tmpl) and put it somewhere.
- At the `geany/geany-themes` folder, do the following:

```sh
ls | sed 's/^/  - /; 1s/^/Images:\n/;' | tee /tmp/Images.yaml 
easygen ~+1/Images.tmpl /tmp/Images.yaml | tee README.md
```

That's it, super easy, and we're done. The image preview gallery is ready to serve (after you `git push` of course). 

~~At the time of the writing, my PR hasn't been accepted, so you can easily see the difference, by browsing/comparing to [here](https://github.com/geany/geany-themes/) and [here](https://github.com/suntong/geany-themes/tree/master/screenshots#darculapng)~~. PR now taken and fork deleted.

For the full story of my PR, there has been [a discussion](https://github.com/geany/www.geany.org/issues/5) on creating a themes gallery on-the-fly using JS or some server-side language. That however, requires extra resource (a public site that host it), and extra time (in coding such frontent/backend implementation). I am not objecting server-side implementation, but just want to show a simple solution, without requiring anything _extra_. 


### Customization

OK, here is what really happened when executing the above two commands:

{% gist https://gist.github.com/suntong/3a31faf8129d3d7a380122d5a6d48ff6 file=steps.md %}

As we can see, [`easgen`](https://github.com/go-easygen/easygen) indeed can be a universal code/text generator, and it is super easy to use. It can do [all sorts of things](https://github.com/go-easygen/easygen#details) that you could image, including code auto-generating, which is what I'm heavily relying on at work recently. 


As explained in the [Getting Started](https://github.com/go-easygen/easygen/wiki/Getting-Started) guide, all you need is a `.yaml` to drive the text/code generation, and a `.tmpl` template file guiding how to represent the given data. 

For this image gallery task especially, [this is what the `.tmpl` template file looks like](https://gist.github.com/suntong/3a31faf8129d3d7a380122d5a6d48ff6/44c4e7fa81592f917fffacf689dd76f469ca954c):

```
{{range .Images}}
### [{{.}}]({{.}})

![{{.}}]({{.}} "{{.}}")

{{end}}
```

Of course, to those people who are not familiar with the powerful Go Template Engine, this looks a bit too cryptic. So I changed it to something more friendly:

{% gist https://gist.github.com/suntong/3a31faf8129d3d7a380122d5a6d48ff6 file=Images.tmpl %}

That's more look like a programming language now. And we have seen the results above. Showing again for comparisons for next steps:

```md
### [bespin.png](bespin.png)

![bespin.png](bespin.png "bespin.png")


### [black.png](black.png)

![black.png](black.png "black.png")


### [darcula.png](darcula.png)

![darcula.png](darcula.png "darcula.png")

. . . 
```

The `easgen` comes with many useful built-in functions to manipulate the given data/text. For example, to remove file extension from the title, we can use this template:

```
{{range .Images}}{{$img:=.}}
### [{{basename $img}}]({{$img}})

![{{$img}}]({{$img}} "{{$img}}")

{{end}}

```

The added `basename` will generate results as such:

```md
### [bespin](bespin.png)

![bespin.png](bespin.png "bespin.png")


### [black](black.png)

![black.png](black.png "black.png")


### [darcula](darcula.png)

![darcula.png](darcula.png "darcula.png")

. . . 

```

The file extensions in the titles are now removed. 

This is only a tip of the iceberg. Let me add another one to showcase the  Go Template Engine's powerful piping functionality, and `easgen` ability to tweak variable names to whatever you like. With the following template:

```
{{range .Images}}{{$img:=.}}
### [{{$img | basename | clk2ss}}]({{$img}})

![{{$img}}]({{$img}} "{{$img}}")

{{end}}

```

We will get:

```md
. . . 

### [SOLARIZED_DARK](solarized-dark.png)

![solarized-dark.png](solarized-dark.png "solarized-dark.png")


### [SOLARIZED_LIGHT](solarized-light.png)

![solarized-light.png](solarized-light.png "solarized-light.png")


### [SPYDER_DARK](spyder-dark.png)

![spyder-dark.png](spyder-dark.png "spyder-dark.png")
. . . 
```

Note that the string `solarized-dark.png` has been transformed into `SOLARIZED_DARK`, and `solarized-light.png` to `SOLARIZED_LIGHT`, so on and so forth. 

Check [here for `easgen`s built-in functions](https://github.com/go-easygen/easygen/issues/25).

This technique will not only apply to geany themes, but can be anywhere. This is what I did to my own project, that A) has a mix of files, and B)  has a `README.md` file already.


```
ls *.gif *.jpg *.png | sed 's/^/  - /; 1s/^/Images:\n/; ' | tee /tmp/Images.yaml 
easygen ~+1/Images.tmpl /tmp/Images.yaml | tee ImagePreview.md

```

And because that the template is super simple/easy, you can be as fancy as you want -- like adding the image size next to its title, just for an example. 


### Takeaway

With [`easgen`](https://github.com/go-easygen/easygen) any text generating tasks will be super easy, both in terms of data preparation, and in terms of how easily to express the representation too. 

### PS:easgen

The `easgen` is now in Debian repo as a standard Debian package -- https://tracker.debian.org/pkg/easygen. Just that, because of Debian freeze at the moment, it is only available in `sid` for now, but will be in `test`, `stable` and in Ubuntu eventually. If you need it now, get the [easygen binary from Debian repo](https://packages.debian.org/unstable/easygen), or grab and compile the Go source. It has minimum dependencies, and very small footprint. 

Enjoy. 

### PPS:credit

The title image was duplicated from [here at gstatic.com](https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcTj60o2oVTi_RU01ss54iOYnUdX4Lprb7tqkI7NSgmQlFzFcl7e)