diff --git a/Cargo.lock b/Cargo.lock index 5a0f783..03c62d3 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -753,15 +753,6 @@ dependencies = [ "version_check 0.9.2", ] -[[package]] -name = "getopts" -version = "0.2.21" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "14dbbfd5c71d70241ecf9e6f13737f7b5ce823821063188d7e46c41d371eebd5" -dependencies = [ - "unicode-width", -] - [[package]] name = "getrandom" version = "0.1.15" @@ -972,9 +963,9 @@ checksum = "47be2f14c678be2fdcab04ab1171db51b2762ce6f0a8ee87c8dd4a04ed216135" [[package]] name = "itoa" -version = "0.4.6" +version = "0.4.7" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "dc6f3ad7b9d11a0c00842ff8de1b60ee58661048eb8049ed33c73594f359d7e6" +checksum = "dd25036021b0de88a0aff6b850051563c6516d0bf53f8638938edbb9de732736" [[package]] name = "js-sys" @@ -1015,14 +1006,13 @@ checksum = "1482821306169ec4d07f6aca392a4681f66c75c9918aa49641a2595db64053cb" [[package]] name = "libreddit" -version = "0.2.2" +version = "0.2.3" dependencies = [ "actix-web", "askama", "async-recursion", "base64 0.13.0", "chrono", - "pulldown-cmark", "reqwest", "serde", "serde_json", @@ -1340,18 +1330,6 @@ dependencies = [ "unicode-xid 0.2.1", ] -[[package]] -name = "pulldown-cmark" -version = "0.8.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ffade02495f22453cd593159ea2f59827aae7f53fa8323f756799b670881dcf8" -dependencies = [ - "bitflags", - "getopts", - "memchr", - "unicase", -] - [[package]] name = "quick-error" version = "1.2.3" @@ -1589,9 +1567,9 @@ dependencies = [ [[package]] name = "serde_json" -version = "1.0.60" +version = "1.0.61" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1500e84d27fe482ed1dc791a56eddc2f230046a040fa908c08bda1d9fb615779" +checksum = "4fceb2595057b6891a4ee808f70054bd2d12f0e97f1cbb78689b59f676df325a" dependencies = [ "itoa", "ryu", @@ -1749,18 +1727,18 @@ dependencies = [ [[package]] name = "thiserror" -version = "1.0.22" +version = "1.0.23" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0e9ae34b84616eedaaf1e9dd6026dbe00dcafa92aa0c8077cb69df1fcfe5e53e" +checksum = "76cc616c6abf8c8928e2fdcc0dbfab37175edd8fb49a4641066ad1364fdab146" dependencies = [ "thiserror-impl", ] [[package]] name = "thiserror-impl" -version = "1.0.22" +version = "1.0.23" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9ba20f23e85b10754cd195504aebf6a27e2e6cbe28c17778a0c930724628dd56" +checksum = "9be73a2caec27583d0046ef3796c3794f868a5bc813db689eed00c7631275cd1" dependencies = [ "proc-macro2 1.0.24", "quote 1.0.8", @@ -2027,12 +2005,6 @@ version = "1.7.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bb0d2e7be6ae3a5fa87eed5fb451aff96f2573d2694942e40543ae0bbe19c796" -[[package]] -name = "unicode-width" -version = "0.1.8" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9337591893a19b88d8d87f2cec1e73fad5cdfd10e5a6f349f498ad6ea2ffb1e3" - [[package]] name = "unicode-xid" version = "0.1.0" diff --git a/Cargo.toml b/Cargo.toml index ca07db2..22a45c7 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -3,7 +3,7 @@ name = "libreddit" description = " Alternative private front-end to Reddit" license = "AGPL-3.0" repository = "https://github.com/spikecodes/libreddit" -version = "0.2.2" +version = "0.2.3" authors = ["spikecodes <19519553+spikecodes@users.noreply.github.com>"] edition = "2018" @@ -18,6 +18,5 @@ reqwest = { version = "0.10", default_features = false, features = ["rustls-tls" askama = "0.8.0" serde = "1.0.117" serde_json = "1.0" -pulldown-cmark = "0.8.0" chrono = "0.4.19" async-recursion = "0.3.1" \ No newline at end of file diff --git a/src/post.rs b/src/post.rs index 2cecb0d..9f846b4 100644 --- a/src/post.rs +++ b/src/post.rs @@ -6,7 +6,6 @@ use async_recursion::async_recursion; use askama::Template; use chrono::{TimeZone, Utc}; -use pulldown_cmark::{html, Options, Parser}; // STRUCTS #[derive(Template)] @@ -27,8 +26,8 @@ async fn render(id: String, sort: Option, comment_id: Option) -> // Build the Reddit JSON API url let url: String = match comment_id { - None => format!("{}.json?sort={}", id, sorting), - Some(val) => format!("{}.json?sort={}&comment={}", id, sorting, val), + None => format!("{}.json?sort={}&raw_json=1", id, sorting), + Some(val) => format!("{}.json?sort={}&comment={}&raw_json=1", id, sorting, val), }; // Send a request to the url, receive JSON in response @@ -95,20 +94,6 @@ async fn media(data: &serde_json::Value) -> (String, String) { (post_type.to_string(), url) } -async fn markdown_to_html(md: &str) -> String { - let mut options = Options::empty(); - options.insert(Options::ENABLE_TABLES); - options.insert(Options::ENABLE_FOOTNOTES); - options.insert(Options::ENABLE_STRIKETHROUGH); - options.insert(Options::ENABLE_TASKLISTS); - let parser = Parser::new_ext(md, options); - - // Write to String buffer. - let mut html_output = String::new(); - html::push_html(&mut html_output, parser); - html_output -} - // POSTS async fn parse_post(json: serde_json::Value) -> Result { // Retrieve post (as opposed to comments) from JSON @@ -126,7 +111,7 @@ async fn parse_post(json: serde_json::Value) -> Result { let post = Post { title: val(post_data, "title").await, community: val(post_data, "subreddit").await, - body: markdown_to_html(post_data["data"]["selftext"].as_str().unwrap()).await, + body: val(post_data,"selftext_html").await, author: val(post_data, "author").await, author_flair: Flair( val(post_data, "author_flair_text").await, @@ -169,7 +154,7 @@ async fn parse_comments(json: serde_json::Value) -> Result, &'stati } let score = comment["data"]["score"].as_i64().unwrap_or(0); - let body = markdown_to_html(comment["data"]["body"].as_str().unwrap_or("")).await; + let body = val(comment, "body_html").await; let replies: Vec = if comment["data"]["replies"].is_object() { parse_comments(comment["data"]["replies"].clone()).await.unwrap_or(Vec::new()) diff --git a/src/subreddit.rs b/src/subreddit.rs index 6cfbb56..ed571ad 100644 --- a/src/subreddit.rs +++ b/src/subreddit.rs @@ -36,14 +36,7 @@ pub async fn render(sub_name: String, sort: Option, ends: (Option, ends: (Option Result { // Build the Reddit JSON API url - let url: String = format!("r/{}/about.json", sub); + let url: String = format!("r/{}/about.json?raw_json=1", sub); // Send a request to the url, receive JSON in response let req = request(url).await; @@ -102,6 +95,7 @@ async fn subreddit(sub: &String) -> Result { name: val(&res, "display_name").await, title: val(&res, "title").await, description: val(&res, "public_description").await, + info: val(&res, "description_html").await.replace("\\", ""), icon: format_url(icon).await, members: format_num(members.try_into().unwrap()), active: format_num(active.try_into().unwrap()), diff --git a/src/user.rs b/src/user.rs index e270fd1..752315c 100644 --- a/src/user.rs +++ b/src/user.rs @@ -21,10 +21,10 @@ async fn render(username: String, sort: Option, ends: (Option, O // Build the Reddit JSON API url let url = match ends.0 { - Some(val) => format!("user/{}/.json?sort={}&before={}&count=25", username, sorting, val), + Some(val) => format!("user/{}/.json?sort={}&before={}&count=25&raw_json=1", username, sorting, val), None => match ends.1 { - Some(val) => format!("user/{}/.json?sort={}&after={}&count=25", username, sorting, val), - None => format!("user/{}/.json?sort={}", username, sorting), + Some(val) => format!("user/{}/.json?sort={}&after={}&count=25&raw_json=1", username, sorting, val), + None => format!("user/{}/.json?sort={}&raw_json=1", username, sorting), }, }; diff --git a/src/utils.rs b/src/utils.rs index 308128c..9b4b649 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -51,11 +51,13 @@ pub struct User { pub description: String, } +#[derive(Default)] // Subreddit struct containing metadata about community pub struct Subreddit { pub name: String, pub title: String, pub description: String, + pub info: String, pub icon: String, pub members: String, pub active: String, @@ -149,7 +151,7 @@ pub async fn fetch_posts(url: String, fallback_title: String) -> Result<(Vec label, .subreddit_details > label { +#user_details > label, #subreddit_details > label { color: var(--accent); font-size: 15px; } @@ -133,23 +141,26 @@ aside { #sort { background: var(--outside); box-shadow: var(--black-contrast); - border: 0px; - padding: 0px 15px; - margin: 20px 0px; + border: 0; + padding: 0 15px; + margin-bottom: 20px; height: 40px; font-size: 15px; - border-radius: 5px 0px 0px 5px; + border-radius: 5px 0 0 5px; appearance: none; } #sort_submit { background: var(--highlighted); - border: 0px; + border: 0; font-size: 15px; height: 40px; - border-radius: 0px 5px 5px 0px; + border-radius: 0 5px 5px 0; } +#sort:hover { background: var(--foreground); } +#sort_submit:hover { color: var(--accent); } + #sort > div, footer > a { box-shadow: var(--black-contrast); background: var(--outside); @@ -180,7 +191,7 @@ aside { } .post.highlighted { - margin-top: 20px; + margin: 20px 0; } .post:hover { @@ -200,7 +211,7 @@ aside { .post_left { text-align: center; background: var(--foreground); - border-radius: 5px 0px 0px 5px; + border-radius: 5px 0 0 5px; min-width: 50px; padding: 5px; } @@ -208,6 +219,7 @@ aside { .post_score { margin-top: 20px; color: var(--accent); + font-size: 16px; } .nsfw { @@ -238,35 +250,17 @@ aside { margin: 5px; } -.post_right > p { - opacity: 0.75; - font-size: 16px; -} - .post_media { max-width: 90%; align-self: center; } -.post_media[src=""] { - display: none; -} - .post_body { opacity: 0.9; font-weight: normal; margin: 10px 5px; } -.post_body > p:not(:first-child) { - margin-top: 20px; -} - -.post_body a { - text-decoration: underline; - color: var(--accent); -} - #post_url { color: var(--accent); } @@ -280,10 +274,6 @@ aside { max-width: 20%; } -.post_thumbnail[src=""] { - display: none; -} - .post_flair { background: var(--accent); color: black; @@ -311,17 +301,13 @@ aside { .comment_left { text-align: center; min-width: 50px; - padding: 5px 0px; + padding: 5px 0; align-items: center; } -.comment_title { - font-size: 20px; -} - -.comment_author { - opacity: 0.9; -} +.comment_title { font-size: 20px; } +.comment_link { text-decoration: underline; } +.comment_author { opacity: 0.9; } .comment_author.op { color: var(--accent); @@ -347,7 +333,8 @@ aside { background: var(--foreground); min-width: 40px; border-radius: 5px; - padding: 10px 0px; + padding: 10px 0; + font-size: 16px; } .comment_right { @@ -366,10 +353,6 @@ aside { align-self: center; } -.comment_image[src=""] { - display: none; -} - .comment_body { opacity: 0.9; font-weight: normal; @@ -418,9 +401,34 @@ aside { background: black; } -/* Code */ +/* Markdown */ -pre { +.md > *:not(:first-child) { + margin-top: 20px; +} + +.md p { font-size: 15px; } +.md h1 { font-size: 22px; } +.md h2 { font-size: 20px; } +.md h3 { font-size: 18px; } +.md h4 { font-size: 16px; } +.md h5 { font-size: 14px; } +.md h6 { font-size: 12px; } + +.md blockquote { + padding-left: 8px; + margin: 4px 0 4px 8px; + border-left: 4px solid var(--highlighted); +} + +.md a { + text-decoration: underline; + color: var(--accent); +} + +.md li { margin: 10px 0; } + +.md pre { background: var(--outside); padding: 20px; margin-top: 10px; @@ -428,11 +436,13 @@ pre { box-shadow: var(--black-contrast); } -code { +.md code { font-family: monospace; font-size: 14px; } +.md code:not(.md pre > code) { background: var(--highlighted); } + /* Tables */ table { @@ -453,7 +463,7 @@ td, th { } .post_left { - border-radius: 0px 0px 5px 5px; + border-radius: 0 0 5px 5px; } .post_right { @@ -470,7 +480,7 @@ td, th { .replies > .comment { margin-left: -25px; - padding: 5px 0px; + padding: 5px 0; } .datetime { @@ -484,6 +494,11 @@ td, th { } aside { - margin: 20px 0px 0px 0px; + margin: 20px 0 0 0; + max-width: 100%; + } + + #sidebar { + margin: 20px 0; } } \ No newline at end of file diff --git a/templates/popular.html b/templates/popular.html index 1708b9d..d802b8c 100644 --- a/templates/popular.html +++ b/templates/popular.html @@ -12,24 +12,24 @@ {% for post in posts %}
-

{{ post.score }}

+

{{ post.score }}

{% if post.nsfw %}
NSFW
{% endif %}
-

+

r/{{ post.community }} {% if post.author_flair.0 != "" %} {{ post.author_flair.0 }} {% endif %} {{ post.time }} -

-

+

+

{% if post.flair.0 != "" %} {{ post.flair.0 }} {% endif %} {{ post.title }} -

+


@@ -37,11 +37,11 @@
{% if ends.0 != "" %} - PREV + PREV {% endif %} {% if ends.1 != "" %} - NEXT + NEXT {% endif %}
diff --git a/templates/post.html b/templates/post.html index 2fe6d21..e8778fd 100644 --- a/templates/post.html +++ b/templates/post.html @@ -9,7 +9,7 @@
-

{{ item.score }}

+

{{ item.score }}

@@ -19,7 +19,7 @@ {% endif %} • {{ item.time }} -

{{ item.body }}

+

{{ item.body }}

{%- endmacro %} @@ -27,11 +27,11 @@
-

{{ post.score }}

+

{{ post.score }}

{% if post.nsfw %}
NSFW
{% endif %}
-

+

r/{{ post.community }} @@ -39,7 +39,7 @@ {{ post.author_flair.0 }} {% endif %} {{ post.time }} -

+

{{ post.title }} {% if post.flair.0 != "" %} @@ -53,7 +53,7 @@ {% else if post.post_type == "link" %} {{ post.media }} {% endif %} -

{{ post.body }}

+
{{ post.body }}
diff --git a/templates/subreddit.html b/templates/subreddit.html index cf4ad3c..d9de2a3 100644 --- a/templates/subreddit.html +++ b/templates/subreddit.html @@ -17,24 +17,24 @@ {% for post in posts %}
-

{{ post.score }}

+

{{ post.score }}

{% if post.nsfw %}
NSFW
{% endif %}
-

+

r/{{ post.community }} {% if post.author_flair.0 != "" %} {{ post.author_flair.0 }} {% endif %} {{ post.time }} -

-

+

+

{% if post.flair.0 != "" %} {{ post.flair.0 }} {% endif %} {{ post.title }} -

+


@@ -52,17 +52,21 @@
{% if sub.name != "" %} {% endif %} diff --git a/templates/user.html b/templates/user.html index 30f76be..7894ccb 100644 --- a/templates/user.html +++ b/templates/user.html @@ -14,42 +14,42 @@ {% if post.title != "Comment" %}
-

{{ post.score }}

+

{{ post.score }}

{% if post.nsfw %}
NSFW
{% endif %}
-

+

r/{{ post.community }} {% if post.author_flair.0 != "" %} {{ post.author_flair.0 }} {% endif %} {{ post.time }} -

-

+

+

{% if post.flair.0 == "Comment" %} {% else if post.flair.0 == "" %} {% else %} {{ post.flair.0 }} {% endif %} {{ post.title }} -

+


{% else %}
-

{{ post.score }}

+

{{ post.score }}

-
-

- COMMENT +
+ + COMMENT {{ post.time }} -

-

{{ post.body }}

-
+ +

{{ post.body }}

+

{% endif %} {% endfor %} @@ -64,11 +64,11 @@