About me: My name is Solène Rapenne. I like learning and sharing experiences about IT stuff. Hobbies: '(BSD OpenBSD h+ Lisp cmdline gaming internet-stuff Crossbow). I love percent and lambda characters. OpenBSD developer solene@.

Contact me: solene on Freenode, solene+www at dataswamp dot org or solene@bsd.network (mastodon)

Minimalistic markdown subset to html converter using awk

Written by Solène, on 26 August 2019.
Tags: #unix #awk

Hello As on my blog I use different markup languages I would like to use a simpler markup language not requiring an extra package. To do so, I wrote an awk script handling titles, paragraphs and code blocks the same way markdown does. It is very easy to use, like: `awk -f mmd file.mmd > output.html` The script is the following:

BEGIN {
	in_code=0
	in_paragraph=0
}

{

# escape < > characters
gsub(/</,"\\<",$0);
gsub(/>/,"\\>",$0);

if(! match($0,/^    /)) {	# close code blocks
	if(in_code)
	{
		in_code=0
		printf "</code></pre>\n"
	}
}				# end close blocks

	 			# display titles
if(match($0,/^#/))
{
	if(match($0,/^(#+)/))
	{
		printf "<h%i>%s</h%i>\n", RLENGTH, substr($0,index($0,$2)), RLENGTH
	}
}				# end display titles
else
if(match($0,/^    /))		# display code blocks
{
	if(in_code==0)
	{
		in_code=1
		printf "<pre><code>"
		print substr($0,5)
	}
	else
	{
		print substr($0,5)
	}
}
else
{
	# close p if current line is empty
	if(length($0) == 0 && in_paragraph == 1 && in_code != 0)
	{
		in_paragraph=0
		printf "</p>"
	}

	# we are still in a paragraph
	if(length($0) != 0 && in_paragraph == 1)
	{
		print
	}

	# open a p tag if previous line is empty
	if(length(previous_line)==0 && in_paragraph==0)
	{
		
		in_paragraph=1
		printf "<p>%s\n", $0
	}
}

previous_line = $0

}

END {
	if(in_code==1)
	{
		printf "</code></pre>\n"
	}
	if(in_paragraph==1)
	{
		printf "</p>\n"
	}
}