Skip to content

Commit 8f776e5

Browse files
committed
add mt2json command
1 parent 853875c commit 8f776e5

File tree

7 files changed

+90
-6
lines changed

7 files changed

+90
-6
lines changed

Gemfile.lock

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
PATH
22
remote: .
33
specs:
4-
movable_type_format (0.1.0)
4+
movable_type_format (0.1.1)
55
activemodel (~> 3.2.1)
66

77
GEM
88
remote: https://rubygems.org/
99
specs:
10-
activemodel (3.2.22)
11-
activesupport (= 3.2.22)
10+
activemodel (3.2.22.5)
11+
activesupport (= 3.2.22.5)
1212
builder (~> 3.0.0)
13-
activesupport (3.2.22)
13+
activesupport (3.2.22.5)
1414
i18n (~> 0.6, >= 0.6.4)
1515
multi_json (~> 1.0)
1616
builder (3.0.4)

exe/mt2json

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#!/usr/bin/env ruby
2+
require "movable_type_format"
3+
require "optparse"
4+
require "json"
5+
6+
def sort_hash_keys(obj)
7+
case obj
8+
when Hash
9+
obj.keys.sort.inject({}){|h, k| h[k] = sort_hash_keys(obj[k]); h }
10+
when Array
11+
obj.map{|a| sort_hash_keys(a) }
12+
else
13+
obj
14+
end
15+
end
16+
17+
options = {
18+
compact_output: false,
19+
}
20+
OptionParser.new do |o|
21+
o.on("-c", "--compact-output"){|b| options[:compact_output] = b }
22+
o.parse!(ARGV)
23+
end
24+
25+
entries = MovableTypeFormat::Parser.parse(ARGF).map{|e| sort_hash_keys(e.serialize) }
26+
if options[:compact_output]
27+
puts JSON.generate(entries)
28+
else
29+
puts JSON.pretty_generate(entries)
30+
end

lib/movable_type_format/collection.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,9 @@ def to_mt
77
def +(another)
88
self.class.new(super)
99
end
10+
11+
def serialize
12+
to_a.map(&:serialize)
13+
end
1014
end
1115
end

lib/movable_type_format/entry.rb

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,25 @@ def sections
1212
@sections ||= Collection.new
1313
end
1414

15+
def serialize
16+
serialized = {}
17+
18+
( Section::Base::NAMES_OF_SINGLE_SECTION +
19+
MovableTypeFormat::Field::KEYS_FOR_METADATA).each do |s|
20+
next if s == "CATEGORY"
21+
method = s.downcase.gsub(/ /, "_")
22+
if v = send(method)
23+
serialized[method] = v
24+
end
25+
end
26+
27+
serialized["categories"] = categories if categories.any?
28+
serialized["comments"] = comments.serialize if comments.any?
29+
serialized["pings"] = pings.serialize if pings.any?
30+
31+
serialized
32+
end
33+
1534
Section::Base::NAMES_OF_SINGLE_SECTION.each do |section_name|
1635
name = section_name.downcase.gsub(/ /, "_")
1736
define_method name do
@@ -51,7 +70,7 @@ def metadata=(v)
5170
end
5271

5372
def comments
54-
sections.select{|s| s.name == "COMMENT" }.freeze
73+
Collection.new(sections.select{|s| s.name == "COMMENT" }).freeze
5574
end
5675

5776
def comments=(v)
@@ -60,7 +79,7 @@ def comments=(v)
6079
end
6180

6281
def pings
63-
sections.select{|s| s.name == "PING" }.freeze
82+
Collection.new(sections.select{|s| s.name == "PING" }).freeze
6483
end
6584

6685
def pings=(v)

lib/movable_type_format/section/base.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,13 @@ def name
5757
@name
5858
end
5959

60+
def serialize
61+
{ "name" => name,
62+
"fields" => fields.inject({}){|h, f| h[f.key] = f.value; h },
63+
"body" => body,
64+
}
65+
end
66+
6067
def to_mt
6168
buffer = ""
6269
buffer << "#{name}:\n" unless metadata?

lib/movable_type_format/section/comment.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,18 @@ class Comment < Base
66
def initialize(fields = Collection.new, body = nil)
77
super "COMMENT", fields, body
88
end
9+
10+
def serialize
11+
serialized = {}
12+
MovableTypeFormat::Field::KEYS_FOR_COMMENT.each do |s|
13+
method = s.downcase.gsub(/ /, "_")
14+
if v = send(method)
15+
serialized[method] = v
16+
end
17+
end
18+
serialized["body"] = body
19+
serialized
20+
end
921
end
1022
end
1123
end

lib/movable_type_format/section/ping.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,18 @@ class Ping < Base
66
def initialize(fields = Collection.new, body = nil)
77
super "PING", fields, body
88
end
9+
10+
def serialize
11+
serialized = {}
12+
MovableTypeFormat::Field::KEYS_FOR_PING.each do |s|
13+
method = s.downcase.gsub(/ /, "_")
14+
if v = send(method)
15+
serialized[method] = v
16+
end
17+
end
18+
serialized["body"] = body
19+
serialized
20+
end
921
end
1022
end
1123
end

0 commit comments

Comments
 (0)