Created
December 6, 2015 05:08
-
-
Save suntong/0268a39d18bf3e684ff9 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
//////////////////////////////////////////////////////////////////////////// | |
// Porgram: Wp2Hugo | |
// Purpose: From wordpress meta to Hugo's | |
// Authors: Tong Sun (c) 2015, All rights reserved | |
//////////////////////////////////////////////////////////////////////////// | |
package main | |
import ( | |
"fmt" | |
"os" | |
"regexp" | |
"time" | |
) | |
import ( | |
"github.com/spakin/awk" | |
) | |
/* | |
Wp2Hugo converts wordpress's markdown meta data format into Hugo's. | |
E.g., for an input of wordpress's markdown like this, | |
# Dbab From Start To Finish | |
[category Tech][tags Debian,Ubuntu,Linux,DHCP,DNS,WPAD,dnsmasq,dbab] | |
The output Hugo's meta data is: | |
+++ | |
title = "Dbab From Start To Finish" | |
date = ["2015-12-05T23:29:19-05:00"] | |
categories = ["Tech"] | |
tags = ["Debian","Ubuntu","Linux","DHCP","DNS","WPAD","dnsmasq","dbab"] | |
menu = "" | |
+++ | |
Usage: | |
Wp2Hugo < wordpress.md > path/to/hugo.md | |
*/ | |
func main() { | |
s := awk.NewScript() | |
s.Begin = func(s *awk.Script) { | |
s.SetFS("[][ ]") | |
} | |
s.State = s.NewValue("") | |
// NR == 1 & $0 ~ /^# +/ { State = $0 } | |
s.AppendStmt(func(s *awk.Script) bool { return s.NR == 1 && s.F(0).Match("^# +") }, | |
func(s *awk.Script) { | |
s.State = s.F(0) | |
s.Next() | |
}) | |
// /^\[category/ { convert meta data format } | |
s.AppendStmt(func(s *awk.Script) bool { return s.F(0).Match("^\\[category") }, | |
func(s *awk.Script) { | |
re := regexp.MustCompile("^# +(.*)$") | |
fmt.Println(re.ReplaceAllString(s.State.(*awk.Value).String(), | |
"+++\ntitle = \"$1\"")) | |
fmt.Printf("date = \"%s\"\n", time.Now().Format(time.RFC3339)) | |
fmt.Printf("categories = [\"%s\"]\n", s.F(3)) | |
fmt.Printf("%s = [\"%s\"]\n", s.F(5), s.F(6)) | |
fmt.Println("+++\n") | |
//s.Exit() | |
s.Next() | |
}) | |
// 1; # i.e., print all | |
s.AppendStmt(nil, nil) | |
if err := s.Run(os.Stdin); err != nil { | |
panic(err) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment