X-Git-Url: https://git.plutz.net/?a=blobdiff_plain;f=markdown.awk;h=0e6d949c4abb66633b68ba5ac770a5605c22d72f;hb=9b2f590896c10e83e0505cc57de52dc72d0dd12e;hp=fbfc455355c14c6df2d34cd44a42daa8dee09794;hpb=e6442fb6aad465f6b0696b2ed1477d2dbc05bfc7;p=cgilite
diff --git a/markdown.awk b/markdown.awk
index fbfc455..0e6d949 100755
--- a/markdown.awk
+++ b/markdown.awk
@@ -38,6 +38,7 @@
#
# Extensions - Block elements:
# ----------------------------
+# - [x] Automatic -wrapping (custom)
# - ? Heading identifiers (php md, pandoc)
# - [x] Automatic heading identifiers (custom)
# - [x] Fenced code blocks (php md, pandoc)
@@ -89,13 +90,13 @@ function HTML ( text ) {
return text;
}
-function URL ( text ) {
+function URL ( text, sharp ) {
gsub( /&/, "%26", text );
gsub( /"/, "%22", text );
gsub( /'/, "%27", text );
gsub( /`/, "%60", text );
gsub( /\?/, "%3F", text );
- gsub( /#/, "%23", text );
+ if (sharp) gsub( /#/, "%23", text );
gsub( /\[/, "%5B", text );
gsub( /\]/, "%5D", text );
gsub( / /, "%20", text );
@@ -195,16 +196,16 @@ function inline( line, LOCAL, len, code, href, guard ) {
title = gensub(/^!\[([^]]*)\]\(([^"\)]+)([ \t]+"([^"]+)")?\)(\{([a-zA-Z \t-]*)\})?/, "\\4", "g", substr(line, 1, len) );
attrib = gensub(/^!\[([^]]*)\]\(([^"\)]+)([ \t]+"([^"]+)")?\)(\{([a-zA-Z \t-]*)\})?/, "\\6", "g", substr(line, 1, len) );
if ( title && attrib ) {
- return "" \
+ return "" \
inline( substr( line, len + 1) );
} else if ( title ) {
- return "" \
+ return "" \
inline( substr( line, len + 1) );
} else if ( attrib ) {
- return "" \
+ return "" \
inline( substr( line, len + 1) );
} else {
- return "" \
+ return "" \
inline( substr( line, len + 1) );
}
@@ -215,10 +216,10 @@ function inline( line, LOCAL, len, code, href, guard ) {
id = gensub(/^!\[([^\n]*)\] ?\[([^\n]*)\].*/, "\\2", 1, substr(line, 1, len) );
if ( ! id ) id = text;
if ( rl_href[id] && rl_title[id] ) {
- return "" \
+ return "" \
inline( substr( line, len + 1) );
} else if ( rl_href[id] ) {
- return "" \
+ return "" \
inline( substr( line, len + 1) );
} else {
return "" HTML(substr(line, 1, len)) inline( substr(line, len + 1) );
@@ -307,13 +308,18 @@ function inline( line, LOCAL, len, code, href, guard ) {
}
# Nested Block, resets heading counters
-function _nblock( block, LOCAL, st, len, hlvl, htxt, hlsav, sec, guard, code, indent, attrib ) {
+function _nblock( block, LOCAL, hlsav, sec ) {
+ # Keeping arrays in a local scope is awkward, so we serialize the HL array
+ # into the scalar hlsav
hlsav = HL[1] " " HL[2] " " HL[3] " " HL[4] " " HL[5] " " HL[6];
HL[1] = 0; HL[2] = 0; HL[3] = 0; HL[4] = 0; HL[5] = 0; HL[6] = 0;
+ # Block Level
+ blvl++;
+
block = _block( block );
sec = ""; for ( n = 1; n <= 6; n++ ) { sec = sec (HL[n]?"":""); }
- split(hlsav, HL);
+ split(hlsav, HL); blvl--;
return block sec;
}
@@ -568,35 +574,47 @@ function _block( block, LOCAL, st, len, hlvl, htxt, sec, guard, code, indent, at
# First Order Heading H1
} else if ( match( block, /^[^\n]+\n===+(\n|$)/ ) ) {
len = RLENGTH; sec = "";
+
for ( n = 1; n <= 6; n++ ) { sec = sec (HL[n]?"":""); }
HL[1]++; HL[2] = 0; HL[3] = 0; HL[4] = 0; HL[5] = 0; HL[6] = 0;
- return sec "" \
- inline( gensub( /\n.*$/, "", "g", block ) ) \
- "
\n\n" \
+ hid = HL[1] ":" URL(gensub( /\n.*$/, "", "g", block ), 1);
+
+ return sec " 1)?"":" id=\"" hid "\"") ">" \
+ "" inline( gensub( /\n.*$/, "", "g", block ) ) \
+ ((blvl > 1)?"":"") \
+ "
\n\n" \
_block( substr( block, len + 1 ) );
# Second Order Heading H2
} else if ( match( block, /^[^\n]+\n---+(\n|$)/ ) ) {
len = RLENGTH; sec = "";
+
for ( n = 2; n <= 6; n++ ) { sec = sec (HL[n]?"":""); }
HL[2]++; HL[3] = 0; HL[4] = 0; HL[5] = 0; HL[6] = 0;
- return sec "" \
+ hid= HL[1] "." HL[2] ":" URL(gensub( /\n.*$/, "", "g", block ), 1);
+
+ return sec " 1)?"":" id=\"" hid "\"") ">" \
"" inline( gensub( /\n.*$/, "", "g", block ) ) \
- "
\n\n" \
+ ((blvl > 1)?"":"") \
+ "\n\n" \
_block( substr( block, len + 1) );
# Nth Order Heading H1 H2 H3 H4 H5 H6
} else if ( match( block, /^#{1,6}[ \t]*[^\n]+([ \t]*#*)(\n|$)/ ) ) {
len = RLENGTH; sec = "";
+
hlvl = length( gensub( /^(#{1,6}).*$/, "\\1", "g", block ) );
htxt = gensub(/^#{1,6}[ \t]*(([^ \t\n]+|[ \t]+[^ \t\n#]|[ \t]+#+[^\n#])+)([ \t]*#*)(\n.*)?$/, "\\1", 1, block);
+
for ( n = hlvl; n <= 6; n++ ) { sec = sec (HL[n]?"":""); }
HL[hlvl]++; for ( n = hlvl + 1; n <= 6; n++) { HL[n] = 0;}
hid = HL[1]; for ( n = 2; n <= hlvl; n++) { hid = hid "." HL[n] ; }
- return sec "" inline( htxt ) \
- "\n\n" \
+ hid = hid ":" URL(htxt, 1);
+
+ return sec " 1)?"":" id=\"" hid "\"") ">" \
+ "" inline( htxt ) \
+ ((blvl > 1)?"":"") \
+ "\n\n" \
_block( substr( block, len + 1) );
# block images (wrapped in