Module: Net::Text::Reflow
- Defined in:
- lib/net/text/reflow.rb
Overview
Contains helper methods to correctly display texts with long lines.
This module expect given text to be Gemtext inspired (i.e. links prefixed with => and “‘ delimitting code blocks).
Class Method Summary collapse
- .format_body(body, length) ⇒ Object
- .reflow_line_prefix(line) ⇒ Object
- .reflow_text_line(line, mono_block_open, length) ⇒ Object
Class Method Details
.format_body(body, length) ⇒ Object
39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/net/text/reflow.rb', line 39 def self.format_body(body, length) new_body = [] mono_block_open = false body.each_line do |line| if line.start_with?('```') mono_block_open = !mono_block_open # Don't include code block toggle lines next end new_body += reflow_text_line(line, mono_block_open, length) end new_body.join("\n") end |
.reflow_line_prefix(line) ⇒ Object
10 11 12 13 14 15 16 17 |
# File 'lib/net/text/reflow.rb', line 10 def self.reflow_line_prefix(line) m = line.match(/\A([*#>]+ )/) return '' unless m # Each quote line should begin with the quote mark return m[1] if m[1].start_with?('>') ' ' * m[1].length end |
.reflow_text_line(line, mono_block_open, length) ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/net/text/reflow.rb', line 19 def self.reflow_text_line(line, mono_block_open, length) line.strip! if mono_block_open || line.start_with?('=>') || line.length < length return [line] end output = [] prefix = reflow_line_prefix(line) limit_chars = ['-', '', ' '].freeze while line.length > length cut_line = line[0...length] cut_index = limit_chars.map { cut_line.rindex(_1) || -1 }.max break if cut_index.zero? # Better do nothing for now output << line[0...cut_index] line = prefix + line[cut_index + 1..] end output << line end |