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 yuiMilestone() <<
"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 )
180 yuiDebug() << std::endl;
181 activeLabelOnly =
true;
186 NCRichText::~NCRichText()
188 yuiDebug() << std::endl;
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()
266 if ( !( plainText || anchors.empty() ) )
273 if ( armed != Anchor::unset )
275 ret = NCursesEvent::menu;
277 NCstring::RecodeFromWchar( anchors[armed].target,
"UTF-8", &str );
278 yuiMilestone() <<
"LINK: " << str << std::endl;
279 ret.selection =
new YMenuItem( str );
290 NCPad * NCRichText::CreatePad()
292 wsze psze( defPadSze() );
294 NCPad * npad =
new NCPad( psze.H, textwidth, *
this );
299 void NCRichText::DrawPad()
302 <<
"Start: plain mode " << plainText << std::endl
303 <<
" padsize " <<
myPad()->size() << std::endl
304 <<
" text length " << text.str().size() << std::endl;
314 yuiDebug() <<
"Done" << std::endl;
318 void NCRichText::DrawPlainPad()
321 yuiDebug() <<
"ftext is " <<
wsze( ftext.Lines(), ftext.Columns() ) << std::endl;
323 AdjustPad(
wsze( ftext.Lines(), ftext.Columns() ) );
327 for ( NCtext::const_iterator line = ftext.begin();
328 line != ftext.end(); ++line, ++cl )
334 void NCRichText::PadPreTXT(
const wchar_t * osch,
const unsigned olen )
336 std::wstring wtxt( osch, olen );
339 wtxt = filterEntities( wtxt );
345 const wchar_t * sch = wtxt.data();
359 inline void SkipToken(
const wchar_t *& wch )
365 while ( *wch && *wch != L
'>' );
372 static std::wstring WStoken( L
" \n\t\v\r\f" );
375 inline void SkipWS(
const wchar_t *& wch )
381 while ( *wch && WStoken.find( *wch ) != std::wstring::npos );
385 static std::wstring WDtoken( L
" <\n\t\v\r\f" );
388 inline void SkipWord(
const wchar_t *& wch )
394 while ( *wch && WDtoken.find( *wch ) == std::wstring::npos );
397 static std::wstring PREtoken( L
"<\n\v\r\f" );
400 inline void SkipPreTXT(
const wchar_t *& wch )
406 while ( *wch && PREtoken.find( *wch ) == std::wstring::npos );
414 void NCRichText::AdjustPrePad(
const wchar_t *osch )
416 const wchar_t * wch = osch;
417 std::wstring wstr( wch, 6 );
422 std::list<NCstring>::const_iterator line;
423 std::wstring::const_iterator wstr_it;
428 wstr.assign( wch, 6 );
430 while ( *wch && wstr != L
"</pre>" );
432 std::wstring wtxt( osch, wch - osch );
435 wtxt = filterEntities( wtxt );
438 boost::replace_all( wtxt, L
"<br>", L
"\n" );
440 yuiDebug() <<
"Text: " << wtxt <<
" initial length: " << wch - osch << std::endl;
446 for ( line = ftext.Text().begin(); line != ftext.Text().end(); ++line )
450 for ( wstr_it = ( *line ).str().begin(); wstr_it != ( *line ).str().end() ; ++wstr_it )
453 if ( *wstr_it ==
'<' )
455 wstr_it = find(wstr_it, (*line).str().end(), L
'>');
457 else if ( *wstr_it ==
'\t' )
463 tmp_len += wcwidth( *wstr_it );
467 if ( tmp_len > llen )
471 if ( llen > textwidth )
474 AdjustPad(
wsze( cl + ftext.Lines(), llen ) );
479 void NCRichText::DrawHTMLPad()
481 yuiDebug() <<
"Start:" << std::endl;
483 liststack = std::stack<int>();
486 armed = Anchor::unset;
494 const wchar_t * wch = (
wchar_t * )text.str().data();
495 const wchar_t * swch = 0;
527 yuiDebug() <<
"Ignoring " << *wch << std::endl;
538 if ( PadTOKEN( swch, wch ) )
549 PadTXT( swch, wch - swch );
554 PadPreTXT( swch, wch - swch );
562 AdjustPad(
wsze( cl, textwidth ) );
564 yuiDebug() <<
"Anchors: " << anchors.size() << std::endl;
566 for (
unsigned i = 0; i < anchors.size(); ++i )
568 yuiDebug() << form(
" %2d: [%2d,%2d] -> [%2d,%2d]",
570 anchors[i].sline, anchors[i].scol,
571 anchors[i].eline, anchors[i].ecol ) << std::endl;
576 inline void NCRichText::PadNL()
580 if ( ++cl == (
unsigned )
myPad()->height() )
582 AdjustPad(
wsze(
myPad()->height() + defPadSze().H, textwidth ) );
591 inline void NCRichText::PadBOL()
598 inline void NCRichText::PadWS(
const bool tab )
603 if ( cc == textwidth )
615 inline void NCRichText::PadTXT(
const wchar_t * osch,
const unsigned olen )
617 std::wstring txt( osch, olen );
619 txt = filterEntities( txt );
621 size_t len = textWidth( txt );
623 if ( !atbol && cc + len > textwidth )
627 const wchar_t * sch = txt.data();
632 cc += wcwidth( *sch );
635 if ( cc >= textwidth )
651 size_t NCRichText::textWidth( std::wstring wstr )
654 std::wstring::const_iterator wstr_it;
656 for ( wstr_it = wstr.begin(); wstr_it != wstr.end() ; ++wstr_it )
659 if ( iswprint( *wstr_it ) )
661 len += wcwidth( *wstr_it );
663 else if ( *wstr_it ==
'\t' )
676 inline void NCRichText::PadSetAttr()
679 chtype nbg = style.plain;
685 else if ( Tattr & T_HEAD )
691 switch ( Tattr & Tfontmask )
717 case T_BOLD|T_IT|T_TT:
727 void NCRichText::PadSetLevel()
729 cindent = listindent * liststack.size();
731 if ( cindent > textwidth / 2 )
732 cindent = textwidth / 2;
742 void NCRichText::PadChangeLevel(
bool down,
int tag )
746 if ( liststack.size() )
751 liststack.push( tag );
758 void NCRichText::openAnchor( std::wstring args )
760 canchor.open( cl, cc );
762 const wchar_t * ch = (
wchar_t * )args.data();
763 const wchar_t * lookupstr = L
"href = ";
764 const wchar_t * lookup = lookupstr;
766 for ( ; *ch && *lookup; ++ch )
768 wchar_t c = towlower( *ch );
775 if ( *lookup != L
' ' )
781 if ( *lookup == L
' ' )
804 const wchar_t * delim = ( *ch == L
'"' ) ? L
"\"" : L
" \t";
805 args = ( *ch == L
'"' ) ? ++ch : ch;
807 std::wstring::size_type end = args.find_first_of( delim );
809 if ( end != std::wstring::npos )
812 canchor.target = args;
816 yuiError() <<
"No value for 'HREF=' in anchor '" << args <<
"'" << std::endl;
821 void NCRichText::closeAnchor()
823 canchor.close( cl, cc );
825 if ( canchor.valid() )
826 anchors.push_back( canchor );
833 bool NCRichText::PadTOKEN(
const wchar_t * sch,
const wchar_t *& ech )
836 if ( *sch++ != L
'<' || *( ech - 1 ) != L
'>' )
840 bool endtag = ( *sch == L
'/' );
846 if ( ech - sch <= 1 )
849 std::wstring value( sch, ech - 1 - sch );
853 std::wstring::size_type argstart = value.find_first_of( L
" \t\n" );
855 if ( argstart != std::wstring::npos )
857 args = value.substr( argstart );
858 value.erase( argstart );
861 for (
unsigned i = 0; i < value.length(); ++i )
863 if ( isupper( value[i] ) )
865 value[i] =
static_cast<char>( tolower( value[i] ) );
871 int headinglevel = 0;
873 TOKEN token = T_UNKNOWN;
875 switch ( value.length() )
879 if ( value[0] ==
'b' ) token = T_BOLD;
880 else if ( value[0] ==
'i' ) token = T_IT;
881 else if ( value[0] ==
'p' ) token = T_PAR;
882 else if ( value[0] ==
'a' ) token = T_ANC;
883 else if ( value[0] ==
'u' ) token = T_BOLD;
888 if ( value == L
"br" ) token = T_BR;
889 else if ( value == L
"em" ) token = T_IT;
890 else if ( value == L
"h1" ) { token = T_HEAD; headinglevel = 1; }
891 else if ( value == L
"h2" ) { token = T_HEAD; headinglevel = 2; }
892 else if ( value == L
"h3" ) { token = T_HEAD; headinglevel = 3; }
893 else if ( value == L
"hr" ) token = T_IGNORE;
894 else if ( value == L
"li" ) token = T_LI;
895 else if ( value == L
"ol" ) { token = T_LEVEL; leveltag = 1; }
896 else if ( value == L
"qt" ) token = T_IGNORE;
897 else if ( value == L
"tt" ) token = T_TT;
898 else if ( value == L
"ul" ) { token = T_LEVEL; leveltag = 0; }
904 if ( value == L
"big" ) token = T_IGNORE;
905 else if ( value == L
"pre" ) token = T_PLAIN;
910 if ( value == L
"bold" ) token = T_BOLD;
911 else if ( value == L
"code" ) token = T_TT;
912 else if ( value == L
"font" ) token = T_IGNORE;
917 if ( value == L
"large" ) token = T_IGNORE;
918 else if ( value == L
"small" ) token = T_IGNORE;
923 if ( value == L
"center" ) token = T_PAR;
924 else if ( value == L
"strong" ) token = T_BOLD;
929 if ( value == L
"blockquote" ) token = T_PAR;
939 if ( token == T_UNKNOWN )
941 yuiDebug() <<
"T_UNKNOWN :" << value <<
":" << args <<
":" << std::endl;
947 if ( token == T_IGNORE )
953 PadChangeLevel( endtag, leveltag );
957 if ( endtag && !cindent )
976 if ( headinglevel && endtag )
1001 if ( liststack.empty() )
1003 tag = std::wstring( listindent, L
' ' );
1009 if ( liststack.top() )
1011 swprintf( buf, 15, L
"%2ld. ", liststack.top()++ );
1015 swprintf( buf, 15, L
" %lc ", listleveltags[liststack.size()%listleveltags.size()] );
1022 cc = ( tag.size() < cc ? cc - tag.size() : 0 );
1026 PadTXT( tag.c_str(), tag.size() );
1038 AdjustPrePad( ech );
1082 void NCRichText::arm(
unsigned i )
1090 yuiDebug() << i <<
" (" << armed <<
")" << std::endl;
1094 if ( armed != Anchor::unset )
1097 anchors[armed].draw( *
myPad(), wStyle().richtext.getArmed( GetState() ), 0 );
1104 if ( armed != Anchor::unset )
1106 anchors[armed].draw( *
myPad(), wStyle().richtext.link, (
int ) wStyle().richtext.visitedlink );
1107 armed = Anchor::unset;
1110 if ( i != Anchor::unset )
1113 anchors[armed].draw( *
myPad(), wStyle().richtext.getArmed( GetState() ), 0 );
1116 if ( showLinkTarget )
1118 if ( armed != Anchor::unset )
1119 NCPadWidget::setLabel(
NCstring( anchors[armed].target ) );
1121 NCPadWidget::setLabel(
NCstring() );
1130 void NCRichText::HScroll(
unsigned total,
unsigned visible,
unsigned start )
1132 NCPadWidget::HScroll( total, visible, start );
1137 void NCRichText::VScroll(
unsigned total,
unsigned visible,
unsigned start )
1139 NCPadWidget::VScroll( total, visible, start );
1141 if ( plainText || anchors.empty() )
1146 vScrollFirstvisible = start;
1148 vScrollNextinvisible = start + visible;
1150 if ( armed != Anchor::unset )
1152 if ( anchors[armed].within( vScrollFirstvisible, vScrollNextinvisible ) )
1158 for (
unsigned i = 0; i < anchors.size(); ++i )
1160 if ( anchors[i].within( vScrollFirstvisible, vScrollNextinvisible ) )
1169 bool NCRichText::handleInput( wint_t key )
1171 if ( plainText || anchors.empty() )
1173 return NCPadWidget::handleInput( key );
1177 bool handled =
true;
1184 unsigned newarmed = Anchor::unset;
1186 if ( armed == Anchor::unset )
1189 for (
unsigned i = anchors.size(); i; )
1193 if ( anchors[i].eline < vScrollFirstvisible )
1200 else if ( armed > 0 )
1202 newarmed = armed - 1;
1205 if ( newarmed == Anchor::unset )
1207 handled = NCPadWidget::handleInput( KEY_UP );
1211 if ( !anchors[newarmed].within( vScrollFirstvisible, vScrollNextinvisible ) )
1212 myPad()->ScrlLine( anchors[newarmed].sline );
1223 unsigned newarmed = Anchor::unset;
1225 if ( armed == Anchor::unset )
1228 for (
unsigned i = 0; i < anchors.size(); ++i )
1230 if ( anchors[i].sline >= vScrollNextinvisible )
1237 else if ( armed + 1 < anchors.size() )
1239 newarmed = armed + 1;
1242 if ( newarmed == Anchor::unset )
1244 handled = NCPadWidget::handleInput( KEY_DOWN );
1248 if ( !anchors[newarmed].within( vScrollFirstvisible, vScrollNextinvisible ) )
1249 myPad()->ScrlLine( anchors[newarmed].sline );
1260 if ( armed != Anchor::unset
1262 && anchors[armed-1].within( vScrollFirstvisible, vScrollNextinvisible ) )
1268 handled = NCPadWidget::handleInput( key );
1276 if ( armed != Anchor::unset
1277 && armed + 1 < anchors.size()
1278 && anchors[armed+1].within( vScrollFirstvisible, vScrollNextinvisible ) )
1284 handled = NCPadWidget::handleInput( key );
1290 handled = NCPadWidget::handleInput( key );