Last active
October 5, 2015 07:53
-
-
Save conoro/c05d40f524bac5462517 to your computer and use it in GitHub Desktop.
HarpJS to Hugo Blog Migrator
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
/* | |
This tool takes a blog built on Harp.js (http://harpjs.com/) with the Baseline | |
theme (https://github.com/rosshj/baseline) and converts it into something | |
usable by Hugo (https://gohugo.io/). It will not work out of the box for you, | |
as it has some customisations to deal with mixed trailing/non-trailing slashes | |
and mixed use of directories for posts. This is what happens when you go from | |
Posterous to WordPress to Harp to Hugo! I also changed the format of _data.json | |
to put all the posts in an array to make it easier to parse before running | |
this tool. But it should be very easy to modify everything to suit your needs. | |
Just run it in the directory with all the Markdown source. | |
The MIT License (MIT) | |
Copyright (c) 2015 Conor O'Neill ([email protected]) | |
Permission is hereby granted, free of charge, to any person obtaining a copy | |
of this software and associated documentation files (the "Software"), to deal | |
in the Software without restriction, including without limitation the rights | |
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
copies of the Software, and to permit persons to whom the Software is | |
furnished to do so, subject to the following conditions: | |
The above copyright notice and this permission notice shall be included in | |
all copies or substantial portions of the Software. | |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
THE SOFTWARE. | |
*/ | |
package main | |
import ( | |
"encoding/json" | |
"fmt" | |
"io/ioutil" | |
"os" | |
"path/filepath" | |
"strings" | |
"time" | |
) | |
type Feed struct { | |
Layout bool | |
} | |
type Sitemap struct { | |
Layout bool | |
} | |
type CNAME struct { | |
Layout bool | |
} | |
type Entry struct { | |
ID int | |
Author string | |
Date int64 | |
Ptype string | |
Description string | |
Slug string | |
Status string | |
Title string | |
FBImage string `json:"FBImage,omitempty"` | |
} | |
type Config struct { | |
Feed Feed | |
Sitemap Sitemap | |
CNAME CNAME | |
Entrys []Entry | |
} | |
var conf Config | |
func visit(path string, f os.FileInfo, err error) error { | |
var slug string | |
if strings.HasSuffix(path, ".md") { | |
if strings.HasSuffix(path, "index.md") { | |
slug = filepath.Base(filepath.Dir(path)) | |
//fmt.Printf("index.md Visited: %s\n", slug) | |
} else { | |
slug = filepath.Base(path)[:len(filepath.Base(path))-3] | |
//fmt.Printf("Non index.md Visited: %s\n", slug) | |
} | |
} else { | |
// fmt.Printf("Non Markdown Visited: %s\n", path) | |
} | |
if len(slug) > 0 { | |
for _, post := range conf.Entrys { | |
if post.Slug == slug { | |
outtext := "+++\n" | |
tm := time.Unix(post.Date/1000, 0) | |
outtext += "date = \"" + tm.Format(time.RFC3339) + "\"\n" | |
outtext += "draft = false\n" | |
outtext += "title = \"" + post.Title + "\"\n" | |
outtext += "description = \"" + post.Description + "\"\n" | |
outtext += "slug = \"" + post.Slug + "\"\n" | |
outtext += "+++\n\n" | |
readfile, err := ioutil.ReadFile(path) | |
if err == nil { | |
outtext += string(readfile) | |
err := ioutil.WriteFile(path, []byte(outtext), 0644) | |
if err != nil { | |
panic(err) | |
} | |
fmt.Printf("Updated: %s\n", path) | |
} | |
} | |
} | |
} | |
return nil | |
} | |
func main() { | |
content, err := ioutil.ReadFile("_data.json") | |
if err != nil { | |
fmt.Print("Error:", err) | |
} | |
err = json.Unmarshal(content, &conf) | |
if err != nil { | |
fmt.Print("Error:", err) | |
} | |
err = filepath.Walk(".", visit) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment