The Battle for Wesnoth  1.15.0-dev
escape.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2008 - 2018 by Mark de Wever <koraq@xs4all.nl>
3  Part of the Battle for Wesnoth Project https://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 #include <sstream>
18 
19 namespace font
20 {
21 /**
22  * Escapes the pango markup characters in a text.
23  *
24  * The markups escaped are the ones used in the pango markup. The special
25  * characters are: @verbatim <>'"& @endverbatim
26  * The escaping is the same as for HTML.
27  *
28  * @param text The text to escape.
29  *
30  * @returns The escaped text.
31  */
32 inline std::string escape_text(const std::string& text)
33 {
34  std::ostringstream ss;
35  for(const char c : text) {
36  switch(c) {
37  case '&': ss << "&amp;"; break;
38  case '<': ss << "&lt;"; break;
39  case '>': ss << "&gt;"; break;
40  case '\'': ss << "&apos;"; break;
41  case '"': ss << "&quot;"; break;
42  default: ss << c;
43  }
44  }
45 
46  return ss.str();
47 }
48 
49 // Escape only the ampersands. This is used by pango_text to try to recover from
50 // markup parsing failure.
51 inline std::string semi_escape_text(const std::string & text)
52 {
53  std::ostringstream ss;
54  for(const char c : text) {
55  if(c == '&') {
56  ss << "&amp;";
57  } else {
58  ss << c;
59  }
60  }
61 
62  return ss.str();
63 }
64 
65 } // end namespace font
Collection of helper functions relating to Pango formatting.
std::string semi_escape_text(const std::string &text)
Definition: escape.hpp:51
std::string escape_text(const std::string &text)
Escapes the pango markup characters in a text.
Definition: escape.hpp:32
mock_char c