Skip to content

Instantly share code, notes, and snippets.

@k-nishijima
Created May 3, 2020 03:22
Show Gist options
  • Save k-nishijima/532ff8dc491333e0d97a12203cedbdb8 to your computer and use it in GitHub Desktop.
Save k-nishijima/532ff8dc491333e0d97a12203cedbdb8 to your computer and use it in GitHub Desktop.
adiary2md
package main
import (
"encoding/xml"
"fmt"
"io/ioutil"
"os"
"strconv"
"strings"
"time"
)
// convert via https://www.onlinetool.io/xmltogo/
type Diary struct {
XMLName xml.Name `xml:"diary"`
Text string `xml:",chardata"`
Day []struct {
Text string `xml:",chardata"`
Date string `xml:"date,attr"`
Title string `xml:"title,attr"`
Attributes struct {
Text string `xml:",chardata"`
Adiary string `xml:"adiary,attr"`
Pkey string `xml:"pkey,attr"`
Enable string `xml:"enable,attr"`
Tm string `xml:"tm,attr"`
AllowHcom string `xml:"allow_hcom,attr"`
AllowCom string `xml:"allow_com,attr"`
Tags string `xml:"tags,attr"`
Parser string `xml:"parser,attr"`
LinkKey string `xml:"link_key,attr"`
Ctype string `xml:"ctype,attr"`
Priority string `xml:"priority,attr"`
Upnode string `xml:"upnode,attr"`
Name string `xml:"name,attr"`
ID string `xml:"id,attr"`
Ip string `xml:"ip,attr"`
Host string `xml:"host,attr"`
Agent string `xml:"agent,attr"`
} `xml:"attributes"`
Body string `xml:"body"`
Comments struct {
Text string `xml:",chardata"`
Comment []struct {
Text string `xml:",chardata"`
Enable string `xml:"enable"`
Hidden string `xml:"hidden"`
ID string `xml:"id"`
Username string `xml:"username"`
Num string `xml:"num"`
Timestamp string `xml:"timestamp"`
Email string `xml:"email"`
URL string `xml:"url"`
Ip string `xml:"ip"`
Host string `xml:"host"`
Agent string `xml:"agent"`
Body string `xml:"body"`
} `xml:"comment"`
} `xml:"comments"`
} `xml:"day"`
}
const (
hugoDateFormat = "2006-01-02T15:04:05-07:00"
imgSrcPrefix = "pub/admin/image/adiary/"
imgDestPrefix = "/images/mixi/"
)
func initDaiary(fileName string) *Diary {
xmlBody, err := ioutil.ReadFile(fileName)
if err != nil {
panic(err)
}
diary := new(Diary)
if err := xml.Unmarshal(xmlBody, diary); err != nil {
panic(err)
}
return diary
}
func write(filename, title, dateStr, comments, body string) error {
content := fmt.Sprintf(`---
title: "%s"
date: "%s"
%s
---
%s
`, title, dateStr, comments, body)
return ioutil.WriteFile(filename, []byte(content), os.ModePerm)
}
func formatDate(tm string) string {
num, _ := strconv.ParseInt(tm, 10, 64)
date := time.Unix(num, 0)
return date.Format(hugoDateFormat)
}
func createFileName(targetDir, dateStr string) string {
t, _ := time.Parse(hugoDateFormat, dateStr)
tmp := targetDir + "/mixi-" + t.Format("2006-01-02.md")
if !existsFile(tmp) {
return tmp
}
for i := 1; i < 100; i++ {
tmp := targetDir + "/mixi-" + t.Format(fmt.Sprintf("2006-01-02-%s.md", strconv.Itoa(i)))
if !existsFile(tmp) {
return tmp
}
}
panic("createFileName failed.")
}
func existsFile(filename string) bool {
_, err := os.Stat(filename)
return err == nil
}
func main() {
if os.Args[1] == "" || os.Args[2] == "" {
fmt.Println("usage : go run main.go xmlfile.xml convertDirName")
os.Exit(2)
}
diary := initDaiary(os.Args[1])
targetDir := os.Args[2]
if err := os.Mkdir(targetDir, 0777); err != nil {
panic(err)
}
for _, d := range diary.Day {
dateStr := formatDate(d.Attributes.Tm)
comments := ""
fmt.Printf("%s | %s\n", dateStr, d.Title)
if len(d.Comments.Comment) > 0 {
for _, c := range d.Comments.Comment {
cmtDateStr := formatDate(c.Timestamp)
fmt.Printf("\t%s | %s | %s\n", cmtDateStr, c.Username, c.Body)
commentStr := fmt.Sprintf(`- date: "%s"
user: "%s"
body: "%s"
`, cmtDateStr, c.Username, c.Body)
comments += commentStr
}
comments = fmt.Sprintf("comments:\n%s", comments)
}
body := strings.ReplaceAll(d.Body, imgSrcPrefix, imgDestPrefix)
targetFile := createFileName(targetDir, dateStr)
if err := write(targetFile, d.Title, dateStr, comments, body); err != nil {
panic(err)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment