src/gnuplot.awk

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    
    # parser for gnuplot commands in Literate C pages
    
    BEGIN {
        nplots = 0
        if (pdf)
    	ext = ".pdf";
        else
    	ext = ".svg";
    }
    
    # This works around a bug in gnuplot 5, where font units are missing
    function fixfonts(output)
    {
        print "! sed -i 's/font-size=\"\\([0-9.]*\\)\"/font-size=\"\\1pt\"/g' " \
    	output;
    }
    
    function defaults()
    {
        printf "set pointsize 0.75; ";
    }
    
    /^[ \t]*~~~/ {
        if (gnuplot) {
    	gnuplot = 0;
    	print "set output";
    	if (match (output, ".*\\.png"))
    	    print "! mogrify -trim" output;
    	else if (match (output, ".*\\" ext))
    	    fixfonts(output);
    	else if (output == "")
    	    fixfonts("_plot" nplots ext);
    	nplots++;
        }
    }
    
    {
        if (gnuplot) {
    	if (match($0, "^[ \t]*reset")) {
    	    printf "reset; ";
    	    if (ext == ".pdf")
    		printf ("set term @PDF; ");
    	    else
    		printf ("set term @SVG; ");
    #	    printf "load '~/.gnuplot'; ";
    	    defaults();
    	    term = ""
    	}
    	else {
    	    if (match($0, "^[ \t]*set[ \t]+output[ \t]+")) {
    		output = substr($0, RLENGTH + 1);
    		if (term == "" && match (output, ".*\\.png"))
    		    printf ("set term @PNG enhanced font \",10\"; ");
    	    }
    	    else if (match($0, "^[ \t]*set[ \t]+term"))
    		term = $0;
    	    print $0;
    	}
        }
        else if (!match($0, "^[ \t]*~~~"))
    	print "#", $0;
    }
    
    /^[ \t]*~~~gnuplot/ {
        gnuplot = 1; output = "";
        printf "set output '_plot" nplots ext "'; ";
        defaults();
    }