-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpost_sync.rb
executable file
·41 lines (37 loc) · 986 Bytes
/
post_sync.rb
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
#!/usr/local/opt/ruby@2.3/bin/ruby
$LOAD_PATH.unshift(File.dirname(__FILE__))
require 'metaweblog'
require 'yaml'
require 'cnblogs_post'
config=YAML.load_file('post_sync.yml')
blogclient=MetaWeblog::Client.new(
config['target'],
'1',
config['username'],
config['password'],
nil
)
filename=ARGV[0]
file=File.new(filename,'r')
content=''
header=''
line_ignore=false
while (line=file.gets)
if line=="---\n"
line_ignore=!line_ignore
end
header+=line unless(!line_ignore)
line.gsub!(/"\/assets/,'"'+config['source']+'/assets')
line.gsub!(/]\(\//,']('+config['source']+'/')
content+=line unless (line_ignore||line=~/^{% include/)
end
headerYml=YAML.load(header)
puts headerYml.inspect
blog=MetaWeblog::CnblogsPost.new({
:title=>"[#{filename[filename.index('/')+1,10]}]#{headerYml['title']}",
:description=>content,
:categories=>['[Markdown]'],
:dateCreated=>Time.parse("#{filename[filename.index('/')+1,10]}")
})
result= blogclient.post(blog)
puts result