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" );
434 boost::replace_all( wtxt, L
"<br/>", L
"\n" );
436 yuiDebug() <<
"Text: " << wtxt <<
" initial length: " << wch - osch << std::endl;
441 std::list<NCstring>::const_iterator line;
445 for ( line = ftext.Text().begin(); line != ftext.Text().end(); ++line )
449 tmp_len = textWidth( (*line).str() );
451 if ( tmp_len > llen )
454 yuiDebug() <<
"Longest line: " << llen << std::endl;
456 if ( llen > textwidth )
459 AdjustPad(
wsze( cl + ftext.Lines(), llen ) );
464 void NCRichText::DrawHTMLPad()
466 yuiDebug() <<
"Start:" << std::endl;
468 liststack = std::stack<int>();
471 armed = Anchor::unset;
479 const wchar_t * wch = (
wchar_t * )text.str().data();
480 const wchar_t * swch = 0;
512 yuiDebug() <<
"Ignoring " << *wch << std::endl;
523 if ( PadTOKEN( swch, wch ) )
534 PadTXT( swch, wch - swch );
539 PadPreTXT( swch, wch - swch );
547 AdjustPad(
wsze( cl, textwidth ) );
549 yuiDebug() <<
"Anchors: " << anchors.size() << std::endl;
551 for (
unsigned i = 0; i < anchors.size(); ++i )
553 yuiDebug() << form(
" %2d: [%2d,%2d] -> [%2d,%2d]",
555 anchors[i].sline, anchors[i].scol,
556 anchors[i].eline, anchors[i].ecol ) << std::endl;
561 inline void NCRichText::PadNL()
565 if ( ++cl == (
unsigned )
myPad()->height() )
567 AdjustPad(
wsze(
myPad()->height() + defPadSze().H, textwidth ) );
576 inline void NCRichText::PadBOL()
583 inline void NCRichText::PadWS(
const bool tab )
588 if ( cc == textwidth )
600 inline void NCRichText::PadTXT(
const wchar_t * osch,
const unsigned olen )
602 std::wstring txt( osch, olen );
604 txt = filterEntities( txt );
606 size_t len = textWidth( txt );
608 if ( !atbol && cc + len > textwidth )
612 const wchar_t * sch = txt.data();
617 cc += wcwidth( *sch );
620 if ( cc >= textwidth )
636 size_t NCRichText::textWidth( std::wstring wstr )
639 std::wstring::const_iterator wstr_it;
641 for ( wstr_it = wstr.begin(); wstr_it != wstr.end() ; ++wstr_it )
644 if ( iswprint( *wstr_it ) )
646 len += wcwidth( *wstr_it );
648 else if ( *wstr_it ==
'\t' )
661 inline void NCRichText::PadSetAttr()
664 chtype nbg = style.plain;
670 else if ( Tattr & T_HEAD )
676 switch ( Tattr & Tfontmask )
702 case T_BOLD|T_IT|T_TT:
712 void NCRichText::PadSetLevel()
714 cindent = listindent * liststack.size();
716 if ( cindent > textwidth / 2 )
717 cindent = textwidth / 2;
727 void NCRichText::PadChangeLevel(
bool down,
int tag )
731 if ( liststack.size() )
736 liststack.push( tag );
743 void NCRichText::openAnchor( std::wstring args )
745 canchor.open( cl, cc );
747 const wchar_t * ch = (
wchar_t * )args.data();
748 const wchar_t * lookupstr = L
"href = ";
749 const wchar_t * lookup = lookupstr;
751 for ( ; *ch && *lookup; ++ch )
753 wchar_t c = towlower( *ch );
760 if ( *lookup != L
' ' )
766 if ( *lookup == L
' ' )
789 const wchar_t * delim = ( *ch == L
'"' ) ? L
"\"" : L
" \t";
790 args = ( *ch == L
'"' ) ? ++ch : ch;
792 std::wstring::size_type end = args.find_first_of( delim );
794 if ( end != std::wstring::npos )
797 canchor.target = args;
801 yuiError() <<
"No value for 'HREF=' in anchor '" << args <<
"'" << std::endl;
806 void NCRichText::closeAnchor()
808 canchor.close( cl, cc );
810 if ( canchor.valid() )
811 anchors.push_back( canchor );
818 bool NCRichText::PadTOKEN(
const wchar_t * sch,
const wchar_t *& ech )
821 if ( *sch++ != L
'<' || *( ech - 1 ) != L
'>' )
825 bool endtag = ( *sch == L
'/' );
831 if ( ech - sch <= 1 )
834 std::wstring value( sch, ech - 1 - sch );
838 std::wstring::size_type argstart = value.find_first_of( L
" \t\n" );
840 if ( argstart != std::wstring::npos )
842 args = value.substr( argstart );
843 value.erase( argstart );
846 for (
unsigned i = 0; i < value.length(); ++i )
848 if ( isupper( value[i] ) )
850 value[i] =
static_cast<char>( tolower( value[i] ) );
856 int headinglevel = 0;
858 TOKEN token = T_UNKNOWN;
860 switch ( value.length() )
864 if ( value[0] ==
'b' ) token = T_BOLD;
865 else if ( value[0] ==
'i' ) token = T_IT;
866 else if ( value[0] ==
'p' ) token = T_PAR;
867 else if ( value[0] ==
'a' ) token = T_ANC;
868 else if ( value[0] ==
'u' ) token = T_BOLD;
873 if ( value == L
"br" ) token = T_BR;
874 else if ( value == L
"em" ) token = T_IT;
875 else if ( value == L
"h1" ) { token = T_HEAD; headinglevel = 1; }
876 else if ( value == L
"h2" ) { token = T_HEAD; headinglevel = 2; }
877 else if ( value == L
"h3" ) { token = T_HEAD; headinglevel = 3; }
878 else if ( value == L
"hr" ) token = T_IGNORE;
879 else if ( value == L
"li" ) token = T_LI;
880 else if ( value == L
"ol" ) { token = T_LEVEL; leveltag = 1; }
881 else if ( value == L
"qt" ) token = T_IGNORE;
882 else if ( value == L
"tt" ) token = T_TT;
883 else if ( value == L
"ul" ) { token = T_LEVEL; leveltag = 0; }
889 if ( value == L
"big" ) token = T_IGNORE;
890 else if ( value == L
"pre" ) token = T_PLAIN;
895 else if ( value == L
"br/" ) token = T_BR;
896 else if ( value == L
"hr/" ) token = T_IGNORE;
901 if ( value == L
"bold" ) token = T_BOLD;
902 else if ( value == L
"code" ) token = T_TT;
903 else if ( value == L
"font" ) token = T_IGNORE;
908 if ( value == L
"large" ) token = T_IGNORE;
909 else if ( value == L
"small" ) token = T_IGNORE;
914 if ( value == L
"center" ) token = T_PAR;
915 else if ( value == L
"strong" ) token = T_BOLD;
920 if ( value == L
"blockquote" ) token = T_PAR;
930 if ( token == T_UNKNOWN )
932 yuiDebug() <<
"T_UNKNOWN :" << value <<
":" << args <<
":" << std::endl;
938 if ( token == T_IGNORE )
944 PadChangeLevel( endtag, leveltag );
948 if ( endtag && !cindent )
967 if ( headinglevel && endtag )
992 if ( liststack.empty() )
994 tag = std::wstring( listindent, L
' ' );
1000 if ( liststack.top() )
1002 swprintf( buf, 15, L
"%2ld. ", liststack.top()++ );
1006 swprintf( buf, 15, L
" %lc ", listleveltags[liststack.size()%listleveltags.size()] );
1013 cc = ( tag.size() < cc ? cc - tag.size() : 0 );
1017 PadTXT( tag.c_str(), tag.size() );
1029 AdjustPrePad( ech );
1073 void NCRichText::arm(
unsigned i )
1081 yuiDebug() << i <<
" (" << armed <<
")" << std::endl;
1085 if ( armed != Anchor::unset )
1088 anchors[armed].draw( *
myPad(), wStyle().richtext.getArmed( GetState() ), 0 );
1095 if ( armed != Anchor::unset )
1097 anchors[armed].draw( *
myPad(), wStyle().richtext.link, (
int ) wStyle().richtext.visitedlink );
1098 armed = Anchor::unset;
1101 if ( i != Anchor::unset )
1104 anchors[armed].draw( *
myPad(), wStyle().richtext.getArmed( GetState() ), 0 );
1107 if ( showLinkTarget )
1109 if ( armed != Anchor::unset )
1110 NCPadWidget::setLabel(
NCstring( anchors[armed].target ) );
1112 NCPadWidget::setLabel(
NCstring() );
1121 void NCRichText::HScroll(
unsigned total,
unsigned visible,
unsigned start )
1123 NCPadWidget::HScroll( total, visible, start );
1128 void NCRichText::VScroll(
unsigned total,
unsigned visible,
unsigned start )
1130 NCPadWidget::VScroll( total, visible, start );
1132 if ( plainText || anchors.empty() )
1137 vScrollFirstvisible = start;
1139 vScrollNextinvisible = start + visible;
1141 if ( armed != Anchor::unset )
1143 if ( anchors[armed].within( vScrollFirstvisible, vScrollNextinvisible ) )
1149 for (
unsigned i = 0; i < anchors.size(); ++i )
1151 if ( anchors[i].within( vScrollFirstvisible, vScrollNextinvisible ) )
1160 bool NCRichText::handleInput( wint_t key )
1162 if ( plainText || anchors.empty() )
1164 return NCPadWidget::handleInput( key );
1168 bool handled =
true;
1175 unsigned newarmed = Anchor::unset;
1177 if ( armed == Anchor::unset )
1180 for (
unsigned i = anchors.size(); i; )
1184 if ( anchors[i].eline < vScrollFirstvisible )
1191 else if ( armed > 0 )
1193 newarmed = armed - 1;
1196 if ( newarmed == Anchor::unset )
1198 handled = NCPadWidget::handleInput( KEY_UP );
1202 if ( !anchors[newarmed].within( vScrollFirstvisible, vScrollNextinvisible ) )
1203 myPad()->ScrlLine( anchors[newarmed].sline );
1214 unsigned newarmed = Anchor::unset;
1216 if ( armed == Anchor::unset )
1219 for (
unsigned i = 0; i < anchors.size(); ++i )
1221 if ( anchors[i].sline >= vScrollNextinvisible )
1228 else if ( armed + 1 < anchors.size() )
1230 newarmed = armed + 1;
1233 if ( newarmed == Anchor::unset )
1235 handled = NCPadWidget::handleInput( KEY_DOWN );
1239 if ( !anchors[newarmed].within( vScrollFirstvisible, vScrollNextinvisible ) )
1240 myPad()->ScrlLine( anchors[newarmed].sline );
1251 if ( armed != Anchor::unset
1253 && anchors[armed-1].within( vScrollFirstvisible, vScrollNextinvisible ) )
1259 handled = NCPadWidget::handleInput( key );
1267 if ( armed != Anchor::unset
1268 && armed + 1 < anchors.size()
1269 && anchors[armed+1].within( vScrollFirstvisible, vScrollNextinvisible ) )
1275 handled = NCPadWidget::handleInput( key );
1281 handled = NCPadWidget::handleInput( key );