diff options
| author | Joel Klinghed <the_jk@spawned.biz> | 2021-11-17 22:34:57 +0100 |
|---|---|---|
| committer | Joel Klinghed <the_jk@spawned.biz> | 2021-11-17 22:34:57 +0100 |
| commit | 6232d13f5321b87ddf12a1aa36b4545da45f173d (patch) | |
| tree | 23f3316470a14136debd9d02f9e920ca2b06f4cc /src/htmlutil.cc | |
Travel3: Simple image and video display site
Reads the images and videos from filesystem and builds a site in
memroy.
Diffstat (limited to 'src/htmlutil.cc')
| -rw-r--r-- | src/htmlutil.cc | 59 |
1 files changed, 59 insertions, 0 deletions
diff --git a/src/htmlutil.cc b/src/htmlutil.cc new file mode 100644 index 0000000..42abc4c --- /dev/null +++ b/src/htmlutil.cc @@ -0,0 +1,59 @@ +#include "common.hh" + +#include "htmlutil.hh" + +namespace html { + +namespace { + +constexpr const std::string_view kBodyChars = "&<>"; +constexpr const std::string_view kAttributeChars = "&<>\"'"; + +} // namespace + +std::string escape(std::string_view in, EscapeTarget target) { + std::string out; + escape(in, &out, target); + return out; +} + +void escape(std::string_view in, std::string* out, EscapeTarget target) { + std::string_view chars; + switch (target) { + case EscapeTarget::BODY: + chars = kBodyChars; + break; + case EscapeTarget::ATTRIBUTE: + chars = kAttributeChars; + break; + } + size_t last = 0; + while (true) { + auto next = in.find_first_of(chars, last); + if (next == std::string::npos) { + out->append(in, last); + break; + } + out->append(in, last, next - last); + switch (in[next]) { + case '&': + out->append("&"); + break; + case '<': + out->append("<"); + break; + case '>': + out->append(">"); + break; + case '"': + out->append("""); + break; + case '\'': + out->append("'"); + break; + } + last = next + 1; + } +} + +} // namespace html |
