summaryrefslogtreecommitdiff
path: root/markdown.cpp
blob: c96de7ab456c42ea796b8ec8261764e3ffc82170 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
#include <unistd.h>

#include <array>
#include <cstdio>
#include <fstream>
#include <future>
#include <iostream>
#include <iterator>
#include <memory>
#include <stdexcept>
#include <string>
#include <string_view>
#include <thread>
#include <unordered_map>

#include <GL/glew.h>
#include <GLFW/glfw3.h>

#include <imgui.h>
#include <imgui_impl_glfw.h>
#include <imgui_impl_opengl3.h>
#include <imgui_md.h>

#define STB_IMAGE_IMPLEMENTATION
#include <stb_image.h>

std::string g_base_address;
std::string g_text;
ImFont * g_font_regular;
ImFont * g_font_bold;
ImFont * g_font_bold_large;

struct Image
{
	ImTextureID m_texture_id;
	int m_width;
	int m_height;
};

std::unordered_map<std::string, Image> g_images;

static std::string get(const std::string & command)
{
	std::string result;
	std::array<char, 512> buffer;
	std::unique_ptr<FILE, decltype(&pclose)> opener(popen(command.c_str(), "r"), pclose);
	if (!opener)
		return std::string(); // TODO: time to do some error handling
	while (nullptr != fgets(buffer.data(), buffer.size(), opener.get()))
	{
		result += buffer.data();
	}
	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");
		}
	}
}

static bool load_image(const std::string & filename, Image & image)
{
	unsigned char * data = stbi_load(filename.c_str(), &image.m_width, &image.m_height, nullptr, 4);
	if (nullptr == data)
		return false;
	GLuint texture;
	glGenTextures(1, &texture);
	glBindTexture(GL_TEXTURE_2D, texture);

	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);

#if defined(GL_UNPACK_ROW_LENGTH)
	glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
#endif

	glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, image.m_width, image.m_height, 0, GL_RGBA, GL_UNSIGNED_BYTE, data);
	stbi_image_free(data);
	image.m_texture_id = reinterpret_cast<ImTextureID>(texture);

	return true;
}

struct Markdown : public imgui_md
{
	ImFont * get_font() const override
	{
		if (m_is_table_header)
		{
			return g_font_bold;
		}
		switch (m_hlevel)
		{
		case 0:
			return m_is_strong ? g_font_bold : g_font_regular;
		case 1:
			return g_font_bold_large;
		default:
			return g_font_bold;
		}
	}

	void open_url() const override
	{
		std::string command = "browse -f ./markdown " + expand_url(m_href);
		std::packaged_task<std::string (const std::string &)> task(get);
		auto future = task.get_future();
		std::thread t(std::move(task), command);
		t.detach();
		const auto result = future.get();
		const auto n = result.find(" ");
		const auto filename = result.substr(n + 1, result.size() - n - 2);
		std::ifstream in;
		in.open(filename);
		g_text = std::string(std::istreambuf_iterator<char>(in), {});
		in.close();
	}

	bool get_image(image_info & nfo) const override
	{
		auto search = g_images.find(m_href);
		if (g_images.end() == search)
		{
			std::string command = "phttp " + expand_url(m_href);
			std::packaged_task<std::string (const std::string &)> task(get);
			auto future = task.get_future();
			std::thread t(std::move(task), command);
			t.detach();
			const auto result = future.get();
			auto n = result.find(" ");
			n = result.find(" ", n + 1);
			const auto filename = result.substr(n + 1, result.size() - n - 2);
			if (!load_image(filename, g_images[m_href]))
				throw std::runtime_error("could not load image");
		}
		nfo.texture_id = g_images[m_href].m_texture_id;
		nfo.size = {g_images[m_href].m_width, g_images[m_href].m_height};
		nfo.uv0 = {0, 0};
		nfo.uv1 = {1, 1};
		nfo.col_tint = {1, 1, 1, 1};
		nfo.col_border = {0, 0, 0, 0};
		return true;
	}
};

static void glfw_error_callback(int error, const char * description)
{
	std::cerr << "glfw (" << error << "): " << description << std::endl;
}

static void markdown(const char * str, const char * str_end)
{
	static Markdown s_printer;
	s_printer.print(str, str_end);
}

ImVec2 g_window_size;

static void resized(GLFWwindow *, int width, int height)
{
	g_window_size.x = static_cast<float>(width);
	g_window_size.y = static_cast<float>(height);
}

static void print_usage(const char * program)
{
	std::cerr << "Usage: " << program << " [-b BASE_ADDRESS] FILE" << std::endl;
}

int main(int argc, char ** argv)
{
	int opt;

	while (-1 != (opt = getopt(argc, argv, "b:")))
	{
		switch (opt)
		{
		case 'b':
			g_base_address = optarg;
			break;
		case '?':
		default:
			print_usage(argv[0]);
			return 2;
		}
	}

	if (optind >= argc)
	{
		print_usage(argv[0]);
		return 2;
	}

	glfwSetErrorCallback(glfw_error_callback);
	if (!glfwInit())
		return 1;

	const char * glsl_version = "#version 130";
	glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
	glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0);

	GLFWwindow * window = glfwCreateWindow(1280, 720, "Markdown", nullptr, nullptr);
	if (nullptr == window)
		return 1;
	glfwMakeContextCurrent(window);
	glfwSwapInterval(1);
	glfwSetWindowSizeCallback(window, resized);

	if (GLEW_OK != glewInit())
		return 1;

	IMGUI_CHECKVERSION();
	ImGui::CreateContext();
	ImGuiIO & io = ImGui::GetIO();
	io.IniFilename = nullptr;
	io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard;
	io.ConfigFlags |= ImGuiConfigFlags_DockingEnable;
	g_font_regular = io.Fonts->AddFontFromFileTTF("OpenSans-Regular.ttf", 18);
	g_font_bold = io.Fonts->AddFontFromFileTTF("OpenSans-Bold.ttf", 18);
	g_font_bold_large = io.Fonts->AddFontFromFileTTF("OpenSans-Bold.ttf", 26);

	ImGui::StyleColorsDark();
	ImGui_ImplGlfw_InitForOpenGL(window, true);
	ImGui_ImplOpenGL3_Init(glsl_version);

	std::ifstream in;
	in.open(argv[optind]);
	g_text = std::string(std::istreambuf_iterator<char>(in), {});
	in.close();

	while (!glfwWindowShouldClose(window))
	{
		glfwPollEvents();
		ImGui_ImplOpenGL3_NewFrame();
		ImGui_ImplGlfw_NewFrame();
		ImGui::NewFrame();

		ImGui::SetNextWindowPos({0, 0});
		ImGui::SetNextWindowSize(g_window_size);
		ImGui::Begin(
			"Markdown",
			nullptr,
			ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoMove |
				ImGuiWindowFlags_NoSavedSettings
		);
		markdown(g_text.data(), g_text.data() + g_text.size());
		ImGui::End();

		ImGui::Render();
		int display_w, display_h;
		glfwGetFramebufferSize(window, &display_w, &display_h);
		glViewport(0, 0, display_w, display_h);
		glClearColor(0, 0, 0, 0);
		glClear(GL_COLOR_BUFFER_BIT);
		ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());

		glfwSwapBuffers(window);
	}

	ImGui_ImplOpenGL3_Shutdown();
	ImGui_ImplGlfw_Shutdown();
	ImGui::DestroyContext();

	glfwDestroyWindow(window);
	glfwTerminate();
}