From fe087699318de38705a68fdf40721d281ebfb5d5 Mon Sep 17 00:00:00 2001 From: Aki Date: Fri, 23 Jul 2021 22:05:24 +0200 Subject: Exported url expansion to a function --- markdown.cpp | 51 ++++++++++++++++++++++++++++----------------------- 1 file changed, 28 insertions(+), 23 deletions(-) (limited to 'markdown.cpp') diff --git a/markdown.cpp b/markdown.cpp index b4d692f..e8b4e71 100644 --- a/markdown.cpp +++ b/markdown.cpp @@ -7,6 +7,7 @@ #include #include #include +#include #include #include #include @@ -38,6 +39,32 @@ static std::string get(const std::string & command) return result; } +static std::string expand_url(const std::string & href) +{ + std::string_view prefix(href.data(), 4); + if (prefix == "http") + return href; + else + { + prefix.remove_suffix(2); + if (prefix == "//") + return "https:" + href; + else + { + prefix.remove_suffix(1); + if (prefix == "/") + { + if (g_base_address.empty()) + throw std::runtime_error("missing base address"); + else + return g_base_address + href; + } + else + throw std::runtime_error("invalid href"); + } + } +} + struct Markdown : public imgui_md { ImFont * get_font() const override @@ -59,29 +86,7 @@ struct Markdown : public imgui_md void open_url() const override { - std::string command = "browse -f markdown "; - std::string_view prefix(m_href.data(), 4); - if (prefix == "http") - command += m_href; - else - { - prefix.remove_suffix(2); - if (prefix == "//") - command += "https:" + m_href; - else - { - prefix.remove_suffix(1); - if (prefix == "/") - { - if (g_base_address.empty()) - return; // TODO: an error - else - command += g_base_address + m_href; - } - else - return; // TODO: also an error - } - } + std::string command = "browse -f markdown " + expand_url(m_href); std::packaged_task task(get); std::thread t(std::move(task), command); t.detach(); -- cgit v1.1