Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ end

group :development, :test do
gem 'bullet'
gem 'front_matter_parser'
gem 'i18n-tasks'
gem 'mailcatcher', require: false
gem 'pry-byebug'
Expand Down
2 changes: 2 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,7 @@ GEM
thor (~> 0.14)
formatador (0.2.5)
foundation_emails (2.2.1.0)
front_matter_parser (0.1.0)
geocoder (1.4.4)
get_process_mem (0.2.1)
gibberish (2.1.0)
Expand Down Expand Up @@ -678,6 +679,7 @@ DEPENDENCIES
fasterer
figaro
foundation_emails
front_matter_parser
gibberish
guard-rspec
gyoku
Expand Down
34 changes: 31 additions & 3 deletions lib/i18n_converter.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
require 'yaml'
require 'active_support/core_ext/hash/conversions'
require 'front_matter_parser'

class I18nConverter
def initialize(stdin:, stdout:)
Expand All @@ -10,9 +11,8 @@ def initialize(stdin:, stdout:)
def xml_to_yml
return if bad_usage?(in_format: :xml, out_format: :yml)

data = Hash.from_xml(stdin.read)
data_hash = data['hash']
data = data_hash if data_hash
data = read_xml

stdout.puts YAML.dump(data)
end

Expand All @@ -23,6 +23,27 @@ def yml_to_xml
stdout.puts data.to_xml
end

def md_to_xml
return if bad_usage?(in_format: :md, out_format: :xml)

parsed = FrontMatterParser::Parser.new(:md).call(stdin.read)
data = {
front_matter: parsed.front_matter,
content: parsed.content,
}
stdout.puts data.to_xml
end

def xml_to_md
return if bad_usage?(in_format: :xml, out_format: :md)

data = read_xml

stdout.puts YAML.dump(data['front_matter'])
stdout.puts '---'
stdout.puts data['content']
end

private

attr_reader :stdin, :stdout
Expand All @@ -36,4 +57,11 @@ def bad_usage?(in_format:, out_format:)
# rubocop:enable Rails/Exit
true
end

def read_xml
data = Hash.from_xml(stdin.read)
data_hash = data['hash']
data = data_hash if data_hash
data
end
end
5 changes: 5 additions & 0 deletions scripts/i18n-md-to-xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/usr/bin/env ruby
$LOAD_PATH.unshift(File.expand_path('../lib', File.dirname(__FILE__)))
require 'i18n_converter'

I18nConverter.new(stdin: STDIN, stdout: STDOUT).md_to_xml
5 changes: 5 additions & 0 deletions scripts/i18n-xml-to-md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/usr/bin/env ruby
$LOAD_PATH.unshift(File.expand_path('../lib', File.dirname(__FILE__)))
require 'i18n_converter'

I18nConverter.new(stdin: STDIN, stdout: STDOUT).xml_to_md
70 changes: 70 additions & 0 deletions spec/lib/i18n_converter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,30 @@
XML
end

let(:markdown_md) do
<<~MD
---
title: Title
description: Description
---
Some **fancy** markdown
MD
end

let(:markdown_xml) do
<<~XML
<?xml version="1.0" encoding="UTF-8"?>
<hash>
<front-matter>
<title>Title</title>
<description>Description</description>
</front-matter>
<content>Some **fancy** markdown
</content>
</hash>
XML
end

subject(:converter) { I18nConverter.new(stdin: stdin, stdout: stdout) }

describe '.yml_to_xml' do
Expand Down Expand Up @@ -78,4 +102,50 @@
expect(stdout.string).to eq(translation_yml)
end
end

describe '.md_to_xml' do
let(:stdout) { StringIO.new }
let(:stdin) { StringIO.new(markdown_md) }

context 'with a TTY on STDIN' do
let(:stdin) { instance_double('IO', tty?: true) }

it 'prints an error and exits' do
expect(converter).to receive(:exit).with(1)

converter.md_to_xml

expect(stdout.string.chomp).to eq("Usage: cat en.md | #{$PROGRAM_NAME} > output.xml")
end
end

it 'outputs XML' do
converter.md_to_xml

expect(stdout.string).to eq(markdown_xml)
end
end

describe '.xml_to_md' do
let(:stdout) { StringIO.new }
let(:stdin) { StringIO.new(markdown_xml) }

context 'with a TTY on STDIN' do
let(:stdin) { instance_double('IO', tty?: true) }

it 'prints an error and exits' do
expect(converter).to receive(:exit).with(1)

converter.xml_to_md

expect(stdout.string.chomp).to eq("Usage: cat en.xml | #{$PROGRAM_NAME} > output.md")
end
end

it 'outputs XML' do
converter.xml_to_md

expect(stdout.string).to eq(markdown_md)
end
end
end