Created
April 7, 2014 07:21
-
-
Save sudix/10016019 to your computer and use it in GitHub Desktop.
SpringSeedというエディタのjsonをmakrdownに変換する。
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 ( | |
"encoding/json" | |
"fmt" | |
"io/ioutil" | |
"os" | |
"path/filepath" | |
"strings" | |
) | |
const ( | |
SpringSeedDir = "/home/sudix/Springseed" | |
MarkDownDir = "/home/sudix/Springseed/markdown" | |
) | |
type Article struct { | |
Name string `json:"name"` | |
Content string `json:"content"` | |
Notebook string `json:"notebook"` | |
Id string `json:"id"` | |
Date int64 `json:"date"` | |
} | |
func parseFile(filePath string) (Article, error) { | |
var article Article | |
b, err := ioutil.ReadFile(filePath) | |
if err != nil { | |
fmt.Errorf("Error! %s\n", err) | |
return article, err | |
} | |
if err := json.Unmarshal(b, &article); err != nil { | |
fmt.Errorf("Error! %s\n", err) | |
return article, err | |
} | |
return article, nil | |
} | |
func writeMarkDown(filePath string, article Article) error { | |
err := ioutil.WriteFile(filePath, []byte(article.Content), 0644) | |
if err != nil { | |
return err | |
} | |
return nil | |
} | |
func main() { | |
fileInfos, err := ioutil.ReadDir(SpringSeedDir) | |
if err != nil { | |
fmt.Errorf("Error! %s\n", err) | |
os.Exit(1) | |
} | |
for _, fileInfo := range fileInfos { | |
if fileInfo.IsDir() { | |
continue | |
} | |
name := fileInfo.Name() | |
if !strings.Contains(name, ".note") { | |
continue | |
} | |
fmt.Println(filepath.Join(SpringSeedDir, name)) | |
article, err := parseFile(filepath.Join(SpringSeedDir, name)) | |
if err != nil { | |
fmt.Println(err) | |
fmt.Errorf("Error! %s\n", err) | |
os.Exit(1) | |
} | |
fileBase := strings.ToLower( | |
strings.Replace(article.Name, " ", "_", -1)) + ".md" | |
fmt.Println(fileBase) | |
mdFilePath := filepath.Join(OutDir, fileBase) | |
writeMarkDown(mdFilePath, article) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment