Skip to content

Commit

Permalink
Add github pages WIP.
Browse files Browse the repository at this point in the history
  • Loading branch information
finnbear committed Dec 1, 2023
1 parent 942431c commit 8bfddbc
Show file tree
Hide file tree
Showing 7 changed files with 158 additions and 9 deletions.
44 changes: 36 additions & 8 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,51 @@ on:
branches: [ master ]
pull_request:
branches: [ master ]
workflow_dispatch:

permissions:
contents: read
pages: write
id-token: write

concurrency:
group: "pages"
cancel-in-progress: false

env:
CARGO_TERM_COLOR: always

jobs:
build:

environment:
name: github-pages
url: ${{ steps.deployment.outputs.page_url }}
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2
- uses: actions-rs/toolchain@v1
with:
toolchain: nightly
override: true
components: rustfmt, clippy
- uses: actions/checkout@v3
with:
persist-credentials: false # otherwise, the token used is the GITHUB_TOKEN, instead of your personal access token.
fetch-depth: 0 # otherwise, there would be errors pushing refs to the destination repository.
- name: Data
run: curl https://raw.githubusercontent.com/vzhou842/profanity-check/master/profanity_check/data/clean_data.csv --output test.csv
- name: Test
run: cargo test --release --features width
run: cargo test --release --features width
- name: Add wasm32 target
run: rustup target add wasm32-unknown-unknown
- name: Install Trunk
uses: baptiste0928/cargo-install@v2
with:
crate: trunk
version: 0.15.0
- name: Build Pages
run: cd pages && trunk --config Trunk.prod.toml build --release --filehash=false
- name: Setup Pages
uses: actions/configure-pages@v3
- name: Upload artifact
uses: actions/upload-pages-artifact@v2
with:
path: './pages/dist/'
- name: Deploy to GitHub Pages
id: deployment
uses: actions/deploy-pages@v2
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,5 @@ src/unicode_confusables.txt
ttf/

.idea
*.iml
*.iml
.vscode/
2 changes: 2 additions & 0 deletions pages/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
dist/
target/
22 changes: 22 additions & 0 deletions pages/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
[package]
name = "pages"
version = "0.1.0"
edition = "2021"

[dependencies]
rustrict = { path = "..", features = ["width"] }
yew = { version = "0.21", features = ["csr"] }

[dependencies.web-sys]
version = "0.3"
features = [
'HtmlInputElement',
'HtmlTextAreaElement',
]

[profile.release]
codegen-units = 1
lto = true
opt-level = "z"
panic = "abort"
strip = "debuginfo"
4 changes: 4 additions & 0 deletions pages/Trunk.prod.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[build]
target = "index.html"
release = true
public_url = "/rustrict/"
18 changes: 18 additions & 0 deletions pages/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Rustrict</title>
<meta name="viewport" content="width=device-width, initial-scale=2" />
<link data-trunk rel="rust" data-wasm-opt="z" data-no-demangle/>
<link
rel="stylesheet"
href="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/css/bootstrap.min.css"
integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u"
crossorigin="anonymous"
/>
</head>
<body style="background-color: #34495e; color: white; padding: 2rem;">

</body>
</html>
74 changes: 74 additions & 0 deletions pages/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
use web_sys::{HtmlInputElement, window, InputEvent, HtmlTextAreaElement, wasm_bindgen::JsCast};
use yew::{html, Html, Callback, function_component, TargetCast};
use rustrict::Censor;

#[function_component(App)]
fn app() -> Html {
let oninput = Callback::from(move |event: InputEvent| {
if let Some(input) = event.target_dyn_into::<HtmlInputElement>() {
let uncensored = input.value();
let analysis_element = window().unwrap().document().unwrap().get_element_by_id("analysis").unwrap();
let censored_element = window().unwrap().document().unwrap().get_element_by_id("censored").unwrap().dyn_into::<HtmlTextAreaElement>().unwrap();
if uncensored.is_empty() {
analysis_element.set_inner_html("N/A");
censored_element.set_value("");
} else {
let (censored, analysis) = Censor::from_str(&uncensored).censor_and_analyze();
let width = rustrict::width_str(&uncensored);
let result = format!("{analysis:?} (width = {width})");
analysis_element.set_inner_html(&result);
censored_element.set_value(&censored);
}
}
});
html! {<>
<h2>{"Rustrict"}</h2>
<h3>{"Input"}</h3>
<input
class="form-control"
{oninput}
type="text"
style="background-color: #2c3e50; color: white; border-width: 0;"
/>
<h3>{"Analysis"}</h3>
<p id="analysis">{"N/A"}</p>
<h3>{"Output"}</h3>
<textarea
id="censored"
class="form-control"
rows="10"
readonly={true}
tabindex="-1"
style="background-color: #2c3e50; resize: vertical; color: white; border-width: 0; user-select: none;"
></textarea>
</>}
}

/*
<script>
async function censor() {
const req = { text: document.getElementById("input").value };
let response = await fetch("/", {
method: "POST",
body: JSON.stringify(req),
headers: {
"Content-Type": "application/json",
},
});
let resp = await response.json();
if (resp.original != req.text) {
return;
}
document.getElementById(
"analysis"
).innerHTML = `${resp.analysis} (width = ${resp.width})`;
document.getElementById("output").value = resp.censored;
}
censor();
</script>
*/

fn main() {
yew::Renderer::<App>::new().render();
}

0 comments on commit 8bfddbc

Please sign in to comment.