From be88df22f767c511ec9d53fa7babb3a057e1eeed Mon Sep 17 00:00:00 2001 From: j_dickert Date: Sun, 5 Apr 2020 11:42:59 +0200 Subject: [PATCH] Add linenumbering Fix rocket dependencies --- Cargo.toml | 2 +- src/main.rs | 17 ++++++++++++----- templates/paste.html | 18 +++++++++++++++++- 3 files changed, 30 insertions(+), 7 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 60c3c47..96ecb27 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -10,7 +10,7 @@ edition = "2018" [dependencies] owning_ref = "0.4" linked-hash-map = "0.5" -rocket = { version = "0.4", default-features = false } +rocket = { version = "0.4.4", default-features = false } askama = "0.8" lazy_static = "1.2" rand = { version = "0.6", features = ["nightly"] } diff --git a/src/main.rs b/src/main.rs index aa6a480..77dcd7d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,5 +1,4 @@ #![feature(proc_macro_hygiene, decl_macro)] -#![feature(type_alias_enum_variants)] #[macro_use] extern crate lazy_static; @@ -19,7 +18,7 @@ use params::{HostHeader, IsPlaintextRequest}; use askama::{Html as AskamaHtml, MarkupDisplay, Template}; -use rocket::http::{ContentType, Status}; +use rocket::http::{ContentType, RawStr, Status}; use rocket::request::Form; use rocket::response::content::{Content, Html}; use rocket::response::Redirect; @@ -100,14 +99,22 @@ fn show_paste(key: String, plaintext: IsPlaintextRequest) -> Result match highlight(&entry, extension) { - Some(html) => MarkupDisplay::new_safe(Cow::Owned(html), AskamaHtml), + Some(html) => html, None => return Err(Status::NotFound), }, - None => MarkupDisplay::new_unsafe(Cow::Borrowed(entry), AskamaHtml), + None => String::from(RawStr::from_str(entry).html_escape()), }; + // Add tags to enable line numbering with CSS + let html = format!( + "{}", + code_highlighted.replace("\n", "") + ); + + let content = MarkupDisplay::new_safe(Cow::Borrowed(&html), AskamaHtml); + let template = ShowPaste { content }; match template.render() { Ok(html) => Ok(Content(ContentType::HTML, html)), diff --git a/templates/paste.html b/templates/paste.html index 2dd6baa..58b243d 100644 --- a/templates/paste.html +++ b/templates/paste.html @@ -9,7 +9,23 @@ font-family: inherit; font-size: 1rem; line-height: inherit; + counter-reset: line; } + code { + counter-increment: line; + } + + code::before { + content: counter(line); + display: inline-block; + width: 2em; /* Fixed width */ + padding: 0 1em 0.3em 0; + margin-right: .5em; + color: #888; + -webkit-user-select: none; + } + + {% endblock styles %} -{% block content %}
{{ content|safe }}
{% endblock content %} \ No newline at end of file +{% block content %}
{{ content|safe }}
{% endblock content %} \ No newline at end of file -- libgit2 1.7.2