PageRenderTime 23ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/ruby/2.2.0/gems/roo-2.7.1/lib/roo/excelx/shared_strings.rb

https://bitbucket.org/saranyutcr/client_cinestaan_cms
Ruby | 157 lines | 116 code | 14 blank | 27 comment | 2 complexity | 7e13bdbc026fceb7d1b9b2619a29efaa MD5 | raw file
Possible License(s): MIT, CC-BY-SA-3.0, BSD-3-Clause, 0BSD, Apache-2.0, JSON
  1. require 'roo/excelx/extractor'
  2. module Roo
  3. class Excelx
  4. class SharedStrings < Excelx::Extractor
  5. COMMON_STRINGS = {
  6. t: "t",
  7. r: "r",
  8. html_tag_open: "<html>",
  9. html_tag_closed: "</html>"
  10. }
  11. def [](index)
  12. to_a[index]
  13. end
  14. def to_a
  15. @array ||= extract_shared_strings
  16. end
  17. def to_html
  18. @html ||= extract_html
  19. end
  20. # Use to_html or to_a for html returns
  21. # See what is happening with commit???
  22. def use_html?(index)
  23. to_html[index][/<([biu]|sup|sub)>/]
  24. end
  25. private
  26. def fix_invalid_shared_strings(doc)
  27. invalid = { '_x000D_' => "\n" }
  28. xml = doc.to_s
  29. return doc unless xml[/#{invalid.keys.join('|')}/]
  30. ::Nokogiri::XML(xml.gsub(/#{invalid.keys.join('|')}/, invalid))
  31. end
  32. def extract_shared_strings
  33. return [] unless doc_exists?
  34. document = fix_invalid_shared_strings(doc)
  35. # read the shared strings xml document
  36. document.xpath('/sst/si').map do |si|
  37. shared_string = ''
  38. si.children.each do |elem|
  39. case elem.name
  40. when 'r'
  41. elem.children.each do |r_elem|
  42. shared_string << r_elem.content if r_elem.name == 't'
  43. end
  44. when 't'
  45. shared_string = elem.content
  46. end
  47. end
  48. shared_string
  49. end
  50. end
  51. def extract_html
  52. return [] unless doc_exists?
  53. fix_invalid_shared_strings(doc)
  54. # read the shared strings xml document
  55. doc.xpath('/sst/si').map do |si|
  56. html_string = '<html>'
  57. si.children.each do |elem|
  58. case elem.name
  59. when 'r'
  60. html_string << extract_html_r(elem)
  61. when 't'
  62. html_string << elem.content
  63. end # case elem.name
  64. end # si.children.each do |elem|
  65. html_string << '</html>'
  66. end # doc.xpath('/sst/si').map do |si|
  67. end # def extract_html
  68. # The goal of this function is to take the following XML code snippet and create a html tag
  69. # r_elem ::: XML Element that is in sharedStrings.xml of excel_book.xlsx
  70. # {code:xml}
  71. # <r>
  72. # <rPr>
  73. # <i/>
  74. # <b/>
  75. # <u/>
  76. # <vertAlign val="subscript"/>
  77. # <vertAlign val="superscript"/>
  78. # </rPr>
  79. # <t>TEXT</t>
  80. # </r>
  81. # {code}
  82. #
  83. # Expected Output ::: "<html><sub|sup><b><i><u>TEXT</u></i></b></sub|/sup></html>"
  84. def extract_html_r(r_elem)
  85. str = ''
  86. xml_elems = {
  87. sub: false,
  88. sup: false,
  89. b: false,
  90. i: false,
  91. u: false
  92. }
  93. b, i, u, sub, sup = false, false, false, false, false
  94. r_elem.children.each do |elem|
  95. case elem.name
  96. when 'rPr'
  97. elem.children.each do |rPr_elem|
  98. case rPr_elem.name
  99. when 'b'
  100. # set formatting for Bold to true
  101. xml_elems[:b] = true
  102. when 'i'
  103. # set formatting for Italics to true
  104. xml_elems[:i] = true
  105. when 'u'
  106. # set formatting for Underline to true
  107. xml_elems[:u] = true
  108. when 'vertAlign'
  109. # See if the Vertical Alignment is subscript or superscript
  110. case rPr_elem.xpath('@val').first.value
  111. when 'subscript'
  112. # set formatting for Subscript to true and Superscript to false ... Can't have both
  113. xml_elems[:sub] = true
  114. xml_elems[:sup] = false
  115. when 'superscript'
  116. # set formatting for Superscript to true and Subscript to false ... Can't have both
  117. xml_elems[:sup] = true
  118. xml_elems[:sub] = false
  119. end
  120. end
  121. end
  122. when 't'
  123. str << create_html(elem.content, xml_elems)
  124. end
  125. end
  126. str
  127. end # extract_html_r
  128. # This will return an html string
  129. def create_html(text, formatting)
  130. tmp_str = ''
  131. formatting.each do |elem, val|
  132. tmp_str << "<#{elem}>" if val
  133. end
  134. tmp_str << text
  135. reverse_format = Hash[formatting.to_a.reverse]
  136. reverse_format.each do |elem, val|
  137. tmp_str << "</#{elem}>" if val
  138. end
  139. tmp_str
  140. end
  141. end # class SharedStrings < Excelx::Extractor
  142. end # class Excelx
  143. end # module Roo