Skip to content

Commit

Permalink
Re wrote generate_post in rust.
Browse files Browse the repository at this point in the history
  • Loading branch information
nimafanniasl committed Feb 11, 2024
1 parent a47fce8 commit 17ba543
Show file tree
Hide file tree
Showing 18 changed files with 244 additions and 122 deletions.
8 changes: 4 additions & 4 deletions .github/workflows/generate-post.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,17 @@ jobs:
run: pip install -r requirements.txt # If you have any dependencies

- name: Generate Post HTML
run: python scripts/generate_post.py ${{ github.event.issue.number }} "${{ github.event.issue.title }}" "${{ github.event.issue.user.login }}" "${{ github.event.issue.body }}"
run: python scripts/generate_post ${{ github.event.issue.number }} "${{ github.event.issue.title }}" "${{ github.event.issue.user.login }}" "${{ github.event.issue.body }}" "${{ format(github.event.issue.created_at, 'YYYY-MM-DD HH:mm:ss') }}"

- name: Add new post to recents page
run: python scripts/add_to_recent.py ${{ github.event.issue.number }} "${{ github.event.issue.title }}" "${{ github.event.issue.user.login }}" "${{ github.event.issue.body }}"
run: python scripts/add_to_recent.py ${{ github.event.issue.number }} "${{ github.event.issue.title }}" "${{ github.event.issue.user.login }}" "${{ github.event.issue.body }}" "${{ format(github.event.issue.created_at, 'YYYY-MM-DD HH:mm:ss') }}"

- name: Commit and Push Changes
run: |
git config --local user.email "[email protected]"
git config --local user.name "GitHub Action"
git add .
git commit -m "Generate HTML for issue #${{ github.event.issue.number }}"
git commit -m "Generate Post page for issue #${{ github.event.issue.number }}"
git push
comment:
Expand All @@ -47,5 +47,5 @@ jobs:
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
body: "Your post going to be available after a few seconds in https://nimafanniasl.github.io/GithubForum/posts/${{ github.event.issue.number }}.html"
body: "Your post is going to be available after a few seconds in https://nimafanniasl.github.io/GithubForum/posts/${{ github.event.issue.number }}.html"
})
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/scripts_rust/target
/scripts_rust/Cargo.lock
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
## A (NOT) Fully functioning Forum made just with Free Github hosting and Github pages. (WIP)

### TODO:
- [ ] Be able Remove Comments
- [ ] Be able to Remove Comments
- [ ] Complete home page
- [ ] Make the footer always stick to the down of the screen
- [ ] Make the header and footer dynamic and load from a javascript file
- [ ] Add reactions to posts
- [ ] access for everyone to assign posts to labels
- [ ] Toggle for switching between light and dark mode
- [ ] Toggle for switching between light and dark mode
- [ ] Complete the categories page
8 changes: 1 addition & 7 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,7 @@ <h1>GithubForum</h1>
</nav>
</header>
<main>
<section class="post">
<h2>Example Post Title</h2>
<p>Posted by: John Doe | Date: September 22, 2024</p>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur sed...</p>
<a href="post.html">Read More</a>
</section>
<!-- Add more posts here -->
<h2 style="color: yellow; text-align: center;">A (NOT) Fully functioning Forum made just with Free Github hosting and Github pages. (WIP)</h1>
</main>
<footer>
<p>&copy; 2024 GithubForum</p>
Expand Down
102 changes: 0 additions & 102 deletions posts/20.html

This file was deleted.

Binary file removed scripts/__pycache__/includes.cpython-312.pyc
Binary file not shown.
5 changes: 3 additions & 2 deletions scripts/add_to_recent.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ def remove_duplicate_posts(html_file, issue_number):
post_title = sys.argv[2]
post_author = sys.argv[3]
post_content = sys.argv[4]
post_created_date = sys.argv[5]

post_content = "".join(post_content[:70]) + "..."
print(post_content)
Expand All @@ -62,12 +63,12 @@ def remove_duplicate_posts(html_file, issue_number):

new_section_html = new_section_html.replace("{POST-TITLE}", post_title)
new_section_html = new_section_html.replace("{POST-AUTHOR}", post_author)
new_section_html = new_section_html.replace("{POST-DATE}", datetime.now().strftime("%Y-%m-%d"))
new_section_html = new_section_html.replace("{POST-DATE}", post_created_date)
new_section_html = new_section_html.replace("{POST-NUMBER}", issue_number)
new_section_html = new_section_html.replace("{MINIFIED-POST-CONTENT}", post_content)
new_section_html = new_section_html.replace("{POST-PAGE-LINK}", f"https://nimafanniasl.github.io/GithubForum/posts/{issue_number}.html")

# Remove suplicate post
# Remove duplicate post
remove_duplicate_posts(recent_html, issue_number)

# Add the new section to the HTML file
Expand Down
Binary file added scripts/generate_post
Binary file not shown.
3 changes: 0 additions & 3 deletions scripts/issue_comment.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@


if mode.lower() == "add":
pfp_url = get_pfp(post_author)

posts_dir = "posts"
issue_html = os.path.join(posts_dir, f"{issue_number}.html")

Expand All @@ -23,7 +21,6 @@
isuue_comment_template = isuue_comment_template.replace("{POST-AUTHOR}", post_author)
isuue_comment_template = isuue_comment_template.replace("{POST-DATE}",
datetime.now().strftime("%Y-%m-%d"))
isuue_comment_template = isuue_comment_template.replace("{PFP-URL}", pfp_url)

# Read existing issue_html content and parse it with BeautifulSoup
with open(issue_html, "r", encoding="utf-8") as html_file:
Expand Down
Binary file added scripts_py/__pycache__/includes.cpython-311.pyc
Binary file not shown.
File renamed without changes.
16 changes: 16 additions & 0 deletions scripts_rust/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
[package]
name = "scripts_rust"
version = "0.1.0"
edition = "2021"
authors = ["nimafanniasl"]

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
reqwest = { version = "0.11.24", features = ["default", "json", "blocking"] }
serde_json = "1.0.113"


[[bin]]
name = "generate_post"
path = "src/generate_post.rs"
77 changes: 77 additions & 0 deletions scripts_rust/src/add_to_recent.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
// import sys
// from datetime import datetime
// from bs4 import BeautifulSoup

// def add_section_to_html(html_file, new_section_html):
// with open(html_file, 'r') as file:
// soup = BeautifulSoup(file, 'html.parser')

// main_tag = soup.find('main')

// if main_tag:
// # Create a new section and add it to the top of the main element
// new_section = BeautifulSoup(new_section_html, 'html.parser')
// main_tag.insert(0, new_section)

// with open(html_file, 'w') as file:
// file.write(str(soup))

// def remove_oldest_section(html_file):
// with open(html_file, 'r') as file:
// soup = BeautifulSoup(file, 'html.parser')

// main_tag = soup.find('main')
// if main_tag:
// sections = main_tag.find_all('section')

// if len(sections) > 10:
// # Remove the oldest section (last one in the list)
// sections[-1].decompose()

// with open(html_file, 'w') as file:
// file.write(str(soup))

// def remove_duplicate_posts(html_file, issue_number):
// with open(html_file, 'r') as file:
// soup = BeautifulSoup(file, 'html.parser')

// main_tag = soup.find('main')
// if main_tag:
// sections = main_tag.find_all('section')

// for section in sections:
// if section.get('id') == str(issue_number):
// section.decompose()

// with open(html_file, 'w') as file:
// file.write(str(soup))

// if __name__ == "__main__":
// issue_number = sys.argv[1]
// post_title = sys.argv[2]
// post_author = sys.argv[3]
// post_content = sys.argv[4]

// post_content = "".join(post_content[:70]) + "..."
// print(post_content)

// recent_html = "recent.html"

// with open("templates/section.html", "r") as section_template:
// new_section_html = section_template.read()

// new_section_html = new_section_html.replace("{POST-TITLE}", post_title)
// new_section_html = new_section_html.replace("{POST-AUTHOR}", post_author)
// new_section_html = new_section_html.replace("{POST-DATE}", datetime.now().strftime("%Y-%m-%d"))
// new_section_html = new_section_html.replace("{POST-NUMBER}", issue_number)
// new_section_html = new_section_html.replace("{MINIFIED-POST-CONTENT}", post_content)
// new_section_html = new_section_html.replace("{POST-PAGE-LINK}", f"https://nimafanniasl.github.io/GithubForum/posts/{issue_number}.html")

// # Remove suplicate post
// remove_duplicate_posts(recent_html, issue_number)

// # Add the new section to the HTML file
// add_section_to_html(recent_html, new_section_html)

// # Remove the oldest section if there are more than 10 sections
// remove_oldest_section(recent_html)
45 changes: 45 additions & 0 deletions scripts_rust/src/generate_post.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
use std::env;
// use std::fmt::format;
use std::fs;
use std::fs::File;
use std::io::prelude::*;

fn main() {
let args: Vec<String> = env::args().collect();

let issue_number = &args[1];
let post_title = &args[2];
let post_author = &args[3];
let post_content = &args[4];
let post_created_date = &args[5];

let template_file_path = "../templates/posttemplate.html";

let template_file = fs::read_to_string(template_file_path)
.expect("Should have been able to read the file");

let output_directory = "../posts";


let mut html_content = String::from(template_file);

html_content = html_content.replace("{POST-TITLE}", &post_title);
html_content = html_content.replace("{POST-AUTHOR}", &post_author);
html_content = html_content.replace("{POST-DATE}", &post_created_date);
html_content = html_content.replace("{POST-NUMBER}", &issue_number);
html_content = html_content.replace("{POST-CONTENT}", &post_content);

match fs::create_dir(&output_directory) {
Ok(()) => println!("Dir created"),
_ => println!("Dir already exists")
}

// Change the file creation and write operations as follows
let mut file = File::create(format!("{}/{}.html", output_directory, issue_number))
.expect("Failed to create file");

file.write_all(html_content.as_bytes())
.expect("Failed to write to file");


}
40 changes: 40 additions & 0 deletions scripts_rust/src/issue_comment.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// import sys
// import os
// from bs4 import BeautifulSoup
// from datetime import datetime
// from includes import *

// mode = sys.argv[1]
// issue_number = sys.argv[2]
// post_author = sys.argv[3]
// post_content = convert_md_to_html(sys.argv[4])


// if mode.lower() == "add":
// pfp_url = get_pfp(post_author)

// posts_dir = "posts"
// issue_html = os.path.join(posts_dir, f"{issue_number}.html")

// with open("templates/comment.html", "r") as isuue_comment:
// isuue_comment_template = isuue_comment.read()

// isuue_comment_template = isuue_comment_template.replace("{POST-CONTENT}", post_content)
// isuue_comment_template = isuue_comment_template.replace("{POST-AUTHOR}", post_author)
// isuue_comment_template = isuue_comment_template.replace("{POST-DATE}",
// datetime.now().strftime("%Y-%m-%d"))
// isuue_comment_template = isuue_comment_template.replace("{PFP-URL}", pfp_url)

// # Read existing issue_html content and parse it with BeautifulSoup
// with open(issue_html, "r", encoding="utf-8") as html_file:
// soup = BeautifulSoup(html_file, "html.parser")

// # Find the <main> section
// main_section = soup.find("main")

// # Append the modified isuue_comment_template to <main>
// main_section.append(BeautifulSoup(isuue_comment_template, "html.parser"))

// # Save the updated HTML back to issue_html
// with open(issue_html, "w", encoding="utf-8") as html_file:
// html_file.write(str(soup))
Loading

0 comments on commit 17ba543

Please sign in to comment.