Article View: news.software.nntp
Article #84648--
From: Anon
Date: Mon, 26 Mar 2012 05:21
Date: Mon, 26 Mar 2012 05:21
211 lines
9131 bytes
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 &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( &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 &B, LnP &P, LnA &PP, LnA EE, int iPP ) {
if ( Ch = *P )
// Return, no need for another (input, HTML) line.
return 1 ;
if ( PRE && !inTag )
Br();
else { LnP &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-mailShow 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