forked from brandur/sorg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsorg.go
124 lines (99 loc) · 3.1 KB
/
sorg.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
package sorg
import (
"fmt"
"os"
"path"
"regexp"
"strings"
"time"
log "github.com/Sirupsen/logrus"
)
const (
// AbsoluteURL is the site's absolute URL. It's usually preferable that
// it's not used, but it is when generating emails.
AbsoluteURL = "https://brandur.org"
// ContentDir is the location of the site's content (articles, fragments,
// assets, etc.).
ContentDir = "./content"
// LayoutsDir is the source directory for view layouts.
LayoutsDir = "./layouts"
// MainLayout is the site's main layout.
MainLayout = LayoutsDir + "/main"
// PagesDir is the source directory for one-off page content.
PagesDir = "./pages"
// PassageLayout is the layout for a Passages & Glass issue (an email
// newsletter).
PassageLayout = LayoutsDir + "/passages"
// TempDir is a temporary directory used to download images that will be
// processed and such.
TempDir = "./tmp"
// ViewsDir is the source directory for views.
ViewsDir = "./views"
)
// TwitterInfo is some HTML that includes a Twitter link which can be appended
// to the publishing info of various content.
const TwitterInfo = `<p>Find me on Twitter at ` +
`<strong><a href="https://twitter.com/brandur">@brandur</a></strong>.</p>`
var errBadFrontmatter = fmt.Errorf("Unable to split YAML frontmatter")
// A list of all directories that are in the built static site.
var outputDirs = []string{
".",
"articles",
"assets",
"assets/" + Release,
"fragments",
"photos",
"reading",
"runs",
"passages",
"twitter",
}
// CreateOutputDirs creates a target directory for the static site and all
// other necessary directories for the build if they don't already exist.
func CreateOutputDirs(targetDir string) error {
start := time.Now()
defer func() {
log.Debugf("Created target directories in %v.", time.Now().Sub(start))
}()
for _, dir := range outputDirs {
dir = path.Join(targetDir, dir)
err := os.MkdirAll(dir, 0755)
if err != nil {
return err
}
}
return nil
}
// InitLog initializes logging for singularity programs.
func InitLog(verbose bool) {
log.SetFormatter(&plainFormatter{})
if verbose {
log.SetLevel(log.DebugLevel)
}
}
// SplitFrontmatter takes content that contains a combination and YAML metadata
// and content, and splits it into its components.
func SplitFrontmatter(content string) (string, string, error) {
parts := regexp.MustCompile("(?m)^---").Split(content, 3)
if len(parts) > 1 && parts[0] != "" {
return "", "", errBadFrontmatter
} else if len(parts) == 2 {
return "", strings.TrimSpace(parts[1]), nil
} else if len(parts) == 3 {
return strings.TrimSpace(parts[1]), strings.TrimSpace(parts[2]), nil
}
return "", strings.TrimSpace(parts[0]), nil
}
// plainFormatter is a logrus formatter that displays text in a much more
// simple fashion that's more suitable as CLI output.
type plainFormatter struct {
}
// Format takes a logrus.Entry and returns bytes that are suitable for log
// output.
func (f *plainFormatter) Format(entry *log.Entry) ([]byte, error) {
bytes := []byte(entry.Message + "\n")
if entry.Level == log.DebugLevel {
bytes = append([]byte("DEBUG: "), bytes...)
}
return bytes, nil
}