File tree Expand file tree Collapse file tree 7 files changed +90
-6
lines changed Expand file tree Collapse file tree 7 files changed +90
-6
lines changed Original file line number Diff line number Diff line change 1
1
PATH
2
2
remote: .
3
3
specs:
4
- movable_type_format (0.1.0 )
4
+ movable_type_format (0.1.1 )
5
5
activemodel (~> 3.2.1 )
6
6
7
7
GEM
8
8
remote: https://rubygems.org/
9
9
specs:
10
- activemodel (3.2.22 )
11
- activesupport (= 3.2.22 )
10
+ activemodel (3.2.22.5 )
11
+ activesupport (= 3.2.22.5 )
12
12
builder (~> 3.0.0 )
13
- activesupport (3.2.22 )
13
+ activesupport (3.2.22.5 )
14
14
i18n (~> 0.6 , >= 0.6.4 )
15
15
multi_json (~> 1.0 )
16
16
builder (3.0.4 )
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change @@ -7,5 +7,9 @@ def to_mt
7
7
def +( another )
8
8
self . class . new ( super )
9
9
end
10
+
11
+ def serialize
12
+ to_a . map ( &:serialize )
13
+ end
10
14
end
11
15
end
Original file line number Diff line number Diff line change @@ -12,6 +12,25 @@ def sections
12
12
@sections ||= Collection . new
13
13
end
14
14
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
+
15
34
Section ::Base ::NAMES_OF_SINGLE_SECTION . each do |section_name |
16
35
name = section_name . downcase . gsub ( / / , "_" )
17
36
define_method name do
@@ -51,7 +70,7 @@ def metadata=(v)
51
70
end
52
71
53
72
def comments
54
- sections . select { |s | s . name == "COMMENT" } . freeze
73
+ Collection . new ( sections . select { |s | s . name == "COMMENT" } ) . freeze
55
74
end
56
75
57
76
def comments = ( v )
@@ -60,7 +79,7 @@ def comments=(v)
60
79
end
61
80
62
81
def pings
63
- sections . select { |s | s . name == "PING" } . freeze
82
+ Collection . new ( sections . select { |s | s . name == "PING" } ) . freeze
64
83
end
65
84
66
85
def pings = ( v )
Original file line number Diff line number Diff line change @@ -57,6 +57,13 @@ def name
57
57
@name
58
58
end
59
59
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
+
60
67
def to_mt
61
68
buffer = ""
62
69
buffer << "#{ name } :\n " unless metadata?
Original file line number Diff line number Diff line change @@ -6,6 +6,18 @@ class Comment < Base
6
6
def initialize ( fields = Collection . new , body = nil )
7
7
super "COMMENT" , fields , body
8
8
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
9
21
end
10
22
end
11
23
end
Original file line number Diff line number Diff line change @@ -6,6 +6,18 @@ class Ping < Base
6
6
def initialize ( fields = Collection . new , body = nil )
7
7
super "PING" , fields , body
8
8
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
9
21
end
10
22
end
11
23
end
You can’t perform that action at this time.
0 commit comments