šŸš€ go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: news.software.readers
Article #249355

--

#249355
From: Anon
Date: Tue, 27 Mar 2012 18:49
219 lines
9937 bytes
<PRE>
The PRE tag ( HTML ) looks best on Google Groups because
it overrides the default: variableā‹…width glyphs, autoā‹…wrapped.

slrn should render HTML as plain text ( a simple task ) because
you ( Peterā‹…Jā‹…Ross ) might want three HTML viewers:
slrn, Lynx and/or FireFox.  Use the default, slrn, or an alternate.
ā‹…ā‹…ā‹…ā‹…ā‹…ā‹…ā‹…ā‹…
I'm fully aware that the code I post benefits no one but me,
no need to remind me; my "hideously obfuscated" code isn't for you.
To me, X.CPP is priceless, literally; "Virtue[work] is its own reward".

When I post code, I think about it āˆ’āˆ’ both before and after āˆ’āˆ’
prompting serious changes āˆ’āˆ’ rewrites āˆ’āˆ’ that's why I do it.

>From "Jeff-Relf.Me/X.CPP", the revised code:

typedef wchar_t  *LnP ; typedef  LnP  *LnA ;
Sh( LnP S, ... ); //  Like "printf()", for my custom console.

  struct  LnT { LnA  BB, PP, EE ;  };
  //  The console lines, a dynamic array of pointers.
  //  "*Ln.PP" is the bottomā‹…most line.
  LnT  Ln ;
  //  Allocate from a temporary heap; never use "free()" or "realloc()".
LnP MallocTmp( int Sz ) { LnP  M ;
  if ( Sz <= 0 || ( M = (LnP)HeapAlloc( Heap, 0, Sz ), !M ) )
    exit(1);  //  Panic ! ( never happens [ borked heap ] )

  return M ;  }

//  Grow the bottomā‹…most console line ( *Ln.PP )
//  Sz² characters ( from B², as passed ).
Cat( LnP B², int Sz² ) { if ( Sz² <= 0 ) return;
  LnP &amp;B = *Ln.PP, B³ ; int Sz³ = szStr( B ) + Sz² + 1 ;
  B³ = MallocTmp( Sz³ * szChr ),
  Str( B³, L"%s%.*s", B, Sz², B² ), B = B³ ;  }

Cat( wchar_t Ch ) { Cat( &amp;Ch, 1 );  }
ā‹…ā‹…ā‹…ā‹…ā‹…ā‹…ā‹…ā‹…ā‹…ā‹…
//  Handy macros.
#define  LOOP  while(1)
#define  Loop( N )  int J = - 1, LLL = N ; while( ++J < LLL )
//  "While(0)" (for example) loops zero times and doesn't alter P,
//  it it's a good way to set Ch and Ch2 to P[0] and P[1].
//
//  The macro is "while( A && B )", basically.
//  B sets Ch and increments P.
//
//  B won't happen unless A is true.  First time through the loop,
//  A is true.  Second pass, A is false if, initially, "*P == 0".
#define  While( ↻ )  Ch = 1, P-- ; while( ( Chāˆ’1 = Ch ) && ( Ch = *++P, Ch2 = !Ch ? 0 : P[1], ↻ ) )
#define  While²( ↻ )  Ch = 1, P²-- ; while( ( Chāˆ’1 = Ch ) && ( Ch = *++P², Ch2 = !Ch ? 0 : P²[1], ↻ ) )

#define  WhileBac( ↻ )  \
  Ch = Ch2 = 0;  if ( !E )  E = B + szStr( B );  P = E ;  \
  while ( P >= B && ( Ch2 = *P, ( Ch = --P < B ? 0 : *P ) && ( ↻ ) ) )

#define  WhileBac²( ↻ )  \
  Ch = Ch2 = 0;  if ( !E² )  E² = B² + szStr( B² );  P² = E² ;  \
  while ( P² >= B² && ( Ch2 = *P², ( Ch = --P² < B² ? 0 : *P² ) && ( ↻ ) ) )

#define  Zero( X )  memset( & X, 0, sizeof X )

//  Loop though the console lines ( or any dynamic array ).
#define  LoopXx( Tt, Xx )  \
  Tt##P P = 0, B ; Tt##A BB = (Xx).BB,  \
  EE = (Xx).PP + 1, PP = BB - 1 ;  \
  if ( BB ) while( B = P = *++PP, PP < EE )
ā‹…ā‹…ā‹…ā‹…ā‹…ā‹…ā‹…ā‹…ā‹…ā‹…
  int Firstā‹…Line, PRE, inTag, Openā‹…A, Indent ;  LnT _id, _Alt, _Src, _hRef ;
  //  Remove trailing spaces and leading (blank) lines.
  //  The PRE tag is honored.
Trim() {
  if ( Firstā‹…Line ) {
    //  The first newline, don't trim anything.
    Firstā‹…Line = 0; return;  }

  LOOP { B = *Ln.PP, E = 0 ;  WhileBac( Ch == L'↔' || Ch <= 32 || Ch == L'\xA0' );
    if ( ++P < E ) *P = 0 ;  if ( !PRE && !*B ) Ln.PP-- ; else break ;  }
    
  if ( P = B, *B == L'↔' ) {
    //  '↔' replaced newlines; remove them ( and whitespace )
    //  from the start of the (console) line.
    While( Ch == L'↔' || Ch <= 32 );  wmemmove( B, P, E - P + 1 ), P = B;  }
  
  //  Set Ch and Ch2 to the first two characters.  
   While(0);  }

Br() {
  if ( Trim(), !PRE && Ch != L'āŒ—' && !( Ch == '\t' && Ch2 == L'\xA0' )
    //  URI lines start with "\t\A0" and
    //  'āŒ—'( U+2317 ) starts a line of tag IDs.
    //
    //  If the line above isn't a URI or tag IDs, print '↔'
    //  instead of a new line ( to conserve screen space ).
    Cat( L'↔' ); return ;  }

  if ( Openā‹…A ) {
    //  Don't wrap text "buttons".
    Cat( 32 ); return ;  }

  if ( _hRef.BB ) {
    LoopXx( Ln, _hRef )
      //  Print out URIs, one per line.
      Sh( L"\t\xA0%s%s", ⁺Folder( B ), Human( B ) );
    Zero( _hRef );  }

  if ( _Alt.BB ) {
    LoopXx( Ln, _Alt )
      //  Print out "alternate (img) text", one per line.
      Sh( L"\t\xA0%s", Human( B ) );
    Zero( _Alt );  }

  if ( _Src.BB ) {
    LoopXx( Ln, _Src )
      //  Print out (img) URIs, one per line.
      Sh( L"\t\xA0%s%s", ⁺Folder( B ), Human( B ) );
    Zero( _Src );  }

  if ( _id.BB ) {
    LoopXx( Ln, _id )
      if ( Human( B ), **Ln.PP != L'āŒ—' )
        //  If the current (console) line is tag IDs, grow it;
        //  otherwise, start a new line.
        Sh( L"āŒ—%s", B );
      else
        //  Concatenate IDs to the previous ones.
        rv = Str( _T, L", āŒ—%s", B ), Cat( _T, rv );

    Zero( _id );  }

  //  Print a blank line (Br), possibly indented.
  Sh( L"%.*s", Indent*2, L"          " );  }

  //  Up to 9,999 lines of HTML can be converted to plainā‹…text.
  const int mxā‹…Lnsā‹…Decode = 9999 ;  LnA  BBā‹…Decode, EEā‹…Decode ;
  LnP  FileName, Wide, BBā‹…Decodeā‹…[ mxā‹…Lnsā‹…Decode ];

  #define Initā‹…Buffā‹…Limited  \
    int iPP = Ln.PP - Ln.BB ;  BBā‹…Decode = BBā‹…Decodeā‹… ;

  //  Save off the console lines ( Ln ).
  #define Buffā‹…Limited  \
    { LnA EE = Ln.PP + !!**Ln.PP, BB = Ln.PP = Ln.BB + iPP ;  \
      int Lines = er( mxā‹…Lnsā‹…Decode, EE - ++BB );  \
      EEā‹…Decode = BBā‹…Decode + Lines,  \
      memmove( BBā‹…Decode, BB, Lines * szPtr ) ;  }

  //  Get the next line or cleanup/exit.
  #define feedMe  { if ( !feedMeā‚ā‚Ž( B, P, PP, EE, iPP ) ) return ;  }
int feedMeā‚ā‚Ž( LnP &amp;B, LnP &amp;P, LnA &amp;PP, LnA EE, int iPP ) {
  //  Set Ch and Ch2 to the first two characters of the line (P).
  While(0);
  if ( Ch )
    //  Keep parsing the current line, don't fetch a new one.
    return 1 ;

  if ( PRE && !inTag )
    Br();
  else { LnP &amp;T = *Ln.PP ;
    // Print a space ( instead of a newline ).
    if ( rv = szStr( T ), rv && T[ rv - 1 ] > 32 ) Cat( 32 );  }

  if ( ++PP >= EE || ( P = B = *PP, B && MimeSep && EqN( B, *MimeSep, Sz_MimeSep ) ) ){
    //  Cleanup and exit.
    Trim(); Buffā‹…Limited  return 0 ;  }

  While(0);  return 1 ;  }

  //  Indentify an HTML tag/entity.
  #define Chā‹…BoTag& ( \
       Ch == '&' && ( Ch2 == '#' || isAlpha( Ch2 ) )  \
    || Ch == '<' && ( Ch2 == '!' || Ch2 == '?' \
    || isAlpha( Ch2 ) || Ch2 == '/' && isAlpha( P[2] ) ) )

//  Convert (console) lines; from BB to EE.
HTMLā‹…toā‹…Text( LnA  BB, LnA  EE ) {
  static rv ;  Initā‹…Buffā‹…Limited  LnA PP = BB - 1 ;
  //  Print a blank line.
  Sh(0);
  LnP  euText, B = L"", P = B, Alt, Src, id, hRef = 0 ; int PlainText ;
  Zero( _Alt ), Zero( _Src ), Zero( _hRef ), Zero( _id );
  Firstā‹…Line = 1, PlainText = PRE = Ch = Indent = Openā‹…A = 0 ;
  āž¤BoTag&:  inTag = 0 ;  feedMe
  if ( PlainText ) {
    //  Don't parse anything.
    euText = P, P += szStr( P ), Ch = 0 ;  }
  else {
    // Parse out HTML tags/entities and text.
    euText = 0 ; While( !Chā‹…BoTag& ) if ( !euText && Ch ) euText = P ;  }

  LnP  BoTag& = P ; LnA  BoTagPP = PP ;  if ( !euText ) goto Parseā‹…Tag ;
  { LnP  B¹, B², P², E¹, E² ;  int Sz¹, Sz², Sz¹⁺², Sz³, SP, SP², Ch, Ch2 ;
    B¹ = E¹ = *Ln.PP, Sz¹ = szStr( B¹ ), E¹ += Sz¹ ;
    //  Add parsedā‹…out text ( euText, B² ) to the current line ( B¹ ).
    if ( B² = euText, E² = BoTag&, !PRE ) {
      //  Consolidate spaces. PRE tags are honored.
      Ch = !Sz¹ ? 0 : E¹[-1], SP = Ch <= 32, P² = B², D = B² - 1 ;
      While²( P² < E² )
        if ( SP² = Ch <= 32 || Ch == L'\xFeFF', !SP && SP² ) *++D = 32, SP = 1 ;
        else if ( !SP² ) *++D = Ch, SP = 0 ;

      // E² marks the end of the (now smaller) line, B².
      E² = ++D ;  }

    if ( Sz² = E² - B², Sz² <= 0 || Sz² == 1 && *B² <= 32 )
      //  Skip the printing, the line is blank.
      goto Parseā‹…Tag ;

    if ( PRE ) { Cat( B², Sz² ); goto Parseā‹…Tag ;  }
    //  Print the parsedā‹…out text, wrapped to ColsWin - 2,
    //  125 columns, monospaced.
    LOOP { if ( Sz³ = ColsWin - 2, Sz¹⁺² = Sz¹ + Sz², Sz¹⁺² <= Sz³ ) { Cat( B², Sz² ); break ;  }
      E² = B² + Sz³ - Sz¹ ; WhileBac²( Iden( Ch ) );
      if ( Sz³ = P² - B² + 1, Sz³ >= 22 ) Cat( B², Sz³ ), Sh(0), Sz¹ = 0 ; 
      else Sh( L"%.*s", Sz¹ = Sz³, B² );   Sz² -= Sz³, B² = ++P² ;  } }
    
  Parseā‹…Tag:  inTag = 1 ;  if ( !Ch ) goto āž¤BoTag& ;
Technical Details
Message-ID: <Jeff-Relf.Me@2012_Mar.27^11.49^AM>
Path: mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!illumos!news.pugleaf.net!.TX!i2pn2.pugleaf.net!archive.newsdeef.eu!mbox2nntp-alt.free.newsservers.20150605.mbox.gz!num2.nntp.dca.giganews.com!num1.nntp.dca.giganews.com!number.nntp.dca.giganews.com!border3.nntp.dca.giganews.com!border1.nntp.dca.giganews.com!nntp.giganews.com!aqua.octanews.net!news-out.octanews.net!indigo.octanews.net!news.glorb.com!news-spur1.glorb.com!newsread.glorb.com!.POSTED!not-for-mail
References: <Jeff-Relf.Me@2012_Mar.25^10.21^PM> <slrnjn1dhq.nii.pjr@pjr.no-ip.org>

Show Full Headers
Complete Article Headers
Message-ID: <Jeff-Relf.Me@2012_Mar.27^11.49^AM>
Subject: --
Date: Tue, 27 Mar 2012 18:49:40 +0000
From: @Jeff-Relf.Me
References: <Jeff-Relf.Me@2012_Mar.25^10.21^PM> <slrnjn1dhq.nii.pjr@pjr.no-ip.org>
Path: mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!illumos!news.pugleaf.net!.TX!i2pn2.pugleaf.net!archive.newsdeef.eu!mbox2nntp-alt.free.newsservers.20150605.mbox.gz!num2.nntp.dca.giganews.com!num1.nntp.dca.giganews.com!number.nntp.dca.giganews.com!border3.nntp.dca.giganews.com!border1.nntp.dca.giganews.com!nntp.giganews.com!aqua.octanews.net!news-out.octanews.net!indigo.octanews.net!news.glorb.com!news-spur1.glorb.com!newsread.glorb.com!.POSTED!not-for-mail
Newsgroups: comp.os.linux.advocacy, alt.free.newsservers, news.software.nntp, news.software.readers
 news.software.readers
Organization: Glorb Internet Services, http://www.glorb.com
Lines: 219
NNTP-Posting-Host: XJGH+5GM8EvggnDfgIBqtQ.436.posting.glorb.com
Content-Type: Text/HTML; charset=UTF-8
X-Complaints-To: abuse@glorb.com
NNTP-Posting-Date: Tue, 27 Mar 2012 18:49:41 +0000 (UTC)
User-Agent: Jeff-Relf.Me/X.TXT
X-Notice: Scanned by Mr. Bill