From 922652417af26cc41354007030ebbb18f7b4efab Mon Sep 17 00:00:00 2001 From: Giga Chubinidze Date: Thu, 1 Sep 2022 10:15:31 +0400 Subject: [PATCH] Fix superscript and subscript in docx report generator [SCI-7141] --- app/services/reports/docx_renderer.rb | 8 ++++++++ .../reports/html_to_word_converter.rb | 20 ++++++++++++++++++- 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/app/services/reports/docx_renderer.rb b/app/services/reports/docx_renderer.rb index b4c5b8cb5..c0af01b12 100644 --- a/app/services/reports/docx_renderer.rb +++ b/app/services/reports/docx_renderer.rb @@ -13,6 +13,14 @@ module Reports br elsif text_el[:type] == 'a' Reports::DocxRenderer.render_link_element(self, text_el, options) + elsif text_el[:type] == 'sup' + text text_el[:value] do + vertical_align 'superscript' + end + elsif text_el[:type] == 'sub' + text text_el[:value] do + vertical_align 'subscript' + end end end end diff --git a/app/services/reports/html_to_word_converter.rb b/app/services/reports/html_to_word_converter.rb index 04a942c16..2645671ec 100644 --- a/app/services/reports/html_to_word_converter.rb +++ b/app/services/reports/html_to_word_converter.rb @@ -58,7 +58,7 @@ module Reports temp_p = [] end elements.push(elem) - elsif %w(br text a).include? elem[:type] + elsif %w(br text a sup sub).include? elem[:type] temp_p.push(elem) end end @@ -106,6 +106,16 @@ module Reports next end + if elem.name == 'sup' + elements.push(sup_element(elem)) + next + end + + if elem.name == 'sub' + elements.push(sub_element(elem)) + next + end + if %w(ul ol).include?(elem.name) elements.push(list_element(elem)) next @@ -257,5 +267,13 @@ module Reports end.reject(&:blank?) { type: 'table', data: rows } end + + def sup_element(element) + { type: 'sup', value: element.text } + end + + def sub_element(element) + { type: 'sub', value: element.text } + end end end