dotfiles

configuration files for shell, text editor, graphical environment, etc.
git clone git://src.adamsgaard.dk/dotfiles # fast
git clone https://src.adamsgaard.dk/dotfiles.git # slow
Log | Files | Refs | README | LICENSE Back to index

mmd (2093B)


      1 #!/usr/bin/awk -f
      2 # usage: awk -f markdown-to-html.awk file.md > file.html
      3 # or cat file.md | mmd
      4 # script originally by Solene Rapenne
      5 # https://dataswamp.org/~solene/2019-08-26-minimal-markdown.html
      6 
      7 BEGIN {
      8 	in_code=0
      9 	in_list_unordered=0
     10 	in_list_ordered=0
     11 	in_paragraph=0
     12 }
     13 
     14 {
     15 	# escape < > characters
     16 	gsub(/</,"\<",$0);
     17 	gsub(/>/,"\>",$0);
     18 
     19 	# close code blocks
     20 	if(! match($0,/^    /)) {
     21 		if(in_code) {
     22 			in_code=0
     23 			printf "</code></pre>\n"
     24 		}
     25 	}
     26 
     27 	# close unordered list
     28 	if(! match($0,/^- /)) {
     29 		if(in_list_unordered) {
     30 			in_list_unordered=0
     31 			printf "</ul>\n"
     32 		}
     33 	}
     34 
     35 	# close ordered list
     36 	if(! match($0,/^[0-9]+\. /)) {
     37 		if(in_list_ordered) {
     38 			in_list_ordered=0
     39 			printf "</ol>\n"
     40 		}
     41 	}
     42 
     43 	# display titles
     44 	if(match($0,/^#/)) {
     45 		if(match($0,/^(#+)/)) {
     46 			printf "<h%i>%s</h%i>\n", RLENGTH, substr($0,index($0,$2)), RLENGTH
     47 		}
     48 
     49 	# display code blocks
     50 	} else if(match($0,/^    /)) {
     51 		if(in_code==0) {
     52 			in_code=1
     53 			printf "<pre><code>"
     54 			print substr($0,5)
     55 		} else {
     56 			print substr($0,5)
     57 		}
     58 
     59 	# display unordered lists
     60 	} else if(match($0,/^- /)) {
     61 		if(in_list_unordered==0) {
     62 			in_list_unordered=1
     63 			printf "<ul>\n"
     64 			printf "<li>%s</li>\n", substr($0,3)
     65 		} else {
     66 			printf "<li>%s</li>\n", substr($0,3)
     67 		}
     68 
     69 	# display ordered lists
     70 	} else if(match($0,/^[0-9]+\. /)) {
     71 		n=index($0," ")+1
     72 		if(in_list_ordered==0) {
     73 			in_list_ordered=1
     74 			printf "<ol>\n"
     75 			printf "<li>%s</li>\n", substr($0,n)
     76 		} else {
     77 			printf "<li>%s</li>\n", substr($0,n)
     78 		}
     79 
     80 	# close p if current line is empty
     81 	} else {
     82 		if(length($0) == 0 && in_paragraph == 1 && in_code != 0) {
     83 			in_paragraph=0
     84 			printf "</p>"
     85 		} # we are still in a paragraph
     86 		if(length($0) != 0 && in_paragraph == 1) {
     87 			print
     88 		} # open a p tag if previous line is empty
     89 		if(length(previous_line)==0 && in_paragraph==0) {
     90 			in_paragraph=1
     91 			printf "<p>%s\n", $0
     92 		}
     93 	}
     94 	previous_line = $0
     95 }
     96 
     97 END {
     98 	if(in_code==1) {
     99 		printf "</code></pre>\n"
    100 	}
    101 	if(in_list_unordered==1) {
    102 		printf "</ul>\n"
    103 	}
    104 	if(in_list_ordered==1) {
    105 		printf "</ol>\n"
    106 	}
    107 	if(in_paragraph==1) {
    108 		printf "</p>\n"
    109 	}
    110 }