xuite_blog_to_hugo_gem 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 43f70bd2e068e61196adf229f70c480fbd00b5a416ed76713188e17248d5ec52
4
+ data.tar.gz: 5039821a6fa8b9ff5e123bcdcdadc08f0cc1669e8126c49c57d46124d2a4bfce
5
+ SHA512:
6
+ metadata.gz: e558cc92dc4670f08416fa57437f94db38a1795eaf79ad079a40166e550b6b6033ff61ab1644395749b64e277810c7d05aec456c6129453e0aa16de7bb621c8e
7
+ data.tar.gz: 1e52de9342f117da4550ccca08a73b3ee840cddd40bd1f954d7a0eb271b74bb1f41258fce870d457f95201d20550b16f7f33f66db2ef41dcd3f6fc0d07fcf6c0
@@ -0,0 +1,4 @@
1
+ #!/usr/bin/env ruby
2
+ require 'xuite_blog_to_hugo_gem'
3
+
4
+ XuiteBlogToHugoGem
@@ -0,0 +1,175 @@
1
+ # encoding: utf-8
2
+ # author: howardsun
3
+ # Date: 2019/12/16
4
+ # Transform exported M.T. file(Movable type file) of xuite blog to be markdown-type file of hugo static site generator with liva-hugo theme posts.
5
+
6
+ require 'reverse_markdown'
7
+ require 'date'
8
+ require 'yaml'
9
+
10
+ module XuiteBlogToHugoGem
11
+
12
+ if ARGV.length != 1
13
+ # 如果命令列不符合只有一個參數的條件,一律不執行本程式後續內容,直接結束
14
+ exit
15
+ end
16
+
17
+ if File.exist?(ARGV[0]) == false
18
+ # 如果命令列的第一個參數表示的檔案不存在,一律不執行本程式後續內容,直接結束
19
+ exit
20
+ end
21
+
22
+ # load input file
23
+ input_file = File.open(ARGV[0], "r")
24
+ #input_file = File.open('./input/sample-input.txt_', "r")
25
+ raw_content = input_file.read
26
+ input_file.close
27
+
28
+
29
+ # Notice: need to save input file as LF as line separator. If it is CRLF, convert it to LF.
30
+ if (raw_content.match?(/\r\n/u))
31
+ raw_content.gsub!(/\r\n/u, "\n")
32
+ end
33
+
34
+ if (raw_content.match?(/\r/u))
35
+ # replace \r with \n
36
+ raw_content.gsub!(/\r/u, "\n")
37
+ end
38
+
39
+
40
+ # need append file first 8 bytes with "-" character
41
+ raw_content.prepend("--------\n")
42
+
43
+
44
+ # split meta and body
45
+ meta = Array.new
46
+ body = Array.new
47
+
48
+
49
+
50
+ # extract meta from raw file content
51
+ meta_match = raw_content.gsub(/(?<=\-{8}).*?(?=-{4})/um)
52
+ # meta_match.with_index { |content, idx|
53
+ # puts "index #{idx} is #{content}" #debug
54
+ # }
55
+
56
+
57
+
58
+ # extract body of post from raw file content
59
+ body_match = raw_content.gsub(/(?<=BODY:\s).*?(?=\-{5})/um)
60
+ # body_match.with_index { |a,index|
61
+ # puts "index #{index} is #{a}" #debug
62
+ # }
63
+
64
+
65
+
66
+
67
+ # mapping meta to hugo post
68
+ =begin
69
+ xuite article ------- hugo post
70
+ meta : : meta
71
+ TITLE: : title
72
+ AUTHOR: <NONE>
73
+ DATE: : date
74
+ MM/DD/YYYY HH:mm:ss AM
75
+ YYYY-MM-DDTHH:mm:ss+HH:mm
76
+
77
+ CATEGORY: : categories
78
+ array
79
+
80
+ : image
81
+ : description
82
+
83
+
84
+ STATUS: : draft
85
+ publish/draft true/false
86
+
87
+ : tags
88
+ array
89
+
90
+ : type
91
+ post/feature
92
+
93
+ : highlight
94
+ true/false
95
+
96
+ =end
97
+
98
+ tmpHash = Hash.new
99
+
100
+ meta_match.with_index { |meta, idx|
101
+
102
+ # create a new hash for each post
103
+ tmpHash[idx] = Hash.new
104
+
105
+ # TITLE to title
106
+ tmpHash[idx]["title"] = /(?<=TITLE:\s).+/u.match(meta).to_s
107
+
108
+ # DATE to date
109
+ tmpHash[idx]['date'] = DateTime.strptime(/(?<=DATE:\s).+/u.match(meta).to_s.concat(' +08'), '%m/%d/%Y %I:%M:%S %p %z').to_s
110
+
111
+ # CATEGORY to categories
112
+ tmpHash[idx]['categories'] = [/(?<=CATEGORY:\s).+/u.match(meta).to_s]
113
+
114
+ # gen image meta
115
+ tmpHash[idx]['image'] = ''
116
+
117
+ # gen description meta
118
+ tmpHash[idx]['description'] = tmpHash[idx]['title']
119
+
120
+ # gen draft meta
121
+ tmpDraft = /(?<=STATUS:\s).+/u.match(meta).to_s
122
+ tmpHash[idx]['draft'] = (tmpDraft.casecmp('publish') === 0)? false : true
123
+
124
+ # gen tags meta
125
+ tmpHash[idx]['tags'] = []
126
+
127
+ # gen type meta
128
+ tmpHash[idx]['type'] = 'post'
129
+
130
+ # gen highlight meta
131
+ tmpHash[idx]['highlight'] = false
132
+
133
+
134
+ }
135
+
136
+ =begin
137
+ body transformation
138
+
139
+ xuite article ------- hugo post
140
+ BODY: : --- and two newlines
141
+
142
+ =end
143
+
144
+ # tranform body to markdown content
145
+ # use reverse_markdown gem
146
+ tmpContent = Hash.new
147
+ body_match.with_index {|content, idx|
148
+
149
+ tmpContent[idx] = ReverseMarkdown.convert content
150
+
151
+ }
152
+
153
+ # Detect whether output folder is created and has right permissions or not
154
+ if Dir.exist?("./output") == false
155
+ begin
156
+ Dir.mkdir("./output")
157
+ rescue SystemCallError
158
+ $stderr.puts "Cannot create output folder, please checkout permission"
159
+ exit
160
+ end
161
+ end
162
+
163
+ # output every post a separated file
164
+ # output file name is test-timestamp.md
165
+ # Notice: f.puts \-{3} and two newline
166
+ meta_match.with_index { |_, index|
167
+ File.open("./output/#{index}.md", "w") { |f|
168
+ f.write(tmpHash[index].to_yaml)
169
+ f.puts("---", "", "", tmpContent[index])
170
+ }
171
+ }
172
+
173
+ puts "Convert complete"
174
+
175
+ end
metadata ADDED
@@ -0,0 +1,74 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: xuite_blog_to_hugo_gem
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Howard Lee
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2020-01-04 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: nokogiri
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: reverse_markdown
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ description:
42
+ email: contact-me@howardlee.cloud
43
+ executables:
44
+ - xuite_blog_to_hugo_gem
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - bin/xuite_blog_to_hugo_gem
49
+ - lib/xuite_blog_to_hugo_gem.rb
50
+ homepage: https://github.com/yhunglee/xuiteMTtoHugoMarkdown
51
+ licenses:
52
+ - MIT
53
+ metadata: {}
54
+ post_install_message:
55
+ rdoc_options: []
56
+ require_paths:
57
+ - lib
58
+ required_ruby_version: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - ">="
61
+ - !ruby/object:Gem::Version
62
+ version: '0'
63
+ required_rubygems_version: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - ">="
66
+ - !ruby/object:Gem::Version
67
+ version: '0'
68
+ requirements: []
69
+ rubygems_version: 3.0.4
70
+ signing_key:
71
+ specification_version: 4
72
+ summary: xuite_blog_to_hugo_gem is the converter from xuite blog .mt file to hugo
73
+ .md
74
+ test_files: []