fix: render only specified gists, if specification is provided

This commit is contained in:
Aravinth Manivannan 2022-01-16 14:14:58 +05:30
parent df14f8b0b8
commit 574d14714d
Signed by untrusted user: realaravinth
GPG key ID: AD9F0F08E855ED88
3 changed files with 56 additions and 20 deletions

View file

@ -50,10 +50,6 @@ pub type PostResp = get_post::GetPostPost;
pub type AppData = web::Data<Data>;
impl PostResp {
pub fn get_gist_id<'a>(&self, url: &'a str) -> &'a str {
url.split('/').last().unwrap()
}
pub fn get_subtitle(&self) -> &str {
self.preview_content.as_ref().unwrap().subtitle.as_str()
}
@ -65,7 +61,7 @@ pub struct GistContent {
pub html_url: String,
}
#[derive(Deserialize, Serialize)]
#[derive(Deserialize, Clone, Serialize)]
pub struct GistFile {
pub file_name: String,
pub content: String,
@ -191,9 +187,25 @@ impl Data {
}
}
pub async fn get_gist(&self, id: String) -> (String, GistContent) {
match self.gists.get(&id) {
Ok(Some(v)) => (id, bincode::deserialize(&v[..]).unwrap()),
pub fn get_gist_id(url: &str) -> &str {
url.split('/').last().unwrap()
}
pub async fn get_gist(&self, gist_url: String) -> (String, GistContent) {
let id = Self::get_gist_id(&gist_url).to_owned();
let file_name = if gist_url.contains('?') {
let parsed = url::Url::parse(&gist_url).unwrap();
if let Some((_, file_name)) = parsed.query_pairs().find(|(k, _)| k == "file") {
Some(file_name.into_owned())
} else {
None
}
} else {
None
};
let gist = match self.gists.get(&id) {
Ok(Some(v)) => bincode::deserialize(&v[..]).unwrap(),
_ => {
const URL: &str = "https://api.github.com/gists/";
@ -211,9 +223,9 @@ impl Data {
.unwrap();
let files = resp.get("files").unwrap();
let v = files.as_object().unwrap();
let mut files = Vec::with_capacity(v.len());
v.iter().for_each(|(name, file_obj)| {
let file = GistFile {
fn to_gist_file(name: &str, file_obj: &serde_json::Value) -> GistFile {
GistFile {
file_name: name.to_string(),
content: file_obj
.get("content")
@ -233,9 +245,15 @@ impl Data {
.as_str()
.unwrap()
.to_owned(),
};
}
}
let mut files = Vec::with_capacity(v.len());
v.iter().for_each(|(name, file_obj)| {
let file = to_gist_file(name, file_obj);
files.push(file);
});
let gist = GistContent {
files,
html_url: resp.get("html_url").unwrap().as_str().unwrap().to_owned(),
@ -244,8 +262,27 @@ impl Data {
self.gists
.insert(&id, bincode::serialize(&gist).unwrap())
.unwrap();
(id, gist)
gist
}
}
};
let gist = if let Some(file_name) = file_name {
let mut files: Vec<GistFile> = Vec::with_capacity(1);
let file = gist
.files
.iter()
.find(|f| f.file_name == file_name)
.unwrap()
.to_owned();
files.push(file);
GistContent {
files,
html_url: gist_url,
}
} else {
gist
};
(id, gist)
}
}

View file

@ -185,8 +185,7 @@ async fn page(path: web::Path<(String, String)>, data: AppData) -> impl Responde
.unwrap()
.href;
if src.contains("gist.github.com") {
let gist_id = post_data.get_gist_id(src);
let fut = data.get_gist(gist_id.to_owned());
let fut = data.get_gist(src.to_owned());
futs.push(fut);
}
}

View file

@ -1,9 +1,9 @@
<. let gist_id = data.get_gist_id(src); .>
<. let gist_id = crate::data::Data::get_gist_id(src); .>
<. let (_, gist)= gists.as_ref().unwrap().iter().find(|(id, _)| id == gist_id).as_ref().unwrap(); .>
<div class="gist_container">
<. for file in &gist.files {.>
<code class="code-block"> <.= file.get_html_content() .> </code>
<.}.>
<. for file in &gist.files {.>
<code class="code-block"> <.= file.get_html_content() .> </code>
<.}.>
<a class="gist_link" href="<.= &gist.html_url .>" target="_blank"
>See gist on GitHub</a
>