Skip to content

Commit

Permalink
add mt2json command
Browse files Browse the repository at this point in the history
  • Loading branch information
labocho committed Nov 10, 2016
1 parent 853875c commit 8f776e5
Show file tree
Hide file tree
Showing 7 changed files with 90 additions and 6 deletions.
8 changes: 4 additions & 4 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
PATH
remote: .
specs:
movable_type_format (0.1.0)
movable_type_format (0.1.1)
activemodel (~> 3.2.1)

GEM
remote: https://rubygems.org/
specs:
activemodel (3.2.22)
activesupport (= 3.2.22)
activemodel (3.2.22.5)
activesupport (= 3.2.22.5)
builder (~> 3.0.0)
activesupport (3.2.22)
activesupport (3.2.22.5)
i18n (~> 0.6, >= 0.6.4)
multi_json (~> 1.0)
builder (3.0.4)
Expand Down
30 changes: 30 additions & 0 deletions exe/mt2json
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#!/usr/bin/env ruby
require "movable_type_format"
require "optparse"
require "json"

def sort_hash_keys(obj)
case obj
when Hash
obj.keys.sort.inject({}){|h, k| h[k] = sort_hash_keys(obj[k]); h }
when Array
obj.map{|a| sort_hash_keys(a) }
else
obj
end
end

options = {
compact_output: false,
}
OptionParser.new do |o|
o.on("-c", "--compact-output"){|b| options[:compact_output] = b }
o.parse!(ARGV)
end

entries = MovableTypeFormat::Parser.parse(ARGF).map{|e| sort_hash_keys(e.serialize) }
if options[:compact_output]
puts JSON.generate(entries)
else
puts JSON.pretty_generate(entries)
end
4 changes: 4 additions & 0 deletions lib/movable_type_format/collection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,9 @@ def to_mt
def +(another)
self.class.new(super)
end

def serialize
to_a.map(&:serialize)
end
end
end
23 changes: 21 additions & 2 deletions lib/movable_type_format/entry.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,25 @@ def sections
@sections ||= Collection.new
end

def serialize
serialized = {}

( Section::Base::NAMES_OF_SINGLE_SECTION +
MovableTypeFormat::Field::KEYS_FOR_METADATA).each do |s|
next if s == "CATEGORY"
method = s.downcase.gsub(/ /, "_")
if v = send(method)
serialized[method] = v
end
end

serialized["categories"] = categories if categories.any?
serialized["comments"] = comments.serialize if comments.any?
serialized["pings"] = pings.serialize if pings.any?

serialized
end

Section::Base::NAMES_OF_SINGLE_SECTION.each do |section_name|
name = section_name.downcase.gsub(/ /, "_")
define_method name do
Expand Down Expand Up @@ -51,7 +70,7 @@ def metadata=(v)
end

def comments
sections.select{|s| s.name == "COMMENT" }.freeze
Collection.new(sections.select{|s| s.name == "COMMENT" }).freeze
end

def comments=(v)
Expand All @@ -60,7 +79,7 @@ def comments=(v)
end

def pings
sections.select{|s| s.name == "PING" }.freeze
Collection.new(sections.select{|s| s.name == "PING" }).freeze
end

def pings=(v)
Expand Down
7 changes: 7 additions & 0 deletions lib/movable_type_format/section/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,13 @@ def name
@name
end

def serialize
{ "name" => name,
"fields" => fields.inject({}){|h, f| h[f.key] = f.value; h },
"body" => body,
}
end

def to_mt
buffer = ""
buffer << "#{name}:\n" unless metadata?
Expand Down
12 changes: 12 additions & 0 deletions lib/movable_type_format/section/comment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,18 @@ class Comment < Base
def initialize(fields = Collection.new, body = nil)
super "COMMENT", fields, body
end

def serialize
serialized = {}
MovableTypeFormat::Field::KEYS_FOR_COMMENT.each do |s|
method = s.downcase.gsub(/ /, "_")
if v = send(method)
serialized[method] = v
end
end
serialized["body"] = body
serialized
end
end
end
end
12 changes: 12 additions & 0 deletions lib/movable_type_format/section/ping.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,18 @@ class Ping < Base
def initialize(fields = Collection.new, body = nil)
super "PING", fields, body
end

def serialize
serialized = {}
MovableTypeFormat::Field::KEYS_FOR_PING.each do |s|
method = s.downcase.gsub(/ /, "_")
if v = send(method)
serialized[method] = v
end
end
serialized["body"] = body
serialized
end
end
end
end

0 comments on commit 8f776e5

Please sign in to comment.