diff options
author | haranp <haranp@c06c8d41-db1a-0410-9941-cceddc491573> | 2007-10-15 23:25:45 +0000 |
---|---|---|
committer | haranp <haranp@c06c8d41-db1a-0410-9941-cceddc491573> | 2007-10-15 23:25:45 +0000 |
commit | 69c6fd7841394d492337a5b3e44da19ec7c7bc3b (patch) | |
tree | c7e5370bd8d68412161369e0404babefce9dcc68 /crawl-ref/source/format.cc | |
parent | 26f6c03dbe7284b13168280c07e77112ebe854d5 (diff) | |
download | crawl-ref-69c6fd7841394d492337a5b3e44da19ec7c7bc3b.tar.gz crawl-ref-69c6fd7841394d492337a5b3e44da19ec7c7bc3b.zip |
Added basic HTML output functionality to formatted_string. Not used
anywhere yet.
git-svn-id: https://crawl-ref.svn.sourceforge.net/svnroot/crawl-ref/trunk@2479 c06c8d41-db1a-0410-9941-cceddc491573
Diffstat (limited to 'crawl-ref/source/format.cc')
-rw-r--r-- | crawl-ref/source/format.cc | 42 |
1 files changed, 42 insertions, 0 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) { |