Hugo context

What we did and what we will do? Previously, we were successfully able to render our home, single and list pages. When we started templating, we looked into a term called, context. We worked with site context and page context and I assumed they are just like references. This post is meant to explore more on context and get a deeper understanding. Context The Context is the object available to you at anytime. This means, if you are in a single page then the context available to you will be the contents of the page, it's title and other related information. On one of the list pages, the context will contain a collection of items. Title and Contents There are some page related contexts like .Title and .Contents which can be used form any one of the layouts. I can change the index.html or single.html or list.html and print the title and contents. I do not have any content in my home page and lists pages right now, so the body will be blank. But for, any one of the blogs we can check the contents being rendered. <html> <head> <title> {{ .Title }} </title> </head> <body> {{ .Content }} </body> </html>curl http://localhost:1313/posts/designing-my-own-theme-in-hugo-ii/ | head -n 10<html> <head><script src="/livereload.js?mindelay=10&amp;v=2&amp;port=1313&amp;path=livereload" data-no-instant defer></script> <title> Designing my own theme in Hugo - II </title> </head> <body> <h2 id="what-we-did-and-what-we-will-do">What we did and what we will do?</h2> <p>This is a part blog in which I am documenting my learning process of creating my own theme in Hugo. Previously, we went through the basic flow of creating a theme.Accessing the site contexts If you want to access the site related information, like the title for your site or any property defined for your site in config.toml, you can use the .Site context. Basically, each page contains a data related to your site inside {{ .Site }} object. Changing the title to {{ .Site.Title }} started displaying me the site's name, which is Shubham's corner. <html> <head> <title> {{ .Site.Title }} </title> </head> <body> {{ .Content }} </body> </html>curl http://localhost:1313/posts/designing-my-own-theme-in-hugo-ii/ | head -n 10<html> <head><script src="/livereload.js?mindelay=10&amp;v=2&amp;port=1313&amp;path=livereload" data-no-instant defer></script> <title> Shubham&#39;s corner </title> </head> <body> <h2 id="what-we-did-and-what-we-will-do">What we did and what we will do?</h2> <p>This is a part blog in which I am documenting my learning process of creating my own theme in Hugo. Previously, we went through the basic flow of creating a theme.Change of contexts There are certain functions which changes the current context. If we want to iterate over all the pages in page list. Then you use range method. The range method provides you an iterator for you collection. If I want to display the title of all the collections in my posts. I can provide a range block as follows. <body> {{ range .Pages }} {{ end }} </body>.Pages provides me a collection of page objects for each of the pages in the list. In between the blocks, my context is changed to the currently iterated page. This means, calling .Title in b/w the blocks will render the title of pages in collection one-by-one. <body> <ul> {{ range .Pages }} <li>{{ .Title }}</li> {{ end }} </ul> </body>curl http://localhost:1313/posts/<html> <body> <ul> <li>Hugo context</li> <li>Designing my own theme in Hugo - II</li> <li>Designing my own theme in Hugo - I</li> <li>Julia and basic matrix operations</li> <li>AOP in pure Java, keeping logging simple and aside</li> <li>Object pool design pattern in Java</li> <li>Downloading a single file from 2 independent apps</li> <li>Syncing org roam files across devices in WSL2 environment with better performance</li> <li>Reflection API in Java</li> </ul> </body></html>You can still access the global contexts by using the $ symbol. $.Title will refer to the title of this page. $.Site.Title will refer to the site's title. <li>{{ $.Site.Title }} - {{ $.Title }} - {{ .Title }}</li>curl http://localhost:1313/posts/<html> <body> <ul> <li>Shubham&#39;s corner - Posts - Hugo context</li> <li>Shubham&#39;s corner - Posts - Designing my own theme in Hugo - II</li> <li>Shubham&#39;s corner - Posts - Designing my own theme in Hugo - I</li> <li>Shubham&#39;s corner - Posts - Julia and basic matrix operations</li> <li>Shubham&#39;s corner - Posts - AOP in pure Java, keeping logging simple and aside</li> <li>Shubham&#39;s corner - Posts - Object pool design pattern in Java</li> <li>Shubham&#39;s corner - Posts - Downloading a single file from 2 independent apps</li> <li>Shubham&#39;s corner - Posts - Syncing org roam files across devices in WSL2 environment with better performance</li> <li>Shubham&#39;s corner - Posts - Reflection API in Java</li> </ul> </body></html>Summary In this post, we explored in depth on contexts. We saw, {{ . }} refers to the current context. For pages, the context provides the title and other related information. We also witnessed the change of context while using range function and also used $ symbol to get the global context. Next, we will explore styling the site using CSS.

Designing my own theme in Hugo - II

What we did and what we will do? This is a part blog in which I am documenting my learning process of creating my own theme in Hugo. Previously, we went through the basic flow of creating a theme. My current theme is poison theme and we replaced the theme with our new theme which we named, "awesometheme". But unfortunately, after applying the new theme, the rendered page was blank. And in this post, we will expand our knowledge of Hugo and lean to render single pages. Layout and content separation Hugo architecture keeps content separate from the layout. This allows me to keep my blog post contents as it is and apply a new theme on the top of it. There are 3 general pages and for each page there is a layout. These pages are home page, single page and list pages. The home page is the landing page of your site. For a blog site, it can be the introductory page with list of recent blogs. The single page defines a page with content. For example, the blogs itself. And list pages are the pages that displays the list of items. For example, a list of blogs ordered in some preference. You can define and customize more pages based on your preference but these are the basic ones. As I already have the content ready, let's explore the layout designs first. Homepage The home page layout is defined under layouts/index.html. Currently, there isn't anything present in my index.html. Let's write something arbitrary in it and check if something happens. <html> <head> <title>Shubham's Blog</title> </head> <body> <div>Hi all,</div> <div>How are you guys</div> </body> </html>curl localhost:1313<html> <head> <meta name="generator" content="Hugo 0.92.2" /><script src="/livereload.js?mindelay=10&amp;v=2&amp;port=1313&amp;path=livereload" data-no-instant defer></script> <title>Shubham's Blog</title> </head> <body> <div>Hi all,</div> <div>How are you guys</div> </body> </html>Now, I can see the title and body as I defined. List Layout Let's try to render a list of blogs. The _default/list.html defines a default layout for all the list items. I can enter any HTML code and it will be displayed. But I want to do something more. I want to iterate through all the blog contents which I keep in posts directory and create a link for the blog page. This is where Hugo uses GoLang's templating library. Anything beginning with a . refers to the current context. I will explore more on contexts later. The reference, .Pages contains a list of all the pages for this section. So if, we are referring to posts directory, then .Pages will contain all the blogs inside the posts directory. The for-loop is done using range keyword. The range keyword iterates through a list and changes the context to the current item. So calling .Title inside the range block will refer to the current blog's title. We can do something as follows. <html> <head> <title>Shubham's Blog</title> </head> <body> <div>List of my recent posts ...</div> <ul>{{range .Pages}} <li>{{.Title}}</li> {{end}} </ul> </body> </html>curl http://localhost:1313/posts/<html> <head><script src="/livereload.js?mindelay=10&amp;v=2&amp;port=1313&amp;path=livereload" data-no-instant defer></script> <title>Shubham's Blog</title> </head> <body> <div>List of my recent posts ...</div> <ul> <li>Designing my own theme in Hugo - II</li> <li>Designing my own theme in Hugo - I</li> <li>Julia and basic matrix operations</li> <li>AOP in pure Java, keeping logging simple and aside</li> <li>Object pool design pattern in Java</li> <li>Downloading a single file from 2 independent apps</li> <li>Syncing org roam files across devices in WSL2 environment with better performance</li> <li>Reflection API in Java</li> </ul> </body> </html>Single page layout The single pages like the actual blog post layout can be defined inside, _default/single.html Same as what we did in the list layout, we can call .Content to display the content of the current page. <html> <head> <title>Shubham's Blog</title> </head> <body> {{.Content}} </body> </html>And awesome, I can see my contents as well. curl http://localhost:1313/posts/designing-my-own-theme-in-hugo-i/ | head -n 10<html> <head><script src="/livereload.js?mindelay=10&amp;v=2&amp;port=1313&amp;path=livereload" data-no-instant defer></script> <title>Shubham's Blog</title> </head> <body> <h2 id="hugo-is-all-about-themes">Hugo is all about themes</h2> <p>Hugo is a developers dream of static sites. Whether you are planning on a new blog site or maybe a documentation for a product, you can easily launch a static site using Hugo.</p> <p>The most powerful feature of Hugo is its simplicity. It is designed to separate your content from styling.Summary This post allowed us to display the contents of our blogs. We saw, how to design our layout for 3 basic types of pages, homepage, list page and single page. We explored some of the GoLang's templating features and got a shallow understanding of contexts. Next, let's make our blog more dynamic and good looking.

Designing my own theme in Hugo - I

Hugo is all about themes Hugo is a developers dream of static sites. Whether you are planning on a new blog site or maybe a documentation for a product, you can easily launch a static site using Hugo. The most powerful feature of Hugo is its simplicity. It is designed to separate your content from styling. This means you can write your content in normal markdown files independent of all the website pages and layouts. The actual design of the website can be stored as a separate theme. And as it is independent of the content, you can easily switch themes without ever thinking about your content. Creating a new theme To create a new theme, you can just use the below hugo command. It will create a new directory under theme with your theme name. hugo new theme <theme-name>The below files will be created on executing the above command. Let's get an idea on what are these files in this blog. . └── awesometheme ├── LICENSE ├── archetypes │   └── default.md ├── layouts │   ├── 404.html │   ├── _default │   │   ├── baseof.html │   │   ├── list.html │   │   └── single.html │   ├── index.html │   └── partials │   ├── footer.html │   ├── head.html │   └── header.html ├── static │   ├── css │   └── js └── theme.toml8 directories, 11 filesThe interesting part are the archetypes and layouts directory. The layout directory contains the template for your pages. A single page template is defined inside single.html. A page containing a list of items like blog posts are defined inside list.html. The archetypes directory provides the defaults for a type of content. When you run hugo new content post/learninghugo, the new file is created using the default template as described in archetypes/default.md, given you don't have any overrides. Applying the theme My blogs are written in Hugo so I can just start experimenting with my blogs theme. This way, I can forget about generating any dummy content for my theme. To tell Hugo to use a theme, you can just update the theme entry in your config. My blog site uses toml, so below are the changes. - theme = "poison" + theme = "awesometheme"Now let's run the dev server using hugo server --buildDrafts. And I got the below error. 2025-02-15 06:00:39.535 +0530 ERROR 2025/02/15 06:00:39 render of "section" failed: "/home/cold/Projects/Personal/blog.shubham.codes/layouts/_default/baseof.html:1:3": execute of template failed: template: cv/cv.html:1:3: executing "cv/cv.html" at <partial "head/head.html" .>: error calling partial: partial "head/head.html" not found ERROR 2025/02/15 06:00:39 failed to render pages: render of "home" failed: "/home/cold/Projects/Personal/blog.shubham.codes/layouts/_default/baseof.html:1:3": execute of template failed: template: index.html:1:3: executing "index.html" at <partial "head/head.html" .>: error calling partial: partial "head/head.html" not foundSeems like, I have overridden the poison theme for customization. But now, I am using my own theme which does not contains the poison partials. And this is causing the issue. Both the error corresponds to my layout's directory. So let's rename layouts to layouts_. And we will need to refactor these layouts as well.And this is why you should always keep your code loosely coupledmv layouts layouts_And it worked. Start building sites … hugo v0.92.2+extended linux/amd64 BuildDate=2023-01-31T11:11:57Z VendorInfo=ubuntu:0.92.2-1ubuntu0.1 | EN -------------------+------- Pages | 13 Paginator pages | 0 Non-page files | 0 Static files | 1014 Processed images | 0 Aliases | 0 Sitemaps | 1 Cleaned | 0Built in 88 ms Watching for changes in /home/cold/Projects/Personal/blog.shubham.codes/{archetypes,assets,content,data,static,themes} Watching for config changes in /home/cold/Projects/Personal/blog.shubham.codes/config.toml Environment: "development" Serving pages from memory Running in Fast Render Mode. For full rebuilds on change: hugo server --disableFastRender Web Server is available at http://localhost:1313/ (bind address 127.0.0.1) Press Ctrl+C to stopBut my screen is blank. Summary In this post, we went thorough the basic flow of creating a new theme in Hugo. Next, we will learn about layouts and implement the single and list layout for the blog.

Julia and basic matrix operations

What is Julia? Julia is a scientific programming language. It is close to R and Python in syntax and scripting feel. But it is more like a light weight MATLAB. I was going with Gilbert Strang's lectures on Linear Algebra and as always, you can't learn if you are not experimenting. I searched for any language support for MATLAB in Doom Emacs. Instead I saw this - ;;julia ; a better, faster MATLAB. And my curious mind was like - Why not give this a try? And here I am. I am not a Julia developer yet. Maybe I'll be one, given the feel of the language. Maybe I'll start saying Julia instead of MATLAB in some days. Let the future be what it will be. Here are some basic matrix operations you can perform in Julia. Installation I am using Ubuntu on WSL2 hosted on Windows 11. And the following worked for me. wget https://julialang-s3.julialang.org/bin/linux/x64/1.8/julia-1.8.1-linux-x86_64.tar.gz tar zxvf julia-1.8.1-linux-x86_64.tar.gz# maybe move it to your home directory mv julia-1.8.1 ~/juliaExport # ~/.bashrc or ~/.zshrc export PATH="$PATH:/home//julia/bin"Julia Linear Algebra For linear algebra operations, Julia has a library called LinearAlgebra. Just like Python where you import your libraries, here you use using keyword. You can define a matrix very easily as below. Let's say you want to define a matrix \(A = \begin{bmatrix} 1 & 2 & 3 \\ 4 & 1 & 6 \\ 7 & 8 & 1 \end{bmatrix}\). You can just write the elements of a row separated by space and a new row is specified by semi-colon. using LinearAlgebra A = [1 2 3; 4 1 6; 7 8 1]: 3×3 Matrix{Int64}: : 1 2 3 : 4 1 6 : 7 8 1Basic Operations The operations like finding the trace (tr) or determinant (det) or rank or inverse (inv) can be easily done as follows. using LinearAlgebra A = [1 2 3; 4 1 6; 7 8 1]tr(A) det(A) rank(A) inv(A)3×3 Matrix{Int64}: 1 2 3 4 1 6 7 8 1// Trace 3// Determinant 104.0// Rank 3// Inverse 3×3 Matrix{Float64}: -0.451923 0.211538 0.0865385 0.365385 -0.192308 0.0576923 0.240385 0.0576923 -0.0673077Calculation of Eigen values and Eigen vectors are also very easy. using LinearAlgebra A = [1 2 3; 4 1 6; 7 8 1] eigvals(A) eigvecs(A)3×3 Matrix{Int64}: 1 2 3 4 1 6 7 8 1// Eigen values 3-element Vector{Float64}: -6.214612641961068 -1.5540265964847833 10.768639238445843// Eigen vectors 3×3 Matrix{Float64}: -0.175709 -0.766257 -0.344989 -0.570057 0.587185 -0.589753 0.802596 0.26089 -0.730188There are different ways you can factorize a matrix and you can do this in Julia as well. LU Factorization LU factorization basically factorizes a matrix A as LU, where L is lower triangular matrix and U is upper triangular matrix. using LinearAlgebra A = [1 2; 4 5]; LU=lu(A)2×2 Matrix{Int64}: 1 2 4 5 LinearAlgebra.LU{Float64, Matrix{Float64}, Vector{Int64}} L factor: 2×2 Matrix{Float64}: 1.0 0.0 0.25 1.0 U factor: 2×2 Matrix{Float64}: 4.0 5.0 0.0 0.75Eigen value decomposition A matrix can be factorized as \(S\Lambda S^{-1}\) where \(S\) is the Eigen vector matrix and \(\Lambda\) is the Eigen values in diagonal matrix. using LinearAlgebra A = [1 2; 4 5]; E=eigen(A)2×2 Matrix{Int64}: 1 2 4 5 Eigen{Float64, Float64, Matrix{Float64}, Vector{Float64}} values: 2-element Vector{Float64}: -0.4641016151377544 6.464101615137754 vectors: 2×2 Matrix{Float64}: -0.806898 -0.343724 0.59069 -0.939071SVD SVD or Singular Value Decomposition is a way to factorize a matrix in \(u\Sigma v\) form where \(u\) and \(v\) are some special vectors and \(\Sigma\) is a special matrix. Going in detail about them would only make this blog grow infinitely. Maybe I should consider writing blogs on mathematical learning in future. using LinearAlgebra A = [1 2; 4 5]; SVD=svd(A)2×2 Matrix{Int64}: 1 2 4 5 LinearAlgebra.SVD{Float64, Float64, Matrix{Float64}, Vector{Float64}} U factor: 2×2 Matrix{Float64}: -0.324536 -0.945873 -0.945873 0.324536 singular values: 2-element Vector{Float64}: 6.767828935632369 0.44327361529561016 Vt factor: 2×2 Matrix{Float64}: -0.606994 -0.794707 0.794707 -0.606994Conclusion Julia has much more to offer. This blog was a basic introduction to Julia in Linear Algebra. I will keep sharing as I explore more of this language.

Object pool design pattern in Java

What is it? The object pool design pattern exposes a manager to manage a pool of reusable objects. The idea is to keep a know number of reusable objects (with a hard limit to initialize some more lazily). Whenever someone need the object from the pool, it will ask the pool manager. If there are free objects, the manager will engage one for your. If there aren't any free objects but the hard limit is not breached, then the manager will initialize a new object and provide you. Else if the hard limit is breached then you will return empty handed. Why is this? This is used when we have an resource object which takes some time to initialize. And once initialized, it can be reused over and over again without a performance hit. Basically, creation is expensive and hence we want to reuse already created instances. Generally, we put a soft limit on the number of resources initially initialized. And we want to create more resources lazily if required. To prevent a huge number of resources from being created, we also put a hard limit. Example At GreyOrange, we use something called as IDC files. These are huge binary files (sometimes 100-200 GBs). They provide the time it takes to travel b/w 2 coordinates. We created an IDC Manager to parse these files and provide us the required information. The initialization takes a huge amount of time (sometimes 10s). Once initialized, it takes less than 1ms to provide the information. Right now, we are good with just once instance of this manager, so it is a singleton class with one buffer linked to an IDC file. But if the demands for parallel calls increases, we might want to implement the manager as a Object Pool. How to implement this? There are 3 requirements.An Object Pool Manager The initial number of objects - m The maximum number of objects - nThe Object Pool Manager will be a singleton class. We cannot allow multiple Object Pool Manager objects as they will create max, n objects each. We will create 2 lists, availableResources and enagaedResources. Initially, we will populate the availableResources with m new resource objects. Each getter call will check the availableResources list for available objects. If the objects are available then it will move the last object to engagedObjects. If the objects are not available then there are 2 choices. Check the hard limit, if not reached then create more objects and add to availableResources. Else return null. A pseudo code for the manager is as follows. class PoolManager { private static PoolManager instance; private List<Object> availableResources; private List<Object> engagedResources; private Integer initialLimit; private Integer hardLimit; private PoolManager() { // Get these properties from already defined config // Assume this is defined as per standard or equivalent configurations initialLimit = Properties.getInstance().getIntegerValue("POOL_INITIAL_LIMIT"); hardLimit = Properties.getInstance().getIntegerValue("POOL_HARD_LIMIT"); availableResources = new ArrayList<>(); engagedResources = new ArrayList<>(); // Initialize the initial number of resources in the pool for (int i=0; i<initialLimit; i++) { availableResources.add(new ResourceObject()); } } public static PoolManager getInstance() { if (instance == null) { synchronized(PoolManager.class) { if (instance == null) { instance = new PoolManager(); } } } return instance; } public Object getObject() { if (!availableResources.isEmpty()) { // A sync is required as 2 thread may want to get a free object at the same time synchronized(availableResources) { Object freeObject = availableResources.remove(availableResources.size()-1); engagedResources.add(freeObject); return freeObject; } } else if (engagedResources.size() < hardLimit) { Object freeObject = new ResourceObject(); availableResources.add(freeObject); return getObject(); } else { return null; } } public void releaseObject(Object engagedObject) { if (engagedObject != null) { try { synchronized(engagedResources) { Object freeObject = engagedResources.remove(engagedObject); availableResources.add(freeObject); } } catch (Exception e){} } } }