🏡 index : ~doyle/bin.git

author j_dickert <jdickert@uwaterloo.ca> 2020-04-05 11:42:59.0 +02:00:00
committer Jordan Doyle <jordan@doyle.la> 2020-04-05 14:48:42.0 +01:00:00
commit
be88df22f767c511ec9d53fa7babb3a057e1eeed [patch]
tree
4e6fcb5fba552342a6c5e1632d006a95bc3f79df
parent
275ace8356c9623723762084b60c0b7938cb550d
download
be88df22f767c511ec9d53fa7babb3a057e1eeed.tar.gz

Add linenumbering Fix rocket dependencies



Diff

 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
+++ a/Cargo.toml
@@ -10,7 +10,7 @@
[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
+++ a/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 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,13 +99,21 @@
    if *plaintext {
        Ok(Content(ContentType::Plain, entry.to_string()))
    } else {
        let content = match ext {
        let code_highlighted = match ext {
            Some(extension) => 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 <code> tags to enable line numbering with CSS 
        let html = format!(
            "<code>{}</code>",
            code_highlighted.replace("\n", "</code><code>")
        );

        let content = MarkupDisplay::new_safe(Cow::Borrowed(&html), AskamaHtml);

        let template = ShowPaste { content };
        match template.render() {
diff --git a/templates/paste.html b/templates/paste.html
index 2dd6baa..58b243d 100644
--- a/templates/paste.html
+++ a/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 %}<pre><code>{{ content|safe }}</code></pre>{% endblock content %}
{% block content %}<pre>{{ content|safe }}</pre>{% endblock content %}