|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +module DiscourseTranslator |
| 4 | + class ContentSplitter |
| 5 | + CHUNK_SIZE = 3000 |
| 6 | + |
| 7 | + BBCODE_PATTERNS = [ |
| 8 | + %r{\[table.*?\].*?\[/table\]}m, |
| 9 | + %r{\[quote.*?\].*?\[/quote\]}m, |
| 10 | + %r{\[details.*?\].*?\[/details\]}m, |
| 11 | + %r{\<details.*?\>.*?\</details\>}m, |
| 12 | + %r{\[spoiler.*?\].*?\[/spoiler\]}m, |
| 13 | + %r{\[code.*?\].*?\[/code\]}m, |
| 14 | + /```.*?```/m, |
| 15 | + ].freeze |
| 16 | + |
| 17 | + TEXT_BOUNDARIES = [ |
| 18 | + /\n\s*\n\s*|\r\n\s*\r\n\s*/, # double newlines with optional spaces |
| 19 | + /[.!?]\s+/, # sentence endings |
| 20 | + /[,;]\s+/, # clause endings |
| 21 | + /\n|\r\n/, # single newlines |
| 22 | + /\s+/, # any whitespace |
| 23 | + ].freeze |
| 24 | + |
| 25 | + def self.split(content) |
| 26 | + return [] if content.nil? |
| 27 | + return [""] if content.empty? |
| 28 | + return [content] if content.length <= CHUNK_SIZE |
| 29 | + |
| 30 | + chunks = [] |
| 31 | + remaining = content.dup |
| 32 | + |
| 33 | + while remaining.present? |
| 34 | + chunk = extract_mixed_chunk(remaining) |
| 35 | + break if chunk.empty? |
| 36 | + chunks << chunk |
| 37 | + remaining = remaining[chunk.length..-1] |
| 38 | + end |
| 39 | + |
| 40 | + chunks |
| 41 | + end |
| 42 | + |
| 43 | + private |
| 44 | + |
| 45 | + def self.extract_mixed_chunk(text, size: CHUNK_SIZE) |
| 46 | + return text if text.length <= size |
| 47 | + flexible_size = size * 1.5 |
| 48 | + |
| 49 | + # try each splitting strategy in order |
| 50 | + split_point = |
| 51 | + [ |
| 52 | + -> { find_nearest_html_end_index(text, size) }, |
| 53 | + -> { find_nearest_bbcode_end_index(text, size) }, |
| 54 | + -> { find_text_boundary(text, size) }, |
| 55 | + -> { size }, |
| 56 | + ].lazy.map(&:call).compact.find { |pos| pos <= flexible_size } |
| 57 | + |
| 58 | + text[0...split_point] |
| 59 | + end |
| 60 | + |
| 61 | + def self.find_nearest_html_end_index(text, target_pos) |
| 62 | + return nil unless text.include?("<") |
| 63 | + |
| 64 | + begin |
| 65 | + doc = Nokogiri::HTML.fragment(text) |
| 66 | + current_length = 0 |
| 67 | + |
| 68 | + doc.children.each do |node| |
| 69 | + html = node.to_html |
| 70 | + end_pos = current_length + html.length |
| 71 | + return end_pos if end_pos > target_pos |
| 72 | + current_length = end_pos |
| 73 | + end |
| 74 | + nil |
| 75 | + rescue Nokogiri::SyntaxError |
| 76 | + nil |
| 77 | + end |
| 78 | + end |
| 79 | + |
| 80 | + def self.find_nearest_bbcode_end_index(text, target_pos) |
| 81 | + BBCODE_PATTERNS.each do |pattern| |
| 82 | + text.scan(pattern) do |_| |
| 83 | + match = $~ |
| 84 | + tag_start = match.begin(0) |
| 85 | + tag_end = match.end(0) |
| 86 | + |
| 87 | + return tag_end if tag_start <= target_pos && tag_end > target_pos |
| 88 | + end |
| 89 | + end |
| 90 | + |
| 91 | + nil |
| 92 | + end |
| 93 | + |
| 94 | + def self.find_text_boundary(text, target_pos) |
| 95 | + search_text = text |
| 96 | + |
| 97 | + TEXT_BOUNDARIES.each do |pattern| |
| 98 | + if pos = search_text.rindex(pattern, target_pos) |
| 99 | + # Include all trailing whitespace |
| 100 | + pos += 1 while pos < search_text.length && search_text[pos].match?(/\s/) |
| 101 | + return pos |
| 102 | + end |
| 103 | + end |
| 104 | + nil |
| 105 | + end |
| 106 | + end |
| 107 | +end |
0 commit comments