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