routes: implement syntax highlighting for plaintext files
Signed-off-by: Gunwant Jain <mail@wantguns.dev>
This commit is contained in:
parent
66ea75de7f
commit
855a0faec2
33
src/main.rs
33
src/main.rs
|
@ -3,12 +3,12 @@
|
|||
|
||||
extern crate tree_magic;
|
||||
|
||||
use std::io;
|
||||
use std::io::prelude::*;
|
||||
use std::collections::HashMap;
|
||||
use std::path::Path;
|
||||
use std::fs::File;
|
||||
|
||||
use rocket::Data;
|
||||
use rocket::http::RawStr;
|
||||
use rocket_contrib::templates::Template;
|
||||
|
||||
mod paste_id;
|
||||
|
@ -16,12 +16,23 @@ mod paste_id;
|
|||
use paste_id::PasteId;
|
||||
|
||||
#[get("/p/<id>")]
|
||||
fn pretty_retrieve(id: PasteId) -> Option<File> {
|
||||
fn pretty_retrieve(id: PasteId) -> Option<Template> {
|
||||
let filename = format!("upload/{id}", id = id);
|
||||
let filepath = Path::new(&filename);
|
||||
let mut file = File::open(&filename).unwrap();
|
||||
|
||||
let mut contents = String::new();
|
||||
file.read_to_string(&mut contents).unwrap();
|
||||
let theme = String::from(".");
|
||||
|
||||
let mut map = HashMap::new();
|
||||
map.insert("title", id.to_string());
|
||||
map.insert("theme", theme);
|
||||
map.insert("code", contents);
|
||||
let rendered = Template::render("pretty", &map);
|
||||
|
||||
match tree_magic::from_filepath(filepath).as_str() {
|
||||
"text/plain" => File::open(&filename).ok(),
|
||||
"text/plain" => Some(rendered),
|
||||
_ => Err("does not have the MIME type of a plaintext file").ok()
|
||||
}
|
||||
}
|
||||
|
@ -36,10 +47,17 @@ fn retrieve(id: PasteId) -> Option<File> {
|
|||
#[post("/", data = "<paste>")]
|
||||
fn upload(paste: Data) -> Result<String, std::io::Error> {
|
||||
let id = PasteId::new(4);
|
||||
let filename = format!("upload/{id}", id = id);
|
||||
let url = format!("{host}/{id}\n", host = "http://localhost:8000", id = id);
|
||||
|
||||
paste.stream_to_file(Path::new(&filename))?;
|
||||
let filename = format!("upload/{id}", id = id);
|
||||
let filepath = Path::new(&filename);
|
||||
|
||||
paste.stream_to_file(filepath)?;
|
||||
|
||||
let url = match tree_magic::from_filepath(filepath).as_str() {
|
||||
"text/plain" => format!("{host}/p/{id}\n", host = "http://localhost:8000", id = id),
|
||||
_ => format!("{host}/{id}\n", host = "http://localhost:8000", id = id),
|
||||
};
|
||||
|
||||
Ok(url)
|
||||
}
|
||||
|
||||
|
@ -67,5 +85,6 @@ USAGE
|
|||
fn main() {
|
||||
rocket::ignite()
|
||||
.mount("/", routes![index, upload, retrieve, pretty_retrieve])
|
||||
.attach(Template::fairing())
|
||||
.launch();
|
||||
}
|
||||
|
|
43
templates/pretty.html.tera
Normal file
43
templates/pretty.html.tera
Normal file
|
@ -0,0 +1,43 @@
|
|||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<title>{{ title }}</title>
|
||||
|
||||
<style>
|
||||
|
||||
/* Use Ayu-Colors by default */
|
||||
.hljs{display:block;overflow-x:auto;background:#191f26;color:#e6e1cf;padding:.5em}.hljs-comment,.hljs-quote{color:#5c6773;font-style:italic}.hljs-attr,.hljs-attribute,.hljs-link,.hljs-regexp,.hljs-selector-class,.hljs-selector-id,.hljs-template-variable,.hljs-variable{color:#f73}.hljs-builtin-name,.hljs-literal,.hljs-meta,.hljs-number,.hljs-params,.hljs-type{color:#fe9}.hljs-bullet,.hljs-string{color:#b8cc52}.hljs-built_in,.hljs-section,.hljs-title{color:#ffb454}.hljs-keyword,.hljs-selector-tag,.hljs-symbol{color:#f73}.hljs-name{color:#36a3d9}.hljs-tag{color:#00568d}.hljs-emphasis{font-style:italic}.hljs-strong{font-weight:700}.hljs-addition{color:#91b362}.hljs-deletion{color:#d96c75}
|
||||
|
||||
/* fill entire page */
|
||||
code {
|
||||
background: #fff;
|
||||
position: fixed;
|
||||
width:100%;
|
||||
bottom: 0;
|
||||
right: 0;
|
||||
left: 0;
|
||||
top: 0;
|
||||
}
|
||||
</style>
|
||||
|
||||
<!-- Bring your own theme -->
|
||||
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.15.10/styles/{{ theme }}.min.css">
|
||||
|
||||
</head>
|
||||
|
||||
|
||||
<body>
|
||||
<pre><code>{{ code }}</code></pre>
|
||||
|
||||
<script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/10.4.1/highlight.min.js"
|
||||
integrity="sha512-DrpaExP2d7RJqNhXB41Q/zzzQrtb6J0zfnXD5XeVEWE8d9Hj54irCLj6dRS3eNepPja7DvKcV+9PnHC7A/g83A=="
|
||||
crossorigin="anonymous"></script>
|
||||
<script>
|
||||
hljs.initHighlightingOnLoad();
|
||||
</script>
|
||||
</body>
|
||||
|
||||
</html>
|
Loading…
Reference in New Issue
Block a user