25 #define YUILogComponent "ncurses"
26 #include <yui/YUILog.h>
27 #include "NCRichText.h"
28 #include "YNCursesUI.h"
29 #include "stringutil.h"
32 #include <boost/algorithm/string.hpp>
34 #include <yui/YMenuItem.h>
35 #include <yui/YApplication.h>
40 const unsigned NCRichText::listindent = 4;
41 const std::wstring NCRichText::listleveltags( L
"@*+o#-%$&" );
43 const bool NCRichText::showLinkTarget =
false;
45 std::map<std::wstring, std::wstring> NCRichText::_charentity;
49 const std::wstring NCRichText::entityLookup(
const std::wstring & val_r )
52 std::wstring::size_type hash = val_r.find( L
"#", 0 );
53 std::wstring ascii = L
"";
55 if ( hash != std::wstring::npos )
57 std::wstring s = val_r.substr( hash + 1 );
60 boost::replace_all( s,
"x",
"0x" );
62 long int c = std::wcstol( s.c_str(), &endptr, 0 );
66 if ( s.c_str() != endptr )
68 std::wostringstream ws;
74 #define REP(l,r) _charentity[l] = r
75 if ( _charentity.empty() )
80 NCstring::RecodeToWchar( YUI::app()->productName(),
"UTF-8", &product );
86 REP( L
"quot", L
"\"" );
87 REP( L
"product", product );
90 std::map<std::wstring, std::wstring>::const_iterator it = _charentity.find( val_r );
92 if ( it != _charentity.end() )
118 const std::wstring NCRichText::filterEntities(
const std::wstring & text )
120 std::wstring txt = text;
123 for ( std::wstring::size_type special = txt.find( L
"&" );
124 special != std::wstring::npos;
125 special = txt.find( L
"&", special + 1 ) )
127 std::wstring::size_type colon = txt.find( L
";", special + 1 );
129 if ( colon == std::wstring::npos )
132 const std::wstring repl = entityLookup( txt.substr( special + 1, colon - special - 1 ) );
135 || txt.substr( special + 1, colon - special - 1 ) == L
"product" )
137 txt.replace( special, colon - special + 1, repl );
140 yuiDebug() <<
"porn.bat" << std::endl;
147 void NCRichText::Anchor::draw(
NCPad & pad,
const chtype attr,
int color )
155 pad.
chgat( -1, attr, color );
162 pad.
chgat( ecol - c, attr, color );
166 NCRichText::NCRichText( YWidget * parent,
const std::string & ntext,
168 : YRichText( parent, ntext, plainTextMode )
171 , plainText( plainTextMode )
181 activeLabelOnly =
true;
186 NCRichText::~NCRichText()
192 int NCRichText::preferredWidth()
194 return wGetDefsze().W;
198 int NCRichText::preferredHeight()
200 return wGetDefsze().H;
207 YRichText::setEnabled( do_bv );
211 void NCRichText::setSize(
int newwidth,
int newheight )
213 wRelocate(
wpos( 0 ),
wsze( newheight, newwidth ) );
217 void NCRichText::setLabel(
const std::string & nlabel )
220 NCPadWidget::setLabel(
NCstring( nlabel ) );
224 void NCRichText::setValue(
const std::string & ntext )
228 YRichText::setValue( ntext );
233 void NCRichText::wRedraw()
238 bool initial = ( !
myPad() || !
myPad()->Destwin() );
240 if ( !( plainText || anchors.empty() ) )
243 NCPadWidget::wRedraw();
245 if ( initial && autoScrollDown() )
254 void NCRichText::wRecoded()
275 if ( !( plainText || anchors.empty() ) )
282 if ( armed != Anchor::unset )
284 ret = NCursesEvent::menu;
286 NCstring::RecodeFromWchar( anchors[armed].target,
"UTF-8", &str );
287 yuiMilestone() <<
"LINK: " << str << std::endl;
289 ret.selection = NULL;
299 NCPad * NCRichText::CreatePad()
301 wsze psze( defPadSze() );
303 NCPad * npad =
new NCPad( psze.H, textwidth, *
this );
308 void NCRichText::DrawPad()
311 yuiDebug() <<
"Start: plain mode " << plainText << std::endl
312 <<
" padsize " <<
myPad()->size() << std::endl
313 <<
" text length " << text.str().size() << std::endl;
328 void NCRichText::DrawPlainPad()
333 AdjustPad(
wsze( ftext.Lines(), ftext.Columns() ) );
337 for ( NCtext::const_iterator line = ftext.begin();
338 line != ftext.end(); ++line, ++cl )
344 void NCRichText::PadPreTXT(
const wchar_t * osch,
const unsigned olen )
346 std::wstring wtxt( osch, olen );
349 wtxt = filterEntities( wtxt );
355 const wchar_t * sch = wtxt.data();
369 inline void SkipToken(
const wchar_t *& wch )
375 while ( *wch && *wch != L
'>' );
382 static std::wstring WStoken( L
" \n\t\v\r\f" );
385 inline void SkipWS(
const wchar_t *& wch )
391 while ( *wch && WStoken.find( *wch ) != std::wstring::npos );
395 static std::wstring WDtoken( L
" <\n\t\v\r\f" );
398 inline void SkipWord(
const wchar_t *& wch )
404 while ( *wch && WDtoken.find( *wch ) == std::wstring::npos );
407 static std::wstring PREtoken( L
"<\n\v\r\f" );
410 inline void SkipPreTXT(
const wchar_t *& wch )
416 while ( *wch && PREtoken.find( *wch ) == std::wstring::npos );
424 void NCRichText::AdjustPrePad(
const wchar_t *osch )
426 const wchar_t * wch = osch;
427 std::wstring wstr( wch, 6 );
432 wstr.assign( wch, 6 );
434 while ( *wch && wstr != L
"</pre>" );
436 std::wstring wtxt( osch, wch - osch );
439 wtxt = filterEntities( wtxt );
442 boost::replace_all( wtxt, L
"<br>", L
"\n" );
443 boost::replace_all( wtxt, L
"<br/>", L
"\n" );
450 std::list<NCstring>::const_iterator line;
454 for ( line = ftext.Text().begin(); line != ftext.Text().end(); ++line )
458 tmp_len = textWidth( (*line).str() );
460 if ( tmp_len > llen )
465 if ( llen > textwidth )
468 AdjustPad(
wsze( cl + ftext.Lines(), llen ) );
473 void NCRichText::DrawHTMLPad()
477 liststack = std::stack<int>();
480 armed = Anchor::unset;
488 const wchar_t * wch = (
wchar_t * )text.str().data();
489 const wchar_t * swch = 0;
521 yuiDebug() <<
"Ignoring " << *wch << std::endl;
532 if ( PadTOKEN( swch, wch ) )
543 PadTXT( swch, wch - swch );
548 PadPreTXT( swch, wch - swch );
556 AdjustPad(
wsze( cl, textwidth ) );
559 yuiDebug() <<
"Anchors: " << anchors.size() << std::endl;
561 for (
unsigned i = 0; i < anchors.size(); ++i )
563 yuiDebug() << form(
" %2d: [%2d,%2d] -> [%2d,%2d]",
565 anchors[i].sline, anchors[i].scol,
566 anchors[i].eline, anchors[i].ecol ) << std::endl;
572 inline void NCRichText::PadNL()
576 if ( ++cl == (
unsigned)
myPad()->height() )
578 AdjustPad(
wsze(
myPad()->height() + defPadSze().H, textwidth ) );
587 inline void NCRichText::PadBOL()
594 inline void NCRichText::PadWS(
bool tab )
599 if ( cc == textwidth )
611 inline void NCRichText::PadTXT(
const wchar_t * osch,
const unsigned olen )
613 std::wstring txt( osch, olen );
615 txt = filterEntities( txt );
617 size_t len = textWidth( txt );
619 if ( !atbol && cc + len > textwidth )
623 const wchar_t * sch = txt.data();
628 cc += wcwidth( *sch );
631 if ( cc >= textwidth )
647 size_t NCRichText::textWidth( std::wstring wstr )
650 std::wstring::const_iterator wstr_it;
652 for ( wstr_it = wstr.begin(); wstr_it != wstr.end() ; ++wstr_it )
655 if ( iswprint( *wstr_it ) )
657 len += wcwidth( *wstr_it );
659 else if ( *wstr_it ==
'\t' )
672 inline void NCRichText::PadSetAttr()
675 chtype nbg = style.plain;
681 else if ( Tattr & T_HEAD )
687 switch ( Tattr & Tfontmask )
713 case T_BOLD|T_IT|T_TT:
723 void NCRichText::PadSetLevel()
725 cindent = listindent * liststack.size();
727 if ( cindent > textwidth / 2 )
728 cindent = textwidth / 2;
738 void NCRichText::PadChangeLevel(
bool down,
int tag )
742 if ( liststack.size() )
747 liststack.push( tag );
754 void NCRichText::openAnchor( std::wstring args )
756 canchor.open( cl, cc );
758 const wchar_t * ch = (
wchar_t * )args.data();
759 const wchar_t * lookupstr = L
"href = ";
760 const wchar_t * lookup = lookupstr;
762 for ( ; *ch && *lookup; ++ch )
764 wchar_t c = towlower( *ch );
771 if ( *lookup != L
' ' )
777 if ( *lookup == L
' ' )
800 const wchar_t * delim = ( *ch == L
'"' ) ? L
"\"" : L
" \t";
801 args = ( *ch == L
'"' ) ? ++ch : ch;
803 std::wstring::size_type end = args.find_first_of( delim );
805 if ( end != std::wstring::npos )
808 canchor.target = args;
812 yuiError() <<
"No value for 'HREF=' in anchor '" << args <<
"'" << std::endl;
817 void NCRichText::closeAnchor()
819 canchor.close( cl, cc );
821 if ( canchor.valid() )
822 anchors.push_back( canchor );
829 bool NCRichText::PadTOKEN(
const wchar_t * sch,
const wchar_t *& ech )
832 if ( *sch++ != L
'<' || *( ech - 1 ) != L
'>' )
836 bool endtag = ( *sch == L
'/' );
842 if ( ech - sch <= 1 )
845 std::wstring value( sch, ech - 1 - sch );
849 std::wstring::size_type argstart = value.find_first_of( L
" \t\n" );
851 if ( argstart != std::wstring::npos )
853 args = value.substr( argstart );
854 value.erase( argstart );
857 for (
unsigned i = 0; i < value.length(); ++i )
859 if ( isupper( value[i] ) )
861 value[i] =
static_cast<char>( tolower( value[i] ) );
867 int headinglevel = 0;
869 TOKEN token = T_UNKNOWN;
871 switch ( value.length() )
875 if ( value[0] ==
'b' ) token = T_BOLD;
876 else if ( value[0] ==
'i' ) token = T_IT;
877 else if ( value[0] ==
'p' ) token = T_PAR;
878 else if ( value[0] ==
'a' ) token = T_ANC;
879 else if ( value[0] ==
'u' ) token = T_BOLD;
884 if ( value == L
"br" ) token = T_BR;
885 else if ( value == L
"em" ) token = T_IT;
886 else if ( value == L
"h1" ) { token = T_HEAD; headinglevel = 1; }
887 else if ( value == L
"h2" ) { token = T_HEAD; headinglevel = 2; }
888 else if ( value == L
"h3" ) { token = T_HEAD; headinglevel = 3; }
889 else if ( value == L
"hr" ) token = T_IGNORE;
890 else if ( value == L
"li" ) token = T_LI;
891 else if ( value == L
"ol" ) { token = T_LEVEL; leveltag = 1; }
892 else if ( value == L
"qt" ) token = T_IGNORE;
893 else if ( value == L
"tt" ) token = T_TT;
894 else if ( value == L
"ul" ) { token = T_LEVEL; leveltag = 0; }
900 if ( value == L
"big" ) token = T_IGNORE;
901 else if ( value == L
"pre" ) token = T_PLAIN;
906 else if ( value == L
"br/" ) token = T_BR;
907 else if ( value == L
"hr/" ) token = T_IGNORE;
912 if ( value == L
"bold" ) token = T_BOLD;
913 else if ( value == L
"code" ) token = T_TT;
914 else if ( value == L
"font" ) token = T_IGNORE;
919 if ( value == L
"large" ) token = T_IGNORE;
920 else if ( value == L
"small" ) token = T_IGNORE;
925 if ( value == L
"center" ) token = T_PAR;
926 else if ( value == L
"strong" ) token = T_BOLD;
931 if ( value == L
"blockquote" ) token = T_PAR;
941 if ( token == T_UNKNOWN )
943 yuiDebug() <<
"T_UNKNOWN :" << value <<
":" << args <<
":" << std::endl;
949 if ( token == T_IGNORE )
955 PadChangeLevel( endtag, leveltag );
959 if ( endtag && !cindent )
978 if ( headinglevel && endtag )
1003 if ( liststack.empty() )
1005 tag = std::wstring( listindent, L
' ' );
1011 if ( liststack.top() )
1013 swprintf( buf, 15, L
"%2ld. ", liststack.top()++ );
1017 swprintf( buf, 15, L
" %lc ", listleveltags[liststack.size()%listleveltags.size()] );
1024 cc = ( tag.size() < cc ? cc - tag.size() : 0 );
1028 PadTXT( tag.c_str(), tag.size() );
1040 AdjustPrePad( ech );
1084 void NCRichText::arm(
unsigned i )
1096 if ( armed != Anchor::unset )
1099 anchors[armed].draw( *
myPad(), wStyle().richtext.getArmed( GetState() ), 0 );
1106 if ( armed != Anchor::unset )
1108 anchors[armed].draw( *
myPad(), wStyle().richtext.link, (
int) wStyle().richtext.visitedlink );
1109 armed = Anchor::unset;
1112 if ( i != Anchor::unset )
1115 anchors[armed].draw( *
myPad(), wStyle().richtext.getArmed( GetState() ), 0 );
1118 if ( showLinkTarget )
1120 if ( armed != Anchor::unset )
1121 NCPadWidget::setLabel(
NCstring( anchors[armed].target ) );
1123 NCPadWidget::setLabel(
NCstring() );
1143 if ( plainText || anchors.empty() )
1148 vScrollFirstvisible = start;
1150 vScrollNextinvisible = start + visible;
1152 if ( armed != Anchor::unset )
1154 if ( anchors[armed].within( vScrollFirstvisible, vScrollNextinvisible ) )
1160 for (
unsigned i = 0; i < anchors.size(); ++i )
1162 if ( anchors[i].within( vScrollFirstvisible, vScrollNextinvisible ) )
1171 bool NCRichText::handleInput( wint_t key )
1173 if ( plainText || anchors.empty() )
1175 return NCPadWidget::handleInput( key );
1179 bool handled =
true;
1186 unsigned newarmed = Anchor::unset;
1188 if ( armed == Anchor::unset )
1191 for (
unsigned i = anchors.size(); i; )
1195 if ( anchors[i].eline < vScrollFirstvisible )
1202 else if ( armed > 0 )
1204 newarmed = armed - 1;
1207 if ( newarmed == Anchor::unset )
1209 handled = NCPadWidget::handleInput( KEY_UP );
1213 if ( !anchors[newarmed].within( vScrollFirstvisible, vScrollNextinvisible ) )
1225 unsigned newarmed = Anchor::unset;
1227 if ( armed == Anchor::unset )
1230 for (
unsigned i = 0; i < anchors.size(); ++i )
1232 if ( anchors[i].sline >= vScrollNextinvisible )
1239 else if ( armed + 1 < anchors.size() )
1241 newarmed = armed + 1;
1244 if ( newarmed == Anchor::unset )
1246 handled = NCPadWidget::handleInput( KEY_DOWN );
1250 if ( !anchors[newarmed].within( vScrollFirstvisible, vScrollNextinvisible ) )
1262 if ( armed != Anchor::unset
1264 && anchors[armed-1].within( vScrollFirstvisible, vScrollNextinvisible ) )
1270 handled = NCPadWidget::handleInput( key );
1278 if ( armed != Anchor::unset
1279 && armed + 1 < anchors.size()
1280 && anchors[armed+1].within( vScrollFirstvisible, vScrollNextinvisible ) )
1286 handled = NCPadWidget::handleInput( key );
1292 handled = NCPadWidget::handleInput( key );
1299 std::string NCRichText::vScrollValue()
const
1306 return std::to_string( mypad->CurPos().L );
1310 void NCRichText::setVScrollValue(
const std::string & newValue )
1314 if ( !mypad || newValue.empty() )
1317 if ( newValue ==
"minimum" )
1319 else if ( newValue ==
"maximum" )
1325 mypad->
ScrlLine( std::stoi( newValue ) );
1329 yuiError() <<
"failed to set vertical scroll value '" << newValue <<
"'" << endl;
1335 std::string NCRichText::hScrollValue()
const
1342 return std::to_string( mypad->CurPos().C );
1346 void NCRichText::setHScrollValue(
const std::string & newValue )
1350 if ( !mypad || newValue.empty() )
1353 if ( newValue ==
"minimum" )
1355 else if ( newValue ==
"maximum" )
1361 mypad->
ScrlCol( std::stoi( newValue ) );
1365 yuiError() <<
"failed to set horizontal scroll value '" << newValue <<
"'" << endl;