Skip to content

Commit

Permalink
basic page generator
Browse files Browse the repository at this point in the history
  • Loading branch information
BachErik committed Jul 7, 2024
1 parent 3b18e89 commit 9b23d80
Show file tree
Hide file tree
Showing 7 changed files with 215 additions and 41 deletions.
53 changes: 21 additions & 32 deletions .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,46 +6,35 @@ on:
workflow_dispatch:

jobs:
build:
build-and-deploy:
runs-on: ubuntu-latest

permissions:
contents: write
concurrency:
group: ${{ github.workflow }}-${{ github.ref }}

steps:
- uses: actions/checkout@v2
- name: Checkout repository
uses: actions/checkout@v3

- name: Set up Go
uses: actions/setup-go@v2
uses: actions/setup-go@v4
with:
go-version: '^1.17'

- name: Build static content
run: go run ./main.go

- name: Upload HTML artifact
uses: actions/upload-artifact@v2
with:
name: static-content
path: ./index.html

deploy:
runs-on: ubuntu-latest
needs: build
go-version: '1.18' # Adjust to the version of Go you are using

# Grant GITHUB_TOKEN the permissions required to make a Pages deployment
permissions:
pages: write # to deploy to Pages
id-token: write # to verify the deployment originates from an appropriate source
- name: Install dependencies
run: go mod tidy

steps:
- name: Checkout code
uses: actions/checkout@v2

- name: Download HTML artifact
uses: actions/download-artifact@v2
with:
name: static-content
- name: Build static site
run: go run main.go

- name: Deploy to GitHub Pages
id: deployment
uses: actions/deploy-pages@v1
uses: peaceiris/actions-gh-pages@v4
# If you're changing the branch from main,
# also change the `main` in `refs/heads/main`
# below accordingly.
if: github.ref == 'refs/heads/main'
with:
artifact_name: "static-content"
github_token: ${{ secrets.GITHUB_TOKEN }}
publish_dir: ./output
119 changes: 119 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
output/

# If you prefer the allow list template instead of the deny list, see community template:
# https://github.com/github/gitignore/blob/main/community/Golang/Go.AllowList.gitignore
#
# Binaries for programs and plugins
*.exe
*.exe~
*.dll
*.so
*.dylib

# Test binary, built with `go test -c`
*.test

# Output of the go coverage tool, specifically when used with LiteIDE
*.out

# Dependency directories (remove the comment below to include it)
# vendor/

# Go workspace file
go.work
go.work.sum

# env file
.env


# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio, WebStorm and Rider
# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839

# User-specific stuff
.idea/**/workspace.xml
.idea/**/tasks.xml
.idea/**/usage.statistics.xml
.idea/**/dictionaries
.idea/**/shelf

# AWS User-specific
.idea/**/aws.xml

# Generated files
.idea/**/contentModel.xml

# Sensitive or high-churn files
.idea/**/dataSources/
.idea/**/dataSources.ids
.idea/**/dataSources.local.xml
.idea/**/sqlDataSources.xml
.idea/**/dynamic.xml
.idea/**/uiDesigner.xml
.idea/**/dbnavigator.xml

# Gradle
.idea/**/gradle.xml
.idea/**/libraries

# Gradle and Maven with auto-import
# When using Gradle or Maven with auto-import, you should exclude module files,
# since they will be recreated, and may cause churn. Uncomment if using
# auto-import.
# .idea/artifacts
# .idea/compiler.xml
# .idea/jarRepositories.xml
# .idea/modules.xml
# .idea/*.iml
# .idea/modules
# *.iml
# *.ipr

# CMake
cmake-build-*/

# Mongo Explorer plugin
.idea/**/mongoSettings.xml

# File-based project format
*.iws

# IntelliJ
out/

# mpeltonen/sbt-idea plugin
.idea_modules/

# JIRA plugin
atlassian-ide-plugin.xml

# Cursive Clojure plugin
.idea/replstate.xml

# SonarLint plugin
.idea/sonarlint/

# Crashlytics plugin (for Android Studio and IntelliJ)
com_crashlytics_export_strings.xml
crashlytics.properties
crashlytics-build.properties
fabric.properties

# Editor-based Rest Client
.idea/httpRequests

# Android studio 3.1+ serialized cache file
.idea/caches/build_file_checksums.ser

.vscode/*
!.vscode/settings.json
!.vscode/tasks.json
!.vscode/launch.json
!.vscode/extensions.json
!.vscode/*.code-snippets

# Local History for Visual Studio Code
.history/

# Built Visual Studio Code Extensions
*.vsix
3 changes: 3 additions & 0 deletions content/page1.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<!-- page1.html -->
<h2>Page 1</h2>
<p>This is the content of page 1.</p>
3 changes: 3 additions & 0 deletions content/page2.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<!-- page2.html -->
<h2>Page 2</h2>
<p>This is the content of page 2.</p>
53 changes: 44 additions & 9 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,22 +1,57 @@
package main

import (
"os"
"fmt"
"io/ioutil"
"log"
"os"
"path/filepath"
)

func main() {
f, err := os.Create("index.html")
// Define the directory paths
contentDir := "content"
templatesDir := "templates"
outputDir := "output"

// Read header and footer templates
header, err := ioutil.ReadFile(filepath.Join(templatesDir, "header.html"))
if err != nil {
fmt.Println(err)
return
log.Fatalf("Error reading header: %v", err)
}

footer, err := ioutil.ReadFile(filepath.Join(templatesDir, "footer.html"))
if err != nil {
log.Fatalf("Error reading footer: %v", err)
}
defer f.Close()

_, err = f.WriteString("<html><body>Hello World!</body></html>")
// Create output directory if it doesn't exist
if _, err := os.Stat(outputDir); os.IsNotExist(err) {
os.Mkdir(outputDir, os.ModePerm)
}

// Read the content directory
files, err := ioutil.ReadDir(contentDir)
if err != nil {
fmt.Println(err)
f.Close()
return
log.Fatalf("Error reading content directory: %v", err)
}

for _, file := range files {
if filepath.Ext(file.Name()) == ".html" {
content, err := ioutil.ReadFile(filepath.Join(contentDir, file.Name()))
if err != nil {
log.Printf("Error reading content file %s: %v", file.Name(), err)
continue
}

outputFilePath := filepath.Join(outputDir, file.Name())
err = ioutil.WriteFile(outputFilePath, append(append(header, content...), footer...), 0644)
if err != nil {
log.Printf("Error writing output file %s: %v", outputFilePath, err)
continue
}

fmt.Printf("Generated %s\n", outputFilePath)
}
}
}
6 changes: 6 additions & 0 deletions templates/footer.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
</main>
<footer>
<p>&copy; 2024 My Static Site</p>
</footer>
</body>
</html>
19 changes: 19 additions & 0 deletions templates/header.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>My Static Site</title>
</head>
<body>
<header>
<h1>Welcome to My Static Site</h1>
<nav>
<ul>
<li><a href="page1.html">Page 1</a></li>
<li><a href="page2.html">Page 2</a></li>
<!-- Add more links as needed -->
</ul>
</nav>
</header>
<main>

0 comments on commit 9b23d80

Please sign in to comment.