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 wstr.assign( wch, 6 );
424 while ( *wch && wstr != L
"</pre>" );
426 std::wstring wtxt( osch, wch - osch );
429 wtxt = filterEntities( wtxt );
432 boost::replace_all( wtxt, L
"<br>", L
"\n" );
434 yuiDebug() <<
"Text: " << wtxt <<
" initial length: " << wch - osch << std::endl;
439 std::list<NCstring>::const_iterator line;
443 for ( line = ftext.Text().begin(); line != ftext.Text().end(); ++line )
447 tmp_len = textWidth( (*line).str() );
449 if ( tmp_len > llen )
452 yuiDebug() <<
"Longest line: " << llen << std::endl;
454 if ( llen > textwidth )
457 AdjustPad(
wsze( cl + ftext.Lines(), llen ) );
462 void NCRichText::DrawHTMLPad()
464 yuiDebug() <<
"Start:" << std::endl;
466 liststack = std::stack<int>();
469 armed = Anchor::unset;
477 const wchar_t * wch = (
wchar_t * )text.str().data();
478 const wchar_t * swch = 0;
510 yuiDebug() <<
"Ignoring " << *wch << std::endl;
521 if ( PadTOKEN( swch, wch ) )
532 PadTXT( swch, wch - swch );
537 PadPreTXT( swch, wch - swch );
545 AdjustPad(
wsze( cl, textwidth ) );
547 yuiDebug() <<
"Anchors: " << anchors.size() << std::endl;
549 for (
unsigned i = 0; i < anchors.size(); ++i )
551 yuiDebug() << form(
" %2d: [%2d,%2d] -> [%2d,%2d]",
553 anchors[i].sline, anchors[i].scol,
554 anchors[i].eline, anchors[i].ecol ) << std::endl;
559 inline void NCRichText::PadNL()
563 if ( ++cl == (
unsigned )
myPad()->height() )
565 AdjustPad(
wsze(
myPad()->height() + defPadSze().H, textwidth ) );
574 inline void NCRichText::PadBOL()
581 inline void NCRichText::PadWS(
const bool tab )
586 if ( cc == textwidth )
598 inline void NCRichText::PadTXT(
const wchar_t * osch,
const unsigned olen )
600 std::wstring txt( osch, olen );
602 txt = filterEntities( txt );
604 size_t len = textWidth( txt );
606 if ( !atbol && cc + len > textwidth )
610 const wchar_t * sch = txt.data();
615 cc += wcwidth( *sch );
618 if ( cc >= textwidth )
634 size_t NCRichText::textWidth( std::wstring wstr )
637 std::wstring::const_iterator wstr_it;
639 for ( wstr_it = wstr.begin(); wstr_it != wstr.end() ; ++wstr_it )
642 if ( iswprint( *wstr_it ) )
644 len += wcwidth( *wstr_it );
646 else if ( *wstr_it ==
'\t' )
659 inline void NCRichText::PadSetAttr()
662 chtype nbg = style.plain;
668 else if ( Tattr & T_HEAD )
674 switch ( Tattr & Tfontmask )
700 case T_BOLD|T_IT|T_TT:
710 void NCRichText::PadSetLevel()
712 cindent = listindent * liststack.size();
714 if ( cindent > textwidth / 2 )
715 cindent = textwidth / 2;
725 void NCRichText::PadChangeLevel(
bool down,
int tag )
729 if ( liststack.size() )
734 liststack.push( tag );
741 void NCRichText::openAnchor( std::wstring args )
743 canchor.open( cl, cc );
745 const wchar_t * ch = (
wchar_t * )args.data();
746 const wchar_t * lookupstr = L
"href = ";
747 const wchar_t * lookup = lookupstr;
749 for ( ; *ch && *lookup; ++ch )
751 wchar_t c = towlower( *ch );
758 if ( *lookup != L
' ' )
764 if ( *lookup == L
' ' )
787 const wchar_t * delim = ( *ch == L
'"' ) ? L
"\"" : L
" \t";
788 args = ( *ch == L
'"' ) ? ++ch : ch;
790 std::wstring::size_type end = args.find_first_of( delim );
792 if ( end != std::wstring::npos )
795 canchor.target = args;
799 yuiError() <<
"No value for 'HREF=' in anchor '" << args <<
"'" << std::endl;
804 void NCRichText::closeAnchor()
806 canchor.close( cl, cc );
808 if ( canchor.valid() )
809 anchors.push_back( canchor );
816 bool NCRichText::PadTOKEN(
const wchar_t * sch,
const wchar_t *& ech )
819 if ( *sch++ != L
'<' || *( ech - 1 ) != L
'>' )
823 bool endtag = ( *sch == L
'/' );
829 if ( ech - sch <= 1 )
832 std::wstring value( sch, ech - 1 - sch );
836 std::wstring::size_type argstart = value.find_first_of( L
" \t\n" );
838 if ( argstart != std::wstring::npos )
840 args = value.substr( argstart );
841 value.erase( argstart );
844 for (
unsigned i = 0; i < value.length(); ++i )
846 if ( isupper( value[i] ) )
848 value[i] =
static_cast<char>( tolower( value[i] ) );
854 int headinglevel = 0;
856 TOKEN token = T_UNKNOWN;
858 switch ( value.length() )
862 if ( value[0] ==
'b' ) token = T_BOLD;
863 else if ( value[0] ==
'i' ) token = T_IT;
864 else if ( value[0] ==
'p' ) token = T_PAR;
865 else if ( value[0] ==
'a' ) token = T_ANC;
866 else if ( value[0] ==
'u' ) token = T_BOLD;
871 if ( value == L
"br" ) token = T_BR;
872 else if ( value == L
"em" ) token = T_IT;
873 else if ( value == L
"h1" ) { token = T_HEAD; headinglevel = 1; }
874 else if ( value == L
"h2" ) { token = T_HEAD; headinglevel = 2; }
875 else if ( value == L
"h3" ) { token = T_HEAD; headinglevel = 3; }
876 else if ( value == L
"hr" ) token = T_IGNORE;
877 else if ( value == L
"li" ) token = T_LI;
878 else if ( value == L
"ol" ) { token = T_LEVEL; leveltag = 1; }
879 else if ( value == L
"qt" ) token = T_IGNORE;
880 else if ( value == L
"tt" ) token = T_TT;
881 else if ( value == L
"ul" ) { token = T_LEVEL; leveltag = 0; }
887 if ( value == L
"big" ) token = T_IGNORE;
888 else if ( value == L
"pre" ) token = T_PLAIN;
893 if ( value == L
"bold" ) token = T_BOLD;
894 else if ( value == L
"code" ) token = T_TT;
895 else if ( value == L
"font" ) token = T_IGNORE;
900 if ( value == L
"large" ) token = T_IGNORE;
901 else if ( value == L
"small" ) token = T_IGNORE;
906 if ( value == L
"center" ) token = T_PAR;
907 else if ( value == L
"strong" ) token = T_BOLD;
912 if ( value == L
"blockquote" ) token = T_PAR;
922 if ( token == T_UNKNOWN )
924 yuiDebug() <<
"T_UNKNOWN :" << value <<
":" << args <<
":" << std::endl;
930 if ( token == T_IGNORE )
936 PadChangeLevel( endtag, leveltag );
940 if ( endtag && !cindent )
959 if ( headinglevel && endtag )
984 if ( liststack.empty() )
986 tag = std::wstring( listindent, L
' ' );
992 if ( liststack.top() )
994 swprintf( buf, 15, L
"%2ld. ", liststack.top()++ );
998 swprintf( buf, 15, L
" %lc ", listleveltags[liststack.size()%listleveltags.size()] );
1005 cc = ( tag.size() < cc ? cc - tag.size() : 0 );
1009 PadTXT( tag.c_str(), tag.size() );
1021 AdjustPrePad( ech );
1065 void NCRichText::arm(
unsigned i )
1073 yuiDebug() << i <<
" (" << armed <<
")" << std::endl;
1077 if ( armed != Anchor::unset )
1080 anchors[armed].draw( *
myPad(), wStyle().richtext.getArmed( GetState() ), 0 );
1087 if ( armed != Anchor::unset )
1089 anchors[armed].draw( *
myPad(), wStyle().richtext.link, (
int ) wStyle().richtext.visitedlink );
1090 armed = Anchor::unset;
1093 if ( i != Anchor::unset )
1096 anchors[armed].draw( *
myPad(), wStyle().richtext.getArmed( GetState() ), 0 );
1099 if ( showLinkTarget )
1101 if ( armed != Anchor::unset )
1102 NCPadWidget::setLabel(
NCstring( anchors[armed].target ) );
1104 NCPadWidget::setLabel(
NCstring() );
1113 void NCRichText::HScroll(
unsigned total,
unsigned visible,
unsigned start )
1115 NCPadWidget::HScroll( total, visible, start );
1120 void NCRichText::VScroll(
unsigned total,
unsigned visible,
unsigned start )
1122 NCPadWidget::VScroll( total, visible, start );
1124 if ( plainText || anchors.empty() )
1129 vScrollFirstvisible = start;
1131 vScrollNextinvisible = start + visible;
1133 if ( armed != Anchor::unset )
1135 if ( anchors[armed].within( vScrollFirstvisible, vScrollNextinvisible ) )
1141 for (
unsigned i = 0; i < anchors.size(); ++i )
1143 if ( anchors[i].within( vScrollFirstvisible, vScrollNextinvisible ) )
1152 bool NCRichText::handleInput( wint_t key )
1154 if ( plainText || anchors.empty() )
1156 return NCPadWidget::handleInput( key );
1160 bool handled =
true;
1167 unsigned newarmed = Anchor::unset;
1169 if ( armed == Anchor::unset )
1172 for (
unsigned i = anchors.size(); i; )
1176 if ( anchors[i].eline < vScrollFirstvisible )
1183 else if ( armed > 0 )
1185 newarmed = armed - 1;
1188 if ( newarmed == Anchor::unset )
1190 handled = NCPadWidget::handleInput( KEY_UP );
1194 if ( !anchors[newarmed].within( vScrollFirstvisible, vScrollNextinvisible ) )
1195 myPad()->ScrlLine( anchors[newarmed].sline );
1206 unsigned newarmed = Anchor::unset;
1208 if ( armed == Anchor::unset )
1211 for (
unsigned i = 0; i < anchors.size(); ++i )
1213 if ( anchors[i].sline >= vScrollNextinvisible )
1220 else if ( armed + 1 < anchors.size() )
1222 newarmed = armed + 1;
1225 if ( newarmed == Anchor::unset )
1227 handled = NCPadWidget::handleInput( KEY_DOWN );
1231 if ( !anchors[newarmed].within( vScrollFirstvisible, vScrollNextinvisible ) )
1232 myPad()->ScrlLine( anchors[newarmed].sline );
1243 if ( armed != Anchor::unset
1245 && anchors[armed-1].within( vScrollFirstvisible, vScrollNextinvisible ) )
1251 handled = NCPadWidget::handleInput( key );
1259 if ( armed != Anchor::unset
1260 && armed + 1 < anchors.size()
1261 && anchors[armed+1].within( vScrollFirstvisible, vScrollNextinvisible ) )
1267 handled = NCPadWidget::handleInput( key );
1273 handled = NCPadWidget::handleInput( key );
virtual void setEnabled(bool do_bv)
int addwstr(const wchar_t *str, int n=-1)
int chgat(int n, attr_t attr, short color, const void *opts=NULL)