I have recently decided to move Infinus Electronics over to Hugo, as WordPress with its weird styling issues and super hard to understand file structure made it way too frustrating to debug and customise. So far, working with Hugo has been a blast, and having worked with frameworks like Laravel and Flask before, the whole organisation of pages by file hierarchy has been a lot more intuitive.
And transparent.
The issue
My first post written on this new platform is the Battery Powered +48V Phantom Power Supply , which of course, had to show a schematic from KiCad.
Now, of course, one could just embed the SVG generated by KiCad using the figure
shortcode or something similar. The thing is, that sticks a bright, honking blob smack in the middle of my slick dark mode theme, and I wasn’t about to ruin this aesthetic just yet.
The solution was to find a way to directly embed the KiCad SVG into the site, then somehow use some CSS class shenanigans to modify the colors when the theme switches.
The solution
Clean up the SVG in Adobe Illustrator
This is very important, since generally, the schematics directly off KiCad have a way oversized background as KiCad just exports the entire sheet, and for whatever reason Hugo does weird stuff the the SVG coordinates if you try to embed the SVG without this step. Also, due to the units and sizes used, the schematic overflows hopelessly.
Export the SVG from KiCad, and be sure to:
- Use the KiCad Default color scheme
- Export with background to avoid the final file being transparent
Then, resize the artboard and background to an appropriate size in Illustrator.
Export the file as SVG, using the Export As function and not just plain saving, although just directly saving it should work too.
In the Export As dialog:
- Select Internal CSS for the styling. This is paramount, as it creates style tags with CSS variables that we can easily modify
- The other settings shouldn’t really matter, but you probably want to check Minify as well
Change all the color values to CSS variables
This allows us to easily modify them by adding the appropriate classes for both the dark and the light themes to a stylesheet.
It changes all the original color hex values to variables that we can modify from somewhere else
Here is the Python script to do so:
|
|
When you run this script, a prompt appears. Select your SVG file, and the script should handle the rest.
Note that doing so will cause the SVG to not be openable in any app, since all the color information is effectively removed.
Please do tinker with this script, I wrote it in a hurry based off this other guide of mine on including Jupyter notebooks in Wordpress , so it’s super hacky and crude. Yes, I copied a lot of it from various online sources.
schematic.css
Note: I am using the PaperMod theme for Hugo. If you’re using something else, the original class names might be different, so you will need to do some sleuthing
Next, we need to create a stylesheet to tell the browser what colors to actually use. Here is where you can pat yourself on the back, and have some fun (or not) in picking colors that you like.
Under \assets\css\extended
create a new file schematics.css
(file name does not matter, call it whatever you like)
Then, this is what I have for this site (at the time of writing):
|
|
The svg
and .dark svg
classes each define the colors that show up on the schematic itself. This is where you would make adjustments to tailor the schematic according to your liking.
The other stuff is a hacky zoom on hover thing that I just slapped together. It is quite bad, so it’ll likely not remain for much longer. I thought I’d include it here just because. I also adjsuted the margins so that everything would remain consistent.
KiCad schematic shortcode
In Hugo, the way you insert functions that do cool stuff into your .md
files is by using shortcodes.
Here is one I came up with to insert a KiCad Schematic SVG file that has been processed:
|
|
Basically what this does, is it creates a div
element with the class name kicad-schematic
, which makes sure the right CSS classes get applied. Then, it directly embeds the content of the SVG file as a child of the div
, after passing it through safeHTML
to avoid escaping all the quote marks and brackets and stuff.
Usage
To use it, in your page .md
file, insert the shortcode as follows:
{{< kicad_schematic "path_to_svg" >}}
So in the case of the Battery Powered +48V Phantom Power Supply page bundle , the code was:
{{< kicad_schematic "Phantom Power Supply.svg" >}}
Fun aside: if you want to embed Hugo shortcodes without actually running them, comment out the innards of the pointy brackets.
So like the above examples, I had {{~</* kicad_schematic "Phantom Power Supply.svg" */>}}
, but without the tilde (~). The tilde is there because if it were not, Hugo removes the /*
and you’d be really confused indeed.
Conclusion
And there we have it, all that work, for a schematic that changes color in dark mode…
Todos
There are still some stuff that I’d like to implement, and I’d update this post when I get around to implementing them:
- Write a script so that people without Adobe Illustrator can do the same thing.
- Right now, the image is really, really small; not great for readability. I want to implement a less hacky hover zoom.
- Add a button under the embedded SVG that would allow someone to download a raw file that has not been modified by the Python script.
- Maybe get Hugo to do the conversion automatically? Like how it processes images.
- Extend this functionality to implement a KiCad PCB viewer that allows the user to toggle each layer on and off, that’d be epic!