-
Notifications
You must be signed in to change notification settings - Fork 4
/
actions.go
109 lines (95 loc) · 2.43 KB
/
actions.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
/*
* wp-import imports posts from WordPress into Write.as / WriteFreely.
* Copyright © 2019, 2024 Musing Studio LLC.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*/
package wpimport
import (
"bytes"
"fmt"
"github.com/frankbille/go-wxr-import"
"github.com/writeas/go-writeas/v2"
"github.com/writeas/godown"
"github.com/writeas/wp-import/core"
"log"
"os"
"regexp"
)
var (
commentReg = regexp.MustCompile("(?m)<!-- ([^m ]|m[^o ]|mo[^r ]|mor[^e ])+ -->\n?")
)
func ImportWordPress(dstBlog string, raw []byte) error {
d := wxr.ParseWxr(raw)
log.Printf("Found %d channels.\n", len(d.Channels))
postsCount := 0
for _, ch := range d.Channels {
log.Printf("Channel: %s\n", ch.Title)
/*
// Create the blog
c := &writeas.CollectionParams{
Title: ch.Title,
Description: ch.Description,
}
log.Printf("Creating %s...\n", ch.Title)
coll, err := core.Client.CreateCollection(c)
if err != nil {
return err
}
log.Printf("Done!\n")
*/
log.Printf("Found %d items.\n", len(ch.Items))
for _, wpp := range ch.Items {
if wpp.PostType != "post" {
continue
}
// Convert to Markdown
b := bytes.NewBufferString("")
r := bytes.NewReader([]byte(wpp.Content))
err := godown.Convert(b, r, nil)
con := b.String()
// Remove unneeded WordPress comments that take up space, like <!-- wp:paragraph -->
con = commentReg.ReplaceAllString(con, "")
// Append tags
tags := ""
sep := ""
for _, cat := range wpp.Categories {
if cat.Domain != "post_tag" {
continue
}
tags += sep + "#" + cat.DisplayName
sep = " "
}
if tags != "" {
con += "\n\n" + tags
}
p := &writeas.PostParams{
Title: wpp.Title,
Slug: wpp.PostName,
Content: con,
Created: &wpp.PostDateGmt,
Updated: &wpp.PostDateGmt,
Font: "norm",
Language: &ch.Language,
Collection: dstBlog,
//Collection: coll.Alias,
}
log.Printf("Creating %s", p.Title)
_, err = core.Client.CreatePost(p)
if err != nil {
fmt.Fprintf(os.Stderr, "create post: %s\n", err)
continue
}
postsCount++
}
log.Printf("Created %d posts.\n", postsCount)
}
return nil
}
func errQuit(m string) {
fmt.Fprintf(os.Stderr, m+"\n")
os.Exit(1)
}