Skip to content

Commit 264def8

Browse files
tokidevelopermarmarek
authored andcommitted
Update merge_md_heading_ids.rb script
1 parent c8afd6f commit 264def8

File tree

1 file changed

+63
-32
lines changed

1 file changed

+63
-32
lines changed

_utils/_translation_utils/merge_md_heading_ids.rb

Lines changed: 63 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -94,10 +94,21 @@ def extract_headline_id(rendered_html_lines, l, c)
9494

9595

9696

97-
def try_create_id(gfm_lines, line_number, this_line, next_line, rendered_html_lines, placeholder)
97+
def try_get_headline_column_and_line(gfm_lines, line_number, placeholder)
9898
# save headline
9999
saved_headline = gfm_lines[line_number]
100100

101+
this_line = gfm_lines[line_number].to_s
102+
if this_line.eql? ""
103+
return nil
104+
end
105+
106+
if line_number < gfm_lines.length - 1
107+
next_line = gfm_lines[line_number + 1].to_s
108+
else
109+
next_line = ""
110+
end
111+
101112
hl = nil
102113

103114
if this_line.start_with?('#')
@@ -113,11 +124,7 @@ def try_create_id(gfm_lines, line_number, this_line, next_line, rendered_html_li
113124
# revert headline
114125
gfm_lines[line_number] = saved_headline
115126

116-
if hl == nil
117-
return nil
118-
end
119-
120-
return extract_headline_id(rendered_html_lines, hl.l, hl.c)
127+
return hl
121128
end
122129

123130

@@ -147,47 +154,65 @@ def generate_unique_placeholder(rendered_html_lines)
147154

148155

149156

150-
def create_line_to_id_map(gfm_lines)
151-
result = {}
157+
def create_id_list(gfm_lines)
158+
result = []
152159
gfm_lines2 = gfm_lines[0..-1]
153160
rendered_html_lines = render(gfm_lines)
154161

155162
placeholder = generate_unique_placeholder(rendered_html_lines)
156163

157164
# line-by-line: assume a headline
158165
n = gfm_lines2.length
159-
for i in 0..(n - 1)
160-
this_line = gfm_lines2[i]
161-
next_line = ''
162-
if i < n - 1
163-
next_line = gfm_lines2[i + 1]
164-
end
165-
hid = try_create_id(gfm_lines2, i, this_line, next_line, rendered_html_lines, placeholder)
166-
if hid != nil
167-
result[i] = hid
166+
for line_number in 0..(n - 1)
167+
hl = try_get_headline_column_and_line(gfm_lines2, line_number, placeholder)
168+
if hl != nil
169+
hid = extract_headline_id(rendered_html_lines, hl.l, hl.c)
170+
result = result + [hid]
168171
end
169172
end
170173
return result
171174
end
172175

173176

174177

175-
def insert_ids_to_gfm_file(line_to_id_map, gfm_lines)
178+
def is_a_headline(gfm_lines, line_number, placeholder)
179+
return try_get_headline_column_and_line(gfm_lines, line_number, placeholder) != nil
180+
end
181+
182+
183+
184+
def insert_ids_into_gfm_file(id_list, gfm_lines)
176185
result = gfm_lines[0..-1]
186+
if id_list.length == 0
187+
return result
188+
end
177189
n = result.length
178-
line_to_id_map.each do |key, value|
179-
str_to_insert = '<a id="' + value + '"></a>' + "\n"
180-
line = result[key]
181-
if !line.nil? and line.start_with?('#')
182-
if key + 1 >= n
183-
result = result + ['']
190+
rendered_html_lines = render(gfm_lines)
191+
placeholder = generate_unique_placeholder(rendered_html_lines)
192+
id_index = 0
193+
194+
for line_number in 0..(gfm_lines.length - 1)
195+
if is_a_headline(gfm_lines, line_number, placeholder)
196+
id = id_list[id_index]
197+
if id != nil
198+
str_to_insert = '<a id="' + id + '"></a>' + "\n"
199+
line = result[line_number]
200+
if !line.nil? and line.start_with?('#')
201+
if line_number + 1 >= n
202+
result = result + ['']
203+
end
204+
result[line_number + 1] = str_to_insert.to_s + result[line_number + 1].to_s
205+
else
206+
if line_number + 2 >= n
207+
result = result + ['']
208+
end
209+
result[line_number + 2] = str_to_insert.to_s + result[line_number + 2].to_s
210+
end
184211
end
185-
result[key + 1] = str_to_insert.to_s + result[key + 1].to_s
186-
else
187-
if key + 2 >= n
188-
result = result + ['']
212+
id_index += 1
213+
if id_index >= id_list.length
214+
break
189215
end
190-
result[key + 2] = str_to_insert.to_s + result[key + 2].to_s
191216
end
192217
end
193218
return result
@@ -216,11 +241,11 @@ def merge_ids_in_gfm_files(orig_gfm_lines, trl_gfm_lines)
216241
# get body from orig
217242
orig_body = orig_gfm_lines[orig_end..-1]
218243

219-
# create line-to-id map
220-
orig_line_to_id_map = create_line_to_id_map(orig_body)
244+
# create id list
245+
orig_id_list = create_id_list(orig_body)
221246

222247
# insert ids
223-
preresult = insert_ids_to_gfm_file(orig_line_to_id_map, trl_body)
248+
preresult = insert_ids_into_gfm_file(orig_id_list, trl_body)
224249

225250
# create translated document with adapted body
226251
result_trl_gfm = trl_yaml_front_matter.join + preresult.join
@@ -300,5 +325,11 @@ def main()
300325

301326
if __FILE__ == $0
302327
main()
328+
329+
# --- for debugging
330+
# orig_gfm_lines = read_file(ARGV[0])
331+
# trl_gfm_lines = read_file(ARGV[1])
332+
# result = merge_ids_in_gfm_files(orig_gfm_lines, trl_gfm_lines)
333+
# write_file(result, '/dev/stdout')
303334
end
304335

0 commit comments

Comments
 (0)