Switch Pages to UTF-8

This commit is contained in:
spikecodes 2020-10-25 19:05:09 -07:00
parent bfc05adbce
commit a1e6f95151
8 changed files with 38 additions and 31 deletions

View File

@ -43,6 +43,9 @@ async fn render(id: String, sort: String) -> Result<HttpResponse> {
}
.render()
.unwrap();
// println!("{}", s);
Ok(HttpResponse::Ok().content_type("text/html").body(s))
}
@ -80,13 +83,6 @@ async fn media(data: &serde_json::Value) -> String {
}
}
fn html_escape(input: String) -> String {
input
.replace("&", "&amp;")
.replace("", "&#39;")
.replace("\"", "&quot;")
}
// POSTS
async fn fetch_post (id: &String) -> Post {
let url: String = format!("https://reddit.com/{}.json", id);
@ -100,9 +96,9 @@ async fn fetch_post (id: &String) -> Post {
let score = post_data["data"]["score"].as_i64().unwrap();
Post {
title: html_escape(val(post_data, "title").await),
title: val(post_data, "title").await,
community: val(post_data, "subreddit").await,
body: html_escape(markdown_to_html(post_data["data"]["selftext"].as_str().unwrap(), &ComrakOptions::default())),
body: markdown_to_html(post_data["data"]["selftext"].as_str().unwrap(), &ComrakOptions::default()),
author: val(post_data, "author").await,
url: val(post_data, "permalink").await,
score: if score>1000 {format!("{}k",score/1000)} else {score.to_string()},
@ -127,8 +123,10 @@ async fn fetch_comments (id: String, sort: &String) -> Result<Vec<Comment>, Box<
let score = comment["data"]["score"].as_i64().unwrap_or(0);
let body = markdown_to_html(comment["data"]["body"].as_str().unwrap_or(""), &ComrakOptions::default());
// println!("{}", body);
comments.push(Comment {
body: html_escape(body),
body: body,
author: val(comment, "author").await,
score: if score>1000 {format!("{}k",score/1000)} else {score.to_string()},
time: Utc.timestamp(unix_time, 0).format("%b %e %Y %H:%M UTC").to_string()

View File

@ -62,13 +62,6 @@ async fn sorted(web::Path((sub, sort)): web::Path<(String, String)>) -> Result<H
// UTILITIES
async fn val (j: &serde_json::Value, k: &str) -> String { String::from(j["data"][k].as_str().unwrap_or("")) }
fn html_escape(input: String) -> String {
input
.replace("&", "&amp;")
.replace("", "&#39;")
.replace("\"", "&quot;")
}
// SUBREDDIT
async fn subreddit(sub: &String) -> Subreddit {
let url: String = format!("https://www.reddit.com/r/{}/about.json", sub);
@ -82,8 +75,8 @@ async fn subreddit(sub: &String) -> Subreddit {
Subreddit {
name: val(&data, "display_name").await,
title: html_escape(val(&data, "title").await),
description: html_escape(val(&data, "public_description").await),
title: val(&data, "title").await,
description: val(&data, "public_description").await,
icon: String::from(icon_parts[0]),
}
}
@ -103,7 +96,7 @@ pub async fn posts(sub: String, sort: &String) -> Vec<Post> {
let unix_time: i64 = post["data"]["created_utc"].as_f64().unwrap().round() as i64;
let score = post["data"]["score"].as_i64().unwrap();
posts.push(Post {
title: html_escape(val(post, "title").await),
title: val(post, "title").await,
community: val(post, "subreddit").await,
author: val(post, "author").await,
score: if score>1000 {format!("{}k",score/1000)} else {score.to_string()},

View File

@ -61,13 +61,6 @@ async fn sorted(web::Path((username, sort)): web::Path<(String, String)>) -> Res
async fn user_val (j: &serde_json::Value, k: &str) -> String { String::from(j["data"]["subreddit"][k].as_str().unwrap()) }
async fn post_val (j: &serde_json::Value, k: &str) -> String { String::from(j["data"][k].as_str().unwrap_or("Comment")) }
fn html_escape(input: String) -> String {
input
.replace("&", "&amp;")
.replace("", "&#39;")
.replace("\"", "&quot;")
}
// USER
async fn user(name: &String) -> User {
let url: String = format!("https://www.reddit.com/user/{}/about.json", name);
@ -80,7 +73,7 @@ async fn user(name: &String) -> User {
icon: user_val(&data, "icon_img").await,
karma: data["data"]["total_karma"].as_i64().unwrap(),
banner: user_val(&data, "banner_img").await,
description: html_escape(user_val(&data, "public_description").await)
description: user_val(&data, "public_description").await
}
}
@ -99,7 +92,7 @@ async fn posts(sub: String, sort: &String) -> Vec<Post> {
let unix_time: i64 = post["data"]["created_utc"].as_f64().unwrap().round() as i64;
let score = post["data"]["score"].as_i64().unwrap();
posts.push(Post {
title: html_escape(post_val(post, "title").await),
title: post_val(post, "title").await,
community: post_val(post, "subreddit").await,
author: post_val(post, "author").await,
score: if score>1000 {format!("{}k",score/1000)} else {score.to_string()},

View File

@ -263,6 +263,7 @@ span {
}
.comment_right {
word-wrap: anywhere;
padding: 20px 25px;
flex-grow: 1;
flex-shrink: 1;

View File

@ -2,7 +2,14 @@
<head>
<title>Libreddit</title>
<meta name="description" content="Alternative private front-end to Reddit">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<link rel="stylesheet" href="/style.css">
<style>
#sort > #sort_{{ sort }} {
background: aqua;
color: black;
}
</style>
</head>
<body>
<header>

View File

@ -3,12 +3,13 @@
<title>{{ post.title }} - r/{{ post.community }}</title>
<meta name="author" content="u/{{ post.author }}">
<meta name="description" content="View on Libreddit, an alternative private front-end to Reddit.">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<link rel="stylesheet" href="/style.css">
<style>
/* #sort > #sort_{{ sort }} {
#sort > #sort_{{ sort }} {
background: aqua;
color: black;
} */
}
</style>
</head>
<body>

View File

@ -2,7 +2,14 @@
<head>
<title>r/{{ sub.name }}: {{ sub.description }}</title>
<meta name="description" content="View on Libreddit, an alternative private front-end to Reddit.">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<link rel="stylesheet" href="/style.css">
<style>
#sort > #sort_{{ sort }} {
background: aqua;
color: black;
}
</style>
</head>
<body>
<header>

View File

@ -2,7 +2,14 @@
<head>
<title>Libreddit: u/{{ user.name }}</title>
<meta name="description" content="View on Libreddit, an alternative private front-end to Reddit.">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<link rel="stylesheet" href="/style.css">
<style>
#sort > #sort_{{ sort }} {
background: aqua;
color: black;
}
</style>
</head>
<body>
<header>