Fryboyter

Hugo - Remove content from sitemap

Google Search Console has complained that two links on my site are blocked by robots.txt even though they appear in the sitemap. How do I get them from the sitemap that Hugo creates automatically?

The solution is actually quite simple. First edit the related articles and add sitemap_exclude: true to the metadata.

---
title: Secret Article
date: 2019-06-11T19:12:45+0200
sitemap_exclude: true
slug: secret-article
---

Now it is necessary to adapt the template with which the sitemap is created. Therefore create the file sitemap.xml in the theme directory under layouts/_default and fill it with the following content.

{{ printf "<?xml version=\"1.0\" encoding=\"utf-8\" standalone=\"yes\" ?>" | safeHTML }}
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
  xmlns:xhtml="http://www.w3.org/1999/xhtml">
  {{ range .Data.Pages }}
  <url>
    <loc>{{ .Permalink }}</loc>{{ if not .Lastmod.IsZero }}
    <lastmod>{{ safeHTML ( .Lastmod.Format "2006-01-02T15:04:05-07:00" ) }}</lastmod>{{ end }}{{ with .Sitemap.ChangeFreq }}
    <changefreq>{{ . }}</changefreq>{{ end }}{{ if ge .Sitemap.Priority 0.0 }}
    <priority>{{ .Sitemap.Priority }}</priority>{{ end }}{{ if .IsTranslated }}{{ range .Translations }}
    <xhtml:link
                rel="alternate"
                hreflang="{{ .Lang }}"
                href="{{ .Permalink }}"
                />{{ end }}
    <xhtml:link
                rel="alternate"
                hreflang="{{ .Lang }}"
                href="{{ .Permalink }}"
                />{{ end }}
  </url>
  {{ end }}
</urlset>

This corresponds initially to the standard template for the sitemap. Now change line 4 as follows.

{{ range .Data.Pages }}{{ if ne .Params.sitemap_exclude true }}

Finally add another {{ end }} to line 21.

{{ end }}{{ end }}

If the page is rebuilt now, all contents where sitemap_exclude: true is specified should no longer appear in the sitemap.

General | OSBN

Some articles are now offered in multiple languages

In the last few years I have been thinking about publishing at least some articles in English from time to time. So far, however, the technical implementation was too cumbersome for me. But with Hugo it’s done pretty easy, as I noticed today.

The first thing to do is to add the following entries to the configuration file config.toml.

DefaultContentLanguage = "de"

    [languages]
    [languages.de]
        languageName = "Deutsche Version"
        weight = 1
    [languages.en]
        languageName = "English version"
        weight = 2

The first line specifies that de is the default language. The rest defines the existing languages, gives them a name and a weighting (the lower the more important).

Now we create a new article. Let’s just call the file manche-artikel-nun-englisch.md. Let’s assume that the article is important enough to be published in English as well. So we create another article and name the file manche-artikel-nun-englisch.en.md. As you can see the title has been extended by a .en. Hugo now automatically understands that the article with .en is the English version and the other one is the German version.

But what if someone opens the German version but doesn’t understand German? There should be a kind of switch available. For this one can use for example the following code.

{{ if .IsTranslated }}
    {{ range .Translations }} | {{ .Language.LanguageName }} {{ end}}
{{ end }}

This checks whether the article is also available in another language. If so, a link to the corresponding versions will be shown. If not, nothing is displayed. I put this code on fryboyter.de under the main heading of all articles. If the link “English version” appears at an article, I wrote the article in English as well. Currently one will find this link only at this article for the moment. Gradually there will be more articles in German and English (also older ones) available.

OSBN | General

Hooks under Mercurial

Yesterday I had published an article about the version control Mercurial. The project in question was managed with Git. There were also some hooks present. For example, you can use them to execute commands automatically when a new commit is uploaded. For this you use the hook “post-receive” as I recently described it in an article. Tux also commented that this is also possible with Mercurial.

Absolutely correct in itself, but either the documentation is in need of improvement or I didn’t get it. I assume the latter at this point.

Under https://www.mercurial-scm.org/wiki/Hook the whole thing is described. Every time I add a commit, a hook should be executed. So I entered the following in the configuration file hgrc.

[hooks]
commit = /pfad/zur/Datei/commithook

Under /path/to/file/ I created the commithook file and entered the script that should be executed. Then I made the file executable.

But it didn’t work. The commits arrived, but the hook was not executed. After some Google-Fu I noticed why. The commit hook only seems to work if you commit it directly in the repository. Not if you push it in, though.

Therefore you have to enter the following into the file hgrc.

[hooks]
changegroup = /pfad/zur/Datei/commithook

Herewith the hook is always executed when committing.

OSBN | General

Convert Git to Mercurial

It often happens that someone switches from one version control system to Git. Sometimes, however, someone wants to switch from Git to another solution. Recently I had such a request. Mercurial was wanted.

The relevant git repository was still quite fresh, but there were already commits in the low three-digit range. Too many to create manually in Mercurial.

Which is not necessary. Mercurial offers the possibility to convert.

Let’s assume that the home directory contains the directory repository. In this directory there is the directory blog.git where the Git repository is located.

First you have to create the subdirectory blog.hg in the repository directory and switch to it. There you create a Mercurial repository with “hg init”. This creates the directory .hg. In this directory you create the file hgrc with the following content.

[extensions]
hgext.convert=

This enables the extension with which the conversion can be performed.

Now execute the following command in the directory blog.hg.

hg convert --datesort ~/repository/blog.git ~/repository/blog.hg

It’ll take a while. But if everything goes as planned, you should get a Mercurial repository with all commits, files, etc. as in the Git repository. If you look at the directory blog.hg, you will still only see the subdirectory .hg. The problem can be solved by executing the command “hg update”. If you then look in the directory blog.hg you can also see all the files you originally added to the Git repository.

OSBN | General

Update Hugo

Hugo is not installed on my webspace by default. Therefore I have to install new versions manually. To make this easier I built a small script.

#!/bin/bash
BIN_DIR=$HOME/bin
CUR_VERSION="$("$BIN_DIR"/hugo version 2>/dev/null | cut -d'v' -f2 | cut -c 1-6)"
NEW_VERSION=$(curl --silent "https://api.github.com/repos/gohugoio/hugo/tags" | jq -r '.[0].name' | tr -d v)

echo "Hugo:  Current Version: $CUR_VERSION => New Version: $NEW_VERSION"

if ! [ "$NEW_VERSION" = "$CUR_VERSION" ]; then

  curl -L --output hugo.tar.gz "https://github.com/gohugoio/hugo/releases/download/v${NEW_VERSION}/hugo_${NEW_VERSION}_Linux-64bit.tar.gz"
  tar -C "${BIN_DIR}" -xvzf hugo.tar.gz hugo
  rm hugo.tar.gz
else
    echo "Die current Version of Hugo ist allready installed"
fi

This checks whether the current version offered on Github is more current than the one available on the web space. If so, the current version of Github will be downloaded and installed on the webspace.

OSBN | General