dotfiles

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

mmd (2052B)


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