Source-highlight Library
srchilite::TextStyle Class Reference

Represents a formatting template where there can be some variables (starting with $, e.g., $style, $text, etc.) that will be replaced with specific elements, e.g., the actual style for the formatting, the text to format, etc. More...

#include <textstyle.h>

Collaboration diagram for srchilite::TextStyle:

Public Member Functions

 TextStyle (const std::string &s="", const char **vars=0)
 
std::string output (const std::string &text, const std::string &style="")
 substitutes $text with text and $style with style More...
 
std::string output (SubstitutionMapping &subst_map)
 for each i substitutes: subst_map[i].first occurrence with subst_map[i].second More...
 
std::string subst_style (const std::string &style="")
 substitutes $style with style More...
 
const std::string & toString () const
 
TextStyle compose (const TextStyle &inner)
 substitutes $text with the string representation of inner e.g., if this is $text and inner is $text this will return $text More...
 
void update (const TextStyle &inner)
 as compose, but acts on this instance More...
 
void update (const std::string &inner)
 as compose, but acts on this instance More...
 
void update (const std::string &text, const std::string &style)
 as output, but acts on this instance More...
 
bool containsStyleVar () const
 
bool empty () const
 

Private Types

typedef std::vector< std::string > StringVector
 
typedef std::vector< int > IndexVector
 
typedef std::map< std::string,
IndexVector > 
SubstitutionIndexes
 

Private Member Functions

void build_vectors ()
 The parts vector contains the string repr split in parts: those that constant parts and those that represent $text and $style variables. More...
 

Private Attributes

boost::regex var_exp
 the regular expression to find variable occurrences
 
std::string repr
 
StringVector parts
 contains all the string parts of this TextStyle.
 
SubstitutionIndexes substitutions
 contains the indexes of parts where to substitute $vars.
 
bool invalid
 whether to rebuild the vectors
 

Detailed Description

Represents a formatting template where there can be some variables (starting with $, e.g., $style, $text, etc.) that will be replaced with specific elements, e.g., the actual style for the formatting, the text to format, etc.

Constructor & Destructor Documentation

srchilite::TextStyle::TextStyle ( const std::string &  s = "",
const char **  vars = 0 
)
Parameters
sthe representation
varsan array of string representing the variables in this TextStyle (e.g., {"linenum", "infilename", "infile", "outfile", 0}), the last element must be 0

Member Function Documentation

void srchilite::TextStyle::build_vectors ( )
private

The parts vector contains the string repr split in parts: those that constant parts and those that represent $text and $style variables.

For instance, if repr == "<span class=\"$style">$text", then we have:

  • parts[0] == "<span class=\"" - parts[1] == "" - parts[2] == "">" - parts[3] == "" - parts[4] == ""

Then style_substitutions contains the index(es) of parts corresponding to $style variable occurrences that will be substituted with values and text_substitutions is the same but for $text occurrences. Thus, in this case:

  • style_substitutions[0] = 1
  • text_substitutions[0] = 3
TextStyle srchilite::TextStyle::compose ( const TextStyle inner)

substitutes $text with the string representation of inner e.g., if this is $text and inner is $text this will return $text

Parameters
inner
Returns
a new TextStyle after substitution
bool srchilite::TextStyle::containsStyleVar ( ) const
Returns
whether this TextStyle contains the $style variable
bool srchilite::TextStyle::empty ( ) const
Returns
whether it is only $style or $text
string srchilite::TextStyle::output ( const std::string &  text,
const std::string &  style = "" 
)

substitutes $text with text and $style with style

First substitute in the vector parts the values for $text and $style and return a string with the substituted values.

Parameters
text
style
Returns
the string after substitutions
Parameters
text
style
Returns
std::string srchilite::TextStyle::output ( SubstitutionMapping subst_map)

for each i substitutes: subst_map[i].first occurrence with subst_map[i].second

Parameters
subst_map
Returns
the string after substitutions
string srchilite::TextStyle::subst_style ( const std::string &  style = "")

substitutes $style with style

Parameters
style
Returns
the string after substitutions
const std::string& srchilite::TextStyle::toString ( ) const
inline
Returns
the string representation
void srchilite::TextStyle::update ( const TextStyle inner)

as compose, but acts on this instance

Parameters
inner
void srchilite::TextStyle::update ( const std::string &  inner)

as compose, but acts on this instance

Parameters
inner
void srchilite::TextStyle::update ( const std::string &  text,
const std::string &  style 
)

as output, but acts on this instance

Parameters
text
style

The documentation for this class was generated from the following files: