Skip to content

Commit

Permalink
Modified mdown.awk to have the same styling and links as the recent d…
Browse files Browse the repository at this point in the history
….awk changes
  • Loading branch information
wernsey committed Apr 20, 2021
1 parent a3464ce commit 4c0fd34
Show file tree
Hide file tree
Showing 2 changed files with 118 additions and 59 deletions.
18 changes: 9 additions & 9 deletions demo.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
*
* Blank lines in the comment separate paragraphs.
*
* Two spaces at the end of the line
* Two spaces at the end of the line
* forces a linebreak. Here is another line<br>break.
*
* Headings can be written like so:
Expand Down Expand Up @@ -71,7 +71,7 @@
* 1. Item 2
* * Item 2.1; List styles can be mixed.
* * Item 2.2
*
*
* The blank line above contains whitespace, hence a new list is not started
* (Paragraphs in lists differ a bit from other markdowns).
* * Item 2.3
Expand Down Expand Up @@ -123,7 +123,7 @@
*
* Code indented with spaces:
* //Some code, indented with spaces
*
*
* int main(int argc, char *argv[]) {
* return 0;
* }
Expand Down Expand Up @@ -159,11 +159,11 @@
*
* Regression test: The #es in this sample would've caused problems:
*
* # Compile like so:
* mvn package
*
* # Generate Javadocs
* mvn javadoc:javadoc
* # Compile like so:
* mvn package
*
* # Generate Javadocs
* mvn javadoc:javadoc
*
* This is a diagram:
*
Expand Down Expand Up @@ -218,7 +218,7 @@
* Image syntax `\![Image Alt Text](example.png)`
* Escaping images `\![Image Alt Text](example.png)` and links `\[Link Alt Text](example.com)`
*
* Images can be encoded as Data URIs: ![Red Dot][reddot]
* Images can be encoded as Data URIs: ![Red Dot][reddot]
* <sub>The red dot comes from [Wikipedia][datauri]</sub>
*
* [dataURI]: https://en.wikipedia.org/wiki/Data_URI_scheme "Data URI scheme"
Expand Down
159 changes: 109 additions & 50 deletions mdown.awk
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,16 @@
BEGIN {

# Configuration options
if(Title=="") Title = "Documentation";
if(Theme=="") Theme = 1;
if(Pretty=="") Pretty = 0;
if(HideToCLevel=="") HideToCLevel = 3;
if(Title== "") Title = "Documentation";
if(Theme== "") Theme = 1;
if(Pretty== "") Pretty = 0;
if(HideToCLevel== "") HideToCLevel = 3;
if(Lang == "") Lang = "en";
#TopLinks = 1;
#classic_underscore = 1;
if(MaxWidth=="") MaxWidth="1080px";
if(NumberHeadings=="") NumberHeadings = 1;
if(NumberH1s=="") NumberH1s = 0;

Mode = "p";
ToC = ""; ToCLevel = 1;
Expand Down Expand Up @@ -50,27 +53,27 @@ END {
Out = Out tag(Mode, Buf);
}

print "<!DOCTYPE html>\n<html><head>"
print "<!DOCTYPE html>\n<html lang=\"" Lang "\"><head>"
print "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">";
print "<title>" Title "</title>";
if(StyleSheet)
print "<link rel=\"stylesheet\" href=\"" StyleSheet "\">";
else
print "<style><!--" CSS "\n--></style>";
print "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">";
if(ToC && match(Out, /!\[toc[-+]?\]/))
print "<script type=\"text/javascript\"><!--\n" \
print "<script><!--\n" \
"function toggle_toc(n) {\n" \
" var toc=document.getElementById('table-of-contents-' + n);\n" \
" var btn=document.getElementById('btn-text-' + n);\n" \
" toc.style.display=(toc.style.display=='none')?'block':'none';\n" \
" btn.innerHTML=(toc.style.display=='none')?'&#x25BC;':'&#x25B2;';\n" \
" btn.innerHTML=(toc.style.display=='none')?'&#x25BA;':'&#x25BC;';\n" \
"}\n" \
"function toggle_toc_ul(n) { \n" \
" var toc=document.getElementById('toc-ul-' + n); \n" \
" var btn=document.getElementById('toc-btn-' + n); \n" \
" if(toc) {\n" \
" toc.style.display=(toc.style.display=='none')?'block':'none'; \n" \
" btn.innerHTML=(toc.style.display=='none')?'&#x25BC;':'&#x25B2;';\n" \
" btn.innerHTML=(toc.style.display=='none')?'&#x25BA;':'&#x25BC;';\n" \
" }\n" \
"}\n" \
"//-->\n</script>";
Expand Down Expand Up @@ -107,7 +110,7 @@ function trim(st) {
sub(/[[:space:]]+$/, "", st);
return st;
}
function filter(st, res,tmp, linkdesc, url, delim, edelim, name, def) {
function filter(st, res,tmp, linkdesc, url, delim, edelim, name, def, plang) {
if(Mode == "p") {
if(match(st, /^[[:space:]]*\[[-._[:alnum:][:space:]]+\]:/)) {
linkdesc = ""; LastLink = 0;
Expand Down Expand Up @@ -216,15 +219,15 @@ function filter(st, res,tmp, linkdesc, url, delim, edelim, name, def) {
else {
gsub(/\t/," ",Buf);
if(length(trim(Buf)) > 0) {
Lang = "";
plang = "";
if(match(Preterm, /^[[:space:]]*```+/)) {
Lang = trim(substr(Preterm, RSTART+RLENGTH));
if(Lang) {
Lang = "class=\"prettyprint lang-" Lang "\"";
plang = trim(substr(Preterm, RSTART+RLENGTH));
if(plang) {
plang = "class=\"prettyprint lang-" plang "\"";
HasCode=1;
}
}
res = tag("pre", tag("code", escape(Buf), Lang));
res = tag("pre", tag("code", escape(Buf), plang));
}
pop();
if(Preterm) sub(/^[[:space:]]*```+[[:alnum:]]*/,"",st);
Expand Down Expand Up @@ -380,10 +383,10 @@ function scrub(st, mp, ms, me, r, p, tg, a) {
r = r "<" tg a ">";
} else if(match(tg, "^[[:alpha:]]+:https://[[:graph:]]+$")) {
if(!a) a = tg;
r = r "<a href=\"" tg "\">" a "</a>";
r = r "<a class=\"normal\" href=\"" tg "\">" a "</a>";
} else if(match(tg, "^[[:graph:]]+@[[:graph:]]+$")) {
if(!a) a = tg;
r = r "<a href=\"" obfuscate("mailto:" tg) "\">" obfuscate(a) "</a>";
r = r "<a class=\"normal\" href=\"" obfuscate("mailto:" tg) "\">" obfuscate(a) "</a>";
} else {
r = r "&lt;";
continue;
Expand All @@ -404,29 +407,36 @@ function scrub(st, mp, ms, me, r, p, tg, a) {

function push(newmode) {Stack[StackTop++] = Mode; Mode = newmode;}
function pop() {Mode = Stack[--StackTop];Buf = ""; return Mode;}
function heading(level, st, res, href) {
st = trim(st);
function heading(level, st, res, href, u, text,svg) {
if(level > 6) level = 6;
st = trim(st);
href = tolower(st);
href = strip_tags(href);
gsub(/[^ [:alnum:]]+/, "", href);
gsub(/ +/, "-", href);
if(!LinkUrls[href]) LinkUrls[href] = "#" href;
if(!LinkUrls[tolower(st)]) LinkUrls[tolower(st)] = "#" href;
res = tag("h" level, st (TopLinks?"&nbsp;&nbsp;<a class=\"top\" title=\"Return to top\" href=\"#\">&#8593;&nbsp;Top</a>":""), "id=\"" href "\"");
gsub(/[^-_ [:alnum:]]+/, "", href);
gsub(/[[:space:]]/, "-", href);
if(TitleUrls[href]) {
for(u = 1; TitleUrls[href "-" u]; u++);
href = href "-" u;
}
TitleUrls[href] = "#" href;

svg = "<svg width=\"16\" height=\"16\" xmlns=\"http:https://www.w3.org/2000/svg\"><g transform=\"rotate(-30, 8, 8)\" stroke=\"#000000\" opacity=\"0.25\"><rect fill=\"none\" height=\"6\" width=\"8\" x=\"2\" y=\"6\" rx=\"1.5\"/><rect fill=\"none\" height=\"6\" width=\"8\" x=\"6\" y=\"4\" rx=\"1.5\"/></g></svg>";
text = "<a href=\"#" href "\" class=\"header\">" st "&nbsp;" svg "</a>" (TopLinks?"&nbsp;&nbsp;<a class=\"top\" title=\"Return to top\" href=\"#\">&#8593;&nbsp;Top</a>":"");

res = tag("h" level, text, "id=\"" href "\"");
for(;ToCLevel < level; ToCLevel++) {
ToC_ID++;
if(ToCLevel < HideToCLevel) {
ToC = ToC "<a class=\"toc-button\" id=\"toc-btn-" ToC_ID "\" onclick=\"toggle_toc_ul('" ToC_ID "')\">&#x25B2;</a>";
ToC = ToC "<ul class=\"toc-" ToCLevel "\" id=\"toc-ul-" ToC_ID "\">";
} else {
ToC = ToC "<a class=\"toc-button\" id=\"toc-btn-" ToC_ID "\" onclick=\"toggle_toc_ul('" ToC_ID "')\">&#x25BC;</a>";
ToC = ToC "<ul style=\"display:none;\" class=\"toc-" ToCLevel "\" id=\"toc-ul-" ToC_ID "\">";
ToC = ToC "<ul class=\"toc toc-" ToCLevel "\" id=\"toc-ul-" ToC_ID "\">";
} else {
ToC = ToC "<a class=\"toc toc-button\" id=\"toc-btn-" ToC_ID "\" onclick=\"toggle_toc_ul('" ToC_ID "')\">&#x25BA;</a>";
ToC = ToC "<ul style=\"display:none;\" class=\"toc toc-" ToCLevel "\" id=\"toc-ul-" ToC_ID "\">";
}
}
for(;ToCLevel > level; ToCLevel--)
ToC = ToC "</ul>";
ToC = ToC "<li class=\"toc-" level "\"><a class=\"toc-" level "\" href=\"#" href "\">" st "</a>\n";
ToC = ToC "<li class=\"toc-" level "\"><a class=\"toc toc-" level "\" href=\"#" href "\">" st "</a>\n";
ToCLevel = level;
return res;
}
Expand All @@ -445,8 +455,8 @@ function make_toc(st, r,p,dis,t,n) {

++n;
dis = index(substr(st,RSTART,RLENGTH),"+");
t = "<div>\n<a id=\"toc-button-" n "\" class=\"toc-button\" onclick=\"toggle_toc(" n ")\"><span id=\"btn-text-" n "\">" (dis?"&#x25B2;":"&#x25BC;") "</span>&nbsp;Contents</a>\n" \
"<div id=\"table-of-contents-" n "\" style=\"display:" (dis?"block":"none") ";\">\n<ul class=\"toc-1\">" ToC "</ul>\n</div>\n</div>";
t = "<div>\n<a id=\"toc-button-" n "\" class=\"toc-button\" onclick=\"toggle_toc(" n ")\"><span id=\"btn-text-" n "\">" (dis?"&#x25BC;":"&#x25BA;") "</span>&nbsp;Contents</a>\n" \
"<div id=\"table-of-contents-" n "\" style=\"display:" (dis?"block":"none") ";\">\n<ul class=\"toc toc-1\">" ToC "</ul>\n</div>\n</div>";
r = r substr(st,1,RSTART-1);
r = r t;
st = substr(st,RSTART+RLENGTH);
Expand All @@ -456,13 +466,13 @@ function make_toc(st, r,p,dis,t,n) {
}
function fix_links(st, lt,ld,lr,url,img,res,rx,pos,pre) {
do {
pre = match(st, /<pre>/); # Don't substitute in <pre> blocks
pre = match(st, /<(pre|code)>/); # Don't substitute in <pre> or <code> blocks
pos = match(st, /\[[^\]]+\]/);
if(!pos)break;
if(pre && pre < pos) {
pre = match(st, /<\/pre>/);
match(st, /<\/(pre|code)>/);
res = res substr(st,1,RSTART+RLENGTH);
st = substr(st, RSTART+RLENGTH);
st = substr(st, RSTART+RLENGTH+1);
continue;
}
img=substr(st,RSTART-1,1)=="!";
Expand Down Expand Up @@ -493,7 +503,7 @@ function fix_links(st, lt,ld,lr,url,img,res,rx,pos,pre) {
if(img)
res = res "<img src=\"" url "\" title=\"" ld "\" alt=\"" lt "\">";
else
res = res "<a href=\"" url "\" title=\"" ld "\">" lt "</a>";
res = res "<a class=\"normal\" href=\"" url "\" title=\"" ld "\">" lt "</a>";
} else if(match(st, /^[[:space:]]*\[[^\]]*\]/)) {
lt = substr(rx, 2, length(rx) - 2);
match(st, /\[[^\]]*\]/);
Expand All @@ -507,8 +517,10 @@ function fix_links(st, lt,ld,lr,url,img,res,rx,pos,pre) {
ld = LinkDescs[lr];
if(img)
res = res "<img src=\"" url "\" title=\"" ld "\" alt=\"" lt "\">";
else if(url)
res = res "<a class=\"normal\" href=\"" url "\" title=\"" ld "\">" lt "</a>";
else
res = res "<a href=\"" url "\" title=\"" ld "\">" lt "</a>";
res = res "[" lt "][" lr "]";
} else
res = res (img?"!":"") rx;
} while(pos > 0);
Expand Down Expand Up @@ -588,28 +600,35 @@ function obfuscate(e, r,i,t,o) {
}
return o;
}
function init_css(Theme, css,ss,hr,c1,c2,c3,c4,c5,bg1,bg2,bg3,bg4,ff,i) {
function init_css(Theme, css,ss,hr,c1,c2,c3,c4,c5,bg1,bg2,bg3,bg4,ff,fs,i) {
if(Theme == "0") return "";

css["body"] = "color:%color1%;font-family:%font-family%;line-height:1.5em;" \
css["body"] = "color:%color1%;font-family:%font-family%;font-size:%font-size%;line-height:1.5em;" \
"padding:1em 2em;width:80%;max-width:%maxwidth%;margin:0 auto;min-height:100%;float:none;";
css["h1"] = "color:%color1%;border-bottom:1px solid %color1%;padding:0.3em 0.1em;";
css["h1"] = "border-bottom:1px solid %color1%;padding:0.3em 0.1em;";
css["h1 a"] = "color:%color1%;";
css["h2"] = "color:%color2%;border-bottom:1px solid %color2%;padding:0.2em 0.1em;";
css["h2 a"] = "color:%color2%;";
css["h3"] = "color:%color3%;border-bottom:1px solid %color3%;padding:0.1em 0.1em;";
css["h4,h5,h6"] = "color:%color4%;padding:0.1em 0.1em;";
css["h1,h2,h3,h4,h5,h6"] = "font-weight:normal;line-height:1.2em;";
css["h3 a"] = "color:%color3%;";
css["h4,h5,h6"] = "padding:0.1em 0.1em;";
css["h4 a,h5 a,h6 a"] = "color:%color4%;";
css["h1,h2,h3,h4,h5,h6"] = "font-weight:bolder;line-height:1.2em;";
css["h4"] = "border-bottom:1px solid %color4%";
css["p"] = "margin:0.5em 0.1em;"
css["hr"] = "background:%color1%;height:1px;border:0;"
css["a"] = "color:%color2%;";
css["a:visited"] = "color:%color2%;";
css["a:active"] = "color:%color4%;";
css["a:hover"] = "color:%color4%;";
css["a.normal, a.toc"] = "color:%color2%;";
#css["a.normal:visited"] = "color:%color2%;";
#css["a.normal:active"] = "color:%color4%;";
css["a.normal:hover, a.toc:hover"] = "color:%color4%;";
css["a.top"] = "font-size:x-small;text-decoration:initial;float:right;";
css["a.header svg"] = "opacity:0;";
css["a.header:hover svg"] = "opacity:1;";
css["a.header"] = "text-decoration: none;";
css["strong,b"] = "color:%color1%";
css["code"] = "color:%color2%;";
css["blockquote"] = "margin-left:1em;color:%color2%;border-left:0.2em solid %color3%;padding:0.25em 0.5em;";
css["pre"] = "color:%color2%;background:%color5%;border:1px solid;border-radius:2px;line-height:1.25em;margin:0.25em 0.5em;padding:0.75em;";
css["code"] = "color:%color2%;font-weight:bold;";
css["blockquote"] = "margin-left:1em;color:%color2%;border-left:0.2em solid %color3%;padding:0.25em 0.5em;overflow-x:auto;";
css["pre"] = "color:%color2%;background:%color5%;border:1px solid;border-radius:2px;line-height:1.25em;margin:0.25em 0.5em;padding:0.75em;overflow-x:auto;";
css["table"] = "border-collapse:collapse;margin:0.5em;";
css["th,td"] = "padding:0.5em 0.75em;border:1px solid %color4%;";
css["th"] = "color:%color2%;border:1px solid %color3%;border-bottom:2px solid %color3%;";
Expand All @@ -619,7 +638,7 @@ function init_css(Theme, css,ss,hr,c1,c2,c3,c4,c5,bg1,bg2,bg3,bg4,ff
css["dl"] = "margin:0.5em;";
css["dt"] = "font-weight:bold;";
css["dd"] = "padding:0.3em;";
css["mark"] = "color:%color2%;background-color:%color5%;";
css["mark"] = "color:%color5%;background-color:%color4%;";
css["del,s"] = "color:%color4%;";
css["a.toc-button"] = "color:%color2%;cursor:pointer;font-size:small;padding: 0.3em 0.5em 0.5em 0.5em;font-family:monospace;border-radius:3px;";
css["a.toc-button:hover"] = "color:%color4%;background:%color5%;";
Expand All @@ -631,11 +650,50 @@ function init_css(Theme, css,ss,hr,c1,c2,c3,c4,c5,bg1,bg2,bg3,bg4,ff
css[".fade"] = "color:%color5%;";
css[".highlight"] = "color:%color2%;background-color:%color5%;";
css["summary"] = "cursor:pointer;";
css["ul.toc"] = "list-style-type:none;";

if(NumberHeadings) {
if(NumberH1s) {
css["body"] = css["body"] "counter-reset: h1 toc1;";
css["h1"] = css["h1"] "counter-reset: h2 h3 h4;";
css["h2"] = css["h2"] "counter-reset: h3 h4;";
css["h3"] = css["h3"] "counter-reset: h4;";
css["h1::before"] = "content: counter(h1) \" \"; counter-increment: h1; margin-right: 10px;";
css["h2::before"] = "content: counter(h1) \".\"counter(h2) \" \";counter-increment: h2; margin-right: 10px;";
css["h3::before"] = "content: counter(h1) \".\"counter(h2) \".\"counter(h3) \" \";counter-increment: h3; margin-right: 10px;";
css["h4::before"] = "content: counter(h1) \".\"counter(h2) \".\"counter(h3)\".\"counter(h4) \" \";counter-increment: h4; margin-right: 10px;";

css["li.toc-1"] = "counter-reset: toc2 toc3 toc4;";
css["li.toc-2"] = "counter-reset: toc3 toc4;";
css["li.toc-3"] = "counter-reset: toc4;";
css["a.toc-1::before"] = "content: counter(h1) \" \";counter-increment: toc1;";
css["a.toc-2::before"] = "content: counter(h1) \".\" counter(toc2) \" \";counter-increment: toc2;";
css["a.toc-3::before"] = "content: counter(h1) \".\" counter(toc2) \".\" counter(toc3) \" \";counter-increment: toc3;";
css["a.toc-4::before"] = "content: counter(h1) \".\" counter(toc2) \".\" counter(toc3) \".\" counter(toc4) \" \";counter-increment: toc4;";

} else {
css["h1"] = css["h1"] "counter-reset: h2 h3 h4;";
css["h2"] = css["h2"] "counter-reset: h3 h4;";
css["h3"] = css["h3"] "counter-reset: h4;";
css["h2::before"] = "content: counter(h2) \" \";counter-increment: h2; margin-right: 10px;";
css["h3::before"] = "content: counter(h2) \".\"counter(h3) \" \";counter-increment: h3; margin-right: 10px;";
css["h4::before"] = "content: counter(h2) \".\"counter(h3)\".\"counter(h4) \" \";counter-increment: h4; margin-right: 10px;";

css["li.toc-1"] = "counter-reset: toc2 toc3 toc4;";
css["li.toc-2"] = "counter-reset: toc3 toc4;";
css["li.toc-3"] = "counter-reset: toc4;";
css["a.toc-2::before"] = "content: counter(toc2) \" \";counter-increment: toc2;";
css["a.toc-3::before"] = "content: counter(toc2) \".\" counter(toc3) \" \";counter-increment: toc3;";
css["a.toc-4::before"] = "content: counter(toc2) \".\" counter(toc3) \".\" counter(toc4) \" \";counter-increment: toc4;";
}
}

# Colors:
c1="#314070";c2="#465DA6";c3="#6676A8";c4="#A88C3F";c5="#E8E4D9";
#c1="#314070";c2="#465DA6";c3="#6676A8";c4="#A88C3F";c5="#E8E4D9";
c1="#314070";c2="#384877";c3="#6676A8";c4="#738FD0";c5="#FBFCFF";
# Font Family:
ff = "sans-serif";
fs = "11pt";

# Alternative color scheme suggestions:
#c1="#303F9F";c2="#0449CC";c3="#2162FA";c4="#4B80FB";c5="#EDF2FF";
Expand All @@ -656,6 +714,7 @@ function init_css(Theme, css,ss,hr,c1,c2,c3,c4,c5,bg1,bg2,bg3,bg4,ff
gsub(/%color4%/,c4,ss);
gsub(/%color5%/,c5,ss);
gsub(/%font-family%/,ff,ss);
gsub(/%font-size%/,fs,ss);
gsub(/%hr%/,hr,ss);
return ss;
}

0 comments on commit 4c0fd34

Please sign in to comment.