Skip to main content
bytesaves from commenter
Source Link
qarz
  • 421
  • 2
  • 7

Python 3.12, 1491 ̶1̶4̶9̶1̶ ̶ 1484 bytes

J=len
import re
def A(s):
 U='color';T='img';S='url';R='span';Q='strong';L='code';Z='blockquote';G=[];C='';I=F=0;K={};D=list(re.finditer('\\[(/?)(\\w+)(?:=([^\\]]+))?\\]',s,re.I))
 for(B,E)in enumerate(D):
  if E[1]:continue
  A=E[2].lower();H=B+1;O=A==L
  while H<J(D):
   if D[H][2].lower()==A:
    if A==L:O=O+1-2*bool(D[H][1])
    if D[H][1]and(A==L and )*(O==0 or )*(A!=L and D[H][1]):K[B]=H;break
   H+=1
 while F<J(D):
  E=D[F];C+=s[I:E.start()];I=E.end();A=E[2].lower();M=E[3];P=E[1]
  if G and G[-1][0]==L:
   if P and A==L and F==K[G[-1][1]]:C+='</code>';G.pop()
   else:C+=E[0]
  elif P:
   if G and G[-1][0]==A:C+=f"</{dict(b=Q,i='em',quote=Z,url='a',color=R,size=R).get(A,A)}>";G.pop()
   else:C+=E[0]
  elif A in'biuscode':
   if F in K:C+=f"<{dict(b=Q,i='em').get(A,A)}>";G+=[(A,F)]
   else:C+=E[0]
  elif A=='quote'elif'quote'==A:
   if F in K:C+=f'<{Z}><cite>{M}</cite>'if M else f'<{Z}>';G+=[(A,F)]
   else:C+=E[0]
  elif A==S:
   if M:
    if F in K:C+=f'<a href="{M}">';G+=[(A,F)]
    else:C+=E[0]
   else:
    B=F+1
    while B<J(D)and not(D[B][1]and D[B][2].lower()==S):B+=1
    if B<J(D):N=s[I:D[B].start()];C+=f'<a href="{N}">{N}</a>';I=D[B].end();F=B
    else:C+=E[0]
  elif A==T:
   B=F+1
   while B<J(D)and not(D[B][1]and D[B][2].lower()==T):B+=1
   if B<J(D):N=s[I:D[B].start()];C+=f'<img src="{N}">';I=D[B].end();F=B
   else:C+=E[0]
  elif A in'colorsize':
   H=U if A==U else'font-size'
   if F in K:C+=f'<span style="{H}:{M}">';G+=[(A,F)]
   else:C+=E[0]
  else:C+=E[0]
  F+=1
 return C+s[I:]

Python 3.12, 1491 bytes

J=len
import re
def A(s):
 U='color';T='img';S='url';R='span';Q='strong';L='code';Z='blockquote';G=[];C='';I=F=0;K={};D=list(re.finditer('\\[(/?)(\\w+)(?:=([^\\]]+))?\\]',s,re.I))
 for(B,E)in enumerate(D):
  if E[1]:continue
  A=E[2].lower();H=B+1;O=A==L
  while H<J(D):
   if D[H][2].lower()==A:
    if A==L:O=O+1-2*bool(D[H][1])
    if A==L and O==0 or A!=L and D[H][1]:K[B]=H;break
   H+=1
 while F<J(D):
  E=D[F];C+=s[I:E.start()];I=E.end();A=E[2].lower();M=E[3];P=E[1]
  if G and G[-1][0]==L:
   if P and A==L and F==K[G[-1][1]]:C+='</code>';G.pop()
   else:C+=E[0]
  elif P:
   if G and G[-1][0]==A:C+=f"</{dict(b=Q,i='em',quote=Z,url='a',color=R,size=R).get(A,A)}>";G.pop()
   else:C+=E[0]
  elif A in'biuscode':
   if F in K:C+=f"<{dict(b=Q,i='em').get(A,A)}>";G+=[(A,F)]
   else:C+=E[0]
  elif A=='quote':
   if F in K:C+=f'<{Z}><cite>{M}</cite>'if M else f'<{Z}>';G+=[(A,F)]
   else:C+=E[0]
  elif A==S:
   if M:
    if F in K:C+=f'<a href="{M}">';G+=[(A,F)]
    else:C+=E[0]
   else:
    B=F+1
    while B<J(D)and not(D[B][1]and D[B][2].lower()==S):B+=1
    if B<J(D):N=s[I:D[B].start()];C+=f'<a href="{N}">{N}</a>';I=D[B].end();F=B
    else:C+=E[0]
  elif A==T:
   B=F+1
   while B<J(D)and not(D[B][1]and D[B][2].lower()==T):B+=1
   if B<J(D):N=s[I:D[B].start()];C+=f'<img src="{N}">';I=D[B].end();F=B
   else:C+=E[0]
  elif A in'colorsize':
   H=U if A==U else'font-size'
   if F in K:C+=f'<span style="{H}:{M}">';G+=[(A,F)]
   else:C+=E[0]
  else:C+=E[0]
  F+=1
 return C+s[I:]

Python 3.12, ̶1̶4̶9̶1̶ ̶ 1484 bytes

J=len
import re
def A(s):
 U='color';T='img';S='url';R='span';Q='strong';L='code';Z='blockquote';G=[];C='';I=F=0;K={};D=list(re.finditer('\\[(/?)(\\w+)(?:=([^\\]]+))?\\]',s,re.I))
 for(B,E)in enumerate(D):
  if E[1]:continue
  A=E[2].lower();H=B+1;O=A==L
  while H<J(D):
   if D[H][2].lower()==A:
    if A==L:O=O+1-2*bool(D[H][1])
    D[H][1]and(A==L)*(O==0)*(A!=L):K[B]=H;break
   H+=1
 while F<J(D):
  E=D[F];C+=s[I:E.start()];I=E.end();A=E[2].lower();M=E[3];P=E[1]
  if G and G[-1][0]==L:
   if P and A==L and F==K[G[-1][1]]:C+='</code>';G.pop()
   else:C+=E[0]
  elif P:
   if G and G[-1][0]==A:C+=f"</{dict(b=Q,i='em',quote=Z,url='a',color=R,size=R).get(A,A)}>";G.pop()
   else:C+=E[0]
  elif A in'biuscode':
   if F in K:C+=f"<{dict(b=Q,i='em').get(A,A)}>";G+=[(A,F)]
   else:C+=E[0]
  elif'quote'==A:
   if F in K:C+=f'<{Z}><cite>{M}</cite>'if M else f'<{Z}>';G+=[(A,F)]
   else:C+=E[0]
  elif A==S:
   if M:
    if F in K:C+=f'<a href="{M}">';G+=[(A,F)]
    else:C+=E[0]
   else:
    B=F+1
    while B<J(D)and not(D[B][1]and D[B][2].lower()==S):B+=1
    if B<J(D):N=s[I:D[B].start()];C+=f'<a href="{N}">{N}</a>';I=D[B].end();F=B
    else:C+=E[0]
  elif A==T:
   B=F+1
   while B<J(D)and not(D[B][1]and D[B][2].lower()==T):B+=1
   if B<J(D):N=s[I:D[B].start()];C+=f'<img src="{N}">';I=D[B].end();F=B
   else:C+=E[0]
  elif A in'colorsize':
   H=U if A==U else'font-size'
   if F in K:C+=f'<span style="{H}:{M}">';G+=[(A,F)]
   else:C+=E[0]
  else:C+=E[0]
  F+=1
 return C+s[I:]
Source Link
qarz
  • 421
  • 2
  • 7

Python 3.12, 1491 bytes

J=len
import re
def A(s):
 U='color';T='img';S='url';R='span';Q='strong';L='code';Z='blockquote';G=[];C='';I=F=0;K={};D=list(re.finditer('\\[(/?)(\\w+)(?:=([^\\]]+))?\\]',s,re.I))
 for(B,E)in enumerate(D):
  if E[1]:continue
  A=E[2].lower();H=B+1;O=A==L
  while H<J(D):
   if D[H][2].lower()==A:
    if A==L:O=O+1-2*bool(D[H][1])
    if A==L and O==0 or A!=L and D[H][1]:K[B]=H;break
   H+=1
 while F<J(D):
  E=D[F];C+=s[I:E.start()];I=E.end();A=E[2].lower();M=E[3];P=E[1]
  if G and G[-1][0]==L:
   if P and A==L and F==K[G[-1][1]]:C+='</code>';G.pop()
   else:C+=E[0]
  elif P:
   if G and G[-1][0]==A:C+=f"</{dict(b=Q,i='em',quote=Z,url='a',color=R,size=R).get(A,A)}>";G.pop()
   else:C+=E[0]
  elif A in'biuscode':
   if F in K:C+=f"<{dict(b=Q,i='em').get(A,A)}>";G+=[(A,F)]
   else:C+=E[0]
  elif A=='quote':
   if F in K:C+=f'<{Z}><cite>{M}</cite>'if M else f'<{Z}>';G+=[(A,F)]
   else:C+=E[0]
  elif A==S:
   if M:
    if F in K:C+=f'<a href="{M}">';G+=[(A,F)]
    else:C+=E[0]
   else:
    B=F+1
    while B<J(D)and not(D[B][1]and D[B][2].lower()==S):B+=1
    if B<J(D):N=s[I:D[B].start()];C+=f'<a href="{N}">{N}</a>';I=D[B].end();F=B
    else:C+=E[0]
  elif A==T:
   B=F+1
   while B<J(D)and not(D[B][1]and D[B][2].lower()==T):B+=1
   if B<J(D):N=s[I:D[B].start()];C+=f'<img src="{N}">';I=D[B].end();F=B
   else:C+=E[0]
  elif A in'colorsize':
   H=U if A==U else'font-size'
   if F in K:C+=f'<span style="{H}:{M}">';G+=[(A,F)]
   else:C+=E[0]
  else:C+=E[0]
  F+=1
 return C+s[I:]