DEV Community

Cover image for Generating image preview easily
suntong
suntong

Posted on

Generating image preview easily

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 and get an idea of what each theme looks like one by one, but that's something I want to avoid totally.

With easgen, 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 and put it somewhere.
  • At the geany/geany-themes folder, do the following:
ls | sed 's/^/  - /; 1s/^/Images:\n/;' | tee /tmp/Images.yaml 
easygen ~+1/Images.tmpl /tmp/Images.yaml | tee README.md
Enter fullscreen mode Exit fullscreen mode

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 and here. PR now taken and fork deleted.

For the full story of my PR, there has been a discussion 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:

As we can see, easgen indeed can be a universal code/text generator, and it is super easy to use. It can do all sorts of things 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 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:

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

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

{{end}}
Enter fullscreen mode Exit fullscreen mode

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:

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

### [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")

. . . 
Enter fullscreen mode Exit fullscreen mode

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}}

Enter fullscreen mode Exit fullscreen mode

The added basename will generate results as such:

### [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")

. . . 

Enter fullscreen mode Exit fullscreen mode

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}}

Enter fullscreen mode Exit fullscreen mode

We will get:

. . . 

### [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")
. . . 
Enter fullscreen mode Exit fullscreen mode

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 easgens built-in functions.

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

Enter fullscreen mode Exit fullscreen mode

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 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, 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

Top comments (0)