Search code examples
awkhugopelican

Migration from Pelican to Hugo


I was reading an article showing how to migrate markdown files from Pelican to Hugo. I'm trying to understand what the awk script is doing. :

# begin block, executed once, 
# to set field separator, output fied separator & print 3 dashes
BEGIN { FS = ":"; OFS = ":"; print "---" }

# ??? 
!c && /^$/ { print "---\n"; c = 1 }

# user defined function?
c { print; next }

# user defined function?
!c {
    # lower first field
    $1 = tolower($1)

    # if first field is "date"
    if ($1 == "date") {
        # transform second field
        $2 = gensub(/ ([^.]+)\.([^.]+).([^.]+)/, " \\3-\\2-\\1", 1, $2)
        $2 = gensub(/-([0-9])-/, "-0\\1-", 1, $2)
    }

    if ($1 == "tags")
        $2 = " [" gensub(/[-a-z]+/, "'\\0'", "g", substr($2, 2)) "]"

    print
}

I don't really understand, what are c and !c are they user defined functions? Without the function keyword and without parameters? What is exactly the meaning of c=1?


Solution

  • c is a variable. c=1 sets the value of c to 1

    • c is a test of variable c and its true, other than 0
    • !c is a test of variable c and its true if c is not set or 0
    • c { print; next } If c is set to some other than nothing or 0, then print (will print the whole line since nothing other is specified). next stop what you are doing and skip to next line and start over.