Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
freetonik committed Oct 24, 2019
0 parents commit 617d62e
Show file tree
Hide file tree
Showing 6 changed files with 310 additions and 0 deletions.
37 changes: 37 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# Shyblog

An extremely simple, fast static blog generator.

**Step 1:** create the following folder structure:

```
├── css
│   └── styles.css
├── markdown
│   └── DD-MM-YYYY-Slug_1.md
│   └── DD-MM-YYYY-Slug_2.md
│   └── DD-MM-YYYY-Slug_3.md
├── index.html
├── post.html
```

(See [/example](example))

**Step 2:** run `shyblog`.

**Step 3:** Your site is generated in `public`.

## Features

- NO front matter
- NO themes
- NO JavaScript
- NO tags, categories, taxonomy
- NO template lookup logic

## Roadmap

- [x] derive dates from filenames
- [ ] RSS generation
- [ ] Syntax highlighting for code with Chroma
- [ ] live preview server (?)
6 changes: 6 additions & 0 deletions example/css/styles.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
body {
padding: 1rem;
font-family: -apple-system,BlinkMacSystemFont,Segoe UI,Helvetica,Arial,sans-serif,Apple Color Emoji,Segoe UI Emoji;
font-size: 16px;
line-height: 1.5;
}
18 changes: 18 additions & 0 deletions example/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">
<link rel="stylesheet" href="css/styles.css">
<title>My Blog</title>
</head>
<body>
<div class="container">
<h1>My blog</h1>
<ul>
{{ range . }}
<li><a href="/posts/{{ .Slug }}/">{{ .Title }}</a></li>
{{ end }}
</ul>
</div>
</body>
</html>
55 changes: 55 additions & 0 deletions example/markdown/24-10-2019-Welcome.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# Simplify and minify

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.

> An apple is a sweet, edible fruit produced by an apple tree (Malus pumila). Apple trees are cultivated worldwide, and are the most widely grown species in the genus Malus. The tree originated in Central Asia, where its wild ancestor, Malus sieversii, is still found today. Apples have been grown for thousands of years in Asia and Europe, and were brought to North America by European colonists. Apples have religious and mythological significance in many cultures, including Norse, Greek and European Christian traditions.
~~test~~ is a footnote.[^1]

[^1]: the footnote text.

---

Headings:

# Heading 1

## Heading 2

### Heading 3

#### Heading 4

##### Heading 5

###### Heading 6

Table:

| Left-Aligned | Center Aligned | Right Aligned |
| :------------ | :-------------: | ------------: |
| col 3 is | some wordy text | \$1600 |
| col 2 is | centered | \$12 |
| zebra stripes | are neat | \$1 |

Lists:

- Unordered list item 1.
- Unordered list item 2.

1. ordered list item 1.
2. ordered list item 2.
- sub-unordered list item 1.
- sub-unordered list item 2.
- [x] something is DONE.
- [ ] something is NOT DONE.

Syntax Highlighting:

```javascript
var num1, num2, sum;
num1 = prompt("Enter first number");
num2 = prompt("Enter second number");
sum = parseInt(num1) + parseInt(num2); // "+" means "add"
alert("Sum = " + sum); // "+" means combine into a string
```
19 changes: 19 additions & 0 deletions example/post.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">
<link rel="stylesheet" href="/css/styles.css">
<title>{{ .Title }}</title>
</head>

<body>

<article class="container">
{{ .Body }}
<p>{{ .Date.Format "Jan 02, 2006" }}</p>
</article>

</body>

</html>
175 changes: 175 additions & 0 deletions underblog.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
package main

import (
"fmt"
"strconv"
"html/template"
"io/ioutil"
"log"
"os"
"io"
"time"
"path/filepath"
"path"
"strings"
"gopkg.in/russross/blackfriday.v2"
)

type Post struct {
Title string
Body template.HTML
Date time.Time
Slug string
}

func main() {
start := time.Now()

fmt.Printf("Starting...\n")

var posts []Post

// Read markdown files folder
files, err := ioutil.ReadDir("./markdown/")
if err != nil {
fmt.Println("I need a folder named 'markdown' to continue")
log.Fatal(err)
}

// For each file, create HTML
for _, file := range files {
if path.Ext(file.Name()) == ".md" || path.Ext(file.Name()) == ".markdown" {
fmt.Println("Processing " + file.Name())
post := createPost(file.Name())
go createPostFile(post)
posts = append(posts, post)
fmt.Println("Done with " + file.Name())
fmt.Println("---")
}
}

// Create blog root HTML
newpath := filepath.Join(".", "public")
os.MkdirAll(newpath, os.ModePerm)
f, err := os.Create("public/index.html")
if err != nil {log.Fatal(err)}
t, _ := template.ParseFiles("index.html")
t.Execute(f, posts)
f.Close()

// Copy styles
copyCssToPublicDir()

elapsed := time.Since(start)
log.Printf("Done in %s", elapsed)
}

func createPost(filename string) Post {
// Get filename without extension
filenameBase := FnameWithoutExtension(filename)
VerifyFilenameBaseFormat(filenameBase)

// Get date and slug from filename
day := filenameBase[0:2]
month := filenameBase[3:5]
year := filenameBase[6:10]
date, err := time.Parse("02-01-2006", day + "-" + month + "-" +year)
if err != nil {log.Fatal(err)}
slug := filenameBase[11:]

// Get body from file
mdfile, err := os.Open("./markdown/" + filename)
if err != nil {log.Fatal(err)}
rawBytes, err := ioutil.ReadAll(mdfile)

// Get title from first line of file
lines := strings.Split(string(rawBytes), "\n")
title := strings.Replace(lines[0], "# ", "", -1)

mdfile.Close()

// Convert Markdown to HTML
html := blackfriday.Run(rawBytes)

// Create a Post struct
post := Post{
Title: title,
Body: template.HTML(html),
Date: date,
Slug: slug }

return post
}

func createPostFile(post Post) {
// Create folder for HTML
newpath := filepath.Join("public/posts", post.Slug)
os.MkdirAll(newpath, os.ModePerm)

// Create HTML file
f, err := os.Create("public/posts/" + post.Slug + "/" + "index.html")
if err != nil {
fmt.Println("Aaa!")
log.Fatal(err) }

// Generate final HTML file from template
t, _ := template.ParseFiles("post.html")
t.Execute(f, post)
f.Close()
}

func FnameWithoutExtension(fn string) string {
return strings.TrimSuffix(fn, path.Ext(fn))
}

func VerifyFilenameBaseFormat(f string) {
errorDescription := "I can't parse this filename. Make sure its name is formatted as: DD-MM-YYY-slug.md"

if len(f) < 12 {
fmt.Println(errorDescription)
os.Exit(1)
}

// day is int?
_, err := strconv.Atoi(f[0:2])
if err != nil {
fmt.Println(errorDescription)
os.Exit(1)
}

// month is int?
_, err2 := strconv.Atoi(f[3:5])
if err2 != nil {
fmt.Println(errorDescription)
os.Exit(1)
}

// year is int?
_, err3 := strconv.Atoi(f[6:10])
if err3 != nil {
fmt.Println(errorDescription)
os.Exit(1)
}
}

func copyCssToPublicDir() {
from, err := os.Open("./css/styles.css")
if err != nil {
log.Fatal(err)
}
defer from.Close()

newpath := filepath.Join("public", "css")
os.MkdirAll(newpath, os.ModePerm)

to, err := os.OpenFile("./public/css/styles.css", os.O_RDWR|os.O_CREATE, 0666)
if err != nil {
log.Fatal(err)
}
defer to.Close()

_, err = io.Copy(to, from)
if err != nil {
log.Fatal(err)
}
}

0 comments on commit 617d62e

Please sign in to comment.