1. 26
  1.  

  2. 7

    See also https://github.com/zimbatm/shab which is a templating language written in 6 lines of bash. It just wraps the template in a heredoc, turning the language inside out. I am very proud of that hack :p

    1. 2

      I also went with heredocs initially, problem is they’re really slow https://www.oilshell.org/blog/2016/10/18.html.

      1. 3

        That overhead is not really noticeable in my experience. In my mind shab is mostly useful to generate a config file from a few variables. Hopefully it will never be used to render a web page or anything big.

        Imagine a context like a docker container where bash is already available. Just add 6 lines and you have a templating language.

      2. 1

        Heh, that looks scarily handy. I especially like the Nix example, where the template syntax needs escaping, to prevent Nix doing the same job without the need for shab at all :P.

        1. 1

          hehe yeah. I wish it was possible to resume Nix evaluation so it could also be used as a templating language when variables are resolved later at runtime.

      3. 4

        Using a single mark to alternate between template and code is probably bad design:you have to parse the code to know if you are in code and if accidentally one is removed the result will be a fully broken mess instead of an immediate error.

        1. 3

          fully broken mess

          This is a satisfying way to describe many things I know; thank you.

          1. 2

            I misunderstand a little bit, what do you mean by “parse the code”?

            1. 1

              Ah, “parse the code” visually speaking? With your eyes. Right?

              1. 1

                Yes

                1. 1

                  I just visually pair them. The code between the tokens is usually short enough that matching them is not an issue. I remember having some issues matching them on a code that was a little bit more complex but I eventually simplified it so the issue was gone.

                  This is the most “complex” code I have written with it:

                  <?xml version='1.0' encoding='utf-8'?>
                  <feed xmlns='http://www.w3.org/2005/Atom'>
                  <id>https://adi.tilde.institute/</id>
                  <title>Adi, Freelance Developer</title>
                  #!
                  for f in *
                  do
                  test -d "$f" && continue
                  case $f in
                  	*.html|*.xml|*.txt|*.css|*.js|*.ico|*.png|*.upp*|feed|urlenc)
                          continue
                          ;;
                  esac
                  stat -f '%Sm' -t '%Y-%m-%dT%H:%M:%SZ' "$f"
                  done | sort -r | head | {
                  read -r u
                  #!
                  <updated>$u</updated>
                  #!
                  }
                  #!
                  <author>
                  <name>Adrian Emil Grigore</name>
                  <email>adi@tilde.institute</email>
                  <uri>https://adi.tilde.institute</uri>
                  </author>
                  <link href='https://adi.tilde.institute/atom.xml' rel='self' />
                  #!
                  for f in *
                  do
                  test -d "$f" && continue
                  case $f in
                  	*.html|*.xml|*.txt|*.css|*.js|*.ico|*.png|*.upp*|feed|urlenc)
                          continue
                          ;;
                  esac
                  #!
                  <entry>
                  	#!
                  	echo "$f" | ./urlenc | {
                  	read -r ef
                  	#!
                  	<id>https://adi.tilde.institute/$ef.html</id>
                  	<link href='https://adi.tilde.institute/$ef.html'/>
                  	#!
                  	}
                  	#!
                  	<title>${f#* }</title>
                  	#!
                  	stat -f '%Sm' -t '%Y-%m-%dT%H:%M:%SZ' "$f" | {
                  		read -r u
                  	#!
                  	<updated>$u</updated>
                  	#!
                  	}
                  	#!
                  	<summary>
                  	#!
                  	tail -n +4 "$f" | head -4 | sed 's/^/	/'
                  	#!
                  	</summary>
                  </entry>
                  #!
                  done
                  #!
                  </feed>
                  
          2. 1

            Wow, such simple yet useful idea. Great job!

            Could you add some explicit version number to it? I’d like to have it packaged for Void Linux but we need version number for that :)

            1. 2

              Hey! Thanks! I’ll add a version number to it. You might have a naming conflicting with https://github.com/maandree/pp tho.

                1. 1

                  Thank you <3