Skip to content

Commit

Permalink
initial version of github pages website
Browse files Browse the repository at this point in the history
  • Loading branch information
Pitasi committed Sep 23, 2023
1 parent c0c6a0d commit b6314ee
Show file tree
Hide file tree
Showing 9 changed files with 184 additions and 0 deletions.
53 changes: 53 additions & 0 deletions .github/workflows/gh-pages.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@

name: Deploy website on GitHub Pages

on:
# Runs on pushes targeting the default branch
push:
branches: ["main"]

# Allows you to run this workflow manually from the Actions tab
workflow_dispatch:

# Sets permissions of the GITHUB_TOKEN to allow deployment to GitHub Pages
permissions:
contents: read
pages: write
id-token: write

# Allow only one concurrent deployment, skipping runs queued between the run in-progress and latest queued.
# However, do NOT cancel in-progress runs as we want to allow these production deployments to complete.
concurrency:
group: "pages"
cancel-in-progress: false

jobs:
# Build job
build:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v3
- name: Setup Pages
uses: actions/configure-pages@v3
- uses: actions-rust-lang/setup-rust-toolchain@v1
- name: Generate HTML
run: cargo run --release
- name: Copy bundle.css
run: cp ./target/csm/bundle.css dist/
- name: Upload artifact
uses: actions/upload-pages-artifact@v2
with:
path: dist/

# Deployment job
deploy:
environment:
name: github-pages
url: ${{ steps.deployment.outputs.page_url }}
runs-on: ubuntu-latest
needs: build
steps:
- name: Deploy to GitHub Pages
id: deployment
uses: actions/deploy-pages@v2
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ members = [
"rscx-macros",
"benches",
]
exclude = [
"website",
]

[workspace.package]
version = "0.1.9"
Expand Down
3 changes: 3 additions & 0 deletions website/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
target
Cargo.lock
dist/
13 changes: 13 additions & 0 deletions website/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[package]
name = "website"
edition = "2021"
version = "0.1.0"

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

[dependencies]
async-recursion = "1.0.5"
csm = { git = "https://github.com/Pitasi/csm", version = "0.1.0" }
rscx = "0.1.9"
rscx-mdx = "0.1.3"
tokio = { version = "1.32.0", features = ["full"] }
15 changes: 15 additions & 0 deletions website/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# website

Static site generator for https://rscx.dev.

It uses rscx (duh), [https://github.com/Pitasi/rscx-mdx](rscx-mdx) for writing
markdown and [https://github.com/Pitasi/csm](csm) for scoped CSS styling.


## Build the site

Install [https://github.com/casey/just](just) and run:

```sh
just build
```
12 changes: 12 additions & 0 deletions website/justfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
dev:
static-web-server --port 8787 --root ./dist &
open http://localhost:8787
cargo watch -i dist/ -s "cargo run && just copy-bundle-css"

build-release: build-html-release copy-bundle-css

build-html-release:
cargo run --release

copy-bundle-css:
cp ./target/csm/bundle.css ./dist/bundle.css
2 changes: 2 additions & 0 deletions website/pages/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# rscx 🪶

1 change: 1 addition & 0 deletions website/pages/subfolder/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# testing subfolders :)
82 changes: 82 additions & 0 deletions website/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
use std::{
fs::{self, File},
io::{Read, Write},
path::Path,
};

use csm::csm;
use rscx::{component, html, EscapeAttribute};
use rscx_mdx::mdx::{Mdx, MdxComponentProps};

#[tokio::main]
async fn main() {
let pages_dir = Path::new("pages");
let dist_dir = Path::new("dist");
let _ = fs::remove_dir_all(&dist_dir);
render(&pages_dir, &dist_dir).await;
}

#[async_recursion::async_recursion]
async fn render(dir: &Path, dist_dir: &Path) {
fs::create_dir_all(&dist_dir).unwrap();

for entry in dir.read_dir().unwrap() {
let entry = entry.unwrap();
let path = entry.path();

if entry.file_type().unwrap().is_dir() {
render(&path, &dist_dir.join(entry.file_name())).await;
continue;
}

let file_name = path.file_name().unwrap().to_str().unwrap();
if !file_name.ends_with(".md") {
continue;
}

println!("building {}", path.as_path().display());
let file_name = file_name.trim_end_matches(".md");

let mut file = File::open(&path).unwrap();
let mut contents = String::new();
file.read_to_string(&mut contents).unwrap();
let mut file = File::create(dist_dir.join(format!("{}.html", file_name))).unwrap();

let html = render_markdown(contents).await;
file.write_all(html.as_bytes()).unwrap();
}
}

async fn render_markdown(md: String) -> String {
html! {
<Shell>
<Mdx source=md handler=handler />
</Shell>
}
}

async fn handler(name: String, _props: MdxComponentProps) -> String {
match name {
_ => html! {},
}
}

#[component]
async fn Shell(children: String) -> String {
html! {
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>rscx</title>
<link rel="stylesheet" href="/bundle.css" />
</head>
<body class=csm!{Body,
max-width: 600px,
margin: auto,
}>
{children}
</body>
</html>
}
}

0 comments on commit b6314ee

Please sign in to comment.