Source-highlight Library
|
00001 // 00002 // Author: Lorenzo Bettini <http://www.lorenzobettini.it>, (C) 2004-2008 00003 // 00004 // Copyright: See COPYING file that comes with this distribution 00005 // 00006 00007 #ifndef SOURCEHIGHLIGHTER_H_ 00008 #define SOURCEHIGHLIGHTER_H_ 00009 00010 #include <string> 00011 #include <stack> 00012 #include <sstream> 00013 #include <boost/shared_ptr.hpp> 00014 00015 #include "highlightstate.h" 00016 #include "eventgenerator.h" 00017 00018 namespace srchilite { 00019 00020 class FormatterManager; 00021 struct HighlightToken; 00022 struct FormatterParams; 00023 class HighlightEventListener; 00024 struct HighlightEvent; 00025 00026 typedef std::stack<HighlightStatePtr> HighlightStateStack; 00027 typedef boost::shared_ptr<HighlightStateStack> HighlightStateStackPtr; 00028 00043 class SourceHighlighter: public EventGenerator<HighlightEventListener, 00044 HighlightEvent> { 00046 HighlightStatePtr mainHighlightState; 00047 00049 HighlightStatePtr currentHighlightState; 00050 00052 HighlightStateStackPtr stateStack; 00053 00055 const FormatterManager *formatterManager; 00056 00061 bool optimize; 00062 00068 bool suspended; 00069 00073 FormatterParams *formatterParams; 00074 00078 std::string currentElement; 00079 00083 std::ostringstream currentElementBuffer; 00084 00089 void enterState(HighlightStatePtr state); 00090 00095 void exitState(int level); 00096 00100 void exitAll(); 00101 00107 HighlightStatePtr getNextState(const HighlightToken &token); 00108 00114 void format(const std::string &elem, const std::string &s); 00115 00120 void flush(); 00121 00122 public: 00126 SourceHighlighter(HighlightStatePtr mainState); 00127 ~SourceHighlighter(); 00128 00133 void highlightParagraph(const std::string ¶graph); 00134 00135 HighlightStatePtr getCurrentState() const { 00136 return currentHighlightState; 00137 } 00138 00139 void setCurrentState(HighlightStatePtr state) { 00140 currentHighlightState = state; 00141 } 00142 00143 HighlightStateStackPtr getStateStack() { 00144 return stateStack; 00145 } 00146 00147 void setStateStack(HighlightStateStackPtr state) { 00148 stateStack = state; 00149 } 00150 00154 void clearStateStack(); 00155 00156 HighlightStatePtr getMainState() const { 00157 return mainHighlightState; 00158 } 00159 00160 const FormatterManager *getFormatterManager() const { 00161 return formatterManager; 00162 } 00163 00164 void setFormatterManager(const FormatterManager *_formatterManager) { 00165 formatterManager = _formatterManager; 00166 } 00167 00168 bool getOptimize() const { 00169 return optimize; 00170 } 00171 00172 void setOptimize(bool b = true) { 00173 optimize = b; 00174 } 00175 00176 void setFormatterParams(FormatterParams *p) { 00177 formatterParams = p; 00178 } 00179 00180 bool isSuspended() const { 00181 return suspended; 00182 } 00183 00184 void setSuspended(bool b = true) { 00185 suspended = b; 00186 } 00187 }; 00188 00189 } 00190 00191 #endif /*SOURCEHIGHLIGHTER_H_*/