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;
280 ret.selection = NULL;
291 NCPad * NCRichText::CreatePad()
293 wsze psze( defPadSze() );
295 NCPad * npad =
new NCPad( psze.H, textwidth, *
this );
300 void NCRichText::DrawPad()
303 <<
"Start: plain mode " << plainText << std::endl
304 <<
" padsize " <<
myPad()->size() << std::endl
305 <<
" text length " << text.str().size() << std::endl;
315 yuiDebug() <<
"Done" << std::endl;
319 void NCRichText::DrawPlainPad()
322 yuiDebug() <<
"ftext is " <<
wsze( ftext.Lines(), ftext.Columns() ) << std::endl;
324 AdjustPad(
wsze( ftext.Lines(), ftext.Columns() ) );
328 for ( NCtext::const_iterator line = ftext.begin();
329 line != ftext.end(); ++line, ++cl )
335 void NCRichText::PadPreTXT(
const wchar_t * osch,
const unsigned olen )
337 std::wstring wtxt( osch, olen );
340 wtxt = filterEntities( wtxt );
346 const wchar_t * sch = wtxt.data();
360 inline void SkipToken(
const wchar_t *& wch )
366 while ( *wch && *wch != L
'>' );
373 static std::wstring WStoken( L
" \n\t\v\r\f" );
376 inline void SkipWS(
const wchar_t *& wch )
382 while ( *wch && WStoken.find( *wch ) != std::wstring::npos );
386 static std::wstring WDtoken( L
" <\n\t\v\r\f" );
389 inline void SkipWord(
const wchar_t *& wch )
395 while ( *wch && WDtoken.find( *wch ) == std::wstring::npos );
398 static std::wstring PREtoken( L
"<\n\v\r\f" );
401 inline void SkipPreTXT(
const wchar_t *& wch )
407 while ( *wch && PREtoken.find( *wch ) == std::wstring::npos );
415 void NCRichText::AdjustPrePad(
const wchar_t *osch )
417 const wchar_t * wch = osch;
418 std::wstring wstr( wch, 6 );
423 wstr.assign( wch, 6 );
425 while ( *wch && wstr != L
"</pre>" );
427 std::wstring wtxt( osch, wch - osch );
430 wtxt = filterEntities( wtxt );
433 boost::replace_all( wtxt, L
"<br>", L
"\n" );
435 yuiDebug() <<
"Text: " << wtxt <<
" initial length: " << wch - osch << std::endl;
440 std::list<NCstring>::const_iterator line;
444 for ( line = ftext.Text().begin(); line != ftext.Text().end(); ++line )
448 tmp_len = textWidth( (*line).str() );
450 if ( tmp_len > llen )
453 yuiDebug() <<
"Longest line: " << llen << std::endl;
455 if ( llen > textwidth )
458 AdjustPad(
wsze( cl + ftext.Lines(), llen ) );
463 void NCRichText::DrawHTMLPad()
465 yuiDebug() <<
"Start:" << std::endl;
467 liststack = std::stack<int>();
470 armed = Anchor::unset;
478 const wchar_t * wch = (
wchar_t * )text.str().data();
479 const wchar_t * swch = 0;
511 yuiDebug() <<
"Ignoring " << *wch << std::endl;
522 if ( PadTOKEN( swch, wch ) )
533 PadTXT( swch, wch - swch );
538 PadPreTXT( swch, wch - swch );
546 AdjustPad(
wsze( cl, textwidth ) );
548 yuiDebug() <<
"Anchors: " << anchors.size() << std::endl;
550 for (
unsigned i = 0; i < anchors.size(); ++i )
552 yuiDebug() << form(
" %2d: [%2d,%2d] -> [%2d,%2d]",
554 anchors[i].sline, anchors[i].scol,
555 anchors[i].eline, anchors[i].ecol ) << std::endl;
560 inline void NCRichText::PadNL()
564 if ( ++cl == (
unsigned )
myPad()->height() )
566 AdjustPad(
wsze(
myPad()->height() + defPadSze().H, textwidth ) );
575 inline void NCRichText::PadBOL()
582 inline void NCRichText::PadWS(
const bool tab )
587 if ( cc == textwidth )
599 inline void NCRichText::PadTXT(
const wchar_t * osch,
const unsigned olen )
601 std::wstring txt( osch, olen );
603 txt = filterEntities( txt );
605 size_t len = textWidth( txt );
607 if ( !atbol && cc + len > textwidth )
611 const wchar_t * sch = txt.data();
616 cc += wcwidth( *sch );
619 if ( cc >= textwidth )
635 size_t NCRichText::textWidth( std::wstring wstr )
638 std::wstring::const_iterator wstr_it;
640 for ( wstr_it = wstr.begin(); wstr_it != wstr.end() ; ++wstr_it )
643 if ( iswprint( *wstr_it ) )
645 len += wcwidth( *wstr_it );
647 else if ( *wstr_it ==
'\t' )
660 inline void NCRichText::PadSetAttr()
663 chtype nbg = style.plain;
669 else if ( Tattr & T_HEAD )
675 switch ( Tattr & Tfontmask )
701 case T_BOLD|T_IT|T_TT:
711 void NCRichText::PadSetLevel()
713 cindent = listindent * liststack.size();
715 if ( cindent > textwidth / 2 )
716 cindent = textwidth / 2;
726 void NCRichText::PadChangeLevel(
bool down,
int tag )
730 if ( liststack.size() )
735 liststack.push( tag );
742 void NCRichText::openAnchor( std::wstring args )
744 canchor.open( cl, cc );
746 const wchar_t * ch = (
wchar_t * )args.data();
747 const wchar_t * lookupstr = L
"href = ";
748 const wchar_t * lookup = lookupstr;
750 for ( ; *ch && *lookup; ++ch )
752 wchar_t c = towlower( *ch );
759 if ( *lookup != L
' ' )
765 if ( *lookup == L
' ' )
788 const wchar_t * delim = ( *ch == L
'"' ) ? L
"\"" : L
" \t";
789 args = ( *ch == L
'"' ) ? ++ch : ch;
791 std::wstring::size_type end = args.find_first_of( delim );
793 if ( end != std::wstring::npos )
796 canchor.target = args;
800 yuiError() <<
"No value for 'HREF=' in anchor '" << args <<
"'" << std::endl;
805 void NCRichText::closeAnchor()
807 canchor.close( cl, cc );
809 if ( canchor.valid() )
810 anchors.push_back( canchor );
817 bool NCRichText::PadTOKEN(
const wchar_t * sch,
const wchar_t *& ech )
820 if ( *sch++ != L
'<' || *( ech - 1 ) != L
'>' )
824 bool endtag = ( *sch == L
'/' );
830 if ( ech - sch <= 1 )
833 std::wstring value( sch, ech - 1 - sch );
837 std::wstring::size_type argstart = value.find_first_of( L
" \t\n" );
839 if ( argstart != std::wstring::npos )
841 args = value.substr( argstart );
842 value.erase( argstart );
845 for (
unsigned i = 0; i < value.length(); ++i )
847 if ( isupper( value[i] ) )
849 value[i] =
static_cast<char>( tolower( value[i] ) );
855 int headinglevel = 0;
857 TOKEN token = T_UNKNOWN;
859 switch ( value.length() )
863 if ( value[0] ==
'b' ) token = T_BOLD;
864 else if ( value[0] ==
'i' ) token = T_IT;
865 else if ( value[0] ==
'p' ) token = T_PAR;
866 else if ( value[0] ==
'a' ) token = T_ANC;
867 else if ( value[0] ==
'u' ) token = T_BOLD;
872 if ( value == L
"br" ) token = T_BR;
873 else if ( value == L
"em" ) token = T_IT;
874 else if ( value == L
"h1" ) { token = T_HEAD; headinglevel = 1; }
875 else if ( value == L
"h2" ) { token = T_HEAD; headinglevel = 2; }
876 else if ( value == L
"h3" ) { token = T_HEAD; headinglevel = 3; }
877 else if ( value == L
"hr" ) token = T_IGNORE;
878 else if ( value == L
"li" ) token = T_LI;
879 else if ( value == L
"ol" ) { token = T_LEVEL; leveltag = 1; }
880 else if ( value == L
"qt" ) token = T_IGNORE;
881 else if ( value == L
"tt" ) token = T_TT;
882 else if ( value == L
"ul" ) { token = T_LEVEL; leveltag = 0; }
888 if ( value == L
"big" ) token = T_IGNORE;
889 else if ( value == L
"pre" ) token = T_PLAIN;
894 if ( value == L
"bold" ) token = T_BOLD;
895 else if ( value == L
"code" ) token = T_TT;
896 else if ( value == L
"font" ) token = T_IGNORE;
901 if ( value == L
"large" ) token = T_IGNORE;
902 else if ( value == L
"small" ) token = T_IGNORE;
907 if ( value == L
"center" ) token = T_PAR;
908 else if ( value == L
"strong" ) token = T_BOLD;
913 if ( value == L
"blockquote" ) token = T_PAR;
923 if ( token == T_UNKNOWN )
925 yuiDebug() <<
"T_UNKNOWN :" << value <<
":" << args <<
":" << std::endl;
931 if ( token == T_IGNORE )
937 PadChangeLevel( endtag, leveltag );
941 if ( endtag && !cindent )
960 if ( headinglevel && endtag )
985 if ( liststack.empty() )
987 tag = std::wstring( listindent, L
' ' );
993 if ( liststack.top() )
995 swprintf( buf, 15, L
"%2ld. ", liststack.top()++ );
999 swprintf( buf, 15, L
" %lc ", listleveltags[liststack.size()%listleveltags.size()] );
1006 cc = ( tag.size() < cc ? cc - tag.size() : 0 );
1010 PadTXT( tag.c_str(), tag.size() );
1022 AdjustPrePad( ech );
1066 void NCRichText::arm(
unsigned i )
1074 yuiDebug() << i <<
" (" << armed <<
")" << std::endl;
1078 if ( armed != Anchor::unset )
1081 anchors[armed].draw( *
myPad(), wStyle().richtext.getArmed( GetState() ), 0 );
1088 if ( armed != Anchor::unset )
1090 anchors[armed].draw( *
myPad(), wStyle().richtext.link, (
int ) wStyle().richtext.visitedlink );
1091 armed = Anchor::unset;
1094 if ( i != Anchor::unset )
1097 anchors[armed].draw( *
myPad(), wStyle().richtext.getArmed( GetState() ), 0 );
1100 if ( showLinkTarget )
1102 if ( armed != Anchor::unset )
1103 NCPadWidget::setLabel(
NCstring( anchors[armed].target ) );
1105 NCPadWidget::setLabel(
NCstring() );
1114 void NCRichText::HScroll(
unsigned total,
unsigned visible,
unsigned start )
1116 NCPadWidget::HScroll( total, visible, start );
1121 void NCRichText::VScroll(
unsigned total,
unsigned visible,
unsigned start )
1123 NCPadWidget::VScroll( total, visible, start );
1125 if ( plainText || anchors.empty() )
1130 vScrollFirstvisible = start;
1132 vScrollNextinvisible = start + visible;
1134 if ( armed != Anchor::unset )
1136 if ( anchors[armed].within( vScrollFirstvisible, vScrollNextinvisible ) )
1142 for (
unsigned i = 0; i < anchors.size(); ++i )
1144 if ( anchors[i].within( vScrollFirstvisible, vScrollNextinvisible ) )
1153 bool NCRichText::handleInput( wint_t key )
1155 if ( plainText || anchors.empty() )
1157 return NCPadWidget::handleInput( key );
1161 bool handled =
true;
1168 unsigned newarmed = Anchor::unset;
1170 if ( armed == Anchor::unset )
1173 for (
unsigned i = anchors.size(); i; )
1177 if ( anchors[i].eline < vScrollFirstvisible )
1184 else if ( armed > 0 )
1186 newarmed = armed - 1;
1189 if ( newarmed == Anchor::unset )
1191 handled = NCPadWidget::handleInput( KEY_UP );
1195 if ( !anchors[newarmed].within( vScrollFirstvisible, vScrollNextinvisible ) )
1196 myPad()->ScrlLine( anchors[newarmed].sline );
1207 unsigned newarmed = Anchor::unset;
1209 if ( armed == Anchor::unset )
1212 for (
unsigned i = 0; i < anchors.size(); ++i )
1214 if ( anchors[i].sline >= vScrollNextinvisible )
1221 else if ( armed + 1 < anchors.size() )
1223 newarmed = armed + 1;
1226 if ( newarmed == Anchor::unset )
1228 handled = NCPadWidget::handleInput( KEY_DOWN );
1232 if ( !anchors[newarmed].within( vScrollFirstvisible, vScrollNextinvisible ) )
1233 myPad()->ScrlLine( anchors[newarmed].sline );
1244 if ( armed != Anchor::unset
1246 && anchors[armed-1].within( vScrollFirstvisible, vScrollNextinvisible ) )
1252 handled = NCPadWidget::handleInput( key );
1260 if ( armed != Anchor::unset
1261 && armed + 1 < anchors.size()
1262 && anchors[armed+1].within( vScrollFirstvisible, vScrollNextinvisible ) )
1268 handled = NCPadWidget::handleInput( key );
1274 handled = NCPadWidget::handleInput( key );
int clear()
Clear the window.
virtual void setEnabled(bool do_bv)
Pure virtual to make sure every widget implements it.
static int tabsize()
Size of a tab on terminal, not window.
void bkgdset(chtype ch)
Set the background property.
int addwstr(const wchar_t *str, int n=-1)
Write the wchar_t str to the window, stop writing if the terminating NUL or the limit n is reached...
int chgat(int n, attr_t attr, short color, const void *opts=NULL)
Change the attributes of the next n characters in the current line.
int move(int y, int x)
Move cursor the this position.