From 8c3501b5d0a478cda423673cc8effc136f3064e0 Mon Sep 17 00:00:00 2001 From: Zach Margolis Date: Wed, 7 Jun 2017 17:00:32 -0400 Subject: [PATCH] Add scripts to help translate markdown files **Why**: Useful for the static site --- Gemfile | 1 + Gemfile.lock | 2 + lib/i18n_converter.rb | 34 ++++++++++++++-- scripts/i18n-md-to-xml | 5 +++ scripts/i18n-xml-to-md | 5 +++ spec/lib/i18n_converter_spec.rb | 70 +++++++++++++++++++++++++++++++++ 6 files changed, 114 insertions(+), 3 deletions(-) create mode 100755 scripts/i18n-md-to-xml create mode 100755 scripts/i18n-xml-to-md diff --git a/Gemfile b/Gemfile index 9517570607c..4348815513d 100644 --- a/Gemfile +++ b/Gemfile @@ -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' diff --git a/Gemfile.lock b/Gemfile.lock index 5ae0b8295c1..672caac3417 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -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) @@ -678,6 +679,7 @@ DEPENDENCIES fasterer figaro foundation_emails + front_matter_parser gibberish guard-rspec gyoku diff --git a/lib/i18n_converter.rb b/lib/i18n_converter.rb index 9c5514598fd..6342436134e 100644 --- a/lib/i18n_converter.rb +++ b/lib/i18n_converter.rb @@ -1,5 +1,6 @@ require 'yaml' require 'active_support/core_ext/hash/conversions' +require 'front_matter_parser' class I18nConverter def initialize(stdin:, stdout:) @@ -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 @@ -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 @@ -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 diff --git a/scripts/i18n-md-to-xml b/scripts/i18n-md-to-xml new file mode 100755 index 00000000000..8d8a8bf24e8 --- /dev/null +++ b/scripts/i18n-md-to-xml @@ -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 diff --git a/scripts/i18n-xml-to-md b/scripts/i18n-xml-to-md new file mode 100755 index 00000000000..c9e9d3e629b --- /dev/null +++ b/scripts/i18n-xml-to-md @@ -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 diff --git a/spec/lib/i18n_converter_spec.rb b/spec/lib/i18n_converter_spec.rb index 5448449019b..428dbfa9d0b 100644 --- a/spec/lib/i18n_converter_spec.rb +++ b/spec/lib/i18n_converter_spec.rb @@ -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 + + + + Title + Description + + Some **fancy** markdown + + + XML + end + subject(:converter) { I18nConverter.new(stdin: stdin, stdout: stdout) } describe '.yml_to_xml' do @@ -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