diff options
Diffstat (limited to 'crawl-ref')
-rw-r--r-- | crawl-ref/source/format.cc | 42 | ||||
-rw-r--r-- | crawl-ref/source/format.h | 3 |
2 files changed, 44 insertions, 1 deletions
diff --git a/crawl-ref/source/format.cc b/crawl-ref/source/format.cc index 400f900dff..33bb2a47d1 100644 --- a/crawl-ref/source/format.cc +++ b/crawl-ref/source/format.cc @@ -166,6 +166,48 @@ formatted_string::operator std::string() const return (s); } +void replace_all_in_string(std::string& s, const std::string& search, + const std::string& replace) +{ + std::string::size_type pos = 0; + while ( (pos = s.find(search, pos)) != std::string::npos ) + { + s.replace(pos, search.size(), replace); + pos += replace.size(); + } +} + +std::string formatted_string::html_dump() const +{ + std::string s; + for (unsigned i = 0; i < ops.size(); ++i) + { + std::string tmp; + switch (ops[i].type) + { + case FSOP_TEXT: + tmp = ops[i].text; + // (very) crude HTMLification + replace_all_in_string(tmp, "&", "&"); + replace_all_in_string(tmp, " ", " "); + replace_all_in_string(tmp, "<", "<"); + replace_all_in_string(tmp, ">", ">"); + replace_all_in_string(tmp, "\n", "<br>"); + s += tmp; + break; + case FSOP_COLOUR: + s += "<font color="; + s += colour_to_str(ops[i].x); + s += ">"; + break; + case FSOP_CURSOR: + // FIXME error handling? + break; + } + } + return s; +} + const formatted_string & formatted_string::operator += (const formatted_string &other) { diff --git a/crawl-ref/source/format.h b/crawl-ref/source/format.h index 8391c2cbb8..6376849b27 100644 --- a/crawl-ref/source/format.h +++ b/crawl-ref/source/format.h @@ -38,6 +38,7 @@ public: void swap(formatted_string& other); std::string::size_type length() const; + std::string html_dump() const; const formatted_string &operator += (const formatted_string &other); @@ -58,7 +59,7 @@ private: int find_last_colour() const; public: - + struct fs_op { fs_op_type type; |