Created
March 2, 2014 03:58
-
-
Save antzucaro/9301687 to your computer and use it in GitHub Desktop.
Blogofile conversion
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"bufio" | |
"fmt" | |
"os" | |
"path/filepath" | |
"strings" | |
"strconv" | |
"time" | |
) | |
type Post struct { | |
Title string | |
Date time.Time | |
Categories []string | |
Slug string | |
Content string | |
} | |
const BASE_DIR = "/home/ant/blog_conversion" | |
func getMDFiles(path string) []string { | |
mdFiles := make([]string, 0, 100) | |
var wf = func(path string, info os.FileInfo, err error) error { | |
if strings.HasSuffix(path, "markdown") { | |
mdFiles = append(mdFiles, path) | |
} | |
return nil | |
} | |
filepath.Walk(path, wf) | |
return mdFiles | |
} | |
func parsePost(fn string) Post { | |
fmt.Println("Opening " + fn + "...") | |
f, err := os.Open(fn) | |
if err != nil { | |
panic(err) | |
} | |
defer f.Close() | |
r := bufio.NewReader(f) | |
post := Post{} | |
l, err := r.ReadString('\n') | |
inYAML := false | |
for err == nil { | |
if strings.HasPrefix(l, "---") { | |
if inYAML { | |
inYAML = false | |
} else { | |
inYAML = true | |
} | |
} | |
if inYAML { | |
l = l[0:len(l)-1] // trim | |
if strings.HasPrefix(l, "title: ") { | |
post.Title = l[7:] | |
} | |
if strings.HasPrefix(l, "date: ") { | |
tString := l[6:] + " EST" | |
t, err := time.Parse("2006/01/02 15:04:05 MST", tString) | |
if err == nil { | |
post.Date = t | |
} | |
} | |
if strings.HasPrefix(l, "categories: ") { | |
l = l[12:] | |
post.Categories = strings.Split(l, ",") | |
// in case there are extra spaces after splitting | |
for i, v := range(post.Categories) { | |
post.Categories[i] = strings.Trim(v, " ") | |
} | |
} | |
if strings.HasPrefix(l, "permalink: ") { | |
post.Slug = filepath.Base(l[11:]) | |
} | |
} else { | |
if !strings.HasPrefix(l, "---") { | |
// if we're not in the YAML, we must be down in the content! | |
post.Content += l | |
} | |
} | |
l, err = r.ReadString('\n') | |
} | |
return post | |
} | |
func convertPostHugo(post Post) { | |
// hugo-specific conversion | |
post.Content = strings.Replace(post.Content, "http://antzucaro.com", "", -1) | |
post.Content = strings.Replace(post.Content, "http://www.antzucaro.com", "", -1) | |
post.Content = strings.Replace(post.Content, "http://media.antzucaro.com", "", -1) | |
// make the directory | |
year := strconv.Itoa(post.Date.Year()) | |
directory := BASE_DIR + "/hugo/" + year | |
os.MkdirAll(directory, os.FileMode(0755)) | |
// now make the actual post markdown file | |
filename := post.Slug + ".md" | |
path := directory + "/" + filename | |
f, err := os.Create(path) | |
if err != nil { | |
panic(err) | |
} | |
defer f.Close() | |
w := bufio.NewWriter(f) | |
fmt.Fprintln(w, "---") | |
fmt.Fprintln(w, "title: " + "\"" + post.Title + "\"") | |
fmt.Fprintln(w, "date: " + "\"" + post.Date.Format("2006-01-02") + "\"") | |
fmt.Fprintln(w, "categories: ") | |
for _, v := range(post.Categories) { | |
fmt.Fprintln(w, " - \"" + v + "\"") | |
} | |
fmt.Fprintln(w, "---") | |
fmt.Fprint(w, post.Content) | |
w.Flush() | |
fmt.Println("Converted post " + post.Title + ".") | |
} | |
func convertPostPelican(post Post) { | |
// make the directory | |
year := strconv.Itoa(post.Date.Year()) | |
directory := BASE_DIR + "/pelican/" + year | |
os.MkdirAll(directory, os.FileMode(0755)) | |
// now make the actual post markdown file | |
filename := post.Slug + ".md" | |
path := directory + "/" + filename | |
f, err := os.Create(path) | |
if err != nil { | |
panic(err) | |
} | |
defer f.Close() | |
w := bufio.NewWriter(f) | |
fmt.Fprintln(w, "Title: " + post.Title) | |
fmt.Fprintln(w, "Date: " + post.Date.Format("2006-01-02")) | |
fmt.Fprint(w, "Tags: ") | |
for i, v := range(post.Categories) { | |
fmt.Fprint(w, v) | |
if i != len(post.Categories) - 1 { | |
fmt.Fprint(w, ", ") | |
} else { | |
fmt.Fprint(w, "\n") | |
} | |
} | |
fmt.Fprintln(w, "Category: blog") | |
fmt.Fprintln(w, "Slug: " + post.Slug) | |
fmt.Fprint(w, "\n") | |
fmt.Fprint(w, post.Content) | |
w.Flush() | |
fmt.Println("Converted post " + post.Title + ".") | |
} | |
func main() { | |
mds := getMDFiles(".") | |
postList := make([]Post, 0, 100) | |
for _, fn := range(mds) { | |
post := parsePost(fn) | |
postList = append(postList, post) | |
} | |
for _, post := range(postList) { | |
hasFamily := false | |
// we're not moving the family posts over. sorry! | |
for _, v := range(post.Categories) { | |
if v == "family" { | |
hasFamily = true | |
} | |
} | |
if !hasFamily { | |
convertPostHugo(post) | |
convertPostPelican(post) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment