isodoc 1.2.6 → 1.2.7
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.github/workflows/rake.yml +64 -0
- data/isodoc.gemspec +2 -1
- data/lib/isodoc/base_style/all.css +4 -0
- data/lib/isodoc/base_style/reset.css +4 -0
- data/lib/isodoc/base_style/reset.scss +5 -0
- data/lib/isodoc/convert.rb +1 -14
- data/lib/isodoc/function/to_word_html.rb +1 -0
- data/lib/isodoc/metadata.rb +28 -109
- data/lib/isodoc/metadata_contributor.rb +90 -0
- data/lib/isodoc/metadata_date.rb +6 -0
- data/lib/isodoc/presentation_function/bibdata.rb +78 -6
- data/lib/isodoc/version.rb +1 -1
- data/lib/isodoc/word_function/postprocess.rb +1 -1
- data/spec/assets/i18n.yaml +11 -4
- data/spec/isodoc/blocks_spec.rb +87 -240
- data/spec/isodoc/i18n_spec.rb +52 -38
- data/spec/isodoc/metadata_spec.rb +47 -6
- data/spec/isodoc/ref_spec.rb +2 -5
- data/spec/spec_helper.rb +2 -0
- metadata +20 -7
- data/.github/workflows/macos.yml +0 -42
- data/.github/workflows/ubuntu.yml +0 -62
- data/.github/workflows/windows.yml +0 -44
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c1a16a6af5cb8621eb1dd8e14a2680e7858476d5ffc1007e45566928e9ebaef7
|
4
|
+
data.tar.gz: ef5696d693f602b825457939bfab6f1ccfd827a9f24cbfa975ee6120c603b264
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f3f8718e404915ce3d9d7ad8de1b4f6c0eb6442c930ced3e05c6f1db25ee23324de1783c3fe96e41e86d765a705b7b41df78bd159ff1333f3809ed2682188cc6
|
7
|
+
data.tar.gz: 0ba52d047fb5b1119d1cf485eea60511066f5fec0152222123cedea05f346a7f2276a9ac90aede713d6ae14b3a2be4c5e41267ffdb4ccdff285b5d13350cd26c
|
@@ -0,0 +1,64 @@
|
|
1
|
+
# Auto-generated by Cimas: Do not edit it manually!
|
2
|
+
# See https://github.com/metanorma/cimas
|
3
|
+
name: rake
|
4
|
+
|
5
|
+
on:
|
6
|
+
push:
|
7
|
+
branches: [ master ]
|
8
|
+
pull_request:
|
9
|
+
|
10
|
+
jobs:
|
11
|
+
rake:
|
12
|
+
name: Test on Ruby ${{ matrix.ruby }} ${{ matrix.os }}
|
13
|
+
runs-on: ${{ matrix.os }}
|
14
|
+
continue-on-error: ${{ matrix.experimental }}
|
15
|
+
strategy:
|
16
|
+
fail-fast: false
|
17
|
+
matrix:
|
18
|
+
ruby: [ '2.6', '2.5', '2.4' ]
|
19
|
+
os: [ ubuntu-latest, windows-latest, macos-latest ]
|
20
|
+
experimental: [ false ]
|
21
|
+
include:
|
22
|
+
- ruby: '2.7'
|
23
|
+
os: 'ubuntu-latest'
|
24
|
+
experimental: true
|
25
|
+
- ruby: '2.7'
|
26
|
+
os: 'windows-latest'
|
27
|
+
experimental: true
|
28
|
+
- ruby: '2.7'
|
29
|
+
os: 'macos-latest'
|
30
|
+
experimental: true
|
31
|
+
steps:
|
32
|
+
- uses: actions/checkout@master
|
33
|
+
|
34
|
+
- name: Use Ruby
|
35
|
+
uses: ruby/setup-ruby@v1
|
36
|
+
with:
|
37
|
+
ruby-version: ${{ matrix.ruby }}
|
38
|
+
bundler-cache: true
|
39
|
+
|
40
|
+
- name: Install Inkscape macOS
|
41
|
+
if: matrix.os == 'macos-latest'
|
42
|
+
run: |
|
43
|
+
brew cask install inkscape
|
44
|
+
inkscape --version
|
45
|
+
|
46
|
+
- name: Install Inkscape Ubuntu
|
47
|
+
if: matrix.os == 'ubuntu-latest'
|
48
|
+
run: |
|
49
|
+
sudo add-apt-repository ppa:inkscape.dev/stable
|
50
|
+
sudo apt update
|
51
|
+
sudo apt install inkscape
|
52
|
+
inkscape --version
|
53
|
+
|
54
|
+
- name: Install Inkscape Windows
|
55
|
+
if: matrix.os == 'windows-latest'
|
56
|
+
run: |
|
57
|
+
choco install --no-progress -y inkscape
|
58
|
+
inkscape --version
|
59
|
+
|
60
|
+
- name: Update gems
|
61
|
+
run: bundle install --jobs 4 --retry 3
|
62
|
+
|
63
|
+
- name: Run specs
|
64
|
+
run: bundle exec rake
|
data/isodoc.gemspec
CHANGED
@@ -36,8 +36,9 @@ Gem::Specification.new do |spec|
|
|
36
36
|
spec.add_dependency "html2doc", "~> 1.0.0"
|
37
37
|
spec.add_dependency "liquid"
|
38
38
|
spec.add_dependency "roman-numerals"
|
39
|
-
spec.add_dependency "metanorma", "~> 1.
|
39
|
+
spec.add_dependency "metanorma", "~> 1.2.0"
|
40
40
|
spec.add_dependency "relaton-cli"
|
41
|
+
spec.add_dependency "gyoku", "~> 1.3"
|
41
42
|
|
42
43
|
spec.add_development_dependency "rake", "~> 13.0"
|
43
44
|
spec.add_development_dependency "byebug", "~> 9.1"
|
@@ -104,6 +104,10 @@ b, strong {
|
|
104
104
|
div.document-stage-band, div.document-type-band {
|
105
105
|
background-color: #333333; }
|
106
106
|
|
107
|
+
a.FootnoteRef + a.FootnoteRef:before {
|
108
|
+
content: ", ";
|
109
|
+
vertical-align: super; }
|
110
|
+
|
107
111
|
#standard-band {
|
108
112
|
background-color: #0AC442; }
|
109
113
|
|
data/lib/isodoc/convert.rb
CHANGED
@@ -116,20 +116,7 @@ module IsoDoc
|
|
116
116
|
end
|
117
117
|
|
118
118
|
# none for this parent gem, but will be populated in child gems
|
119
|
-
# which have access to stylesheets &c
|
120
|
-
# {
|
121
|
-
# htmlstylesheet: html_doc_path("htmlstyle.scss"),
|
122
|
-
# htmlcoverpage: html_doc_path("html_rsd_titlepage.html"),
|
123
|
-
# htmlintropage: html_doc_path("html_rsd_intro.html"),
|
124
|
-
# scripts: html_doc_path("scripts.html"),
|
125
|
-
# wordstylesheet: html_doc_path("wordstyle.scss"),
|
126
|
-
# standardstylesheet: html_doc_path("rsd.scss"),
|
127
|
-
# header: html_doc_path("header.html"),
|
128
|
-
# wordcoverpage: html_doc_path("word_rsd_titlepage.html"),
|
129
|
-
# wordintropage: html_doc_path("word_rsd_intro.html"),
|
130
|
-
# ulstyle: l3
|
131
|
-
# olstyle: l2
|
132
|
-
# }
|
119
|
+
# which have access to stylesheets &c
|
133
120
|
def default_file_locations(_options)
|
134
121
|
{}
|
135
122
|
end
|
data/lib/isodoc/metadata.rb
CHANGED
@@ -1,6 +1,7 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
require_relative './metadata_date'
|
4
|
+
require_relative './metadata_contributor'
|
4
5
|
|
5
6
|
module IsoDoc
|
6
7
|
class Metadata
|
@@ -15,7 +16,7 @@ module IsoDoc
|
|
15
16
|
end
|
16
17
|
|
17
18
|
def initialize(lang, script, i18n, fonts_options = {})
|
18
|
-
@metadata = {}
|
19
|
+
@metadata = { lang: lang, script: script }
|
19
20
|
DATETYPES.each { |w| @metadata["#{w.gsub(/-/, '_')}date".to_sym] = 'XXX' }
|
20
21
|
@lang = lang
|
21
22
|
@script = script
|
@@ -37,122 +38,33 @@ module IsoDoc
|
|
37
38
|
@metadata[key] = value
|
38
39
|
end
|
39
40
|
|
40
|
-
|
41
|
-
authors.reduce([]) do |ret, a|
|
42
|
-
if a.at(ns('./name/completename'))
|
43
|
-
ret << a.at(ns('./name/completename')).text
|
44
|
-
else
|
45
|
-
fn = []
|
46
|
-
forenames = a.xpath(ns('./name/forename'))
|
47
|
-
forenames.each { |f| fn << f.text }
|
48
|
-
surname = a&.at(ns('./name/surname'))&.text
|
49
|
-
ret << fn.join(' ') + ' ' + surname
|
50
|
-
end
|
51
|
-
end
|
52
|
-
end
|
53
|
-
|
54
|
-
def extract_person_affiliations(authors)
|
55
|
-
authors.reduce([]) do |m, a|
|
56
|
-
name = a&.at(ns('./affiliation/organization/name'))&.text
|
57
|
-
location = a&.at(ns('./affiliation/organization/address/'\
|
58
|
-
'formattedAddress'))&.text
|
59
|
-
m << (!name.nil? && !location.nil? ? "#{name}, #{location}" :
|
60
|
-
(name || location || ''))
|
61
|
-
m
|
62
|
-
end
|
63
|
-
end
|
64
|
-
|
65
|
-
def extract_person_names_affiliations(authors)
|
66
|
-
names = extract_person_names(authors)
|
67
|
-
affils = extract_person_affiliations(authors)
|
68
|
-
ret = {}
|
69
|
-
affils.each_with_index do |a, i|
|
70
|
-
ret[a] ||= []
|
71
|
-
ret[a] << names[i]
|
72
|
-
end
|
73
|
-
ret
|
74
|
-
end
|
75
|
-
|
76
|
-
def personal_authors(isoxml)
|
77
|
-
authors = isoxml.xpath(ns("//bibdata/contributor[role/@type = 'author' "\
|
78
|
-
"or xmlns:role/@type = 'editor']/person"))
|
79
|
-
set(:authors, extract_person_names(authors))
|
80
|
-
set(:authors_affiliations, extract_person_names_affiliations(authors))
|
81
|
-
end
|
41
|
+
NOLANG = "[not(@language) or @language = '']".freeze
|
82
42
|
|
83
|
-
def
|
84
|
-
|
85
|
-
agency(xml)
|
86
|
-
end
|
87
|
-
|
88
|
-
def bibdate(isoxml, _out)
|
89
|
-
isoxml.xpath(ns('//bibdata/date')).each do |d|
|
90
|
-
set("#{d['type'].gsub(/-/, '_')}date".to_sym, Common::date_range(d))
|
91
|
-
end
|
43
|
+
def currlang
|
44
|
+
"[@language = '#{@lang}']"
|
92
45
|
end
|
93
46
|
|
94
47
|
def doctype(isoxml, _out)
|
95
|
-
b = isoxml&.at(ns(
|
48
|
+
b = isoxml&.at(ns("//bibdata/ext/doctype#{NOLANG}"))&.text || return
|
96
49
|
set(:doctype, status_print(b))
|
97
|
-
|
98
|
-
set(:doctype_display, status_print(
|
99
|
-
end
|
100
|
-
|
101
|
-
def iso?(org)
|
102
|
-
name = org&.at(ns('./name'))&.text
|
103
|
-
abbrev = org&.at(ns('./abbreviation'))&.text
|
104
|
-
(abbrev == 'ISO' ||
|
105
|
-
name == 'International Organization for Standardization')
|
106
|
-
end
|
107
|
-
|
108
|
-
def agency1(xml)
|
109
|
-
agency = ''
|
110
|
-
publisher = []
|
111
|
-
xml.xpath(ns("//bibdata/contributor[xmlns:role/@type = 'publisher']/"\
|
112
|
-
'organization')).each do |org|
|
113
|
-
name = org&.at(ns('./name'))&.text
|
114
|
-
agency1 = org&.at(ns('./abbreviation'))&.text || name
|
115
|
-
publisher << name if name
|
116
|
-
agency = iso?(org) ? "ISO/#{agency}" : "#{agency}#{agency1}/"
|
117
|
-
end
|
118
|
-
[agency, publisher]
|
119
|
-
end
|
120
|
-
|
121
|
-
def agency(xml)
|
122
|
-
agency, publisher = agency1(xml)
|
123
|
-
set(:agency, agency.sub(%r{/$}, ''))
|
124
|
-
set(:publisher, @i18n.multiple_and(publisher, @labels['and']))
|
125
|
-
agency_addr(xml)
|
126
|
-
end
|
127
|
-
|
128
|
-
def agency_addr(xml)
|
129
|
-
a = xml.at(ns("//bibdata/contributor[xmlns:role/@type = 'publisher'][1]/"\
|
130
|
-
"organization")) or return
|
131
|
-
n = a.at(ns("./subdivision")) and set(:subdivision, n.text)
|
132
|
-
n = a.at(ns("./address/formattedAddress")) and
|
133
|
-
set(:pub_address, n.children.to_xml)
|
134
|
-
n = a.at(ns("./phone[not(@type = 'fax')]")) and set(:pub_phone, n.text)
|
135
|
-
n = a.at(ns("./phone[@type = 'fax']")) and set(:pub_fax, n.text)
|
136
|
-
n = a.at(ns("./email")) and set(:pub_email, n.text)
|
137
|
-
n = a.at(ns("./uri")) and set(:pub_uri, n.text)
|
50
|
+
b1 = isoxml&.at(ns("//bibdata/ext/doctype#{currlang}"))&.text || b
|
51
|
+
set(:doctype_display, status_print(b1))
|
138
52
|
end
|
139
53
|
|
140
|
-
def docstatus(
|
54
|
+
def docstatus(xml, _out)
|
141
55
|
set(:unpublished, true)
|
142
|
-
return unless
|
143
|
-
|
144
|
-
set(:stage, status_print(
|
145
|
-
|
146
|
-
|
147
|
-
(i = isoxml&.at(ns('//bibdata/status/substage'))&.text) &&
|
56
|
+
return unless s = xml.at(ns("//bibdata/status/stage#{NOLANG}"))
|
57
|
+
s1 = xml.at(ns("//bibdata/status/stage#{currlang}")) || s
|
58
|
+
set(:stage, status_print(s.text))
|
59
|
+
s1 and set(:stage_display, status_print(s1.text))
|
60
|
+
(i = xml&.at(ns("//bibdata/status/substage#{NOLANG}"))&.text) and
|
148
61
|
set(:substage, i)
|
149
|
-
(
|
150
|
-
set(:substage_display,
|
151
|
-
(
|
152
|
-
set(:iteration,
|
153
|
-
set(:unpublished, unpublished(
|
154
|
-
unpublished(
|
155
|
-
set(:stageabbr, stage_abbr(docstatus.text))
|
62
|
+
(i1 = xml&.at(ns("//bibdata/status/substage#{currlang}"))&.text || i) and
|
63
|
+
set(:substage_display, i1)
|
64
|
+
(i2 = xml&.at(ns('//bibdata/status/iteration'))&.text) and
|
65
|
+
set(:iteration, i2)
|
66
|
+
set(:unpublished, unpublished(s.text))
|
67
|
+
unpublished(s.text) && set(:stageabbr, stage_abbr(s.text))
|
156
68
|
end
|
157
69
|
|
158
70
|
def stage_abbr(docstatus)
|
@@ -172,6 +84,13 @@ module IsoDoc
|
|
172
84
|
set(:docnumber, dn&.text)
|
173
85
|
end
|
174
86
|
|
87
|
+
def otherid(isoxml, _out)
|
88
|
+
dn = isoxml.at(ns('//bibdata/docidentifier[@type = "ISBN"]'))
|
89
|
+
set(:isbn, dn&.text)
|
90
|
+
dn = isoxml.at(ns('//bibdata/docidentifier[@type = "ISBN10"]'))
|
91
|
+
set(:isbn10, dn&.text)
|
92
|
+
end
|
93
|
+
|
175
94
|
def docnumeric(isoxml, _out)
|
176
95
|
dn = isoxml.at(ns('//bibdata/docnumber'))
|
177
96
|
set(:docnumeric, dn&.text)
|
@@ -197,7 +116,7 @@ module IsoDoc
|
|
197
116
|
end
|
198
117
|
|
199
118
|
def title(isoxml, _out)
|
200
|
-
main = isoxml&.at(ns("//bibdata/title[@language='
|
119
|
+
main = isoxml&.at(ns("//bibdata/title[@language='#{@lang}']"))&.text
|
201
120
|
set(:doctitle, main)
|
202
121
|
end
|
203
122
|
|
@@ -0,0 +1,90 @@
|
|
1
|
+
module IsoDoc
|
2
|
+
class Metadata
|
3
|
+
def extract_person_names(authors)
|
4
|
+
authors.reduce([]) do |ret, a|
|
5
|
+
if a.at(ns('./name/completename'))
|
6
|
+
ret << a.at(ns('./name/completename')).text
|
7
|
+
else
|
8
|
+
fn = []
|
9
|
+
forenames = a.xpath(ns('./name/forename'))
|
10
|
+
forenames.each { |f| fn << f.text }
|
11
|
+
surname = a&.at(ns('./name/surname'))&.text
|
12
|
+
ret << fn.join(' ') + ' ' + surname
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
def extract_person_affiliations(authors)
|
18
|
+
authors.reduce([]) do |m, a|
|
19
|
+
name = a&.at(ns('./affiliation/organization/name'))&.text
|
20
|
+
location = a&.at(ns('./affiliation/organization/address/'\
|
21
|
+
'formattedAddress'))&.text
|
22
|
+
m << (!name.nil? && !location.nil? ? "#{name}, #{location}" :
|
23
|
+
(name || location || ''))
|
24
|
+
m
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
def extract_person_names_affiliations(authors)
|
29
|
+
names = extract_person_names(authors)
|
30
|
+
affils = extract_person_affiliations(authors)
|
31
|
+
ret = {}
|
32
|
+
affils.each_with_index do |a, i|
|
33
|
+
ret[a] ||= []
|
34
|
+
ret[a] << names[i]
|
35
|
+
end
|
36
|
+
ret
|
37
|
+
end
|
38
|
+
|
39
|
+
def personal_authors(isoxml)
|
40
|
+
authors = isoxml.xpath(ns("//bibdata/contributor[role/@type = 'author' "\
|
41
|
+
"or xmlns:role/@type = 'editor']/person"))
|
42
|
+
set(:authors, extract_person_names(authors))
|
43
|
+
set(:authors_affiliations, extract_person_names_affiliations(authors))
|
44
|
+
end
|
45
|
+
|
46
|
+
def author(xml, _out)
|
47
|
+
personal_authors(xml)
|
48
|
+
agency(xml)
|
49
|
+
end
|
50
|
+
|
51
|
+
def iso?(org)
|
52
|
+
name = org&.at(ns('./name'))&.text
|
53
|
+
abbrev = org&.at(ns('./abbreviation'))&.text
|
54
|
+
(abbrev == 'ISO' ||
|
55
|
+
name == 'International Organization for Standardization')
|
56
|
+
end
|
57
|
+
|
58
|
+
def agency1(xml)
|
59
|
+
agency = ''
|
60
|
+
publisher = []
|
61
|
+
xml.xpath(ns("//bibdata/contributor[xmlns:role/@type = 'publisher']/"\
|
62
|
+
'organization')).each do |org|
|
63
|
+
name = org&.at(ns('./name'))&.text
|
64
|
+
agency1 = org&.at(ns('./abbreviation'))&.text || name
|
65
|
+
publisher << name if name
|
66
|
+
agency = iso?(org) ? "ISO/#{agency}" : "#{agency}#{agency1}/"
|
67
|
+
end
|
68
|
+
[agency, publisher]
|
69
|
+
end
|
70
|
+
|
71
|
+
def agency(xml)
|
72
|
+
agency, publisher = agency1(xml)
|
73
|
+
set(:agency, agency.sub(%r{/$}, ''))
|
74
|
+
set(:publisher, @i18n.multiple_and(publisher, @labels['and']))
|
75
|
+
agency_addr(xml)
|
76
|
+
end
|
77
|
+
|
78
|
+
def agency_addr(xml)
|
79
|
+
a = xml.at(ns("//bibdata/contributor[xmlns:role/@type = 'publisher'][1]/"\
|
80
|
+
"organization")) or return
|
81
|
+
n = a.at(ns("./subdivision")) and set(:subdivision, n.text)
|
82
|
+
n = a.at(ns("./address/formattedAddress")) and
|
83
|
+
set(:pub_address, n.children.to_xml)
|
84
|
+
n = a.at(ns("./phone[not(@type = 'fax')]")) and set(:pub_phone, n.text)
|
85
|
+
n = a.at(ns("./phone[@type = 'fax']")) and set(:pub_fax, n.text)
|
86
|
+
n = a.at(ns("./email")) and set(:pub_email, n.text)
|
87
|
+
n = a.at(ns("./uri")) and set(:pub_uri, n.text)
|
88
|
+
end
|
89
|
+
end
|
90
|
+
end
|
data/lib/isodoc/metadata_date.rb
CHANGED
@@ -40,5 +40,11 @@ module IsoDoc
|
|
40
40
|
Date.parse(isodate).strftime("%B %d, %Y")
|
41
41
|
end
|
42
42
|
end
|
43
|
+
|
44
|
+
def bibdate(isoxml, _out)
|
45
|
+
isoxml.xpath(ns('//bibdata/date')).each do |d|
|
46
|
+
set("#{d['type'].gsub(/-/, '_')}date".to_sym, Common::date_range(d))
|
47
|
+
end
|
48
|
+
end
|
43
49
|
end
|
44
50
|
end
|
@@ -1,11 +1,22 @@
|
|
1
1
|
module IsoDoc
|
2
2
|
class PresentationXMLConvert < ::IsoDoc::Convert
|
3
3
|
def bibdata(docxml)
|
4
|
+
a = bibdata_current(docxml) or return
|
5
|
+
bibdata_i18n(a)
|
6
|
+
a.next =
|
7
|
+
"<localized-strings>#{i8n_name(trim_hash(@i18n.get), "").join("")}"\
|
8
|
+
"</localized-strings>"
|
9
|
+
end
|
10
|
+
|
11
|
+
def bibdata_current(docxml)
|
4
12
|
a = docxml.at(ns("//bibdata")) or return
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
a.
|
13
|
+
a.xpath(ns("./language")).each do |l|
|
14
|
+
l.text == @lang and l["current"] = "true"
|
15
|
+
end
|
16
|
+
a.xpath(ns("./script")).each do |l|
|
17
|
+
l.text == @script and l["current"] = "true"
|
18
|
+
end
|
19
|
+
a
|
9
20
|
end
|
10
21
|
|
11
22
|
def bibdata_i18n(b)
|
@@ -15,10 +26,71 @@ module IsoDoc
|
|
15
26
|
end
|
16
27
|
|
17
28
|
def hash_translate(bibdata, hash, xpath)
|
18
|
-
hash.is_a? Hash or return
|
19
29
|
x = bibdata.at(ns(xpath)) or return
|
30
|
+
x["language"] = ""
|
31
|
+
hash.is_a? Hash or return
|
20
32
|
hash[x.text] or return
|
21
|
-
x.
|
33
|
+
x.next = x.dup
|
34
|
+
x.next["language"] = @lang
|
35
|
+
x.next.children = hash[x.text]
|
36
|
+
end
|
37
|
+
|
38
|
+
def i18n_tag(k, v)
|
39
|
+
"<localized-string key='#{k}' language='#{@lang}'>#{v}</localized-string>"
|
40
|
+
end
|
41
|
+
|
42
|
+
def i18n_safe(k)
|
43
|
+
k.gsub(/\s|\./, "_")
|
44
|
+
end
|
45
|
+
|
46
|
+
def i8n_name(h, pref)
|
47
|
+
if h.is_a? Hash then i8n_name1(h, pref)
|
48
|
+
elsif h.is_a? Array
|
49
|
+
h.reject { |a| blank?(a) }.each_with_object([]).
|
50
|
+
with_index do |(v1, g), i|
|
51
|
+
i8n_name(v1, "#{i18n_safe(k)}.#{i}").each { |x| g << x }
|
52
|
+
end
|
53
|
+
else [i18n_tag(pref, h)]
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
def i8n_name1(h, pref)
|
58
|
+
h.reject { |k, v| blank?(v) }.each_with_object([]) do |(k, v), g|
|
59
|
+
if v.is_a? Hash then i8n_name(v, i18n_safe(k)).each { |x| g << x }
|
60
|
+
elsif v.is_a? Array
|
61
|
+
v.reject { |a| blank?(a) }.each_with_index do |v1, i|
|
62
|
+
i8n_name(v1, "#{i18n_safe(k)}.#{i}").each { |x| g << x }
|
63
|
+
end
|
64
|
+
else
|
65
|
+
g << i18n_tag("#{pref}#{pref.empty? ? "" : "."}#{i18n_safe(k)}", v)
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
#https://stackoverflow.com/a/31822406
|
71
|
+
def blank?(v)
|
72
|
+
v.nil? || v.respond_to?(:empty?) && v.empty?
|
73
|
+
end
|
74
|
+
|
75
|
+
def trim_hash(h)
|
76
|
+
loop do
|
77
|
+
h_new = trim_hash1(h)
|
78
|
+
break h if h==h_new
|
79
|
+
h = h_new
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
83
|
+
def trim_hash1(h)
|
84
|
+
return h unless h.is_a? Hash
|
85
|
+
h.each_with_object({}) do |(k,v), g|
|
86
|
+
next if blank?(v)
|
87
|
+
g[k] = if v.is_a? Hash then trim_hash1(h[k])
|
88
|
+
elsif v.is_a? Array
|
89
|
+
h[k].map { |a| trim_hash1(a) }.reject { |a| blank?(a) }
|
90
|
+
else
|
91
|
+
v
|
92
|
+
end
|
93
|
+
end
|
22
94
|
end
|
23
95
|
end
|
24
96
|
end
|