Skip to content

Commit

Permalink
Build any PHP to HTML (#1)
Browse files Browse the repository at this point in the history
* .

* works

* use tagged image
  • Loading branch information
atas authored Oct 13, 2023
1 parent 966b227 commit 5b4ecae
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 40 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build-deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ jobs:
runs-on: ubuntu-latest

container:
image: ghcr.io/atas/ssg-builder:1.0.0
image: ghcr.io/atas/ssg-builder:202310131126

steps:
- name: Checkout repository
Expand Down
15 changes: 10 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,22 @@
# You would only need to use these if you want to build your own custom builder image.
# Otherwise, by default the workflow uses the pre-built image from ghcr.io/atas/ssg-builder:latest

.PHONY: build-local-image update-workflow-image run-local-image

build-local-image:
docker build system/workflow-image -t atas-ssg-builder:latest

run-local-image:
docker run --rm -it -v $(shell pwd):/github/workspace atas-ssg-builder:latest

# Updates the GHCR:latest image
update-workflow-image:
docker login ghcr.io
docker buildx build --platform linux/amd64 system/workflow-image -t ghcr.io/atas/ssg-builder:latest
docker push ghcr.io/atas/ssg-builder:latest


.PHONY: run-local-image

run-local-image:
docker run --rm -it -v $(shell pwd):/github/workspace atas-ssg-builder:latest
# Creates a new tag for GHCR:latest image with the current date and time
tag-workflow-image:
docker tag ghcr.io/atas/ssg-builder:latest ghcr.io/atas/ssg-builder:$(shell date +%Y%m%d%H%M)
docker push ghcr.io/atas/ssg-builder:$(shell date +%Y%m%d%H%M)

9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,3 +102,12 @@ Ensure local hostname matches the `local_hostname` in `config.json` file. Add it
necessary with IP `127.0.0.1`

Locally, http works fine, https is used in build process with self-signed cert to generate URLs correctly.


## Architectural Considerations

### Caching ./pages and ./posts for performance in local env

If you have a lot of pages and posts, local development environment will be likely slow. We can introduce caching to
speed it up, but if you have over a hundred pages and posts, you are probably better off with one of the more
advanced static site generators instead of this little tool.
93 changes: 62 additions & 31 deletions system/bin/generate_html.sh
Original file line number Diff line number Diff line change
@@ -1,66 +1,97 @@
#!/bin/bash

echo "Creating symbolic link /workspace to $(pwd)"
ln -s "$(pwd)" /workspace

php-fpm -D
service nginx start
# Create a symbolic link to the current directory
# if it doesn't exist
if [[ ! -L "/workspace" ]]; then
echo "Creating symbolic link /workspace to $(pwd)"
ln -s "$(pwd)" /workspace
fi

host=$(jq -r '.hostname' config.json)
# Check if php-fpm is running
if ! pgrep "php-fpm" > /dev/null; then
php-fpm -D
fi

if ! grep -qF "$host" "/etc/hosts"; then
echo "$host" >> "/etc/hosts"
# Check if nginx is running
if ! pgrep "nginx" > /dev/null; then
service nginx start
fi

echo 127.0.0.1 "$host" >> /etc/hosts
hostname=$(jq -r '.hostname' config.json)

rm -rf html
mkdir -p html
if ! grep -qF "$hostname" "/etc/hosts"; then
echo "$hostname" >> "/etc/hosts"
fi

echo 127.0.0.1 "$hostname" >> /etc/hosts

# Curl the URL $1 and save it to $2
function curl_and_save() {
echo Hitting URL "$1"
curl -k "$1" > "$2"
echo "curl_and_save https://$hostname$1 > $2"
curl -ks --fail "https://$hostname$1" > "$2"

# Check if the file exists and is not zero bytes
if [[ ! -s $2 ]]; then
echo "Error: The file does not exist or is zero bytes."
exit 1
fi

# Check if the file is missing the specific text string
if ! grep -q "<!-- Built with Ata's SSG https://www.github.com/atas/ssg -->" "$2"; then
echo "Error: The file does not contain the attribution HTML comment OR something else is wrong in the output. As the MIT license requirement, please do not remove it to help other fellow developers discover this neat tool."
# Check if file exists
if [[ ! -e "$2" ]]; then
echo "Error: File $2 does not exist."
exit 1
fi

# Check if file size is zero
if [[ ! -s "$2" ]]; then
echo "Error: File $2 is empty."
exit 1
fi
}

curl_and_save "https://$host/" "html/index.html"
#region Building HTML files
rm -rf html
mkdir -p html

# generate html from all md files at pages directory
# pages/*.md HTML BUilding
for file in pages/*.md; do
filename=$(basename "$file" .md)
curl_and_save "https://$host/$filename" "html/$filename.html"
curl_and_save "/$filename" "html/$filename.html"
done

# generate html from all php files at home directory except index.php, page.php and post.php
for file in *.php; do
if [[ "$file" != "index.php" && "$file" != "page.php" && "$file" != "post.php" ]]; then
filename=$(basename "$file" .php)
curl_and_save "https://$host/$filename" "html/$filename.html"
fi
done

# generate html from all md files at posts directory
# posts/*.md HTML Building
mkdir -p html/p
for file in posts/*.md; do
filename=$(basename "$file" .md)
slug=$(awk -F': ' '/^slug:|^[ \t]*"slug":/ {gsub(/["\r]/, "", $2); print $2}' $file)
curl_and_save "https://$host/p/$slug" "html/p/$slug.html"
curl_and_save "/p/$slug" "html/p/$slug.html"
done

# **/*.php Any php file HTML Building
find . -name "*.php" ! -path "./system/*" ! -path "./vendor/*" ! -path "./layout/*" | while read -r file; do
file="${file#./}" #Remove the leading ./
if [[ "$file" == "page.php" || "$file" == "post.php" ]]; then #pages and posts are handled above separately
continue;
fi

without_extension="${file%.*}"
# Replace the .php extension with .txt
html_file="html/${file%.php}.html"

# Create the directory structure if it doesn't exist
mkdir -p "$(dirname "$html_file")"

filename=$(basename "$file" .php)
curl_and_save "/$without_extension" "$html_file"
done

#endregion

# ./assets/* copying to ./html
cp -R assets html/assets
# Build CSS
lessc html/assets/styles/style.less html/assets/styles/style.css

# Remove .less as .css file is built
rm -f html/assets/styles/*.less

# Robots.txt is good to have
cp robots.txt html/
7 changes: 5 additions & 2 deletions system/workflow-image/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ RUN apt-get update && apt-get install -y \
node-less \
git \
jq \
&& docker-php-ext-configure gd --with-freetype --with-jpeg \
&& docker-php-ext-install gd pdo_mysql
procps \
vim \
&& docker-php-ext-configure gd --with-freetype --with-jpeg

# Remove default Nginx configuration
RUN rm -rf /etc/nginx/sites-available/default \
Expand All @@ -28,6 +29,8 @@ RUN openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout /etc/nginx/nginx

RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer

RUN echo "alias ll='ls -lha'" >> /root/.bashrc

LABEL org.opencontainers.image.source = "https://github.com/atas/ssg"

ENTRYPOINT ["/entrypoint.sh"]
2 changes: 1 addition & 1 deletion system/workflow-image/nginx.conf
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ server {
location / {
# If matching .php file found, rewrite to that, e.g. custom-page.php for /custom-page
if (-f $request_filename.php) {
rewrite /$1.php last;
rewrite ^(.*)$ $1.php last;
}

try_files $uri $uri.html $uri/ @try-render-page;
Expand Down

0 comments on commit 5b4ecae

Please sign in to comment.