The Battle for Wesnoth  1.19.8+dev
markup.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2024
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 "color.hpp"
18 #include "exceptions.hpp"
19 // This file isn't needed by any of these functions, but this allows any
20 // standard color to be passed to span_color without an extra include.
21 #include "font/standard_colors.hpp"
22 #include "formatter.hpp"
23 
24 #include <string>
25 #include <string_view>
26 
27 class config;
28 
29 namespace markup
30 {
31 /**
32  * A Help markup tag corresponding to a linebreak.
33  * @see gui2::rich_label for details on how this tag is parsed.
34  */
35 constexpr std::string_view br{"<br/>"};
36 
37 /**
38  * Wraps the given data in the specified formatting tag.
39  *
40  * @param tag The formatting tag ("b", "i", etc).
41  * @param data The content to wrap with @a tag.
42  * Each argument must be writable to a stringstream.
43  *
44  * @note Special formatting characters in the input are not escaped.
45  * If such behavior is needed, it must be handled by the caller.
46  * If the concatenation of @a data results in an empty string,
47  * an empty string is returned in lieu of formatting tags.
48  */
49 template<typename... Args>
50 std::string tag(std::string_view tag, Args&&... data)
51 {
52  std::string input = (formatter() << ... << data);
53  if(input.empty()) return {};
54  return formatter() << "<" << tag << ">" << input << "</" << tag << ">";
55 }
56 
57 /**
58  * Wraps the given data in a @c span tag with the specified attribute and value.
59  *
60  * @param key The span attribute ("color", "size", etc).
61  * @param value The attribute value.
62  * @param data The content to format.
63  *
64  * @note See @ref tag for more information.
65  */
66 template<typename Value, typename... Args>
67 std::string span_attribute(std::string_view key, const Value& value, Args&&... data)
68 {
69  std::string input = (formatter() << ... << data);
70  if(input.empty()) return {};
71  return formatter() << "<span " << key << "='" << value << "'>" << input << "</span>";
72 }
73 
74 /**
75  * Applies Pango markup to the input specifying its display color.
76  *
77  * @param color The color_t object from which to retrieve the color.
78  * @param data Variable list of content to enclose inside the span tag.
79  * Each argument must be writable to a stringstream.
80  *
81  * @returns @code `<span color='#color'>#data</span>` @endcode
82  *
83  * @note Special formatting characters in the input are not escaped.
84  * If such behavior is needed, it must be handled by the caller.
85  */
86 template<typename... Args>
87 std::string span_color(const color_t& color, Args&&... data)
88 {
89  return span_attribute("color", color.to_hex_string(), std::forward<Args>(data)...);
90 }
91 
92 /**
93  * Applies Pango markup to the input specifying its display color.
94  *
95  * @param color The hex color string.
96  * @param data Variable list of content to enclose inside the span tag.
97  * Each argument must be writable to a stringstream.
98  *
99  * @returns @code `<span color='#color'>#data</span>` @endcode
100  *
101  * @note Special formatting characters in the input are not escaped.
102  * If such behavior is needed, it must be handled by the caller.
103  */
104 template<typename... Args>
105 std::string span_color(std::string_view color, Args&&... data)
106 {
107  return span_attribute("color", color, std::forward<Args>(data)...);
108 }
109 
110 /**
111  * Applies Pango markup to the input specifying its display size.
112  *
113  * @param size A Pango string size specifier (large, small, x-large, etc).
114  * @param data Variable list of content to concatenate inside the span tag.
115  * Each argument must be writable to a stringstream.
116  *
117  * @returns @code `<span size='#size'>#data</span>` @endcode
118  *
119  * @note Special formatting characters in the input are not escaped.
120  * If such behavior is needed, it must be handled by the caller.
121  */
122 template<typename... Args>
123 std::string span_size(std::string_view size, Args&&... data)
124 {
125  return span_attribute("size", size, std::forward<Args>(data)...);
126 }
127 
128 /**
129  * Applies bold Pango markup to the input.
130  *
131  * @param data Variable list of content to concatenate inside the bold tag.
132  * Each argument must be writable to a stringstream.
133  *
134  * @note Special formatting characters in the input are not escaped.
135  * If such behavior is needed, it must be handled by the caller.
136  */
137 template<typename... Args>
138 std::string bold(Args&&... data)
139 {
140  return tag("b", std::forward<Args>(data)...);
141 }
142 
143 /**
144  * Applies italic Pango markup to the input.
145  *
146  * @param data Variable list of content to enclose inside the italic tag.
147  * Each argument must be writable to a stringstream.
148  *
149  * @note Special formatting characters in the input are not escaped.
150  * If such behavior is needed, it must be handled by the caller.
151  */
152 template<typename... Args>
153 std::string italic(Args&&... data)
154 {
155  return tag("i", std::forward<Args>(data)...);
156 }
157 
158 /**
159  * Generates a Help markup tag corresponding to an image.
160  *
161  * @param src The WML path to the image (i.e., 'units/drakes/arbiter.png')
162  * @param align Alignment of the image. Possible values: left, right, center.
163  * @param floating Is the image a floating image or an inline image?
164  *
165  * @note Special formatting characters in the input are not escaped.
166  * If such behavior is needed, it must be handled by the caller.
167  * @see gui2::rich_label for details on how this tag is parsed.
168  */
169 std::string img(const std::string& src, const std::string& align = "left", bool floating = false);
170 
171 /**
172  * Generates a Help markup tag corresponding to a reference or link.
173  *
174  * @param text User visible text/caption of the link.
175  * @param dst Destination of the link. Can be any string depending on the link handler
176  * in the parsing @ref gui2::rich_label.
177  *
178  * @note Special formatting characters in the input are not escaped.
179  * If such behavior is needed, it must be handled by the caller.
180  * @see gui2::rich_label for details on how this tag is parsed.
181  */
182 std::string make_link(const std::string& text, const std::string& dst);
183 
184 //
185 // Markup Parser
186 //
187 
188 /** Thrown when the help system fails to parse something. */
189 struct parse_error : public game::error
190 {
191  parse_error(const std::string& msg) : game::error(msg) {}
192 };
193 
194 /**
195  * Parse a xml style marked up text string. Return a config with the different parts of the
196  * text. Each markup item is a separate part while the text between
197  * markups are separate parts.
198  */
199 config parse_text(const std::string &text);
200 
201 } // namespace markup
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:158
std::ostringstream wrapper.
Definition: formatter.hpp:40
std::string italic(Args &&... data)
Applies italic Pango markup to the input.
Definition: markup.hpp:153
std::string span_attribute(std::string_view key, const Value &value, Args &&... data)
Wraps the given data in a span tag with the specified attribute and value.
Definition: markup.hpp:67
std::string img(const std::string &src, const std::string &align, bool floating)
Generates a Help markup tag corresponding to an image.
Definition: markup.cpp:31
std::string make_link(const std::string &text, const std::string &dst)
Generates a Help markup tag corresponding to a reference or link.
Definition: markup.cpp:25
std::string bold(Args &&... data)
Applies bold Pango markup to the input.
Definition: markup.hpp:138
std::string span_color(const color_t &color, Args &&... data)
Applies Pango markup to the input specifying its display color.
Definition: markup.hpp:87
constexpr std::string_view br
A Help markup tag corresponding to a linebreak.
Definition: markup.hpp:35
std::string tag(std::string_view tag, Args &&... data)
Wraps the given data in the specified formatting tag.
Definition: markup.hpp:50
config parse_text(const std::string &text)
Parse a xml style marked up text string.
Definition: markup.cpp:403
std::string span_size(std::string_view size, Args &&... data)
Applies Pango markup to the input specifying its display size.
Definition: markup.hpp:123
std::size_t size(std::string_view str)
Length in characters of a UTF-8 string.
Definition: unicode.cpp:85
static void msg(const char *act, debug_info &i, const char *to="", const char *result="")
Definition: debugger.cpp:109
std::string_view data
Definition: picture.cpp:178
rect dst
Location on the final composed sheet.
rect src
Non-transparent portion of the surface to compose.
The basic class for representing 8-bit RGB or RGBA colour values.
Definition: color.hpp:59
std::string to_hex_string() const
Returns the stored color in rrggbb hex format.
Definition: color.cpp:88
Base class for all the errors encountered by the engine.
Definition: exceptions.hpp:29
Thrown when the help system fails to parse something.
Definition: markup.hpp:190
parse_error(const std::string &msg)
Definition: markup.hpp:191