Skip to content

Commit

Permalink
fix: render only specified gists, if specification is provided
Browse files Browse the repository at this point in the history
  • Loading branch information
realaravinth committed Jan 16, 2022
1 parent df14f8b commit 574d147
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 20 deletions.
65 changes: 51 additions & 14 deletions src/data.rs
Expand Up @@ -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()
}
Expand All @@ -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,
Expand Down Expand Up @@ -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/";

Expand All @@ -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")
Expand All @@ -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(),
Expand All @@ -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)
}
}
3 changes: 1 addition & 2 deletions src/proxy.rs
Expand Up @@ -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);
}
}
Expand Down
8 changes: 4 additions & 4 deletions templates/gist_insert.html
@@ -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
>
Expand Down

0 comments on commit 574d147

Please sign in to comment.