Browse Source

Show line numbers

master
Dylan Baker 5 years ago
parent
commit
c97cc81fa8
2 changed files with 34 additions and 12 deletions
  1. 15
    2
      src/routes.rs
  2. 19
    10
      templates/snippets/show.tera

+ 15
- 2
src/routes.rs View File

@@ -23,11 +23,24 @@ pub fn index() -> Template {
23 23
     Template::render("index", &context)
24 24
 }
25 25
 
26
+#[derive(Serialize, Deserialize)]
27
+struct Context {
28
+    snippet: Snippet,
29
+    lines: Vec<String>,
30
+}
31
+
26 32
 #[get("/snippets/<id>")]
27 33
 pub fn show_snippet(id: i32, connection: DbConn) -> Template {
28
-    let mut context: HashMap<&str, Snippet> = HashMap::new();
34
+    let mut context: HashMap<&str, Context> = HashMap::new();
29 35
     if let Ok(snippet) = snippet::get(&connection, id) {
30
-        context.insert("snippet", snippet);
36
+        let lines: Vec<String> = snippet
37
+            .formatted_body
38
+            .split('\n')
39
+            .collect::<Vec<&str>>()
40
+            .iter()
41
+            .map(|line| line.to_string())
42
+            .collect();
43
+        context.insert("context", Context { snippet, lines });
31 44
     }
32 45
 
33 46
     Template::render("snippets/show", &context)

+ 19
- 10
templates/snippets/show.tera View File

@@ -1,6 +1,6 @@
1 1
 <html>
2 2
   <head>
3
-    <title>{% if snippet %}{{ snippet.title }}{% else %}404 - snippet not found{% endif %}</title>
3
+    <title>{% if context %}{{ context.snippet.title }}{% else %}404 - snippet not found{% endif %}</title>
4 4
     <style>
5 5
       .container {
6 6
         margin: auto;
@@ -16,29 +16,38 @@
16 16
         font-weight: normal;
17 17
       }
18 18
 
19
-      .body > pre {
19
+      pre {
20
+        counter-reset: line;
20 21
         font-size: 16px;
21 22
         padding: 15px;
22 23
       }
24
+
25
+      pre code {
26
+          counter-increment: line;
27
+      }
28
+
29
+      pre code:before {
30
+          content: counter(line);
31
+          margin-right: 10px;
32
+      }
23 33
     </style>
24 34
     <meta name="viewport" content="width=device-width">
25 35
   </head>
26 36
   <body>
27 37
     <div class="container">
28 38
       <h1 class="header">
29
-        {% if snippet %}
30
-          {{ snippet.title }}
31
-
32
-          {% if id %}
33
-            <a class="header__raw-link" href="/snippets/{{ id }}/raw">raw</a>
34
-          {% endif %}
39
+        {% if context %}
40
+          {{ context.snippet.title }}
41
+          <a class="header__raw-link" href="/snippets/{{ context.snippet.id }}/raw">raw</a>
35 42
         {% else %}
36 43
           404 - snippet not found
37 44
         {% endif %}
38 45
       </h1>
39
-      {% if snippet %}
46
+      {% if context %}
40 47
         <div class="body">
41
-          {{ snippet.formatted_body | safe }}
48
+          {% set length = context.lines | length - 1 %}
49
+          {{ context.lines | first | safe }}{% for line in context.lines | slice(start=1, end=length) %}<code id="line-{{ loop.index }}">{{ line | safe }}</code>
50
+{% endfor %}</pre>
42 51
         </div>
43 52
       {% endif %}
44 53
     </div>

Loading…
Cancel
Save