The Battle for Wesnoth  1.13.10+dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
escape.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2008 - 2017 by Mark de Wever <koraq@xs4all.nl>
3  Part of the Battle for Wesnoth Project http://www.wesnoth.org/
4 
5  This program is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation; either version 2 of the License, or
8  (at your option) any later version.
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY.
11 
12  See the COPYING file for more details.
13 */
14 
15 #pragma once
16 
17 namespace font {
18 
19 /**
20  * Escapes the pango markup characters in a text.
21  *
22  * The markups escaped are the ones used in the pango markup. The special
23  * characters are: @verbatim <>'"& @endverbatim
24  * The escaping is the same as for HTML.
25  *
26  * @param text The text to escape.
27  *
28  * @returns The escaped text.
29  */
30 inline std::string escape_text(const std::string& text)
31 {
32  std::string result;
33  for(const char c : text) {
34  switch(c) {
35  case '&': result += "&amp;"; break;
36  case '<': result += "&lt;"; break;
37  case '>': result += "&gt;"; break;
38  case '\'': result += "&apos;"; break;
39  case '"': result += "&quot;"; break;
40  default: result += c;
41  }
42  }
43  return result;
44 }
45 
46 // Escape only the ampersands. This is used by pango_text to try to recover from
47 // markup parsing failure.
49  std::string semi_escaped;
50  for(const char c : text) {
51  if(c == '&') {
52  semi_escaped += "&amp;";
53  } else {
54  semi_escaped += c;
55  }
56  }
57  return semi_escaped;
58 }
59 
60 } // end namespace font
std::vector< char_t > string
Note: Specific to sdl_ttf.
std::string semi_escape_text(const std::string &text)
Definition: escape.hpp:48
std::string escape_text(const std::string &text)
Escapes the pango markup characters in a text.
Definition: escape.hpp:30
mock_char c