~abyxcos/smallblog

f4b95c0c53a8e5a1154ee827e1ec4d58b091fbd2 — abyxcos 4 years ago c7f2f96
Allow a specific path to be passed in as the directory to read files from. This cleaned up a bit of the path code, but it still needs a solid going over with Pathify.
1 files changed, 26 insertions(+), 6 deletions(-)

M smallblog.go
M smallblog.go => smallblog.go +26 -6
@@ 8,6 8,7 @@ import (
	"path/filepath"
	"regexp"
	"sort"
	"strings"
	"text/template"
	"time"



@@ 20,8 21,12 @@ import (
var (
	configPath  = flag.String("c", "templates/site.conf", "Path to your config file")
	templateDir = flag.String("templates", "templates/", "Path to your templates directory")
	outputDir   = flag.String("o", "", "Path to place your generated files in case you don't want to co-mingle source and generated html")
	outputDir   = flag.String("o", "./", "Path to place your generated files in case you don't want to co-mingle source and generated html")
	altIndex    = flag.String("index", "index.html", "Path and filename for the generated index file in case you want to provide your own landing page but still want to keep the index in a separate folder/location")

	// Base directory to read from is not a flag, but argument 0
	// after all the flags have been parsed.
	baseDir string
)

// Define our config structure


@@ 36,8 41,6 @@ type Config struct {

// A few regexes we will use later
var (
	MarkdownFilterRE = regexp.MustCompile(`(.*).md$`)

	PostTitleRE  = regexp.MustCompile(`^# (.*)`)
	PostDateRE   = regexp.MustCompile(`(?i)^date: (.*)`)
	PostAuthorRE = regexp.MustCompile(`(?i)^author: (.*)`)


@@ 201,6 204,7 @@ func renderTemplate(fileName string, tmplFile string, data interface{}) {
	}
}

//
// smallblog.go
func main() {
	var err error


@@ 208,8 212,20 @@ func main() {
	var posts Posts
	tags := make(map[string]Posts)

	// Parse any flags we were passed
	flag.Parse()
	// Check if we got any arguments. Our only argument
	// should be a path to read from.
	if len(flag.Args()) > 1 {
		flag.PrintDefaults()
		log.Fatal("Too many arguments")
	} else if len(flag.Args()) == 1 {
		baseDir = flag.Arg(0)
	} else {
		baseDir = "./"
	}

	// Find and parse our config file
	_, err = toml.DecodeFile(*configPath, &config)
	if err != nil {
		log.Print("Configuration file error:")


@@ 229,9 245,9 @@ func main() {
	}

	// Collect and parse all relevant files
	err = filepath.Walk(".", func(path string, info os.FileInfo, err error) error {
	err = filepath.Walk(baseDir, func(path string, info os.FileInfo, err error) error {
		// Ignore non-markdown files. We can't do much with them.
		if !MarkdownFilterRE.MatchString(info.Name()) {
		if filepath.Ext(info.Name()) != ".md" {
			return nil
		}



@@ 245,7 261,8 @@ func main() {

		// Generate the standalone page while we're here
		// Save our filename/path because we'll need it later
		post.FileName = MarkdownFilterRE.ReplaceAllString(path, "$1.html")
		post.FileName = strings.TrimSuffix(path, ".md") + ".html"
		post.FileName = strings.TrimPrefix(post.FileName, baseDir)
		renderTemplate(post.FileName, "single_post.tmpl", Content{
			Config: config,
			Title:  post.Title,


@@ 265,6 282,9 @@ func main() {
	if err != nil {
		log.Fatal(err)
	}
	if len(posts) == 0 {
		log.Fatal("No input files, exiting now.")
	}

	// Sort our posts from newest to oldest
	sort.Sort(posts)