The Battle for Wesnoth  1.19.5+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 "config.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"
24 
25 #include <string>
26 
27 namespace markup {
28 
29 const static std::string br = "<br/>";
30 
31 /**
32  * Returns the contents enclosed inside `<tag_name>` and `</tag_name>`
33  */
34 template<typename... Args>
35 std::string tag(const std::string& tag_name, Args&&... contents)
36 {
37  return ((formatter()
38  << "<" << tag_name << ">")
39  << ...
40  << contents)
41  << "</" << tag_name << ">";
42 }
43 
44 /**
45  * Returns the contents enclosed inside `<tag_name>` and `</tag_name>`
46  * This version escapes single quotes and backslashes.
47  */
48 template<typename... Args>
49 std::string tag_esc(const std::string& tag_name, Args&&... contents)
50 {
51  return ((formatter()
52  << "<" << tag_name << ">")
53  << ...
54  << utils::escape(contents, "'\\"))
55  << "</" << tag_name << ">";
56 }
57 
58 /**
59  * Returns a Pango formatting string using the provided color_t object.
60  *
61  * The string returned will be in format: `<span foreground=#color>#data</span>`
62  *
63  * @param color The color_t object from which to retrieve the color.
64  * @param data The string to enclose inside the tag. All elements in this list
65  * will be concatenated to formatter().
66  */
67 template<typename... Args>
68 std::string span_color(const color_t& color, Args&&... data)
69 {
70  return ((formatter() << "<span color='" << color.to_hex_string() << "'>") << ... << data) << "</span>";
71 }
72 
73 /**
74  * Returns a Pango formatting string using the provided hex color string.
75  *
76  * The string returned will be in format: `<span foreground=#color>#data</span>`
77  *
78  * @param color The hex color string.
79  * @param data The string to enclose inside the tag. All elements in this list
80  * will be concatenated to formatter().
81  */
82 template<typename... Args>
83 std::string span_color(const std::string& color, Args&&... data)
84 {
85  return ((formatter() << "<span color='" << color << "'>") << ... << data) << "</span>";
86 }
87 
88 /**
89  * Returns a Pango formatting string that set the font size of the enclosed data.
90  *
91  * The string returned will be in format: `<span size=#size>#data</span>`
92  *
93  * @param size The font size. String so values like x-large, large etc could be used.
94  * @param data The string to enclose inside the tag. All elements in this list
95  * will be concatenated to formatter().
96  */
97 template<typename... Args>
98 std::string span_size(const std::string& size, Args&&... data)
99 {
100  return ((formatter() << "<span size='" << size << "'>") << ... << data) << "</span>";
101 }
102 
103 /**
104  * Returns a Pango formatting string corresponding to bold formatting
105  *
106  * @param s The string to enclose in bold tag.
107  */
108 template<typename... Args>
109 std::string bold(Args&&... s)
110 {
111  return tag_esc("b", (formatter() << ... << s).str());
112 }
113 
114 /**
115  * Returns a Pango formatting string corresponding to italic formatting
116  *
117  * @param s The string to enclose in italic tag.
118  */
119 template<typename... Args>
120 std::string italic(Args&&... s)
121 {
122  return tag_esc("i", (formatter() << ... << s).str());
123 }
124 
125 std::string img(const std::string& src, const std::string& align = "left", const bool floating = false);
126 
127 std::string make_link(const std::string& text, const std::string& dst);
128 
129 //
130 // Markup Parser
131 //
132 
133 /** Thrown when the help system fails to parse something. */
134 struct parse_error : public game::error
135 {
136  parse_error(const std::string& msg) : game::error(msg) {}
137 };
138 
139 /**
140  * Parse a xml style marked up text string. Return a config with the different parts of the
141  * text. Each markup item is a separate part while the text between
142  * markups are separate parts.
143  */
144 config parse_text(const std::string &text);
145 
146 } //end namespace markup
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:172
std::ostringstream wrapper.
Definition: formatter.hpp:40
Definitions for the interface to Wesnoth Markup Language (WML).
std::string bold(Args &&... s)
Returns a Pango formatting string corresponding to bold formatting.
Definition: markup.hpp:109
static const std::string br
Definition: markup.hpp:29
std::string span_size(const std::string &size, Args &&... data)
Returns a Pango formatting string that set the font size of the enclosed data.
Definition: markup.hpp:98
std::string make_link(const std::string &text, const std::string &dst)
Definition: markup.cpp:23
std::string img(const std::string &src, const std::string &align, const bool floating)
Definition: markup.cpp:29
std::string tag(const std::string &tag_name, Args &&... contents)
Returns the contents enclosed inside <tag_name> and </tag_name>
Definition: markup.hpp:35
std::string span_color(const color_t &color, Args &&... data)
Returns a Pango formatting string using the provided color_t object.
Definition: markup.hpp:68
std::string italic(Args &&... s)
Returns a Pango formatting string corresponding to italic formatting.
Definition: markup.hpp:120
std::string tag_esc(const std::string &tag_name, Args &&... contents)
Returns the contents enclosed inside <tag_name> and </tag_name> This version escapes single quotes an...
Definition: markup.hpp:49
config parse_text(const std::string &text)
Parse a xml style marked up text string.
Definition: markup.cpp:401
std::size_t size(const std::string &str)
Length in characters of a UTF-8 string.
Definition: unicode.cpp:85
std::string escape(const std::string &str, const char *special_chars)
Prepends a configurable set of characters with a backslash.
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:135
parse_error(const std::string &msg)
Definition: markup.hpp:136
static map_location::direction s