-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpost.php
144 lines (122 loc) · 4.29 KB
/
post.php
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
135
136
137
138
139
140
141
142
143
144
<?php
$contents_all = json_decode(file_get_contents('typecho_contents.json'), true)[2]['data'];
$metas_all = json_decode(file_get_contents('typecho_metas.json'), true)[2]['data'];
$relationships_all = json_decode(file_get_contents('typecho_relationships.json'), true)[2]['data'];
$repo = "AyagawaSeirin/Blog@gh-pages";
$post_sort = [];//文章数据合集
$pid_list = [];//文章id合集
//文章基础信息载入
foreach ($contents_all as $value) {
if ($value['type'] != 'post' && $value['type'] != 'post_draft') continue;
$pid = $value['cid'];
if ($value['type'] == 'post_draft') {
$status = 'draft';
} else {
$status = $value['status'];
}
$time = date('Y-m-d H:i:s', $value['created']);
array_push($pid_list, $pid);
$post_sort[$pid] = array(
"pid" => $pid,
"title" => $value['title'],
"time" => $time,
"text" => $value['text'],
"status" => $status,
"tag" => [],
"category" => [],
);
}
//metas基础信息载入
$metas_sort = [];//metas数据合集
foreach ($metas_all as $value) {
$mid = $value['mid'];
$name = $value['name'];
$slug = $value['slug'];
$type = $value['type'];
$description = $value['description'];
$metas_sort[$mid] = array(
"mid" => $mid,
"name" => $name,
"slug" => $slug,
"type" => $type,
"description" => $description,
);
}
//处理tag和category
foreach ($relationships_all as $value) {
$pid = $value['cid'];
$mid = $value['mid'];
if (!in_array($pid, $pid_list)) continue;
$metas_now = $metas_sort[$mid];
//echo $metas_now['type'];
array_push($post_sort[$pid][$metas_now['type']], $metas_now['slug']);
}
//print_r($post_sort);
//开始写出文件
foreach ($post_sort as $value) {
$post_content = "---\ntitle: " . $value['title'] . "\ndate: " . $value['time'] . "\npid: " . $value['pid'];
//处理tag
$tag = "[";
foreach ($value['tag'] as $tag_now) {
$tag .= $tag_now . ",";
}
if (count($value['tag']) != 0) {
$tag = substr($tag, 0, -1);
}
$tag .= "]\n";
$post_content .= "\ntags: " . $tag;
//处理category
// $category = "";
// foreach ($value['category'] as $category_now){
// $category.="\n -[".$category_now."]";
// }
//hexo不支持同级分类,固只保留第一个分类确保URL可访问。
$category = "";
if (count($value['category']) != 0) {
$category = $value['category'][0];
}
$post_content .= "\ncategories: " . $category . "\n---\n" . $value['text'];
//选择文章写入目录
if ($value['status'] == "publish") {
$path = "./posts/";
} else {
$path = "./" . $value['status'] . "/";
}
//创建文章资源目录
mkdir($path . $value['title'],0777,true);
//处理文章图片
$pattern = "/http(.*?)uploads\/(.*?)..\.(png|jpg)/";
preg_match_all($pattern, $value['text'], $matches);
foreach ($matches[0] as $img_now) {
//先取出文件名
$array_url = explode("/", $img_now);
$img_name = array_pop($array_url);
//判断文件是否为本博文件
$right = getSubstr($img_now, "uploads");
$img_path = "./usr/uploads" . $right;
$new_path = $path . $value['title'] . "/" . $img_name;
if (file_exists($img_path)) {
//是本博文件,直接复制
copy($img_path, $new_path);
} else {
//其他网站的图片,下载下来
file_put_contents($new_path,file_get_contents($img_now));
}
//替换文章中的图片url
$img_real = "/".$category."/".$value['pid']."/".$img_name;
// 方案1:利用post_asset_folder(hexo-asset-image)构造静态文件时处理图片url,直接从仓库读取
// $post_content = str_replace($img_now,$img_name,$post_content);
// 方案2:直接上JsDelivr
$post_content = str_replace($img_now,"https://cdn.jsdelivr.net/gh/".$repo.$img_real,$post_content);
//写入文件
$file = fopen($path . $value['title'] . ".md", "w");
fwrite($file, $post_content);
}
echo "Complete: ".$value['title']."\n";
}
echo "All files processed!";
function getSubstr($str, $leftStr)
{
$left = strpos($str, $leftStr);
return substr($str, $left + strlen($leftStr));
}