Skip to content

Commit

Permalink
fix cmd option and nested block command exception (#2)
Browse files Browse the repository at this point in the history
* fix cmd option and nested block command exception

* fix

* fix
  • Loading branch information
srz-zumix authored Jan 11, 2021
1 parent ecedd4f commit 2544624
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 12 deletions.
38 changes: 27 additions & 11 deletions lib/review/retrovert/converter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,11 @@ def update_config(outdir)
end

def replace_compatible_block_command_outline(content, command, new_command, option_count)
content.gsub!(/^\/\/#{command}(?<option>(\[[^\r\n]*?\]){0,#{option_count}})(\[[^\r\n]*\])*{(?<inner>.*?)\/\/}/m, "//#{new_command}\\k<option>{\\k<inner>//}")
if option_count > 0
content.gsub!(/^\/\/#{command}(?<option>(\[[^\r\n]*?\]){0,#{option_count}})(\[[^\r\n]*\])*{(?<inner>.*?)\/\/}/m, "//#{new_command}\\k<option>{\\k<inner>//}")
else
content.gsub!(/^\/\/#{command}(\[[^\r\n]*\])*{(?<inner>.*?)\/\/}/m, "//#{new_command}{\\k<inner>//}")
end
end

def replace_compatible_block_command_to_outside(content, command, new_command, option_count, add_options="", new_body="")
Expand Down Expand Up @@ -105,22 +109,29 @@ def replace_inline_command(content, command, new_command)
content.gsub!(/@<#{command}>/, "@<#{new_command}>")
end

def replace_block_command_nested_boxed_article(content, box)
def replace_block_command_nested_boxed_article_i(content, box, depth)
found = false
content.dup.scan(/(^\/\/#{box})(\[[^\r\n]*?\])*(?:(\$)|(?:({)|(\|)))(.*?)(^\/\/)(?(3)(\$)|(?(4)(})|(\|)).*?[\r\n]+)/m) { |m|
matched = m[0..-1].join
inner = m[5]
# info depth
im = inner.match(/^\/\/(\w+)((\[.*?\])*)([$|{])/)
unless im.nil?
inner_cmd = im[1]
inner_open = im[4]
inner_opts = im[2]
first_opt = inner_opts.match(/^\[(.*?)\]/)[1]
first_opt_m = inner_opts.match(/^\[(.*?)\]/)
first_opt = ""

# is_commentout = false
is_commentout = true
unless first_opt.empty?
if inner.match(/@<.*?>[$|{]#{first_opt}/)
is_commentout = false
if first_opt_m
first_opt_v = first_opt_m[1]
unless first_opt_v.empty?
if inner.match(/@<.*?>[$|{]#{first_opt}/)
is_commentout = false
first_opt = "\\[#{first_opt_v}\\]"
end
end
end
cmd_begin = m[0..4].join
Expand All @@ -129,21 +140,21 @@ def replace_block_command_nested_boxed_article(content, box)
if inner_open == m[2..4].join
# if same fence then cmd_end == inner_end
if is_commentout
inner.gsub!(/(^\/\/(\w+\[.*?\])*#{inner_open})/, '#@#\1')
inner.gsub!(/(^\/\/(\w+(\[.*?\]|))*#{inner_open})/, '#@#\1')
content.gsub!(/#{Regexp.escape(matched)}/m, "#{cmd_begin}#{inner}#@##{cmd_end}")
else
imb = inner.match(/(\R((^\/\/\w+(\[.*?\])*)\s*)*^\/\/\w+\[#{first_opt}\](\[.*?\])*#{inner_open}.*)\R/m)
imb = inner.match(/(\R((^\/\/\w+(\[.*?\])*)\s*)*^\/\/#{inner_cmd}#{first_opt}(\[.*?\])*#{inner_open}.*)\R/m)
to_out_block = imb[1]
inner.gsub!(/#{Regexp.escape(to_out_block)}/m, '')
content.gsub!(/#{Regexp.escape(matched)}/m, "#{cmd_begin}#{inner}#{cmd_end}#{to_out_block}")
end
else
close = inner_open == '{' ? '}' : inner_open
if is_commentout
inner.gsub!(/(^\/\/(\w+\[.*?\])*#{inner_open})(.*?)(^\/\/#{close})/m, '#@#\1\2#@#\3')
inner.gsub!(/(^\/\/(\w+(\[.*?\]|))*#{inner_open})(.*?)(^\/\/#{close})/m, '#@#\1\2#@#\3')
content.gsub!(/#{Regexp.escape(matched)}/m, "#{cmd_begin}#{inner}#{cmd_end}")
else
imb = inner.match(/\R((^\/\/\w+(\[.*?\])*)\s*)*^\/\/(\w+)\[#{first_opt}\](\[[^\r\n]*?\])*(?:(\$)|(?:({)|(\|)))(.*?)(^\/\/)(?(3)(\$)|(?(4)(})|(\|)))/m)
imb = inner.match(/\R((^\/\/\w+(\[.*?\])*)\s*)*^\/\/(#{inner_cmd})#{first_opt}(\[[^\r\n]*?\])*(?:(\$)|(?:({)|(\|)))(.*?)(^\/\/)(?(3)(\$)|(?(4)(})|(\|)))/m)
to_out_block = imb[0]
inner.gsub!(/#{Regexp.escape(to_out_block)}/m, '')
content.gsub!(/#{Regexp.escape(matched)}/m, "#{cmd_begin}#{inner}#{cmd_end}#{to_out_block}")
Expand All @@ -153,10 +164,14 @@ def replace_block_command_nested_boxed_article(content, box)
end
}
if found
replace_block_command_nested_boxed_article(content, box)
replace_block_command_nested_boxed_article_i(content, box, depth+1)
end
end

def replace_block_command_nested_boxed_article(content, box)
replace_block_command_nested_boxed_article_i(content, box, 0)
end

def replace_block_command_nested_boxed_articles(content)
replace_block_command_nested_boxed_article(content, 'note')
replace_block_command_nested_boxed_article(content, 'memo')
Expand Down Expand Up @@ -303,6 +318,7 @@ def update_content(outdir, contentfile)
end
# Re:VIEW Starter commands
replace_compatible_block_command_outline(content, 'terminal', 'cmd', 1)
replace_compatible_block_command_outline(content, 'cmd', 'cmd', 0)
replace_compatible_block_command_to_outside(content, 'sideimage', 'image', 1, '[]')
replace_block_command_outline(content, 'abstract', 'lead', true)
delete_block_command(content, 'needvspace')
Expand Down
3 changes: 2 additions & 1 deletion spec/review/retrovert_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,8 @@
expect(file02).to be_an_existing_file
text = File.open(File.join(aruba.current_directory, file02)).read()
expect(text).not_to match(/^\/\/terminal/)
expect(text).not_to match(/^\/\/cmd\[.*?\]\[.*?\]/)
expect(text).not_to match(/^\/\/cmd\[.*?\]/)
expect(text).to match(/^\/\/cmd{/)
end

it 'block comaptible command replace with options and inner text go outside' do
Expand Down

0 comments on commit 2544624

Please sign in to comment.