1 // 2 // C++ Interface: textstyle 3 // 4 // Description: 5 // 6 // 7 // Author: Lorenzo Bettini <http://www.lorenzobettini.it>, (C) 2005-2009 8 // 9 // Copyright: See COPYING file that comes with this distribution 10 // 11 // 12 13 #ifndef _TEXTSTYLE_H_ 14 #define _TEXTSTYLE_H_ 15 16 #include <string> 17 #include <vector> 18 #include <map> 19 #include <boost/regex.hpp> 20 21 namespace srchilite { 22 23 #define STYLE_VAR_TEXT "$style" // the text of the style variable 24 #define TEXT_VAR_TEXT "$text" // the text of the text variable 25 #define STYLE_VAR "\\" STYLE_VAR_TEXT // the name of the style variable as regexp 26 #define TEXT_VAR "\\" TEXT_VAR_TEXT // the name of the text variable as regexp 27 /// map for substitutions 28 typedef std::map<std::string, std::string> SubstitutionMapping; 29 30 /** 31 * Represents a formatting template where there can be some 32 * variables (starting with $, e.g., $style, $text, etc.) that will be replaced 33 * with specific elements, e.g., the actual style for the formatting, the 34 * text to format, etc. 35 */ 36 class TextStyle { 37 private: 38 typedef std::vector<std::string> StringVector; 39 typedef std::vector<int> IndexVector; 40 typedef std::map<std::string, IndexVector> SubstitutionIndexes; 41 42 /// the regular expression to find variable occurrences 43 boost::regex var_exp; 44 45 std::string repr; 46 47 /// contains all the string parts of this TextStyle. 48 StringVector parts; 49 50 /// contains the indexes of parts where to substitute $vars. 51 SubstitutionIndexes substitutions; 52 53 /// whether to rebuild the vectors 54 bool invalid; 55 56 void build_vectors(); 57 58 public: 59 /** 60 * @param s the representation 61 * @param vars an array of string representing the variables in this TextStyle 62 * (e.g., {"linenum", "infilename", "infile", "outfile", 0}), the last element must 63 * be 0 64 */ 65 TextStyle(const std::string &s = "", const char **vars = 0); 66 ~TextStyle(); 67 68 /** 69 * substitutes $text with text and $style with style 70 * @param text 71 * @param style 72 * @return the string after substitutions 73 */ 74 std::string output(const std::string &text, const std::string &style = ""); 75 76 /** 77 * for each i substitutes: subst_map[i].first occurrence with subst_map[i].second 78 * @param subst_map 79 * @return the string after substitutions 80 */ 81 std::string output(SubstitutionMapping &subst_map); 82 83 /** 84 * substitutes $style with style 85 * @param style 86 * @return the string after substitutions 87 */ 88 std::string subst_style(const std::string &style = ""); 89 90 /** 91 * @return the string representation 92 */ toString()93 const std::string &toString() const { 94 return repr; 95 } 96 97 /** 98 * substitutes $text with the string representation of inner 99 * e.g., if this is <b>$text</b> and inner is <i>$text</i> 100 * this will return <b><i>$text</i></b> 101 * @param inner 102 * @return a new TextStyle after substitution 103 */ 104 TextStyle compose(const TextStyle &inner); 105 106 /** 107 * as compose, but acts on this instance 108 * @param inner 109 */ 110 void update(const TextStyle &inner); 111 112 /** 113 * as compose, but acts on this instance 114 * @param inner 115 */ 116 void update(const std::string &inner); 117 118 /** 119 * as output, but acts on this instance 120 * @param text 121 * @param style 122 */ 123 void update(const std::string &text, const std::string &style); 124 125 /** 126 * @return whether this TextStyle contains the $style variable 127 */ 128 bool containsStyleVar() const; 129 130 /** 131 * @return whether it is only $style or $text 132 */ 133 bool empty() const; 134 }; 135 136 } 137 138 #endif /*_TEXTSTYLE_H_*/ 139