~/selfhut

selfhut/src/repository/tree.rs -rw-r--r-- 4.99 kB
468a751a — Arthur Melton mailing_list option 2 years ago
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
use crate::config::CONFIG;
use crate::git::commits::get_commits;
use crate::git::file::{file, files};
use crate::git::main_branch::main_branch;
use crate::utils::repo_config::repo_config;
use crate::PathBufWithDotfiles;
use rocket_dyn_templates::{context, Template};
use std::ffi::OsStr;

use syntect::highlighting::ThemeSet;
use syntect::parsing::SyntaxSet;

use std::path::Path;
use syntect::html::highlighted_html_for_string;

#[get("/<repo>/tree", rank = 2)]
pub fn tree_main(repo: String) -> Option<Template> {
    let main_branch = main_branch(repo.clone())?;
    Some(Template::render(
        "repository/tree",
        context! {
            title: format!("/ :: {}", repo.clone()),
            repo: repo.clone(),
            config: repo_config(repo.clone()),
            domain: CONFIG.domain.to_string(),
            active: "tree",
            commit: match get_commits(repo.clone(), 1, None, None) {
                Some(x) => match x.get(0) {
                    Some(x) => Some(x.clone()),
                    None => None
                }
                None => None
            },
            branch: main_branch.clone(),
            files: files(repo, main_branch, "".to_string()),
            current_dir_file: "/",
            current_dir: "/",
            payment: CONFIG.payment_link.clone(),
            mailing_list: CONFIG.mailing_list.clone()
        },
    ))
}

#[get("/<repo>/tree/<branch>/<location..>", rank = 2)]
pub fn tree(repo: String, branch: String, location: PathBufWithDotfiles) -> Option<Template> {
    match file(
        repo.clone(),
        branch.clone(),
        location.get().display().to_string(),
    ) {
        Some(file) => {
            let mut content = "".to_string();
            let mut lines: Vec<usize> = (1..1).collect();
            if file.1.is_some() {
                let ps = SyntaxSet::load_defaults_newlines();
                let ts = ThemeSet::load_defaults();
                let syntax = match ps.find_syntax_by_extension(
                    Path::new(&file.0.name)
                        .extension()
                        .and_then(OsStr::to_str)
                        .unwrap_or("txt"),
                ) {
                    Some(x) => x,
                    None => ps.find_syntax_by_extension("txt").unwrap(),
                };
                let s = file.1.as_ref().unwrap();
                lines = (1..s.lines().count() + 1).collect();
                content =
                    highlighted_html_for_string(s, &ps, syntax, &ts.themes["Solarized (light)"])
                        .ok()?
            }
            Some(Template::render(
                "repository/file",
                context! {
                    title: format!("/{} :: {}", location.get().display(), repo.clone()),
                    repo: repo.clone(),
                    config: repo_config(repo.clone()),
                    domain: CONFIG.domain.to_string(),
                    active: "tree",
                    commit: match get_commits(repo.clone(), 1, None, Some(format!("{}", location.get().display()).replace("//", "/"))) {
                        Some(x) => match x.clone().get(0) {
                            Some(x) => Some(x.clone()),
                            None => None
                        }
                        None => None
                    },
                    branch: branch.clone(),
                    files: file,
                    current_dir_file: format!("/{}/", location.get().display()).replace("//", "/"),
                    current_dir: format!("/{}", location.get().display()),
                    fluid: "true",
                    content,
                    lines,
                    payment: CONFIG.payment_link.clone(),
                    mailing_list: CONFIG.mailing_list.clone()
                },
            ))
        }
        None => Some(Template::render(
            "repository/tree",
            context! {
                title: format!("/{} :: {}", location.get().display(), repo.clone()),
                repo: repo.clone(),
                config: repo_config(repo.clone()),
                domain: CONFIG.domain.to_string(),
                active: "tree",
                commit: match get_commits(repo.clone(), 1, None, Some(format!("{}", location.get().display()).replace("//", "/"))) {
                    Some(x) => match x.clone().get(0) {
                        Some(x) => Some(x.clone()),
                        None => None
                    }
                    None => None
                },
                branch: branch.clone(),
                files: files(repo, branch, location.get().display().to_string()),
                current_dir_file: format!("/{}/", location.get().display()).replace("//", "/"),
                current_dir: format!("/{}", location.get().display()),
                payment: CONFIG.payment_link.clone(),
                mailing_list: CONFIG.mailing_list.clone()
            },
        )),
    }
}