summaryrefslogtreecommitdiff
path: root/markdown.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'markdown.cpp')
-rw-r--r--markdown.cpp51
1 files changed, 28 insertions, 23 deletions
diff --git a/markdown.cpp b/markdown.cpp
index b4d692f..e8b4e71 100644
--- a/markdown.cpp
+++ b/markdown.cpp
@@ -7,6 +7,7 @@
#include <iostream>
#include <iterator>
#include <memory>
+#include <stdexcept>
#include <string>
#include <string_view>
#include <thread>
@@ -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<std::string (const std::string &)> task(get);
std::thread t(std::move(task), command);
t.detach();