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