tmux-erb-parser 0.1.6 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: dfb0e75db1dafde63630267ce708bbd43162673eaa36404c9d01ab04aa261d97
4
- data.tar.gz: 9a4c16207774dde887524d40fe603baf375ffcd218be9048ec25775d68466834
3
+ metadata.gz: b21b6279300cff2863e40a40358e778886c3edcf47a7de559847aa48d7007fae
4
+ data.tar.gz: 354e717ab451bb2ea70bfbf121fdb12048c2250e6db18c4c2eb1d42561579315
5
5
  SHA512:
6
- metadata.gz: 79398b044ce99cf705241ac6ba0790bb8d81d9e72aa18376351f90d3d97db9209b2bb9a92438b5c6f881f98dcd141d38cbaca488ecbe63fc4f2c89f71965092e
7
- data.tar.gz: 73fd791b56c59b811cfd7818647a60c654a65df6033e254a372d49a0fc259942b4af16aee5d79709698c67aecadc1256576886beaa40acb0fa07e09c1cdba9c9
6
+ metadata.gz: 69945ff6ac0d8a61860ea885b93c9a6edb86a5bb09e312678c94936502d19e708663cc57d93a6542ffa44e974bddddb1d14b039164888f0cfa39f581ebe6f06f
7
+ data.tar.gz: 5ac5c80541ef86f2c87c2cdc5de3b478bcdbe96b77509eb547ec2af83cebeb9fe4553f7ef4f1a248a78f0f6c9f601120f1328763f6a36c5f1530318ec38b8e49
data/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # Tmux ERB Parser
2
2
  [![Gem Version](https://badge.fury.io/rb/tmux-erb-parser.svg)](https://badge.fury.io/rb/tmux-erb-parser)
3
- [![Build Status](https://travis-ci.org/epaew/tmux-erb-parser.svg?branch=master)](https://travis-ci.org/epaew/tmux-erb-parser)
3
+ [![Build Status](https://github.com/epaew/tmux-erb-parser/workflows/Run%20TestUnit/badge.svg)](https://github.com/epaew/tmux-erb-parser/actions?query=workflow%3A%22Run+TestUnit%22+branch%3A%22master%22)
4
4
  [![Maintainability](https://api.codeclimate.com/v1/badges/a4c67b3c8ba8e555d98f/maintainability)](https://codeclimate.com/github/epaew/tmux-erb-parser/maintainability)
5
5
  [![Test Coverage](https://api.codeclimate.com/v1/badges/a4c67b3c8ba8e555d98f/test_coverage)](https://codeclimate.com/github/epaew/tmux-erb-parser/test_coverage)
6
6
 
@@ -20,7 +20,9 @@ A Tmux plugin for loading tmux.conf written in Ruby (eRuby) notation.
20
20
  1. Create your `tmux.conf.erb` and place it.
21
21
  * By default, tmux-erb-parser loads all `*.erb` files in `~/.config/tmux/`
22
22
  * Or you can change the load path. (Please see below.)
23
- * Sample configuration file: [sample.tmux.conf.erb](test/fixtures/sample.tmux.conf.erb)
23
+ * Sample configuration file:
24
+ * [sample.tmux.conf.erb](test/fixtures/sample.tmux.conf.erb)
25
+ * [sample.tmux.conf.yaml](test/fixtures/sample.tmux.conf.yaml)
24
26
  2. Install tmux-erb-parser and run tmux!
25
27
 
26
28
  ### Install with tpm (Tmux Plugin Manager)
@@ -0,0 +1,81 @@
1
+ # frozen_string_literal: true
2
+
3
+ module TmuxERBParser
4
+ class Converter
5
+ class << self
6
+ def convert(structured)
7
+ structured = [structured] if structured.is_a?(Hash)
8
+
9
+ structured.inject([]) do |result, hash|
10
+ result << '' unless result.empty?
11
+
12
+ comment = hash.is_a?(Hash) && hash.delete('comment')
13
+ result << "# #{comment}" if comment
14
+
15
+ result.concat([*convert_structured(hash)])
16
+ end
17
+ end
18
+
19
+ private
20
+
21
+ def convert_hash(hash, prefix = [])
22
+ converted = hash.map do |key, value|
23
+ case key
24
+ when '"'
25
+ convert_structured(value, [*prefix, %('"')])
26
+ when 'if', 'if-shell', 'run', 'run-shell'
27
+ convert_structured_shell(value, [*prefix, key])
28
+ when /style/
29
+ convert_structured_style(value, [*prefix, key])
30
+ else
31
+ convert_structured(value, [*prefix, key])
32
+ end
33
+ end
34
+ converted.flatten
35
+ end
36
+
37
+ def convert_string(string, prefix = [])
38
+ [*prefix, string].join(' ')
39
+ end
40
+
41
+ def convert_structured(item, prefix = [])
42
+ case item
43
+ when Array
44
+ item.map { |i| convert_structured(i, prefix) }
45
+ when Hash
46
+ convert_hash(item, prefix)
47
+ else
48
+ convert_string(item, prefix)
49
+ end
50
+ end
51
+
52
+ def convert_structured_shell(item, prefix = [])
53
+ case item
54
+ when Array
55
+ args = item.map { |i| convert_structured(i, []) }
56
+ args = args.flatten.map do |arg|
57
+ arg.include?(' ') ? %('#{arg}') : arg
58
+ end
59
+
60
+ [*prefix, *args].join(' ')
61
+ else
62
+ convert_string(item, prefix)
63
+ end
64
+ end
65
+
66
+ def convert_structured_style(item, prefix = [])
67
+ case item
68
+ when Array
69
+ convert_string(item.join(','), prefix)
70
+ when Hash
71
+ convert_string(
72
+ item.map { |key, value| "#{key}=#{value}" }.join(','),
73
+ prefix
74
+ )
75
+ else
76
+ convert_string(item, prefix)
77
+ end
78
+ end
79
+ end
80
+ end
81
+ end
@@ -3,6 +3,7 @@
3
3
  require 'erb'
4
4
  require 'json'
5
5
  require 'yaml'
6
+ require_relative 'converter'
6
7
  require_relative 'helpers'
7
8
 
8
9
  module TmuxERBParser
@@ -31,27 +32,24 @@ module TmuxERBParser
31
32
 
32
33
  private
33
34
 
34
- def generate_conf(_structured)
35
- # TODO
36
- # parse_result = []
37
- end
38
-
39
35
  def parse_string(input, type)
40
36
  erb_result = ERB.new(input).result(TmuxERBParser::Helpers.binding)
41
37
 
42
38
  case type
43
39
  when :json
44
- generate_conf(JSON.parse(erb_result))
40
+ Converter.convert(JSON.parse(erb_result))
45
41
  when :yml, :yaml
46
- generate_conf(YAML.load_stream(erb_result))
42
+ if RUBY_VERSION.to_f < 2.6
43
+ Converter.convert(YAML.safe_load(erb_result, [], [], true))
44
+ else
45
+ Converter.convert(YAML.safe_load(erb_result, aliases: true))
46
+ end
47
47
  else
48
- # rubocop:disable Metrics/LineLength
48
+ # rubocop:disable Layout/LineLength
49
49
  erb_result
50
- .gsub(/(\R){3,}/) { Regexp.last_match(1) * 2 } # reduce continuity blanklines
51
- .gsub(/(\R)+\z/) { Regexp.last_match(1) } # remove blankline at EOF
52
- .each_line
53
- .map(&:chomp)
54
- # rubocop:enable Metrics/LineLength
50
+ .gsub(/(\R){3,}/) { Regexp.last_match(1) * 2 } # reduce continuity blankline
51
+ .each_line(chomp: true)
52
+ # rubocop:enable Layout/LineLength
55
53
  end
56
54
  end
57
55
 
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module TmuxERBParser
4
- VERSION = '0.1.6'
4
+ VERSION = '0.2.0'
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tmux-erb-parser
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.6
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - epaew
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-04-04 00:00:00.000000000 Z
11
+ date: 2020-04-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -136,6 +136,7 @@ files:
136
136
  - erb-parser.tmux
137
137
  - lib/tmux-erb-parser.rb
138
138
  - lib/tmux-erb-parser/command.rb
139
+ - lib/tmux-erb-parser/converter.rb
139
140
  - lib/tmux-erb-parser/helpers.rb
140
141
  - lib/tmux-erb-parser/helpers/environment_helper.rb
141
142
  - lib/tmux-erb-parser/helpers/format_helper.rb