Updated blog

This commit is contained in:
rami 2024-06-15 15:15:13 -04:00
parent 1f9081b540
commit 639c8e8815
14 changed files with 209 additions and 96 deletions

1
.gitignore vendored
View File

@ -2,3 +2,4 @@
server server
posts/ posts/
compile_commands.json compile_commands.json
.cache

View File

@ -1,9 +1,9 @@
CC := g++ CC := g++
CFLAGS := -Wall -g -std=c++23 CFLAGS := -Wall -g -std=c++23 -I./include/
TARGET := server TARGET := server
LIBPATHS := LIBPATHS :=
LIBS := LIBS := -lcurl
SRCS := $(wildcard *.cpp) SRCS := $(wildcard *.cpp)
OBJS := $(patsubst %.cpp, %.o, $(SRCS)) OBJS := $(patsubst %.cpp, %.o, $(SRCS))
@ -18,3 +18,6 @@ clean:
all: run all: run
run: $(TARGET) run: $(TARGET)
./$(TARGET) ./$(TARGET)
pch: include/crow_all.h.gch
include/crow_all.h.gch: include/crow_all.h
$(CC) $(CFLAGS) -lpthread -c $< -o $@

1
README.md Normal file
View File

@ -0,0 +1 @@
I'm going to rewrite this in rust when I learn it

View File

@ -1,8 +1,9 @@
#include <crow_all.h>
#include <filesystem> #include <filesystem>
#include <string> #include <string>
#include <array> #include <array>
#include <memory> #include <memory>
#include "crow_all.h" #include <curl/curl.h>
std::string exec(const std::string &cmd) { std::string exec(const std::string &cmd) {
std::array<char, 128> buffer; std::array<char, 128> buffer;
@ -17,36 +18,58 @@ std::string exec(const std::string &cmd) {
return result; return result;
} }
int main() { std::string url_decode(const std::string& encoded) {
crow::SimpleApp app; int output_length;
const auto decoded_value = curl_easy_unescape(nullptr, encoded.c_str(), static_cast<int>(encoded.length()), &output_length);
std::string result(decoded_value, output_length);
curl_free(decoded_value);
return result;
}
CROW_ROUTE(app, "/")([](){ std::string process_file(const std::filesystem::path &path) {
return exec("pandoc --template='templates/post.txt' -t plain '" + path.string() + "'");;
}
namespace route {
std::string root() {
return crow::mustache::load_text("index.html"); return crow::mustache::load_text("index.html");
}); }
CROW_ROUTE(app, "/contact")([](){ crow::mustache::rendered_template blog() {
return crow::mustache::load_text("contact.html");
});
CROW_ROUTE(app, "/blog")([](){
std::string posts = ""; std::string posts = "";
std::filesystem::path postsDir("posts"); std::filesystem::path postsDir("posts");
std::vector<std::future<std::string>> futures;
for (auto const& entry : std::filesystem::directory_iterator{postsDir}) { for (auto const& entry : std::filesystem::directory_iterator{postsDir}) {
posts.append("<a href=\"/blog/"); futures.emplace_back(std::async(std::launch::async, process_file, entry.path()));
posts.append(entry.path().filename()); }
posts.append("\"><li>");
posts.append(entry.path().filename());
posts.append("</li></a>"); for (auto& future : futures) {
posts += future.get();
} }
auto page = crow::mustache::load("posts.html"); auto page = crow::mustache::load("posts.html");
crow::mustache::context ctx( {{"posts", posts}} ); crow::mustache::context ctx( {{"posts", posts}} );
return page.render(ctx); return page.render(ctx);
}); }
CROW_ROUTE(app, "/blog/<string>")([](const std::string &name){ std::string post(const std::string &name) {
return exec("pandoc --standalone --template templates/template.html ./posts/" + name); std::string decoded_name = url_decode(name);
}); if (std::filesystem::exists("posts/" + decoded_name))
return exec("pandoc --standalone --template templates/template.html './posts/" + decoded_name + "'");
else
return std::string("<h1>Not found</h1>");
}
};
int main() {
crow::SimpleApp app;
CROW_ROUTE(app, "/")(route::root);
CROW_ROUTE(app, "/blog")(route::blog);
CROW_ROUTE(app, "/blog/<string>")(route::post);
app.port(8080).multithreaded().run(); app.port(8080).multithreaded().run();
} }

BIN
static/blog.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 11 KiB

BIN
static/gitea.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 49 KiB

BIN
static/pencil.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 13 KiB

64
static/style.css Normal file
View File

@ -0,0 +1,64 @@
@import url('https://fonts.googleapis.com/css2?family=Ubuntu+Mono:ital,wght@0,400;0,700;1,400;1,700&family=Ubuntu:ital,wght@0,300;0,400;0,500;0,700;1,300;1,400;1,500;1,700&display=swap');
* {
font-family: "Ubuntu", sans-serif;
}
::selection {
background: #e67e80;
color: #232a2e;
}
body {
margin: 0;
padding: 0;
background: url(/static/tile.png);
color: #d3c6aa;
font-family: sans-serif;
display: flex;
justify-content: center;
align-items: center;
}
a {
color: #d699b6;
}
.sourceCode code {
font-family: "Ubuntu Mono", monospace;
}
code {
background: #232a2e;
font-family: "Ubuntu Mono", monospace;
overflow-y: scroll;
}
.sourceCode {
background: #232a2e;
overflow-y: scroll;
padding: 10px;
}
html,body{min-height: 100vh;}
#wrapper {
width: 50%;
min-height: 100vh;
background: #2d353b;
padding: 0px 30px 0px 30px;
line-height: 1.5;
}
.title {
display: flex;
flex-direction: row;
justify-content: space-between;
align-items: center;
}
.date {
font-family: "Ubuntu Mono", monospace;
}
.back {
font-size: 20px;
}

BIN
static/tile.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.4 KiB

View File

@ -6,14 +6,32 @@
<link rel="preconnect" href="https://fonts.googleapis.com"> <link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin> <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=JetBrains+Mono&display=swap" rel="stylesheet"> <link href="https://fonts.googleapis.com/css2?family=JetBrains+Mono&display=swap" rel="stylesheet">
<meta name="keywords" content="Rami Kaldany">
<title>ramik.dev</title> <title>ramik.dev</title>
<style> <style>
@import url('https://fonts.googleapis.com/css2?family=Ubuntu+Mono:ital,wght@0,400;0,700;1,400;1,700&family=Ubuntu:ital,wght@0,300;0,400;0,500;0,700;1,300;1,400;1,500;1,700&display=swap');
* { * {
margin: 0; margin: 0;
padding: 0; padding: 0;
color: #d3c6aa; color: #d3c6aa;
font-family: 'JetBrains Mono', monospace; font-family: 'Ubuntu Mono', monospace;
font-size: 30px;
}
.gitea {
background: url(static/gitea.png);
background-size: 100% 100%;
width: 50px;
height: 27px;
margin: 5px;
}
.blog {
background: url(static/pencil.png);
background-size: 100% 100%;
width: 30px;
height: 30px;
margin: 5px;
} }
html, body { html, body {
@ -34,8 +52,12 @@
} }
ul > li { ul > li {
margin: 5px; margin: 10px;
font-size: 15px; }
ul > li > a {
display: flex;
flex-direction: row;
} }
#wrapper { #wrapper {
@ -55,8 +77,6 @@
<ul> <ul>
<li>[</li> <li>[</li>
<li><a href="#">../</a></li> <li><a href="#">../</a></li>
<li><a href="#">about</a></li>
<li><a href="/contact">contact</a></li>
<li><a href="https://git.ramik.dev/rami">git</a></li> <li><a href="https://git.ramik.dev/rami">git</a></li>
<li><a href="/blog">blog</a></li> <li><a href="/blog">blog</a></li>
<li>]</li> <li>]</li>

1
templates/post.txt Normal file
View File

@ -0,0 +1 @@
<li><span class="sourceCode">$date$</span>&nbsp;&mdash;&nbsp;<a class="title" href="$path$">$title$</a></li>

View File

@ -1,3 +1,34 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="date" content='$date-meta$'>
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=JetBrains+Mono&display=swap" rel="stylesheet">
<link rel="stylesheet" href="/static/style.css">
<title>Blog</title>
<style>
ul {
padding: 0;
}
li {
width: 100%;
height: 30px;
list-style: none;
display: flex;
justify-content: left;
align-items: center;
}
</style>
</head>
<body>
<div id="wrapper">
<h1>All my posts: </h1>
<hr>
<ul> <ul>
{{& posts }} {{& posts }}
</ul> </ul>
</div>
</body>
</html>

View File

@ -2,54 +2,23 @@
<html lang="en"> <html lang="en">
<head> <head>
<meta charset="utf-8"> <meta charset="utf-8">
<meta name="date" content='$date-meta$'> <meta name="date" content='$date$'>
<link rel="preconnect" href="https://fonts.googleapis.com"> <link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin> <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=JetBrains+Mono&display=swap" rel="stylesheet"> <link href="https://fonts.googleapis.com/css2?family=JetBrains+Mono&display=swap" rel="stylesheet">
<style> <link rel="stylesheet" href="/static/style.css">
::selection {
background: #e67e80;
color: #232a2e;
}
body {
color: #d3c6aa;
font-family: sans-serif;
background: #2d353b;
display: flex;
justify-content: center;
align-items: center;
}
a {
color: #d699b6;
}
.sourceCode code {
font-family: 'JetBrains Mono', monospace;
}
code {
background: #232a2e;
font-family: 'JetBrains Mono', monospace;
overflow-y: scroll;
}
div.sourceCode {
background: #232a2e;
overflow-y: scroll;
padding: 10px;
}
#wrapper {
width: 50%;
height: 100%;
}
</style>
<title>$title$</title> <title>$title$</title>
</head> </head>
<body> <body>
<div id="wrapper"> <div id="wrapper">
<div class="title">
<a class="back" title="Back" href="/blog">..</a>
<h2>
$title$
</h2>
<span class="date">$date$</span>
</div>
<hr>
$body$ $body$
</div> </div>
</body> </body>