🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: news.software.readers
Article #249322

--

#249322
From: Anon
Date: Mon, 26 Mar 2012 05:21
211 lines
9131 bytes
ďťż<PRE>
Below is some code ( Jeff-Relf.Me/X.CPP ) I wrote
to convert HTML to plain text. ( emails, websites, posts )

As simple as it is, slrn can't do it.
Forte Agent tries but fails; it's mostly about NZB files, videos. 
Mozilla Thunderbird is flawless, Win Live Mail is OK⋅ish.
 
I use one of three browsers, depending on what I want to do.
1: X.EXE ( X.CPP, like Lynx but with images )
2: FireFox ( NoScript, Jeff-Relf.Me/userContent.CSS.TXT, etc. )
3: Internet Explorer ( for animations/crap )

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.
  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 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 )
#define  While( Should_Loop )  Ch = 1, P-- ;  \
  while( Ch−1 = Ch, Ch && ( Ch = *++P, Ch2 = !Ch ? 0 : P[1], Should_Loop ) )

#define  While²( Should_Loop )  Ch = 1, P²-- ;  \
  while( Ch−1 = Ch, Ch && ( Ch = *++P², Ch2 = !Ch ? 0 : P²[1], Should_Loop ) )

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

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

#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 PRE, inTag, Open⋅A, Indent ;  LnT _id, _Alt, _Src, _hRef ;
  //  Remove trailing spaces and leading (blank) lines.
  //  PRE tags are honored.
rm⋅Trailing() {
  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 ;  } }

Br() { int rv ;  rm⋅Trailing();
  if ( !PRE && !_Alt.BB && !_Src.BB && !_hRef.BB && !_id.BB ) {
    //  Print '↡' instead of a new line, conserve screen space. 
    Cat( L'↡' ); return ;  }

  if ( Open⋅A ) {
    //  Don't wrap link⋅labels.
    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 )
      //  Print (tag) IDs, one per line.
      if ( Human( B ), PP == BB && **Ln.PP != L'⌗' )
        //  First ID, give it its own 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 ) {

  if ( Ch = *P )
    //  Return, no need for another (input, HTML) line.
    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.
    rm⋅Trailing(); Buff⋅Limited  return 0 ;  }

  //  Set Ch and Ch2 to the first two characters.
  Ch = *B, Ch2 = !Ch ? 0 : B[1];  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, 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 = 0, P = 0, Alt, Src, id, hRef = 0 ;
  int PlainText ;
  Zero( _Alt ), Zero( _Src ), Zero( _hRef ), Zero( _id );
  PlainText = PRE = Ch = Indent = Open⋅A = 0 ;

  ➤BoTag&:  inTag = 0 ;  feedMe
  if ( PlainText ) {
    //  PlainText, no HTML.
    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 NonText ;
  { LnP  Bš, B², P², Eš, E² ;  int Szš, Sz², Szš⁺², Sz³, SP, SP², Ch, Ch2 ;
    //  Remove leading spaces.
    if ( B¹ = P² = E¹ = *Ln.PP, Sz¹ = szStr( P² ), E¹ += Sz¹, *P² == L'↡' ) {
      While²( Ch == L'↡' || Ch <= 32 );
      Szš -= P² - Bš, wmemmove( Bš, P², Szš + 1 );  }

    //  Append 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 ) goto NonText ;
    if ( PRE ) { Cat( B², Sz² ); goto NonText ;  }
    Szš = szStr( Bš );
    //  Wrap the ( now larger ) line.
    //  ( ColsWin - 2, 126 columns, monospaced ). 
    LOOP {
    
      if ( Sz³ = ColsWin - 2, Szš⁺² = Szš + Sz², Szš⁺² <= Sz³ ) {
        //  Nothing to wrap, grow the line and/or break.
        Cat( B², Sz² ); break ;  }

      E² = B² + Sz³ - Szš ; WhileBac²( Iden( Ch ) );
      if ( Sz³ = P² - B² + 1, Sz³ >= 22 )
        //  Add part of B² ( no less than 22 characters )
        //  to the current line. 
        Cat( B², Sz³ ), Sh(0), Szš = 0 ; 
      else
        //  Give B² its own line.
        Sh( L"%.*s", Szš = Sz³, B² );
      
      //  Set B² to the not⋅yet⋅printed. ( or blank it )
      Sz² -= Sz³, B² = ++P² ;  } }
    
  NonText: //  Parse the HTML tag.
    inTag = 1 ;  if ( !Ch ) goto ➤BoTag& ;
Technical Details
Message-ID: <Jeff-Relf.Me@2012_Mar.25^10.21^PM>
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!border1.nntp.ams3.giganews.com!border1.nntp.ams2.giganews.com!border3.nntp.ams.giganews.com!border1.nntp.ams.giganews.com!nntp.giganews.com!usenetcore.com!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!news.glorb.com!news-spur1.glorb.com!newsread.glorb.com!.POSTED!not-for-mail

Show Full Headers
Complete Article Headers
Message-ID: <Jeff-Relf.Me@2012_Mar.25^10.21^PM>
Subject: --
Date: Mon, 26 Mar 2012 05:21:37 +0000
From: @Jeff-Relf.Me
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!border1.nntp.ams3.giganews.com!border1.nntp.ams2.giganews.com!border3.nntp.ams.giganews.com!border1.nntp.ams.giganews.com!nntp.giganews.com!usenetcore.com!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!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: 211
NNTP-Posting-Host: XJGH+5GM8EvggnDfgIBqtQ.319.posting.glorb.com
Content-Type: Text/HTML; charset=UTF-8
X-Complaints-To: abuse@glorb.com
NNTP-Posting-Date: Mon, 26 Mar 2012 05:21:37 +0000 (UTC)
User-Agent: Jeff-Relf.Me/X.TXT
X-Notice: Scanned by Mr. Bill
X-Original-Bytes: 9040