-
Notifications
You must be signed in to change notification settings - Fork 0
/
export-wp.go
134 lines (118 loc) · 3.16 KB
/
export-wp.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
package main
import (
"encoding/xml"
"flag"
"fmt"
"io/ioutil"
"log"
"os"
"path/filepath"
"time"
)
func main() {
var (
outdir, xmlFilename string
localMedia string // the url for media the WP site served itself
)
flag.StringVar(&outdir, "outdir", "", "name of the output directory")
flag.StringVar(&xmlFilename, "xmlfile", "", "name of the input XML file")
flag.StringVar(&localMedia, "localmedia", "", "url of the local media section")
flag.Parse()
fmt.Println("flags:", outdir, xmlFilename)
if len(outdir) == 0 || len(xmlFilename) == 0 {
log.Fatalf("flags missing")
}
// Open our xmlFile
xmlFile, err := os.Open(xmlFilename)
if err != nil {
log.Fatalf("could not open file %s: %v", xmlFilename, err)
}
fmt.Println("Successfully opened:", xmlFilename)
defer xmlFile.Close()
byteValue, err := ioutil.ReadAll(xmlFile)
if err != nil {
log.Fatalf("could not read xml: %v", err)
}
var doc rss
err = xml.Unmarshal(byteValue, &doc)
if err != nil {
log.Fatalf("could not parse xml: %v", err)
}
renderer := contentRenderer{
transformContent: cleanContent,
}
fmt.Println("read items:", len(doc.Items))
itemsByKind := make(map[string][]item)
for _, it := range doc.Items {
_, found := itemsByKind[it.PostType]
if !found {
itemsByKind[it.PostType] = make([]item, 0)
}
itemsByKind[it.PostType] = append(itemsByKind[it.PostType], it)
}
for kind, items := range itemsByKind {
if kind == "attachment" || kind == "nav_menu_item" {
continue
}
fmt.Println(kind, len(items))
err := os.Mkdir(filepath.Join(outdir, kind), 0750)
if err != nil {
log.Fatalf("could not create dir: %v", err)
}
fmt.Println("created dir", filepath.Join(outdir, kind))
for _, it := range items {
if len(it.Slug) == 0 {
continue
}
if it.Status == "trash" || it.Status == "trashed" || it.Status == "draft" {
continue
}
name := it.Slug
if kind == "post" {
dt, err := time.Parse("2006-01-02 15:04:05", it.PostDate)
if err == nil {
name = filepath.Join(dt.Format("2006/01/02"), it.Slug)
}
}
err = os.MkdirAll(filepath.Join(outdir, kind, name), 0750)
if err != nil {
log.Fatalf("could not create dir: %v", err)
}
fmt.Println("created dir", filepath.Join(outdir, kind, name))
f, err := os.Create(filepath.Join(outdir, kind, name, "index.md"))
if err != nil {
log.Fatalf("could not create file: %v", err)
}
err = renderer.toMarkdown(it, f)
if err != nil {
log.Println("could not write post: ", err)
}
err = f.Sync()
if err != nil {
log.Println("could not flush file: ", err)
}
err = f.Close()
if err != nil {
log.Println("could not close file: ", err)
}
if len(it.Comments) > 0 {
f, err := os.Create(filepath.Join(outdir, kind, name, "comments.html"))
if err != nil {
log.Fatalf("could not create file: %v", err)
}
err = renderer.renderThreads(f, threadComments(it.Comments))
if err != nil {
log.Println("ayyyyssss", err)
}
err = f.Sync()
if err != nil {
log.Println("could not flush file: ", err)
}
err = f.Close()
if err != nil {
log.Println("could not close file: ", err)
}
}
}
}
}