add line numbers
This commit is contained in:
parent
7c9ed689ba
commit
2bfba6f981
4 changed files with 95 additions and 13 deletions
35
src/lib.rs
35
src/lib.rs
|
@ -162,6 +162,13 @@ impl Fedhub {
|
||||||
tree_name: String,
|
tree_name: String,
|
||||||
filepath: Option<PathBuf>,
|
filepath: Option<PathBuf>,
|
||||||
) -> Result<Response<Body>> {
|
) -> Result<Response<Body>> {
|
||||||
|
#[derive(Serialize)]
|
||||||
|
struct Entry {
|
||||||
|
name: String,
|
||||||
|
is_directory: bool,
|
||||||
|
url: String,
|
||||||
|
}
|
||||||
|
|
||||||
let tree = repo.find_tree(tree_id)?;
|
let tree = repo.find_tree(tree_id)?;
|
||||||
let mut ctx = self.context();
|
let mut ctx = self.context();
|
||||||
ctx.insert("repo_name", &path);
|
ctx.insert("repo_name", &path);
|
||||||
|
@ -170,6 +177,14 @@ impl Fedhub {
|
||||||
let mut entries = Vec::new();
|
let mut entries = Vec::new();
|
||||||
let mut readme = None;
|
let mut readme = None;
|
||||||
for entry in tree.iter() {
|
for entry in tree.iter() {
|
||||||
|
let mut is_directory = false;
|
||||||
|
match entry.kind().unwrap() {
|
||||||
|
ObjectType::Blob => {}
|
||||||
|
ObjectType::Tree => {
|
||||||
|
is_directory = true;
|
||||||
|
}
|
||||||
|
_ => {}
|
||||||
|
}
|
||||||
if let (Some(ObjectType::Blob), Some("README.md")) = (entry.kind(), entry.name()) {
|
if let (Some(ObjectType::Blob), Some("README.md")) = (entry.kind(), entry.name()) {
|
||||||
let object = entry.to_object(&repo)?;
|
let object = entry.to_object(&repo)?;
|
||||||
let blob = object.as_blob().unwrap();
|
let blob = object.as_blob().unwrap();
|
||||||
|
@ -191,11 +206,19 @@ impl Fedhub {
|
||||||
entry.name().unwrap()
|
entry.name().unwrap()
|
||||||
),
|
),
|
||||||
};
|
};
|
||||||
entries.push(json!({
|
entries.push(Entry {
|
||||||
"name": entry.name().unwrap(),
|
name: entry.name().unwrap().to_string(),
|
||||||
"url": url,
|
is_directory,
|
||||||
}));
|
url
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
entries.sort_by(|left, right| {
|
||||||
|
let mut left_string = (!left.is_directory as u8).to_string();
|
||||||
|
left_string += &left.name;
|
||||||
|
let mut right_string = (!right.is_directory as u8).to_string();
|
||||||
|
right_string += &right.name;
|
||||||
|
left_string.cmp(&right_string)
|
||||||
|
});
|
||||||
ctx.insert("entries", &entries);
|
ctx.insert("entries", &entries);
|
||||||
if let Some(readme) = readme {
|
if let Some(readme) = readme {
|
||||||
ctx.insert(
|
ctx.insert(
|
||||||
|
@ -226,14 +249,14 @@ impl Fedhub {
|
||||||
json!(null)
|
json!(null)
|
||||||
} else {
|
} else {
|
||||||
let str_contents = String::from_utf8(blob.content().to_vec())?;
|
let str_contents = String::from_utf8(blob.content().to_vec())?;
|
||||||
json!(str_contents)
|
json!(str_contents.lines().collect::<Vec<_>>())
|
||||||
};
|
};
|
||||||
ctx.insert(
|
ctx.insert(
|
||||||
"blob",
|
"blob",
|
||||||
&json!({
|
&json!({
|
||||||
"name": filepath,
|
"name": filepath,
|
||||||
"binary": blob.is_binary(),
|
"binary": blob.is_binary(),
|
||||||
"contents": contents,
|
"lines": contents,
|
||||||
}),
|
}),
|
||||||
);
|
);
|
||||||
return Ok(Response::new(TERA.render("repo_blob.html", &ctx)?.into()));
|
return Ok(Response::new(TERA.render("repo_blob.html", &ctx)?.into()));
|
||||||
|
|
|
@ -3,6 +3,7 @@
|
||||||
--text-color: #D4D4D4;
|
--text-color: #D4D4D4;
|
||||||
--link-color: lightskyblue;
|
--link-color: lightskyblue;
|
||||||
--sans-font: "Helvetica", "Arial", "Liberation Sans", sans-serif;
|
--sans-font: "Helvetica", "Arial", "Liberation Sans", sans-serif;
|
||||||
|
--mono-font: "Roboto Mono", "Roboto Mono for Powerline", "Inconsolata", "Consolas", monospace;
|
||||||
}
|
}
|
||||||
|
|
||||||
body {
|
body {
|
||||||
|
@ -44,3 +45,36 @@ a {
|
||||||
a:hover {
|
a:hover {
|
||||||
text-decoration: underline;
|
text-decoration: underline;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
.file-list {
|
||||||
|
margin-bottom: 24px;
|
||||||
|
width: 100%;
|
||||||
|
}
|
||||||
|
|
||||||
|
.file-list td {
|
||||||
|
padding: 3px 4px;
|
||||||
|
}
|
||||||
|
|
||||||
|
table.striped tr:nth-child(even) {
|
||||||
|
background-color: rgba(255, 255, 255, 0.05);
|
||||||
|
}
|
||||||
|
|
||||||
|
.blob-contents {
|
||||||
|
width: 100%;
|
||||||
|
overflow-x: auto;
|
||||||
|
font-family: var(--mono-font);
|
||||||
|
display: flex;
|
||||||
|
}
|
||||||
|
|
||||||
|
.blob-contents .left {
|
||||||
|
margin-right: 12px;
|
||||||
|
text-align: right;
|
||||||
|
}
|
||||||
|
|
||||||
|
.readme {
|
||||||
|
border-top: 1px dotted var(--link-color);
|
||||||
|
}
|
||||||
|
|
||||||
|
.right-justified {
|
||||||
|
text-align: right;
|
||||||
|
}
|
|
@ -3,5 +3,23 @@
|
||||||
{% block content %}
|
{% block content %}
|
||||||
<h1>{{ repo_name }}: {{ tree_name }}: {{ blob.name }}</h1>
|
<h1>{{ repo_name }}: {{ tree_name }}: {{ blob.name }}</h1>
|
||||||
|
|
||||||
<pre>{{ blob.contents }}</pre>
|
{% if blob.lines %}
|
||||||
|
<div class="blob-contents">
|
||||||
|
<div class="left">
|
||||||
|
{% for _ in blob.lines %}
|
||||||
|
<a name="L{{ loop.index }}" />
|
||||||
|
<a href="#L{{ loop.index }}" title="permalink to line {{ loop.index }}">{{ loop.index }}</a>
|
||||||
|
<br />
|
||||||
|
{% endfor %}
|
||||||
|
</div>
|
||||||
|
<div class="right">
|
||||||
|
{% for line in blob.lines %}
|
||||||
|
{{ line }}
|
||||||
|
<br />
|
||||||
|
{% endfor %}
|
||||||
|
</div>
|
||||||
|
</pre>
|
||||||
|
{% else %}
|
||||||
|
This blob contains binary data.
|
||||||
|
{% endif %}
|
||||||
{% endblock %}
|
{% endblock %}
|
||||||
|
|
|
@ -4,14 +4,21 @@
|
||||||
<h1>{{ repo_name }}: {{ tree_name }}{% if filepath %}: {{ filepath }}{% endif %}</h1>
|
<h1>{{ repo_name }}: {{ tree_name }}{% if filepath %}: {{ filepath }}{% endif %}</h1>
|
||||||
|
|
||||||
<h3>entries:</h3>
|
<h3>entries:</h3>
|
||||||
<ul>
|
<table class="file-list striped">
|
||||||
{% for entry in entries %}
|
<tbody>
|
||||||
<li><a href="{{ entry.url | safe }}">{{ entry.name }}</a></li>
|
{% for entry in entries %}
|
||||||
{% endfor %}
|
<tr>
|
||||||
</ul>
|
<td>
|
||||||
|
<a href="{{ entry.url | safe }}">{{ entry.name }}</a>
|
||||||
|
{% if entry.is_directory %}/{% endif %}
|
||||||
|
</td>
|
||||||
|
</tr>
|
||||||
|
{% endfor %}
|
||||||
|
</tbody>
|
||||||
|
</table>
|
||||||
|
|
||||||
{% if readme %}
|
{% if readme %}
|
||||||
<div>
|
<div class="readme">
|
||||||
{{ readme.rendered | safe }}
|
{{ readme.rendered | safe }}
|
||||||
</div>
|
</div>
|
||||||
{% endif %}
|
{% endif %}
|
||||||
|
|
Loading…
Add table
Reference in a new issue