Source-highlight Library
sourcehighlighter.h
1//
2// Author: Lorenzo Bettini <http://www.lorenzobettini.it>, (C) 2004-2008
3//
4// Copyright: See COPYING file that comes with this distribution
5//
6
7#ifndef SOURCEHIGHLIGHTER_H_
8#define SOURCEHIGHLIGHTER_H_
9
10#include <string>
11#include <stack>
12#include <sstream>
13#include <boost/shared_ptr.hpp>
14
15#include "highlightstate.h"
16#include "eventgenerator.h"
17
18namespace srchilite {
19
20class FormatterManager;
21struct HighlightToken;
22struct FormatterParams;
23class HighlightEventListener;
24struct HighlightEvent;
25
26typedef std::stack<HighlightStatePtr> HighlightStateStack;
27typedef boost::shared_ptr<HighlightStateStack> HighlightStateStackPtr;
28
43class SourceHighlighter: public EventGenerator<HighlightEventListener,
44 HighlightEvent> {
47
50
52 HighlightStateStackPtr stateStack;
53
56
62
69
74
78 std::string currentElement;
79
83 std::ostringstream currentElementBuffer;
84
89 void enterState(HighlightStatePtr state);
90
95 void exitState(int level);
96
100 void exitAll();
101
108
114 void format(const std::string &elem, const std::string &s);
115
120 void flush();
121
122public:
128
133 void highlightParagraph(const std::string &paragraph);
134
135 HighlightStatePtr getCurrentState() const {
137 }
138
139 void setCurrentState(HighlightStatePtr state) {
140 currentHighlightState = state;
141 }
142
143 HighlightStateStackPtr getStateStack() {
144 return stateStack;
145 }
146
147 void setStateStack(HighlightStateStackPtr state) {
148 stateStack = state;
149 }
150
154 void clearStateStack();
155
156 HighlightStatePtr getMainState() const {
157 return mainHighlightState;
158 }
159
160 const FormatterManager *getFormatterManager() const {
161 return formatterManager;
162 }
163
164 void setFormatterManager(const FormatterManager *_formatterManager) {
165 formatterManager = _formatterManager;
166 }
167
168 bool getOptimize() const {
169 return optimize;
170 }
171
172 void setOptimize(bool b = true) {
173 optimize = b;
174 }
175
176 void setFormatterParams(FormatterParams *p) {
177 formatterParams = p;
178 }
179
180 bool isSuspended() const {
181 return suspended;
182 }
183
184 void setSuspended(bool b = true) {
185 suspended = b;
186 }
187};
188
189}
190
191#endif /*SOURCEHIGHLIGHTER_H_*/
A generic event generator, for listeners of type EventListener and events of type EventType.
Definition: eventgenerator.h:18
Associates to an element name the corresponding formatter.
Definition: formattermanager.h:24
The main class performing the highlighting of a single line.
Definition: sourcehighlighter.h:44
void flush()
Makes sure to flush the possible buffer of the current element (e.g., during optimizations)
Definition: sourcehighlighter.cpp:211
void format(const std::string &elem, const std::string &s)
Formats the given string as the specified element.
Definition: sourcehighlighter.cpp:182
HighlightStatePtr currentHighlightState
the current highlight state
Definition: sourcehighlighter.h:49
void highlightParagraph(const std::string &paragraph)
Highlights a paragraph (a line actually)
Definition: sourcehighlighter.cpp:51
bool suspended
Whether formatting is currently suspended.
Definition: sourcehighlighter.h:68
void exitState(int level)
Exits level states (-1 means exit all states)
Definition: sourcehighlighter.cpp:163
std::string currentElement
The current element being formatted (used for optmization and buffering)
Definition: sourcehighlighter.h:78
HighlightStatePtr getNextState(const HighlightToken &token)
Computes the (possible) next state for the given rule (if required, also performs reference replaceme...
Definition: sourcehighlighter.cpp:128
const FormatterManager * formatterManager
the formatter manager, used to format element strings
Definition: sourcehighlighter.h:55
FormatterParams * formatterParams
Additional parameters for the formatters.
Definition: sourcehighlighter.h:73
std::ostringstream currentElementBuffer
The buffer for the text for the current element.
Definition: sourcehighlighter.h:83
bool optimize
Whether to optimize output (e.g., adjacent text parts belonging to the same element will be buffered ...
Definition: sourcehighlighter.h:61
HighlightStatePtr mainHighlightState
the main (and initial) highlight state
Definition: sourcehighlighter.h:46
void enterState(HighlightStatePtr state)
Enters a new state (using the stack)
Definition: sourcehighlighter.cpp:154
void exitAll()
Exits all states in the stack (and thus go back to the initial main state)
Definition: sourcehighlighter.cpp:172
void clearStateStack()
Clears the statck of states.
Definition: sourcehighlighter.cpp:177
HighlightStateStackPtr stateStack
the stack for the highlight states
Definition: sourcehighlighter.h:52
C++ class: doctemplate.h.
Definition: bufferedoutput.cpp:13
boost::shared_ptr< HighlightState > HighlightStatePtr
the reference to an HighlightState
Definition: highlightstate.h:41
Additional parameters that can be passed to a formatter.
Definition: formatterparams.h:18
Token containing information for performing the highlight.
Definition: highlighttoken.h:33