👌 IMPROVE: md2gopher functional

This commit is contained in:
Josemar Lohn 2021-02-12 02:08:01 -03:00
parent cf7e11ae96
commit ff37a61556
2 changed files with 158 additions and 75 deletions

View File

@ -22,40 +22,40 @@ function oprint(t){
} }
# https://unix.stackexchange.com/a/94751 # https://unix.stackexchange.com/a/94751
function centralize(t,c){ function centralize(text, border , margin){
L = col - c - length(t); L = col - border - margin - length(text);
#print("====" length(t) "=====" t "=====\n") #print("====" length(t) "=====" t "=====\n")
for(i=1; i<=int(L/2); i++) for(i=1; i<=(int(L/2) + margin) ; i++)
t = " "t; text = " "text;
for(i=1; i<=int(L/2+.5); i++) for(i=1; i<=int(L/2+.5) ; i++)
t = t " " text = text " "
return t return text
} }
# function from https://unix.stackexchange.com/a/282338 # function from https://unix.stackexchange.com/a/282338
function justify(t,i,nbchar,nbspc,spaces,spcpf,r){ function justify(text, margin, j, nbchar, wreturn, spcpf, spaces, nbspc){
$0=t $0=text
if (NF <= 1) { return t } if (NF <= 1) { return text }
else { else {
nbchar = 0 nbchar = 0
for (i = 1; i <= NF; i++) { for (i = 1; i <= NF; i++) {
nbchar += length($i) nbchar += length($i)
} }
nbspc = col - nbchar nbspc = (col - margin) - nbchar - 1
spcpf = int(nbspc / (NF - 1)) spcpf = int(nbspc / (NF - 1))
for (i = 1; i < NF; i++) { for (i = 1; i < NF; i++) {
r = r $i wreturn = wreturn $i
spaces = (NF == 2 || i == NF - 1) ? nbspc : spcpf spaces = (NF == 2 || i == NF - 1) ? nbspc : spcpf
if (spaces < 1) spaces = 1 if (spaces < 1) spaces = 1
for (j = 0; j < spaces; j++) { for (j = 0; j < spaces; j++) {
r = r " " wreturn = wreturn " "
} }
nbspc -= spaces nbspc -= spaces
} }
r = r $NF wreturn = wreturn $NF
} }
return r return wreturn
} }
function subref(id){ function subref(id){
@ -104,6 +104,7 @@ function nextil(t) {
} }
ilcode = !ilcode; ilcode = !ilcode;
return t1 tag nextil(t2); return t1 tag nextil(t2);
return t1 tag nextil(t2);
} }
if(tag == "<"){ if(tag == "<"){
# Autolinks # Autolinks
@ -240,7 +241,8 @@ function nextil(t) {
else else
stag[++ns] = ntag; stag[++ns] = ntag;
tag = "<" tag ">"; tag = "<" tag ">";
return t1 tag nextil(t2); #return t1 tag nextil(t2);
return t1 nextil(t2);
} }
} }
@ -253,22 +255,29 @@ function inline(t) {
} }
#https://unix.stackexchange.com/a/337656 #https://unix.stackexchange.com/a/337656
function wrap(t,align,q,y,z,final) { function wrap(t,align) {
final=""
z=""
y=0
margin_spaces=""
localmargin = blockquote * 5
for(c = 0; c < localmargin; c++)
margin_spaces = " " margin_spaces
while (t) while (t)
{ {
q = match(t, / |$/); y += q q = match(t, / |$/); y += q
if (y > col) { if (y > col - localmargin) {
#print "|"localmargin"|"blockquote"|"nnl"|"
if (align != 0) if (align != 0)
{ {
if (align=="c") if (align=="c")
#print "zzzzzzzzzzzzzzzzzzzzzzzzzzzzz" final = final margin_spaces centralize(z,localmargin) RS
final = final centralize(z) RS
if (align=="j") if (align=="j")
final = final justify(z) RS final = final margin_spaces justify(z,localmargin) RS
} }
else else
{ {
final = final z RS final = final z RS
} }
y = q - 1 y = q - 1
z = "" z = ""
@ -279,10 +288,10 @@ function wrap(t,align,q,y,z,final) {
} }
if (align=="c") if (align=="c")
{ {
final = final centralize(z) final = final margin_spaces centralize(z, localmargin)
} }
else { else {
final = final z final = final margin_spaces z
} }
return final return final
} }
@ -309,10 +318,12 @@ BEGIN {
html = 0; html = 0;
nl = 0; nl = 0;
nr = 0; nr = 0;
margin = 0;
otext = ""; otext = "";
text = ""; text = "";
par = "p"; par = "p";
col=70; col=70;
listitem=0;
c=0; do { lineheader = "=" lineheader; c++ } while ( c < col ) c=0; do { lineheader = "=" lineheader; c++ } while ( c < col )
c=0; do { lineheadersmall = "-" lineheadersmall; c++ } while ( c < col ) c=0; do { lineheadersmall = "-" lineheadersmall; c++ } while ( c < col )
} }
@ -347,38 +358,6 @@ BEGIN {
next; next;
} }
# html
!html && /^<(address|blockquote|center|dir|div|dl|fieldset|form|h[1-6r]|\
isindex|menu|noframes|noscript|ol|p|pre|table|ul|!--)/ {
if(code)
oprint("</pre></code>");
for(; !text && block[nl] == "blockquote"; nl--)
oprint("</blockquote>");
match($0, /^<(address|blockquote|center|dir|div|dl|fieldset|form|h[1-6r]|\
isindex|menu|noframes|noscript|ol|p|pre|table|ul|!--)/);
htag = substr($0, 2, RLENGTH - 1);
if(!match($0, "(<\\/" htag ">)|((^<hr ?\\/?)|(--)>$)"))
html = 1;
if(html && match($0, /^<hr/))
hr = 1;
oprint($0);
next;
}
html && (/(^<\/(address|blockquote|center|dir|div|dl|fieldset|form|h[1-6r]|\
isindex|menu|noframes|noscript|ol|p|pre|table|ul).*)|(--)>$/ ||
(hr && />$/)) {
html = 0;
hr = 0;
oprint($0);
next;
}
html {
oprint($0);
next;
}
# List and quote blocks # List and quote blocks
# Remove indentation # Remove indentation
@ -393,12 +372,13 @@ nnl < nl && !blank && text && ! /^ ? ? ?([*+-]|([0-9]+\.)+)( +| )/ { nnl = nl; }
{ {
while(sub(/^> /, "")) while(sub(/^> /, ""))
nblock[++nnl] = "blockquote"; nblock[++nnl] = "blockquote";
blockquote = nnl;
} }
# Horizontal rules # Horizontal rules
{ hr = 0; } { hr = 0; }
(blank || (!text && !code)) && /^ ? ? ?([-*_][ ]*)([-*_][ ]*)([-*_][ ]*)+$/ { (blank || (!text && !code)) && /^ ? ? ?([-*_][ ]*)([-*_][ ]*)([-*_][ ]*)+$/ {
if(code){ if(code){
oprint("</pre></code>"); #oprint("</pre></code>");
code = 0; code = 0;
} }
blank = 0; blank = 0;
@ -415,12 +395,15 @@ block[nl] ~ /[ou]l/ && /^$/ {
sub(/^ ? ? ?[*+-]( +| )/, ""); sub(/^ ? ? ?[*+-]( +| )/, "");
nnl++; nnl++;
nblock[nnl] = "ul"; nblock[nnl] = "ul";
listtype="ul";
newli = 1; newli = 1;
} }
(nnl != nl || !text || block[nl] ~ /[ou]l/) && /^ ? ? ?([0-9]+\.)+( +| )/ { (nnl != nl || !text || block[nl] ~ /[ou]l/) && /^ ? ? ?([0-9]+\.)+( +| )/ {
sub(/^ ? ? ?([0-9]+\.)+( +| )/, ""); sub(/^ ? ? ?([0-9]+\.)+( +| )/, "");
nnl++; nnl++;
nblock[nnl] = "ol"; nblock[nnl] = "ol";
listtype="ol";
listitem++
newli = 1; newli = 1;
} }
newli { newli {
@ -429,7 +412,12 @@ newli {
blank = 0; blank = 0;
printp(par); printp(par);
if(nnl == nl && block[nl] == nblock[nl]) if(nnl == nl && block[nl] == nblock[nl])
oprint("</li><li>"); #oprint("</li><li>");
if(listtype == "ul")
printf "- ";
if(listtype == "ol")
printf "%s - ", listitem;
} }
blank && ! /^$/ { blank && ! /^$/ {
if(match(block[nnl], /[ou]l/) && !par) if(match(block[nnl], /[ou]l/) && !par)
@ -442,26 +430,36 @@ blank && ! /^$/ {
# Close old blocks and open new ones # Close old blocks and open new ones
nnl != nl || nblock[nl] != block[nl] { nnl != nl || nblock[nl] != block[nl] {
if(code){ if(code){
oprint("</pre></code>"); #oprint("</pre></code>");
code = 0; code = 0;
} }
printp(par); printp(par);
b = (nnl > nl) ? nblock[nnl] : block[nnl]; b = (nnl > nl) ? nblock[nnl] : block[nnl];
par = (match(b, /[ou]l/)) ? "" : "p"; par = (match(b, /[ou]l/)) ? "" : "p";
} }
nnl < nl || (nnl == nl && nblock[nl] != block[nl]) { nnl < nl || (nnl == nl && nblock[nl] != block[nl]) {
for(; nl > nnl || (nnl == nl && pblock[nl] != block[nl]); nl--){ for(; nl > nnl || (nnl == nl && pblock[nl] != block[nl]); nl--){
if(match(block[nl], /[ou]l/)) if(match(block[nl], /[ou]l/))
oprint("</li>"); #oprint("</li>");
oprint("</" block[nl] ">"); printf ""
#oprint("</" block[nl] ">");
#if(listtype == "ol")
# listitem = 0;
} }
} }
nnl > nl { nnl > nl {
for(; nl < nnl; nl++){ for(; nl < nnl; nl++){
block[nl + 1] = nblock[nl + 1]; block[nl + 1] = nblock[nl + 1];
oprint("<" block[nl + 1] ">"); #oprint("<" block[nl + 1] ">");
if(match(block[nl + 1], /[ou]l/)) if(match(block[nl + 1], /[ou]l/))
oprint("<li>"); #oprint("<li>");
if(listtype == "ul")
printf "- ";
#if(listtype == "ol")
#printf "%s - ", listitem;
#listitem=0
} }
} }
hr { hr {
@ -476,19 +474,20 @@ code && /^$/ {
blank = 1; blank = 1;
next; next;
} }
!text && sub(/^( | )/, "") { !text && sub(/^( | )/, "") {
if(blanK) if(blanK)
oprint(""); oprint("");
blank = 0; blank = 0;
if(!code) #if(!code)
oprint("<code><pre>"); # oprint("<code><pre>");
code = 1; code = 1;
$0 = eschtml($0); $0 = eschtml($0);
oprint($0); oprint($0);
next; next;
} }
code { code {
oprint("</pre></code>"); #oprint("</pre></code>");
code = 0; code = 0;
} }
@ -512,7 +511,7 @@ text && /^-+$/ {printp("h2"); next;}
next; next;
} }
if (n == 3) { if (n == 3) {
oprint(text centralize($0) "\n" lineheadersmall "\n") oprint("\n" text centralize($0) "\n" lineheadersmall "\n")
next; next;
} }
if (n > 3) { if (n > 3) {
@ -539,15 +538,16 @@ function alen(a, ix, k) {
END { END {
if(code){ if(code){
oprint("</pre></code>"); # oprint("</pre></code>");
code = 0; code = 0;
} }
printp(par); #printp(par);
for(; nl > 0; nl--){ #for(; nl > 0; nl--){
if(match(block[nl], /[ou]l/)) # if(match(block[nl], /[ou]l/))
oprint("</li>"); # listitem=0
oprint("</" block[nl] ">"); #oprint("</li>");
} # oprint("</" block[nl] ">");
#}
gsub(/<<[^"]*/, "", otext); gsub(/<<[^"]*/, "", otext);
print(otext); print(otext);

83
spec/test.md Normal file
View File

@ -0,0 +1,83 @@
# A First Level Header
## A Second Level Header
Now is the time for all good men to come to
the aid of their country. This is just a
regular paragraph.
The quick brown fox jumped over the lazy
dog's back.
### Header 3
> This is a blockquote.
>
> This is the second paragraph in the blockquote. This is the second paragraph in the blockquote. This is the second paragraph in the blockquote.
>
> This is a test
>
>> Blockcode level2
>>
>> level2
Some of these words *are emphasized*.
Some of these words _are emphasized also_.
Use two asterisks for **strong emphasis**.
Or, if you prefer, __use two underscores instead__.
* Candy.
* Gum.
* Booze.
1. Candy.
2. Gum.
3. Booze.
- Candy.
- Gum.
- Booze.
1. Red
2. Green
3. Blue
* A list item.
With multiple paragraphs.
* Another item in the list.
This is an [example link](http://example.com/).
This is an [example link](http://example.com/ "With a Title").
I get 10 times more traffic from [Google][1] than from
[Yahoo][2] or [MSN][3].
[1]: http://google.com/ "Google"
[2]: http://search.yahoo.com/ "Yahoo Search"
[3]: http://search.msn.com/ "MSN Search"
I start my morning with a cup of coffee and
[The New York Times][NY Times].
[ny times]: http://www.nytimes.com/
![alt text](/path/to/img.jpg "Title")
![alt text][id]
[id]: /path/to/img.jpg "Title"
I strongly recommend against using any `<blink>` tags.
I wish SmartyPants used named entities like `&mdash;`
instead of decimal-encoded entites like `&#8212;`.